summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/resources/media_router_bindings.js
blob: b6252c50e999fe49cbc08973532b30b1db95939b (plain)
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var mediaRouter;

define('media_router_bindings', [
    'mojo/public/js/bindings',
    'mojo/public/js/core',
    'content/public/renderer/service_provider',
    'chrome/browser/media/router/media_router.mojom',
    'extensions/common/mojo/keep_alive.mojom',
    'mojo/public/js/connection',
    'mojo/public/js/router',
], function(bindings,
            core,
            serviceProvider,
            mediaRouterMojom,
            keepAliveMojom,
            connector,
            routerModule) {
  'use strict';

  /**
   * Converts a media sink to a MediaSink Mojo object.
   * @param {!MediaSink} sink A media sink.
   * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object.
   */
  function sinkToMojo_(sink) {
    return new mediaRouterMojom.MediaSink({
      'name': sink.friendlyName,
      'description': sink.description,
      'domain': sink.domain,
      'sink_id': sink.id,
      'icon_type': sinkIconTypeToMojo(sink.iconType),
    });
  }

  /**
   * Converts a media sink's icon type to a MediaSink.IconType Mojo object.
   * @param {!MediaSink.IconType} type A media sink's icon type.
   * @return {!mediaRouterMojom.MediaSink.IconType} A Mojo MediaSink.IconType
   *     object.
   */
  function sinkIconTypeToMojo(type) {
    switch (type) {
      case 'cast':
        return mediaRouterMojom.MediaSink.IconType.CAST;
      case 'cast_audio':
        return mediaRouterMojom.MediaSink.IconType.CAST_AUDIO;
      case 'cast_audio_group':
        return mediaRouterMojom.MediaSink.IconType.CAST_AUDIO_GROUP;
      case 'generic':
        return mediaRouterMojom.MediaSink.IconType.GENERIC;
      case 'hangout':
        return mediaRouterMojom.MediaSink.IconType.HANGOUT;
      default:
        console.error('Unknown sink icon type : ' + type);
        return mediaRouterMojom.MediaSink.IconType.GENERIC;
    }
  }

  /**
   * Returns a Mojo MediaRoute object given a MediaRoute and a
   * media sink name.
   * @param {!MediaRoute} route
   * @return {!mojo.MediaRoute}
   */
  function routeToMojo_(route) {
    return new mediaRouterMojom.MediaRoute({
      'media_route_id': route.id,
      'media_source': route.mediaSource,
      'media_sink_id': route.sinkId,
      'description': route.description,
      'icon_url': route.iconUrl,
      'is_local': route.isLocal,
      'custom_controller_path': route.customControllerPath,
      // Begin newly added properties, followed by the milestone they were
      // added.  The guard should be safe to remove N+2 milestones later.
      'for_display': route.forDisplay, // M47
      'off_the_record': !!route.offTheRecord  // M50
    });
  }

  /**
   * Converts a route message to a RouteMessage Mojo object.
   * @param {!RouteMessage} message
   * @return {!mediaRouterMojom.RouteMessage} A Mojo RouteMessage object.
   */
  function messageToMojo_(message) {
    if ("string" == typeof message.message) {
      return new mediaRouterMojom.RouteMessage({
        'type': mediaRouterMojom.RouteMessage.Type.TEXT,
        'message': message.message,
      });
    } else {
      return new mediaRouterMojom.RouteMessage({
        'type': mediaRouterMojom.RouteMessage.Type.BINARY,
        'data': message.message,
      });
    }
  }

  /**
   * Converts presentation connection state to Mojo enum value.
   * @param {!string} state
   * @return {!mediaRouterMojom.MediaRouter.PresentationConnectionState}
   */
  function presentationConnectionStateToMojo_(state) {
    var PresentationConnectionState =
        mediaRouterMojom.MediaRouter.PresentationConnectionState;
    switch (state) {
      case 'connecting':
        return PresentationConnectionState.CONNECTING;
      case 'connected':
        return PresentationConnectionState.CONNECTED;
      case 'closed':
        return PresentationConnectionState.CLOSED;
      case 'terminated':
        return PresentationConnectionState.TERMINATED;
      default:
        console.error('Unknown presentation connection state: ' + state);
        return PresentationConnectionState.TERMINATED;
    }
  }

  /**
   * Converts presentation connection close reason to Mojo enum value.
   * @param {!string} reason
   * @return {!mediaRouterMojom.MediaRouter.PresentationConnectionCloseReason}
   */
  function presentationConnectionCloseReasonToMojo_(reason) {
    var PresentationConnectionCloseReason =
        mediaRouterMojom.MediaRouter.PresentationConnectionCloseReason;
    switch (reason) {
      case 'error':
        return PresentationConnectionCloseReason.CONNECTION_ERROR;
      case 'closed':
        return PresentationConnectionCloseReason.CLOSED;
      case 'went_away':
        return PresentationConnectionCloseReason.WENT_AWAY;
      default:
        console.error('Unknown presentation connection close reason : ' +
            reason);
        return PresentationConnectionCloseReason.CONNECTION_ERROR;
    }
  }

  /**
   * Parses the given route request Error object and converts it to the
   * corresponding result code.
   * @param {!Error} error
   * @return {!mediaRouterMojom.RouteRequestResultCode}
   */
  function getRouteRequestResultCode_(error) {
    if (error.message.startsWith('timeout'))
      return mediaRouterMojom.RouteRequestResultCode.TIMED_OUT;
    else
      return mediaRouterMojom.RouteRequestResultCode.UNKNOWN_ERROR;
  }

  /**
   * Creates and returns a successful route response from given route.
   * @param {!MediaRoute} route
   * @return {!Object}
   */
  function toSuccessRouteResponse_(route) {
    return {
        route: routeToMojo_(route),
        result_code: mediaRouterMojom.RouteRequestResultCode.OK
    };
  }

  /**
   * Creates and returns a error route response from given Error object
   * @param {!Error} error
   * @return {!Object}
   */
  function toErrorRouteResponse_(error) {
    return {
        error_text: 'Error creating route: ' + error.message,
        result_code: getRouteRequestResultCode_(error)
    };
  }

  /**
   * Creates a new MediaRouter.
   * Converts a route struct to its Mojo form.
   * @param {!MediaRouterService} service
   * @constructor
   */
  function MediaRouter(service) {
    /**
     * The Mojo service proxy. Allows extension code to call methods that reside
     * in the browser.
     * @type {!MediaRouterService}
     */
    this.service_ = service;

    /**
     * The provider manager service delegate. Its methods are called by the
     * browser-resident Mojo service.
     * @type {!MediaRouter}
     */
    this.mrpm_ = new MediaRouteProvider(this);

    /**
     * The message pipe that connects the Media Router to mrpm_ across
     * browser/renderer IPC boundaries. Object must remain in scope for the
     * lifetime of the connection to prevent the connection from closing
     * automatically.
     * @type {!mojo.MessagePipe}
     */
    this.pipe_ = core.createMessagePipe();

    /**
     * Handle to a KeepAlive service object, which prevents the extension from
     * being suspended as long as it remains in scope.
     * @type {boolean}
     */
    this.keepAlive_ = null;

    /**
     * The stub used to bind the service delegate to the Mojo interface.
     * Object must remain in scope for the lifetime of the connection to
     * prevent the connection from closing automatically.
     * @type {!mojom.MediaRouter}
     */
    this.mediaRouteProviderStub_ = connector.bindHandleToStub(
        this.pipe_.handle0, mediaRouterMojom.MediaRouteProvider);

    // Link mediaRouteProviderStub_ to the provider manager delegate.
    bindings.StubBindings(this.mediaRouteProviderStub_).delegate = this.mrpm_;
  }

  /**
   * Registers the Media Router Provider Manager with the Media Router.
   * @return {!Promise<string>} Instance ID for the Media Router.
   */
  MediaRouter.prototype.start = function() {
    return this.service_.registerMediaRouteProvider(this.pipe_.handle1).then(
        function(result) {
          return result.instance_id;
        }.bind(this));
  }

  /**
   * Sets the service delegate methods.
   * @param {Object} handlers
   */
  MediaRouter.prototype.setHandlers = function(handlers) {
    this.mrpm_.setHandlers(handlers);
  }

  /**
   * The keep alive status.
   * @return {boolean}
   */
  MediaRouter.prototype.getKeepAlive = function() {
    return this.keepAlive_ != null;
  };

  /**
   * Called by the provider manager when a sink list for a given source is
   * updated.
   * @param {!string} sourceUrn
   * @param {!Array<!MediaSink>} sinks
   */
  MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks) {
    this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_));
  };

  /**
   * Called by the provider manager to keep the extension from suspending
   * if it enters a state where suspension is undesirable (e.g. there is an
   * active MediaRoute.)
   * If keepAlive is true, the extension is kept alive.
   * If keepAlive is false, the extension is allowed to suspend.
   * @param {boolean} keepAlive
   */
  MediaRouter.prototype.setKeepAlive = function(keepAlive) {
    if (keepAlive === false && this.keepAlive_) {
      this.keepAlive_.close();
      this.keepAlive_ = null;
    } else if (keepAlive === true && !this.keepAlive_) {
      this.keepAlive_ = new routerModule.Router(
          serviceProvider.connectToService(
              keepAliveMojom.KeepAlive.name));
    }
  };

  /**
   * Called by the provider manager to send an issue from a media route
   * provider to the Media Router, to show the user.
   * @param {!Object} issue The issue object.
   */
  MediaRouter.prototype.onIssue = function(issue) {
    function issueSeverityToMojo_(severity) {
      switch (severity) {
        case 'fatal':
          return mediaRouterMojom.Issue.Severity.FATAL;
        case 'warning':
          return mediaRouterMojom.Issue.Severity.WARNING;
        case 'notification':
          return mediaRouterMojom.Issue.Severity.NOTIFICATION;
        default:
          console.error('Unknown issue severity: ' + severity);
          return mediaRouterMojom.Issue.Severity.NOTIFICATION;
      }
    }

    function issueActionToMojo_(action) {
      switch (action) {
        case 'ok':
          return mediaRouterMojom.Issue.ActionType.OK;
        case 'cancel':
          return mediaRouterMojom.Issue.ActionType.CANCEL;
        case 'dismiss':
          return mediaRouterMojom.Issue.ActionType.DISMISS;
        case 'learn_more':
          return mediaRouterMojom.Issue.ActionType.LEARN_MORE;
        default:
          console.error('Unknown issue action type : ' + action);
          return mediaRouterMojom.Issue.ActionType.OK;
      }
    }

    var secondaryActions = (issue.secondaryActions || []).map(function(e) {
      return issueActionToMojo_(e);
    });
    this.service_.onIssue(new mediaRouterMojom.Issue({
      'route_id': issue.routeId,
      'severity': issueSeverityToMojo_(issue.severity),
      'title': issue.title,
      'message': issue.message,
      'default_action': issueActionToMojo_(issue.defaultAction),
      'secondary_actions': secondaryActions,
      'help_url': issue.helpUrl,
      'is_blocking': issue.isBlocking
    }));
  };

  /**
   * Called by the provider manager when the set of active routes
   * has been updated.
   * @param {!Array<MediaRoute>} routes The active set of media routes.
   * @param {string=} opt_sourceUrn The sourceUrn associated with this route
   *     query. This parameter is optional and can be empty.
   * @param {Array<string>=} opt_joinableRouteIds The active set of joinable
   *     media routes. This parameter is optional and can be empty.
   */
  MediaRouter.prototype.onRoutesUpdated =
      function(routes, opt_sourceUrn, opt_joinableRouteIds) {
    // TODO(boetger): This check allows backward compatibility with the Cast SDK
    // and can be removed when the Cast SDK is updated.
    if (typeof(opt_sourceUrn) != 'string') {
      opt_sourceUrn = '';
    }

    this.service_.onRoutesUpdated(
        routes.map(routeToMojo_),
        opt_sourceUrn || '',
        opt_joinableRouteIds || []);
  };

  /**
   * Called by the provider manager when sink availability has been updated.
   * @param {!mediaRouterMojom.MediaRouter.SinkAvailability} availability
   *     The new sink availability.
   */
  MediaRouter.prototype.onSinkAvailabilityUpdated = function(availability) {
    this.service_.onSinkAvailabilityUpdated(availability);
  };

  /**
   * Called by the provider manager when the state of a presentation connected
   * to a route has changed.
   * @param {string} routeId
   * @param {string} state
   */
  MediaRouter.prototype.onPresentationConnectionStateChanged =
      function(routeId, state) {
    this.service_.onPresentationConnectionStateChanged(
        routeId, presentationConnectionStateToMojo_(state));
  };

  /**
   * Called by the provider manager when the state of a presentation connected
   * to a route has closed.
   * @param {string} routeId
   * @param {string} reason
   * @param {string} message
   */
  MediaRouter.prototype.onPresentationConnectionClosed =
      function(routeId, reason, message) {
    this.service_.onPresentationConnectionClosed(
        routeId, presentationConnectionCloseReasonToMojo_(reason), message);
  };

  /**
   * Object containing callbacks set by the provider manager.
   *
   * @constructor
   * @struct
   */
  function MediaRouterHandlers() {
    /**
     * @type {function(!string, !string, !string, !string, !number}
     */
    this.createRoute = null;

    /**
     * @type {function(!string, !string, !string, !number)}
     */
    this.joinRoute = null;

    /**
     * @type {function(string)}
     */
    this.terminateRoute = null;

    /**
     * @type {function(string)}
     */
    this.startObservingMediaSinks = null;

    /**
     * @type {function(string)}
     */
    this.stopObservingMediaSinks = null;

    /**
     * @type {function(string, string): Promise}
     */
    this.sendRouteMessage = null;

    /**
     * @type {function(string, Uint8Array): Promise}
     */
    this.sendRouteBinaryMessage = null;

    /**
     * @type {function(string):
     *     Promise.<{messages: Array.<RouteMessage>, error: boolean}>}
     */
    this.listenForRouteMessages = null;

    /**
     * @type {function(string)}
     */
    this.stopListeningForRouteMessages = null;

    /**
     * @type {function(string)}
     */
    this.detachRoute = null;

    /**
     * @type {function()}
     */
    this.startObservingMediaRoutes = null;

    /**
     * @type {function()}
     */
    this.stopObservingMediaRoutes = null;

    /**
     * @type {function()}
     */
    this.connectRouteByRouteId = null;
  };

  /**
   * Routes calls from Media Router to the provider manager extension.
   * Registered with the MediaRouter stub.
   * @param {!MediaRouter} MediaRouter proxy to call into the
   * Media Router mojo interface.
   * @constructor
   */
  function MediaRouteProvider(mediaRouter) {
    mediaRouterMojom.MediaRouteProvider.stubClass.call(this);

    /**
     * Object containing JS callbacks into Provider Manager code.
     * @type {!MediaRouterHandlers}
     */
    this.handlers_ = new MediaRouterHandlers();

    /**
     * Proxy class to the browser's Media Router Mojo service.
     * @type {!MediaRouter}
     */
    this.mediaRouter_ = mediaRouter;
  }
  MediaRouteProvider.prototype = Object.create(
      mediaRouterMojom.MediaRouteProvider.stubClass.prototype);

  /*
   * Sets the callback handler used to invoke methods in the provider manager.
   *
   * @param {!MediaRouterHandlers} handlers
   */
  MediaRouteProvider.prototype.setHandlers = function(handlers) {
    this.handlers_ = handlers;
    var requiredHandlers = [
      'stopObservingMediaRoutes',
      'startObservingMediaRoutes',
      'sendRouteMessage',
      'sendRouteBinaryMessage',
      'listenForRouteMessages',
      'stopListeningForRouteMessages',
      'detachRoute',
      'terminateRoute',
      'joinRoute',
      'createRoute',
      'stopObservingMediaSinks',
      'startObservingMediaRoutes',
      'connectRouteByRouteId'
    ];
    requiredHandlers.forEach(function(nextHandler) {
      if (handlers[nextHandler] === undefined) {
        console.error(nextHandler + ' handler not registered.');
      }
    });
  }

  /**
   * Starts querying for sinks capable of displaying the media source
   * designated by |sourceUrn|.  Results are returned by calling
   * OnSinksReceived.
   * @param {!string} sourceUrn
   */
  MediaRouteProvider.prototype.startObservingMediaSinks =
      function(sourceUrn) {
    this.handlers_.startObservingMediaSinks(sourceUrn);
  };

  /**
   * Stops querying for sinks capable of displaying |sourceUrn|.
   * @param {!string} sourceUrn
   */
  MediaRouteProvider.prototype.stopObservingMediaSinks =
      function(sourceUrn) {
    this.handlers_.stopObservingMediaSinks(sourceUrn);
  };

  /**
   * Requests that |sinkId| render the media referenced by |sourceUrn|. If the
   * request is from the Presentation API, then origin and tabId will
   * be populated.
   * @param {!string} sourceUrn Media source to render.
   * @param {!string} sinkId Media sink ID.
   * @param {!string} presentationId Presentation ID from the site
   *     requesting presentation. TODO(mfoltz): Remove.
   * @param {!string} origin Origin of site requesting presentation.
   * @param {!number} tabId ID of tab requesting presentation.
   * @param {!number} timeoutMillis If positive, the timeout duration for the
   *     request, measured in seconds. Otherwise, the default duration will be
   *     used.
   * @param {!boolean} offTheRecord If true, the route is being requested by
   *     an off the record (incognito) profile.
   * @return {!Promise.<!Object>} A Promise resolving to an object describing
   *     the newly created media route, or rejecting with an error message on
   *     failure.
   */
  MediaRouteProvider.prototype.createRoute =
      function(sourceUrn, sinkId, presentationId, origin, tabId,
          timeoutMillis, offTheRecord) {
    return this.handlers_.createRoute(
        sourceUrn, sinkId, presentationId, origin, tabId, timeoutMillis,
        offTheRecord)
        .then(function(route) {
          return toSuccessRouteResponse_(route);
        },
        function(err) {
          return toErrorRouteResponse_(err);
        });
  };

  /**
   * Handles a request via the Presentation API to join an existing route given
   * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for
   * validating same-origin/tab scope.
   * @param {!string} sourceUrn Media source to render.
   * @param {!string} presentationId Presentation ID to join.
   * @param {!string} origin Origin of site requesting join.
   * @param {!number} tabId ID of tab requesting join.
   * @param {!number} timeoutMillis If positive, the timeout duration for the
   *     request, measured in seconds. Otherwise, the default duration will be
   *     used.
   * @param {!boolean} offTheRecord If true, the route is being requested by
   *     an off the record (incognito) profile.
   * @return {!Promise.<!Object>} A Promise resolving to an object describing
   *     the newly created media route, or rejecting with an error message on
   *     failure.
   */
  MediaRouteProvider.prototype.joinRoute =
      function(sourceUrn, presentationId, origin, tabId, timeoutMillis,
               offTheRecord) {
    return this.handlers_.joinRoute(
        sourceUrn, presentationId, origin, tabId, timeoutMillis, offTheRecord)
        .then(function(route) {
          return toSuccessRouteResponse_(route);
        },
        function(err) {
          return toErrorRouteResponse_(err);
        });
  };

  /**
   * Handles a request via the Presentation API to join an existing route given
   * by |sourceUrn| and |routeId|. |origin| and |tabId| are used for
   * validating same-origin/tab scope.
   * @param {!string} sourceUrn Media source to render.
   * @param {!string} routeId Route ID to join.
   * @param {!string} presentationId Presentation ID to join.
   * @param {!string} origin Origin of site requesting join.
   * @param {!number} tabId ID of tab requesting join.
   * @param {!number} timeoutMillis If positive, the timeout duration for the
   *     request, measured in seconds. Otherwise, the default duration will be
   *     used.
   * @return {!Promise.<!Object>} A Promise resolving to an object describing
   *     the newly created media route, or rejecting with an error message on
   *     failure.
   */
  MediaRouteProvider.prototype.connectRouteByRouteId =
      function(sourceUrn, routeId, presentationId, origin, tabId,
          timeoutMillis) {
    return this.handlers_.connectRouteByRouteId(
        sourceUrn, routeId, presentationId, origin, tabId, timeoutMillis)
        .then(function(route) {
          return toSuccessRouteResponse_(route);
        },
        function(err) {
          return toErrorRouteResponse_(err);
        });
  };

  /**
   * Terminates the route specified by |routeId|.
   * @param {!string} routeId
   */
  MediaRouteProvider.prototype.terminateRoute = function(routeId) {
    this.handlers_.terminateRoute(routeId);
  };

  /**
   * Posts a message to the route designated by |routeId|.
   * @param {!string} routeId
   * @param {!string} message
   * @return {!Promise.<boolean>} Resolved with true if the message was sent,
   *    or false on failure.
   */
  MediaRouteProvider.prototype.sendRouteMessage = function(
      routeId, message) {
    return this.handlers_.sendRouteMessage(routeId, message)
        .then(function() {
          return {'sent': true};
        }, function() {
          return {'sent': false};
        });
  };

  /**
   * Sends a binary message to the route designated by |routeId|.
   * @param {!string} routeId
   * @param {!Uint8Array} data
   * @return {!Promise.<boolean>} Resolved with true if the data was sent,
   *    or false on failure.
   */
  MediaRouteProvider.prototype.sendRouteBinaryMessage = function(
      routeId, data) {
    return this.handlers_.sendRouteBinaryMessage(routeId, data)
        .then(function() {
          return {'sent': true};
        }, function() {
          return {'sent': false};
        });
  };

  /**
   * Listen for next batch of messages from one of the routeIds.
   * @param {!string} routeId
   * @return {!Promise.<{messages: Array.<RouteMessage>, error: boolean}>}
   *     Resolved with a list of messages, and a boolean indicating if an error
   *     occurred.
   */
  MediaRouteProvider.prototype.listenForRouteMessages = function(routeId) {
    return this.handlers_.listenForRouteMessages(routeId)
        .then(function(messages) {
          return {'messages': messages.map(messageToMojo_), 'error': false};
        }, function() {
          return {'messages': [], 'error': true};
        });
  };

  /**
   * If there is an outstanding |listenForRouteMessages| promise for
   * |routeId|, resolve that promise with an empty array.
   * @param {!string} routeId
   */
  MediaRouteProvider.prototype.stopListeningForRouteMessages = function(
      routeId) {
    return this.handlers_.stopListeningForRouteMessages(routeId);
  };

  /**
   * Indicates that the presentation connection that was connected to |routeId|
   * is no longer connected to it.
   * @param {!string} routeId
   */
  MediaRouteProvider.prototype.detachRoute = function(
      routeId) {
    this.handlers_.detachRoute(routeId);
  };

  /**
   * Requests that the provider manager start sending information about active
   * media routes to the Media Router.
   * @param {!string} sourceUrn
   */
  MediaRouteProvider.prototype.startObservingMediaRoutes = function(sourceUrn) {
    this.handlers_.startObservingMediaRoutes(sourceUrn);
  };

  /**
   * Requests that the provider manager stop sending information about active
   * media routes to the Media Router.
   * @param {!string} sourceUrn
   */
  MediaRouteProvider.prototype.stopObservingMediaRoutes = function(sourceUrn) {
    this.handlers_.stopObservingMediaRoutes(sourceUrn);
  };

  mediaRouter = new MediaRouter(connector.bindHandleToProxy(
      serviceProvider.connectToService(
          mediaRouterMojom.MediaRouter.name),
      mediaRouterMojom.MediaRouter));

  return mediaRouter;
});