File: dial_media_route_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (683 lines) | stat: -rw-r--r-- 28,825 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/router/providers/dial/dial_media_route_provider.h"

#include <utility>

#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/no_destructor.h"
#include "base/notimplemented.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/media/router/data_decoder_util.h"
#include "chrome/browser/media/router/providers/dial/dial_media_route_provider_metrics.h"
#include "chrome/grit/generated_resources.h"
#include "components/media_router/common/media_source.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"

namespace media_router {

namespace {
constexpr char kLoggerComponent[] = "DialMediaRouteProvider";

url::Origin CreateOrigin(const std::string& url) {
  return url::Origin::Create(GURL(url));
}

static constexpr int kMaxPendingDialLaunches = 10;

}  // namespace

DialMediaRouteProvider::DialMediaRouteProvider(
    mojo::PendingReceiver<mojom::MediaRouteProvider> receiver,
    mojo::PendingRemote<mojom::MediaRouter> media_router,
    DialMediaSinkServiceImpl* media_sink_service,
    const std::string& hash_token,
    const scoped_refptr<base::SequencedTaskRunner>& task_runner)
    : media_sink_service_(media_sink_service),
      internal_message_util_(hash_token) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
  DCHECK(media_sink_service_);

  task_runner->PostTask(
      FROM_HERE,
      base::BindOnce(&DialMediaRouteProvider::Init, base::Unretained(this),
                     std::move(receiver), std::move(media_router)));
}

void DialMediaRouteProvider::Init(
    mojo::PendingReceiver<mojom::MediaRouteProvider> receiver,
    mojo::PendingRemote<mojom::MediaRouter> media_router) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  receiver_.Bind(std::move(receiver));
  media_router_.Bind(std::move(media_router));
  media_sink_service_->AddObserver(this);

  media_router_->GetLogger(logger_.BindNewPipeAndPassReceiver());

  // |activity_manager_| might have already been set in tests.
  if (!activity_manager_) {
    activity_manager_ = std::make_unique<DialActivityManager>(
        media_sink_service_->app_discovery_service());
  }
}

DialMediaRouteProvider::~DialMediaRouteProvider() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(media_sink_queries_.empty());
  media_sink_service_->RemoveObserver(this);
}

void DialMediaRouteProvider::CreateRoute(const std::string& media_source,
                                         const std::string& sink_id,
                                         const std::string& presentation_id,
                                         const url::Origin& origin,
                                         int32_t frame_tree_node_id,
                                         base::TimeDelta timeout,
                                         CreateRouteCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const MediaSinkInternal* sink = media_sink_service_->GetSinkById(sink_id);
  if (!sink) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to create route. Cannot find sink with the sink id", sink_id,
        media_source, presentation_id);
    std::move(callback).Run(std::nullopt, nullptr, "Unknown sink " + sink_id,
                            mojom::RouteRequestResultCode::SINK_NOT_FOUND);
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kSinkNotFound);
    return;
  }

  auto activity =
      DialActivity::From(presentation_id, *sink, media_source, origin);
  if (!activity) {
    logger_->LogError(mojom::LogCategory::kRoute, kLoggerComponent,
                      "Failed to create route. Unsupported source.", sink_id,
                      media_source, presentation_id);
    std::move(callback).Run(
        std::nullopt, nullptr, "Unsupported source " + media_source,
        mojom::RouteRequestResultCode::NO_SUPPORTED_PROVIDER);
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kUnsupportedSource);
    return;
  }

  const MediaRoute::Id& route_id = activity->route.media_route_id();
  if (activity_manager_->GetActivity(route_id) != nullptr) {
    logger_->LogError(mojom::LogCategory::kRoute, kLoggerComponent,
                      "Failed to create route. Route already exists.", sink_id,
                      media_source, presentation_id);
    std::move(callback).Run(
        std::nullopt, nullptr, "Route already exists",
        mojom::RouteRequestResultCode::ROUTE_ALREADY_EXISTS);
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kRouteAlreadyExists);
    return;
  }
  // Check if there's already a route to sink.
  if (activity_manager_->GetActivityBySinkId(sink_id) != nullptr) {
    // Terminate the existing session before creating new one.
    TerminateRoute(
        activity_manager_->GetActivityBySinkId(sink_id)->route.media_route_id(),
        base::DoNothing());
    logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                     "Existing route terminated successfully.", sink_id,
                     MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                     MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
  }

  activity_manager_->AddActivity(*activity);
  logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                   "Successfully created a new route.", sink_id, media_source,
                   presentation_id);
  std::move(callback).Run(activity->route, /*presentation_connection*/ nullptr,
                          /*error_text*/ std::nullopt,
                          mojom::RouteRequestResultCode::OK);

  // When a custom DIAL launch request is received, DialMediaRouteProvider will
  // create a MediaRoute immediately in order to start exchanging messages with
  // the Cast SDK to complete the launch sequence. The first messages that the
  // MRP needs to send are the RECEIVER_ACTION and NEW_SESSION.
  std::vector<mojom::RouteMessagePtr> messages;
  messages.emplace_back(internal_message_util_.CreateReceiverActionCastMessage(
      activity->launch_info.client_id, *sink));
  messages.emplace_back(internal_message_util_.CreateNewSessionMessage(
      activity->launch_info.app_name, activity->launch_info.client_id, *sink));
  media_router_->OnRouteMessagesReceived(route_id, std::move(messages));
}

