1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 define('media_router_bindings', [
8 'mojo/public/js/bindings',
10 'content/public/renderer/service_provider',
11 'chrome/browser/media/router/media_router.mojom',
12 'extensions/common/mojo/keep_alive.mojom',
13 'mojo/public/js/connection',
14 'mojo/public/js/router',
25 * Converts a media sink to a MediaSink Mojo object.
26 * @param {!MediaSink} sink A media sink.
27 * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object.
29 function sinkToMojo_(sink) {
30 return new mediaRouterMojom.MediaSink({
31 'name': sink.friendlyName,
37 * Returns a Mojo MediaRoute object given a MediaRoute and a
39 * @param {!MediaRoute} route
40 * @param {!string} sinkName
41 * @return {!mojo.MediaRoute}
43 function routeToMojo_(route, sinkName) {
44 return new mediaRouterMojom.MediaRoute({
45 'media_route_id': route.id,
46 'media_source': route.mediaSource,
47 'media_sink': new mediaRouterMojom.MediaSink({
48 'sink_id': route.sinkId,
51 'description': route.description,
52 'icon_url': route.iconUrl,
53 'is_local': route.isLocal
58 * Converts a route message to a RouteMessage Mojo object.
59 * @param {!RouteMessage} message
60 * @return {!mediaRouterMojom.RouteMessage} A Mojo RouteMessage object.
62 function messageToMojo_(message) {
63 if ("string" == typeof message.message) {
64 return new mediaRouterMojom.RouteMessage({
65 'route_id': message.routeId,
66 'type': mediaRouterMojom.RouteMessage.Type.TEXT,
67 'message': message.message,
70 return new mediaRouterMojom.RouteMessage({
71 'route_id': message.routeId,
72 'type': mediaRouterMojom.RouteMessage.Type.BINARY,
73 'data': message.message,
79 * Creates a new MediaRouter.
80 * Converts a route struct to its Mojo form.
81 * @param {!MediaRouterService} service
84 function MediaRouter(service) {
86 * The Mojo service proxy. Allows extension code to call methods that reside
88 * @type {!MediaRouterService}
90 this.service_ = service;
93 * The provider manager service delegate. Its methods are called by the
94 * browser-resident Mojo service.
95 * @type {!MediaRouter}
97 this.mrpm_ = new MediaRouteProvider(this);
100 * The message pipe that connects the Media Router to mrpm_ across
101 * browser/renderer IPC boundaries. Object must remain in scope for the
102 * lifetime of the connection to prevent the connection from closing
104 * @type {!mojo.MessagePipe}
106 this.pipe_ = core.createMessagePipe();
109 * Handle to a KeepAlive service object, which prevents the extension from
110 * being suspended as long as it remains in scope.
113 this.keepAlive_ = null;
116 * The stub used to bind the service delegate to the Mojo interface.
117 * Object must remain in scope for the lifetime of the connection to
118 * prevent the connection from closing automatically.
119 * @type {!mojom.MediaRouter}
121 this.mediaRouteProviderStub_ = connector.bindHandleToStub(
122 this.pipe_.handle0, mediaRouterMojom.MediaRouteProvider);
124 // Link mediaRouteProviderStub_ to the provider manager delegate.
125 bindings.StubBindings(this.mediaRouteProviderStub_).delegate = this.mrpm_;
129 * Registers the Media Router Provider Manager with the Media Router.
130 * @return {!Promise<string>} Instance ID for the Media Router.
132 MediaRouter.prototype.start = function() {
133 return this.service_.registerMediaRouteProvider(this.pipe_.handle1).then(
135 return result.instance_id;
140 * Sets the service delegate methods.
141 * @param {Object} handlers
143 MediaRouter.prototype.setHandlers = function(handlers) {
144 this.mrpm_.setHandlers(handlers);
148 * The keep alive status.
151 MediaRouter.prototype.getKeepAlive = function() {
152 return this.keepAlive_ != null;
156 * Called by the provider manager when a sink list for a given source is
158 * @param {!string} sourceUrn
159 * @param {!Array<!MediaSink>} sinks
161 MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks) {
162 this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_));
166 * Called by the provider manager to keep the extension from suspending
167 * if it enters a state where suspension is undesirable (e.g. there is an
168 * active MediaRoute.)
169 * If keepAlive is true, the extension is kept alive.
170 * If keepAlive is false, the extension is allowed to suspend.
171 * @param {boolean} keepAlive
173 MediaRouter.prototype.setKeepAlive = function(keepAlive) {
174 if (keepAlive === false && this.keepAlive_) {
175 this.keepAlive_.close();
176 this.keepAlive_ = null;
177 } else if (keepAlive === true && !this.keepAlive_) {
178 this.keepAlive_ = new routerModule.Router(
179 serviceProvider.connectToService(
180 keepAliveMojom.KeepAlive.name));
185 * Called by the provider manager to send an issue from a media route
186 * provider to the Media Router, to show the user.
187 * @param {!Object} issue The issue object.
189 MediaRouter.prototype.onIssue = function(issue) {
190 function issueSeverityToMojo_(severity) {
193 return mediaRouterMojom.Issue.Severity.FATAL;
195 return mediaRouterMojom.Issue.Severity.WARNING;
197 return mediaRouterMojom.Issue.Severity.NOTIFICATION;
199 console.error('Unknown issue severity: ' + severity);
200 return mediaRouterMojom.Issue.Severity.NOTIFICATION;
204 function issueActionToMojo_(action) {
207 return mediaRouterMojom.Issue.ActionType.OK;
209 return mediaRouterMojom.Issue.ActionType.CANCEL;
211 return mediaRouterMojom.Issue.ActionType.DISMISS;
213 return mediaRouterMojom.Issue.ActionType.LEARN_MORE;
215 console.error('Unknown issue action type : ' + action);
216 return mediaRouterMojom.Issue.ActionType.OK;
220 var secondaryActions = (issue.secondaryActions || []).map(function(e) {
221 return issueActionToMojo_(e);
223 this.service_.onIssue(new mediaRouterMojom.Issue({
224 'route_id': issue.routeId,
225 'severity': issueSeverityToMojo_(issue.severity),
226 'title': issue.title,
227 'message': issue.message,
228 'default_action': issueActionToMojo_(issue.defaultAction),
229 'secondary_actions': secondaryActions,
230 'help_url': issue.helpUrl,
231 'is_blocking': issue.isBlocking
236 * Called by the provider manager when the set of active routes
238 * @param {!Array<MediaRoute>} routes The active set of media routes.
239 * @param {!Array<MediaSink>} sinks The active set of media sinks.
241 MediaRouter.prototype.onRoutesUpdated = function(routes, sinks) {
242 // Create an inverted index relating sink IDs to their names.
243 var sinkNameMap = {};
244 for (var i = 0; i < sinks.length; i++) {
245 sinkNameMap[sinks[i].id] = sinks[i].friendlyName;
248 // Convert MediaRoutes to Mojo objects and add their sink names
250 var mojoRoutes = routes.map(function(nextRoute) {
251 return routeToMojo_(nextRoute, sinkNameMap[nextRoute.sinkId]);
254 this.service_.onRoutesUpdated(mojoRoutes, sinks.map(sinkToMojo_));
258 * Called by the Provider Manager when an error was encountered in response
259 * to a media route creation request.
260 * @param {!string} requestId The request id.
261 * @param {!string} error The error.
263 MediaRouter.prototype.onRouteResponseError =
264 function(requestId, error) {
265 this.service_.onRouteResponseError(requestId, error);
269 * Called by the provider manager when a route was able to be created by a
270 * media route provider.
272 * @param {string} requestId The media route request id.
273 * @param {string} routeId The id of the media route that was created.
275 MediaRouter.prototype.onRouteResponseReceived =
276 function(requestId, routeId) {
277 this.service_.onRouteResponseReceived(requestId, routeId);
281 * Object containing callbacks set by the provider manager.
282 * TODO(mfoltz): Better named ProviderManagerDelegate?
287 function MediaRouterHandlers() {
289 * @type {function(!string, !string, !string, !string, !number}
291 this.createRoute = null;
294 * @type {function(!string, !string, !string, !number)}
296 this.joinRoute = null;
299 * @type {function(string)}
301 this.closeRoute = null;
304 * @type {function(string)}
306 this.startObservingMediaSinks = null;
309 * @type {function(string)}
311 this.stopObservingMediaSinks = null;
314 * @type {function(string, string): Promise}
316 this.sendRouteMessage = null;
319 * @type {function(Array.<string>): Promise.<Array.<RouteMessage>>}
321 this.listenForRouteMessages = null;
326 this.startObservingMediaRoutes = null;
331 this.stopObservingMediaRoutes = null;
335 * Routes calls from Media Router to the provider manager extension.
336 * Registered with the MediaRouter stub.
337 * @param {!MediaRouter} MediaRouter proxy to call into the
338 * Media Router mojo interface.
341 function MediaRouteProvider(mediaRouter) {
342 mediaRouterMojom.MediaRouteProvider.stubClass.call(this);
345 * Object containing JS callbacks into Provider Manager code.
346 * @type {!MediaRouterHandlers}
348 this.handlers_ = new MediaRouterHandlers();
351 * Proxy class to the browser's Media Router Mojo service.
352 * @type {!MediaRouter}
354 this.mediaRouter_ = mediaRouter;
356 MediaRouteProvider.prototype = Object.create(
357 mediaRouterMojom.MediaRouteProvider.stubClass.prototype);
360 * Sets the callback handler used to invoke methods in the provider manager.
362 * TODO(mfoltz): Rename to something more explicit?
363 * @param {!MediaRouterHandlers} handlers
365 MediaRouteProvider.prototype.setHandlers = function(handlers) {
366 this.handlers_ = handlers;
367 var requiredHandlers = [
368 'stopObservingMediaRoutes',
369 'startObservingMediaRoutes',
371 'listenForRouteMessages',
375 'stopObservingMediaSinks',
376 'startObservingMediaRoutes'
378 requiredHandlers.forEach(function(nextHandler) {
379 if (handlers[nextHandler] === undefined) {
380 console.error(nextHandler + ' handler not registered.');
386 * Starts querying for sinks capable of displaying the media source
387 * designated by |sourceUrn|. Results are returned by calling
389 * @param {!string} sourceUrn
391 MediaRouteProvider.prototype.startObservingMediaSinks =
392 function(sourceUrn) {
393 this.handlers_.startObservingMediaSinks(sourceUrn);
397 * Stops querying for sinks capable of displaying |sourceUrn|.
398 * @param {!string} sourceUrn
400 MediaRouteProvider.prototype.stopObservingMediaSinks =
401 function(sourceUrn) {
402 this.handlers_.stopObservingMediaSinks(sourceUrn);
406 * Requests that |sinkId| render the media referenced by |sourceUrn|. If the
407 * request is from the Presentation API, then origin and tabId will
409 * @param {!string} sourceUrn Media source to render.
410 * @param {!string} sinkId Media sink ID.
411 * @param {!string} presentationId Presentation ID from the site
412 * requesting presentation. TODO(mfoltz): Remove.
413 * @param {!string} origin Origin of site requesting presentation.
414 * @param {!number} tabId ID of tab requesting presentation.
415 * @return {!Promise.<!Object>} A Promise resolving to an object describing
416 * the newly created media route, or rejecting with an error message on
419 MediaRouteProvider.prototype.createRoute =
420 function(sourceUrn, sinkId, presentationId, origin, tabId) {
421 return this.handlers_.createRoute(
422 sourceUrn, sinkId, presentationId, origin, tabId)
423 .then(function(route) {
424 // Sink name is not used, so it is omitted here.
425 return {route: routeToMojo_(route, "")};
427 .catch(function(err) {
428 return {error_text: 'Error creating route: ' + err.message};
433 * Handles a request via the Presentation API to join an existing route given
434 * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for
435 * validating same-origin/tab scope.
436 * @param {!string} sourceUrn Media source to render.
437 * @param {!string} presentationId Presentation ID to join.
438 * @param {!string} origin Origin of site requesting join.
439 * @param {!number} tabId ID of tab requesting join.
440 * @return {!Promise.<!Object>} A Promise resolving to an object describing
441 * the newly created media route, or rejecting with an error message on
444 MediaRouteProvider.prototype.joinRoute =
445 function(sourceUrn, presentationId, origin, tabId) {
446 return this.handlers_.joinRoute(sourceUrn, presentationId, origin, tabId)
447 .then(function(newRoute) {
448 // Sink name is not used, so it is omitted here.
449 return {route: routeToMojo_(newRoute, "")};
452 return {error_text: 'Error joining route: ' + err.message};
457 * Closes the route specified by |routeId|.
458 * @param {!string} routeId
460 MediaRouteProvider.prototype.closeRoute = function(routeId) {
461 this.handlers_.closeRoute(routeId);
465 * Posts a message to the route designated by |routeId|.
466 * @param {!string} routeId
467 * @param {!string} message
468 * @return {!Promise.<boolean>} Resolved with true if the message was sent,
469 * or false on failure.
471 MediaRouteProvider.prototype.sendRouteMessage = function(
473 return this.handlers_.sendRouteMessage(routeId, message)
482 * Listen for next batch of messages from one of the routeIds.
483 * @param {!Array.<string>} routeIds
484 * @return {!Promise.<Array.<RouteMessage>>} Resolved with a list of messages,
485 * an empty list if an error occurred.
487 MediaRouteProvider.prototype.listenForRouteMessages = function(routeIds) {
488 return this.handlers_.listenForRouteMessages(routeIds)
489 .then(function(messages) {
490 return {'messages': messages.map(messageToMojo_)};
492 return {'messages': []};
497 * Requests that the provider manager start sending information about active
498 * media routes to the Media Router.
500 MediaRouteProvider.prototype.startObservingMediaRoutes = function() {
501 this.handlers_.startObservingMediaRoutes();
505 * Requests that the provider manager stop sending information about active
506 * media routes to the Media Router.
508 MediaRouteProvider.prototype.stopObservingMediaRoutes = function() {
509 this.handlers_.stopObservingMediaRoutes();
512 mediaRouter = new MediaRouter(connector.bindHandleToProxy(
513 serviceProvider.connectToService(
514 mediaRouterMojom.MediaRouter.name),
515 mediaRouterMojom.MediaRouter));