void DialMediaRouteProvider::JoinRoute(const std::string& media_source,
                                       const std::string& presentation_id,
                                       const url::Origin& origin,
                                       int32_t frame_tree_node_id,
                                       base::TimeDelta timeout,
                                       JoinRouteCallback callback) {
  const DialActivity* activity = activity_manager_->GetActivityToJoin(
      presentation_id, MediaSource(media_source), origin);
  if (activity) {
    std::move(callback).Run(
        activity->route, /*presentation_connection*/ nullptr,
        /*error_text*/ std::nullopt, mojom::RouteRequestResultCode::OK);
  } else {
    std::move(callback).Run(std::nullopt, /*presentation_connection*/ nullptr,
                            "DIAL activity not found",
                            mojom::RouteRequestResultCode::ROUTE_NOT_FOUND);
  }
}

void DialMediaRouteProvider::TerminateRoute(const std::string& route_id,
                                            TerminateRouteCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const DialActivity* activity = activity_manager_->GetActivity(route_id);
  if (!activity) {
    logger_->LogInfo(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to terminate route. Route not found with route id.", "",
        MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    std::move(callback).Run("Activity not found",
                            mojom::RouteRequestResultCode::ROUTE_NOT_FOUND);
    return;
  }

  const MediaRoute& route = activity->route;
  const MediaSinkInternal* sink =
      media_sink_service_->GetSinkById(route.media_sink_id());
  if (!sink) {
    logger_->LogError(mojom::LogCategory::kRoute, kLoggerComponent,
                      "Failed to terminate route. Sink not found with sink id.",
                      route.media_sink_id(),
                      MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                      MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    std::move(callback).Run("Sink not found",
                            mojom::RouteRequestResultCode::SINK_NOT_FOUND);
    return;
  }

  DoTerminateRoute(*activity, *sink, std::move(callback));
}

void DialMediaRouteProvider::SendRouteMessage(const std::string& media_route_id,
                                              const std::string& message) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  GetDataDecoder().ParseJson(
      message, base::BindOnce(&DialMediaRouteProvider::HandleParsedRouteMessage,
                              weak_ptr_factory_.GetWeakPtr(), media_route_id));
}

void DialMediaRouteProvider::HandleParsedRouteMessage(
    const MediaRoute::Id& route_id,
    data_decoder::DataDecoder::ValueOrError result) {
  if (!result.has_value() || !result.value().is_dict()) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        base::StrCat({"Failed to parse the route message. ", result.error()}),
        "", MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    return;
  }

  std::string error;
  std::unique_ptr<DialInternalMessage> internal_message =
      DialInternalMessage::From(std::move(result.value().GetDict()), &error);
  if (!internal_message) {
    logger_->LogError(mojom::LogCategory::kRoute, kLoggerComponent,
                      base::StrCat({"Invalid route message. ", error}), "",
                      MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                      MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    return;
  }

  const DialActivity* activity = activity_manager_->GetActivity(route_id);
  if (!activity) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to handle the route message. Route not found with route id.",
        "", MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    return;
  }

  const MediaRoute& route = activity->route;
  const MediaSinkInternal* sink =
      media_sink_service_->GetSinkById(route.media_sink_id());
  if (!sink) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to handle the route message. Sink not found with sink id.",
        route.media_sink_id(),
        MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    return;
  }

  // TODO(crbug.com/40090609): Investigate whether the direct use of
  // PresentationConnection in this class to communicate with the SDK client can
  // result in eliminating the need for CLIENT_CONNECT messages.
  if (internal_message->type == DialInternalMessageType::kClientConnect) {
    HandleClientConnect(*activity, *sink);
  } else if (internal_message->type ==
             DialInternalMessageType::kCustomDialLaunch) {
    HandleCustomDialLaunchResponse(*activity, *internal_message);
  } else if (internal_message->type == DialInternalMessageType::kDialAppInfo) {
    HandleDiapAppInfoRequest(*activity, *internal_message, *sink);
  } else if (DialInternalMessageUtil::IsStopSessionMessage(*internal_message)) {
    logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                     "Received a stop session message.", route.media_sink_id(),
                     MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                     MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    DoTerminateRoute(*activity, *sink, base::DoNothing());
  }
}

void DialMediaRouteProvider::HandleClientConnect(
    const DialActivity& activity,
    const MediaSinkInternal& sink) {
  // Get the latest app info required to handle a CUSTOM_DIAL_LAUNCH message to
  // the SDK client.
  media_sink_service_->app_discovery_service()->FetchDialAppInfo(
      sink, activity.launch_info.app_name,
      base::BindOnce(&DialMediaRouteProvider::SendCustomDialLaunchMessage,
                     weak_ptr_factory_.GetWeakPtr(),
                     activity.route.media_route_id()));
}

void DialMediaRouteProvider::SendCustomDialLaunchMessage(
    const MediaRoute::Id& route_id,
    const MediaSink::Id& sink_id,
    const std::string& app_name,
    DialAppInfoResult result) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Check if there is app info.
  if (!result.app_info) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to send custom app launch message. Cannot find app info.",
        sink_id, MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    // Note: this leaves the route in a stuck state; the client must terminate
    // the route. Maybe we should clean up the route here.
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kAppInfoNotFound);
    return;
  }

  // Check if activity still exists.
  auto* activity = activity_manager_->GetActivity(route_id);
  if (!activity) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to send custom app launch message. The route is closed.",
        sink_id, MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    return;
  }

  auto* sink = media_sink_service_->GetSinkById(sink_id);
  if (!sink) {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        "Failed to send custom app launch message. Sink not found.", sink_id,
        MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    // TODO(imcheng): We should remove the route when the sink is removed.
    return;
  }

  logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                   "Sending custom app launch message", sink->id(),
                   MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                   MediaRoute::GetPresentationIdFromMediaRouteId(route_id));

  auto message_and_seq_number =
      internal_message_util_.CreateCustomDialLaunchMessage(
          activity->launch_info.client_id, *sink, *result.app_info);
  pending_dial_launches_.insert(message_and_seq_number.second);
  if (pending_dial_launches_.size() > kMaxPendingDialLaunches) {
    pending_dial_launches_.erase(pending_dial_launches_.begin());
  }

  std::vector<mojom::RouteMessagePtr> messages;
  messages.emplace_back(std::move(message_and_seq_number.first));
  media_router_->OnRouteMessagesReceived(route_id, std::move(messages));
}

void DialMediaRouteProvider::SendDialAppInfoResponse(
    const MediaRoute::Id& route_id,
    int sequence_number,
    const MediaSink::Id& sink_id,
    const std::string& app_name,
    DialAppInfoResult result) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto* activity = activity_manager_->GetActivity(route_id);
  auto* sink = media_sink_service_->GetSinkById(sink_id);
  // If the activity no longer exists, there is no need to inform the sender
  // client of the activity status.
  if (!activity || !sink) {
    return;
  }
  mojom::RouteMessagePtr message;
  if (result.app_info) {
    message = internal_message_util_.CreateDialAppInfoMessage(
        activity->launch_info.client_id, *sink, *result.app_info,
        sequence_number, DialInternalMessageType::kDialAppInfo);
  } else {
    message = internal_message_util_.CreateDialAppInfoErrorMessage(
        result.result_code, activity->launch_info.client_id, sequence_number,
        result.error_message, result.http_error_code);
  }
  std::vector<mojom::RouteMessagePtr> messages;
  messages.emplace_back(std::move(message));
  media_router_->OnRouteMessagesReceived(route_id, std::move(messages));
}

void DialMediaRouteProvider::HandleCustomDialLaunchResponse(
    const DialActivity& activity,
    const DialInternalMessage& message) {
  if (!pending_dial_launches_.erase(message.sequence_number)) {
    return;
  }

  const MediaRoute::Id& media_route_id = activity.route.media_route_id();
  activity_manager_->LaunchApp(
      media_route_id, CustomDialLaunchMessageBody::From(message),
      base::BindOnce(&DialMediaRouteProvider::HandleAppLaunchResult,
                     base::Unretained(this), media_route_id));
}

void DialMediaRouteProvider::HandleDiapAppInfoRequest(
    const DialActivity& activity,
    const DialInternalMessage& message,
    const MediaSinkInternal& sink) {
  media_sink_service_->app_discovery_service()->FetchDialAppInfo(
      sink, activity.launch_info.app_name,
      base::BindOnce(&DialMediaRouteProvider::SendDialAppInfoResponse,
                     weak_ptr_factory_.GetWeakPtr(),
                     activity.route.media_route_id(), message.sequence_number));
}

void DialMediaRouteProvider::HandleAppLaunchResult(
    const MediaRoute::Id& route_id,
    bool success) {
  if (success) {
    logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                     "Successfully launched app.",
                     MediaRoute::GetSinkIdFromMediaRouteId(route_id),
                     MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                     MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kSuccess);
  } else {
    logger_->LogError(mojom::LogCategory::kRoute, kLoggerComponent,
                      "Failed to launch app.",
                      MediaRoute::GetSinkIdFromMediaRouteId(route_id),
                      MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                      MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    DialMediaRouteProviderMetrics::RecordCreateRouteResult(
        DialCreateRouteResult::kAppLaunchFailed);
  }
  NotifyAllOnRoutesUpdated();
}

void DialMediaRouteProvider::DoTerminateRoute(const DialActivity& activity,
                                              const MediaSinkInternal& sink,
                                              TerminateRouteCallback callback) {
  const MediaRoute::Id& route_id = activity.route.media_route_id();
  std::pair<std::optional<std::string>, mojom::RouteRequestResultCode>
      can_stop_app = activity_manager_->CanStopApp(route_id);
  if (can_stop_app.second == mojom::RouteRequestResultCode::OK) {
    std::vector<mojom::RouteMessagePtr> messages;
    messages.emplace_back(
        internal_message_util_.CreateReceiverActionStopMessage(
            activity.launch_info.client_id, sink));
    media_router_->OnRouteMessagesReceived(route_id, std::move(messages));
    activity_manager_->StopApp(
        route_id,
        base::BindOnce(&DialMediaRouteProvider::HandleStopAppResult,
                       base::Unretained(this), route_id, std::move(callback)));
  } else {
    logger_->LogError(
        mojom::LogCategory::kRoute, kLoggerComponent,
        base::StringPrintf(
            "Failed to terminate route. %s mojom::RouteRequestResultCode: %d",
            can_stop_app.first.value_or("").c_str(),
            static_cast<int>(can_stop_app.second)),
        sink.id(), MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
        MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
    std::move(callback).Run(can_stop_app.first, can_stop_app.second);
  }
}

void DialMediaRouteProvider::HandleStopAppResult(
    const MediaRoute::Id& route_id,
    TerminateRouteCallback callback,
    const std::optional<std::string>& message,
    mojom::RouteRequestResultCode result_code) {
  switch (result_code) {
    case mojom::RouteRequestResultCode::OK:
      logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                       "Successfully terminated route.", "",
                       MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                       MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
      break;
    case mojom::RouteRequestResultCode::ROUTE_ALREADY_TERMINATED:
      logger_->LogInfo(mojom::LogCategory::kRoute, kLoggerComponent,
                       "Tried to stop a session that no longer exists.", "",
                       MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
                       MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
      break;
    default:
      // In this case, the app may still be running on the receiver but we can
      // not terminate it. So, we remove it from the list of routes tracked by
      // Chrome, and inform the user to stop it from the receiver side as well.
      logger_->LogError(
          mojom::LogCategory::kRoute, kLoggerComponent,
          base::StringPrintf("Removed a route that may still be running on the "
                             "receiver. %s RouteRequestResult: %d",
                             message.value_or("").c_str(),
                             static_cast<int>(result_code)),
          "", MediaRoute::GetMediaSourceIdFromMediaRouteId(route_id),
          MediaRoute::GetPresentationIdFromMediaRouteId(route_id));
      media_router_->OnIssue(
          {l10n_util::GetStringUTF8(IDS_MEDIA_ROUTER_ISSUE_CANNOT_TERMINATE),
           IssueInfo::Severity::WARNING,
           MediaRoute::GetSinkIdFromMediaRouteId(route_id)});
  }
  // We set the PresentationConnection state to "terminated" per the API spec:
  // https://w3c.github.io/presentation-api/#terminating-a-presentation-in-a-controlling-browsing-context
  media_router_->OnPresentationConnectionStateChanged(
      route_id, blink::mojom::PresentationConnectionState::TERMINATED);
  NotifyAllOnRoutesUpdated();
  std::move(callback).Run(message, result_code);
}

void DialMediaRouteProvider::NotifyAllOnRoutesUpdated() {
  auto routes = activity_manager_->GetRoutes();
  NotifyOnRoutesUpdated(routes);
}

void DialMediaRouteProvider::NotifyOnRoutesUpdated(
    const std::vector<MediaRoute>& routes) {
  media_router_->OnRoutesUpdated(mojom::MediaRouteProviderId::DIAL, routes);
}

void DialMediaRouteProvider::SendRouteBinaryMessage(
    const std::string& media_route_id,
    const std::vector<uint8_t>& data) {
  NOTIMPLEMENTED();
}

void DialMediaRouteProvider::StartObservingMediaSinks(
    const std::string& media_source) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (media_source.empty()) {
    std::vector<MediaSinkInternal> sinks;
    for (const auto& sink_it : media_sink_service_->GetSinks()) {
      sinks.push_back(sink_it.second);
    }
    OnSinksDiscovered(sinks);
    return;
  }

  MediaSource dial_source(media_source);
  if (!dial_source.IsDialSource()) {
    return;
  }

  std::string app_name = dial_source.AppNameFromDialSource();
  if (app_name.empty()) {
    return;
  }

  auto& sink_query = media_sink_queries_[app_name];
  if (!sink_query) {
    sink_query = std::make_unique<MediaSinkQuery>();
    sink_query->subscription =
        media_sink_service_->StartMonitoringAvailableSinksForApp(
            app_name, base::BindRepeating(
                          &DialMediaRouteProvider::OnAvailableSinksUpdated,
                          base::Unretained(this)));
  }

  // Return cached results immediately.
  if (sink_query->media_sources.insert(dial_source).second) {
    auto sinks = media_sink_service_->GetAvailableSinks(app_name);
    NotifyOnSinksReceived(dial_source.id(), sinks, GetOrigins(app_name));
  }
}

void DialMediaRouteProvider::StopObservingMediaSinks(
    const std::string& media_source) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  MediaSource dial_source(media_source);
  std::string app_name = dial_source.AppNameFromDialSource();
  if (!dial_source.id().empty() && app_name.empty()) {
    return;
  }

  const auto& sink_query_it = media_sink_queries_.find(app_name);
  if (sink_query_it == media_sink_queries_.end()) {
    return;
  }

  auto& media_sources = sink_query_it->second->media_sources;
  media_sources.erase(dial_source);
  if (media_sources.empty()) {
    media_sink_queries_.erase(sink_query_it);
  }
}

void DialMediaRouteProvider::StartObservingMediaRoutes() {
  // Return current set of routes.
  auto routes = activity_manager_->GetRoutes();
  if (!routes.empty()) {
    NotifyOnRoutesUpdated(routes);
  }
}

void DialMediaRouteProvider::DetachRoute(const std::string& route_id) {
  NOTIMPLEMENTED();
}

void DialMediaRouteProvider::DiscoverSinksNow() {
  media_sink_service_->DiscoverSinksNow();
}

void DialMediaRouteProvider::BindMediaController(
    const std::string& route_id,
    mojo::PendingReceiver<mojom::MediaController> media_controller,
    mojo::PendingRemote<mojom::MediaStatusObserver> observer,
    BindMediaControllerCallback callback) {
  NOTIMPLEMENTED();
  std::move(callback).Run(false);
}

void DialMediaRouteProvider::GetState(GetStateCallback callback) {
  NOTIMPLEMENTED();
  std::move(callback).Run(mojom::ProviderStatePtr());
}

void DialMediaRouteProvider::SetActivityManagerForTest(
    std::unique_ptr<DialActivityManager> activity_manager) {
  DCHECK(!activity_manager_);
  activity_manager_ = std::move(activity_manager);
}

void DialMediaRouteProvider::OnSinksDiscovered(
    const std::vector<MediaSinkInternal>& sinks) {
  // Send a list of all available sinks to Media Router as sinks not associated
  // with any particular source.
  NotifyOnSinksReceived(MediaSource::Id(), sinks, {});
}

void DialMediaRouteProvider::OnAvailableSinksUpdated(
    const std::string& app_name) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const auto& sink_query_it = media_sink_queries_.find(app_name);
  if (sink_query_it == media_sink_queries_.end()) {
    return;
  }

  const auto& media_sources = sink_query_it->second->media_sources;
  std::vector<url::Origin> origins = GetOrigins(app_name);

  auto sinks = media_sink_service_->GetAvailableSinks(app_name);
  for (const auto& media_source : media_sources) {
    NotifyOnSinksReceived(media_source.id(), sinks, origins);
  }
}

void DialMediaRouteProvider::NotifyOnSinksReceived(
    const MediaSource::Id& source_id,
    const std::vector<MediaSinkInternal>& sinks,
    const std::vector<url::Origin>& origins) {
  media_router_->OnSinksReceived(mojom::MediaRouteProviderId::DIAL, source_id,
                                 sinks, origins);
}

std::vector<url::Origin> DialMediaRouteProvider::GetOrigins(
    const std::string& app_name) {
  static const base::NoDestructor<
      base::flat_map<std::string, std::vector<url::Origin>>>
      origin_allowlist(
          {{"YouTube",
            {CreateOrigin("https://music.youtube.com/"),
             CreateOrigin("https://music-green-qa.youtube.com/"),
             CreateOrigin("https://music-release-qa.youtube.com/"),
             CreateOrigin("https://tv.youtube.com"),
             CreateOrigin("https://tv-green-qa.youtube.com"),
             CreateOrigin("https://tv-release-qa.youtube.com"),
             CreateOrigin("https://web-green-qa.youtube.com"),
             CreateOrigin("https://web-release-qa.youtube.com"),
             CreateOrigin("https://www.youtube.com")}},
           {"Netflix", {CreateOrigin("https://www.netflix.com")}},
           {"Pandora", {CreateOrigin("https://www.pandora.com")}},
           {"Radio", {CreateOrigin("https://www.pandora.com")}},
           {"Hulu", {CreateOrigin("https://www.hulu.com")}},
           {"Vimeo", {CreateOrigin("https://www.vimeo.com")}},
           {"Dailymotion", {CreateOrigin("https://www.dailymotion.com")}},
           {"com.dailymotion", {CreateOrigin("https://www.dailymotion.com")}}});

  auto origins_it = origin_allowlist->find(app_name);
  if (origins_it == origin_allowlist->end()) {
    return std::vector<url::Origin>();
  }

  return origins_it->second;
}

DialMediaRouteProvider::MediaSinkQuery::MediaSinkQuery() = default;
DialMediaRouteProvider::MediaSinkQuery::~MediaSinkQuery() = default;

}  // namespace media_router