OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 var mediaRouterObserver; | |
6 | |
7 define('media_router_bindings', [ | |
8 'mojo/public/js/bindings', | |
9 'mojo/public/js/core', | |
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', | |
15 ], function(bindings, | |
16 core, | |
17 serviceProvider, | |
18 mediaRouterMojom, | |
19 keepAliveMojom, | |
20 connector, | |
21 routerModule) { | |
22 'use strict'; | |
23 | |
haibinlu
2015/06/01 18:36:33
add @param and @return
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
24 function routeToMojo(route, opt_sinkName) { | |
25 return new mediaRouterMojom.MediaRoute({ | |
26 'media_route_id': route.id, | |
27 'media_source': route.mediaSource, // nullable | |
haibinlu
2015/06/01 18:36:33
is 'nullable' necessary? since mojo def already do
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
28 'media_sink': new mediaRouterMojom.MediaSink({ | |
29 'sink_id': route.sinkId, | |
30 'name': opt_sinkName, | |
haibinlu
2015/06/01 18:36:33
this "new mediaRouterMojom.MediaSink" can use a ut
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
31 }), | |
32 'description': route.description, | |
33 'icon_url': route.iconUrl, // nullable | |
haibinlu
2015/06/01 18:36:32
ditto
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
34 'is_local': route.isLocal | |
35 }); | |
36 } | |
37 | |
38 /** | |
39 * @constructor | |
haibinlu
2015/06/01 18:36:33
@param
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
40 */ | |
41 function MediaRouterObserver(service) { | |
42 /** | |
43 * The Mojo service proxy. Allows extension code to call methods that reside | |
haibinlu
2015/06/01 18:36:33
document type for each member.
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
44 * in the browser. | |
45 */ | |
46 this.observer_ = service; | |
47 | |
48 /** | |
49 * The MRPM service delegate. Its methods are called by the browser-resident | |
50 * Mojo service. | |
51 */ | |
52 this.mrpm_ = new MediaRouter(this); | |
53 | |
54 /** | |
55 * The message pipe that connects the Media Router to mrpm_ across | |
56 * browser/renderer IPC boundaries. | |
57 * Object must remain in scope for the lifetime of the connection to | |
58 * prevent the connection from closing automatically. | |
59 */ | |
60 this.pipe_ = core.createMessagePipe(); | |
61 | |
62 /** | |
63 * Handle to a KeepAlive service object, which prevents the extension from | |
64 * being suspended as long as it remains in scope | |
65 */ | |
66 this.keepAlive_ = null; | |
67 | |
68 // Define the stub used to bind this.mrpm_ to the Mojo interface. | |
69 // Object must remain in scope for the lifetime of the connection to | |
70 // prevent the connection from closing automatically. | |
71 this.mediaRouterStub_ = connector.bindHandleToStub( | |
72 this.pipe_.handle0, mediaRouterMojom.MediaRouter); | |
73 | |
74 // Link the stub to impl code. | |
75 bindings.StubBindings(this.mediaRouterStub_).delegate = this.mrpm_; | |
76 } | |
77 | |
78 | |
79 /** | |
80 * Starts the Media Router service. | |
81 * @return {Promise<string>} instanceId | |
haibinlu
2015/06/01 18:36:32
!Promise or Promsie?
haibinlu
2015/06/01 18:36:33
explain instanceId
Kevin Marshall
2015/06/02 14:33:41
Done.
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
82 */ | |
83 MediaRouterObserver.prototype.start = function() { | |
84 // Register the MRPM with the MR service by connecting it to one of the | |
haibinlu
2015/06/01 18:36:33
can this comment be part of the method doc?
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
85 // pipe's handles. | |
86 return this.observer_.provideMediaRouter(this.pipe_.handle1).then( | |
87 function(result) { | |
88 return result.instance_id; | |
89 }.bind(this)); | |
90 } | |
91 | |
92 | |
93 /** | |
94 * Converts a receiver object to a MediaSink Mojo object. | |
95 * @param {Object} receiver | |
haibinlu
2015/06/01 18:36:33
'receiver' to 'sink'. we should use the naming con
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
96 * @return {mediaRouterMojom.MediaSink} | |
97 */ | |
98 MediaRouterObserver.sinkToMojo_ = function(receiver) { | |
99 return new mediaRouterMojom.MediaSink({ | |
100 'name': receiver.friendlyName, | |
101 'sink_id': receiver.id, | |
102 }); | |
103 } | |
104 | |
105 | |
106 /** | |
107 * Sets the service delegate methods. | |
108 * @type {Object} | |
haibinlu
2015/06/01 18:36:34
what is @type?
is the 'handlers' nullable?
Kevin Marshall
2015/06/02 14:33:40
It is nullable.
| |
109 */ | |
110 MediaRouterObserver.prototype.setHandlers = function(handlers) { | |
111 this.mrpm_.setHandlers(handlers); | |
112 } | |
113 | |
114 | |
115 /** | |
116 * Gets the keep alive status. | |
117 * @return {boolean} | |
118 */ | |
119 MediaRouterObserver.prototype.getKeepAlive = function() { | |
120 return this.keepAlive_ != null; | |
121 }; | |
122 | |
123 | |
124 /** | |
125 * Called by the Provider Manager when a sink list for a given source is | |
126 * updated. | |
127 * @param {string} sourceUrn | |
128 * @param {Array<Object>} sinks | |
haibinlu
2015/06/01 18:36:32
Array or !Array?
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
129 */ | |
130 MediaRouterObserver.prototype.onSinksReceived = function(sourceUrn, sinks) { | |
131 this.observer_.onSinksReceived(sourceUrn, | |
132 sinks.map(MediaRouterObserver.sinkToMojo_)); | |
133 }; | |
134 | |
135 | |
136 /** | |
137 * Called by the Provider Manager to keep the extension from suspending | |
138 * if it enters a state where suspension is undesirable (e.g. there is an | |
139 * active MediaRoute.) | |
140 * If keepAlive is true, the extension is kept alive. | |
141 * If keepAlive is false, the extension is allowed to suspend. | |
142 * | |
143 * @param {boolean} keepAlive | |
144 */ | |
145 MediaRouterObserver.prototype.setKeepAlive = function(keepAlive) { | |
146 if (keepAlive === false && this.keepAlive_) { | |
147 this.keepAlive_.close(); | |
148 this.keepAlive_ = null; | |
149 } else if (keepAlive === true && !this.keepAlive_) { | |
150 this.keepAlive_ = new routerModule.Router( | |
151 serviceProvider.connectToService( | |
152 keepAliveMojom.KeepAlive.name)); | |
153 } | |
154 }; | |
155 | |
156 | |
157 /** | |
158 * Sends a message to an active mediaRoute. | |
haibinlu
2015/06/01 18:36:33
media route
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
159 * | |
160 * @param {string} routeId | |
161 * @param {Object} message | |
haibinlu
2015/06/01 18:36:32
@param {!Object|string} message A message that can
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
162 */ | |
163 MediaRouterObserver.prototype.onMessage = function(routeId, message) { | |
164 this.observer_.onMessage(routeId, JSON.stringify(message)); | |
165 }; | |
166 | |
167 | |
168 /** | |
169 * Reports an issue to the Media Router. | |
170 * | |
171 * @param {Object} issue | |
haibinlu
2015/06/01 18:36:32
!Object
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
172 */ | |
173 MediaRouterObserver.prototype.onIssue = function(issue) { | |
174 function issueSeverityToMojo_(severity) { | |
175 switch (severity) { | |
176 case 'fatal': | |
177 return mediaRouterMojom.Issue.Severity.FATAL; | |
178 case 'warning': | |
179 return mediaRouterMojom.Issue.Severity.WARNING; | |
180 case 'notification': | |
181 return mediaRouterMojom.Issue.Severity.NOTIFICATION; | |
182 default: | |
183 console.error('Unknown issue severity: ' + severity); | |
184 return mediaRouterMojom.Issue.Severity.NOTIFICATION; | |
185 } | |
186 } | |
187 | |
188 function issueActionToMojo_(action) { | |
189 switch (action) { | |
190 case 'ok': | |
191 return mediaRouterMojom.Issue.ActionType.OK; | |
192 case 'cancel': | |
193 return mediaRouterMojom.Issue.ActionType.CANCEL; | |
194 case 'dismiss': | |
195 return mediaRouterMojom.Issue.ActionType.DISMISS; | |
196 case 'learn_more': | |
197 return mediaRouterMojom.Issue.ActionType.LEARN_MORE; | |
198 default: | |
199 console.error('Unknown issue action type : ' + action); | |
200 return mediaRouterMojom.Issue.ActionType.OK; | |
201 } | |
202 } | |
203 | |
204 var secondaryActions = (issue.secondaryActions || []).map(function(e) { | |
205 return issueActionToMojo_(e); | |
206 }); | |
207 this.observer_.onIssue(new mediaRouterMojom.Issue({ | |
208 'route_id': issue.routeId, | |
209 'severity': issueSeverityToMojo_(issue.severity), | |
210 'title': issue.title, | |
211 'message': issue.message, | |
212 'default_action': issueActionToMojo_(issue.defaultAction), | |
213 'secondary_actions': secondaryActions, | |
214 'help_url': issue.helpUrl, | |
215 'is_blocking': issue.isBlocking | |
216 })); | |
217 }; | |
218 | |
219 | |
haibinlu
2015/06/01 18:36:33
@param
Kevin Marshall
2015/06/02 14:33:41
Done.
| |
220 MediaRouterObserver.prototype.onRoutesUpdated = function(routes, sinks) { | |
221 // Create an inverted index relating sink IDs to their names. | |
222 var sinkNameMap = {}; | |
223 console.log('sinks: ' + JSON.stringify(sinks)); | |
haibinlu
2015/06/01 18:36:33
do we always want to log sinks?
Kevin Marshall
2015/06/02 14:33:40
We don't, thanks.
| |
224 for (var i = 0; i < sinks.length; i++) { | |
225 sinkNameMap[sinks[i].id] = sinks[i].friendlyName; | |
226 } | |
227 | |
228 // Convert mr.Routes to Mojo routes, and adding their sink names | |
haibinlu
2015/06/01 18:36:33
the type, mr.Routes is not in Chromium.
Kevin Marshall
2015/06/02 14:33:40
done.
| |
229 // via sinkNameMap. | |
230 var mojoRoutes = []; | |
231 for (var j = 0; j < routes.length; j++) { | |
232 mojoRoutes.push(routeToMojo(routes[j], sinkNameMap[routes[j].sinkId])); | |
233 } | |
234 | |
235 this.observer_.onRoutesUpdated( | |
236 mojoRoutes, | |
237 sinks.map(MediaRouterObserver.sinkToMojo_)); | |
238 }; | |
239 | |
240 | |
241 /** | |
242 * Called by the Provider Manager when an error was encountered for a media | |
243 * route. | |
244 * | |
245 * @param {string} requestId | |
haibinlu
2015/06/01 18:36:33
document the requestId
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
246 * @param {string} error | |
247 */ | |
248 MediaRouterObserver.prototype.onRouteResponseError = | |
249 function(requestId, error) { | |
250 this.observer_.onRouteResponseError(requestId, error); | |
251 }; | |
252 | |
253 | |
254 MediaRouterObserver.prototype.onRouteResponseReceived = | |
255 function(requestId, routeId) { | |
256 this.observer_.onRouteResponseReceived(requestId, routeId); | |
257 }; | |
258 | |
259 | |
260 /** | |
261 * Object containing JS callbacks into Provider Manager code. | |
262 * @constructor | |
263 * @struct | |
264 */ | |
265 function MediaRouterHandlers() { | |
266 /** | |
267 * @type {function(!string, !string, !string=, !string=, !number=} | |
268 */ | |
269 this.createRoute = null; | |
270 | |
271 /** | |
272 * @type {function(!string, !string, !string, !number)} | |
haibinlu
2015/06/01 18:36:33
use '!string' or 'string' consistently
Kevin Marshall
2015/06/02 14:33:40
Done.
| |
273 */ | |
274 this.joinRoute = null; | |
275 | |
276 /** | |
277 * @type {function(string)} | |
278 */ | |
279 this.closeRoute = null; | |
280 | |
281 /** | |
282 * @type {function(string)} | |
283 */ | |
284 this.startObservingMediaSinks = null; | |
285 | |
286 /** | |
287 * @type {function(string)} | |
288 */ | |
289 this.stopObservingMediaSinks = null; | |
290 | |
291 /** | |
292 * @type {function(string, string, string)} | |
293 */ | |
294 this.postMessage = null; | |
295 | |
296 /** | |
297 * @type {function()} | |
298 */ | |
299 this.startObservingMediaRoutes = null; | |
300 | |
301 /** | |
302 * @type {function()} | |
303 */ | |
304 this.stopObservingMediaRoutes = null; | |
305 }; | |
306 | |
307 | |
308 /** | |
309 * Routes calls from Media Router to the Provider Manager extension. | |
310 * Registered with the MediaRouter stub. | |
311 * | |
312 * @constructor | |
313 */ | |
314 function MediaRouter(mediaRouterObserver) { | |
315 /** | |
316 * Object containing JS callbacks into Provider Manager code. | |
317 * @type {MediaRouterHandlers} | |
haibinlu
2015/06/01 18:36:33
!MediaRouterHandlers
| |
318 */ | |
319 this.handlers_ = new MediaRouterHandlers(); | |
320 | |
321 /** | |
322 * Proxy class to the browser's Media Router Mojo service. | |
323 */ | |
haibinlu
2015/06/01 18:36:34
@type
| |
324 this.mediaRouter_ = mediaRouterObserver; | |
325 } | |
326 MediaRouter.prototype = Object.create( | |
327 mediaRouterMojom.MediaRouter.stubClass.prototype); | |
328 | |
329 | |
330 /* | |
331 * Sets the callback handler used to invoke methods in the Provider Manager. | |
332 * @param {MediaRouterHandlers} handlers | |
333 */ | |
334 MediaRouter.prototype.setHandlers = function(handlers) { | |
335 this.handlers_ = handlers; | |
haibinlu
2015/06/01 18:36:33
can you check all the required methods here instea
Kevin Marshall
2015/06/02 14:33:40
Good idea, done.
| |
336 } | |
337 | |
338 | |
339 /** | |
340 * Starts querying for sinks capable of displaying the media |sourceUrn|. | |
341 * Results are returned by calling OnSinksReceived. | |
342 * @param {string} sourceUrn | |
343 */ | |
344 MediaRouter.prototype.startObservingMediaSinks = | |
345 function(sourceUrn) { | |
346 if (!this.handlers_ || !this.handlers_.startObservingMediaSinks) { | |
347 console.error('startObservingMediaSinks handler not registered.'); | |
348 return; | |
349 } | |
350 this.handlers_.startObservingMediaSinks(sourceUrn); | |
351 }; | |
352 | |
353 | |
354 /** | |
355 * Stops querying for sinks capable of displaying the media |sourceUrn|. | |
356 * @param {string} sourceUrn | |
357 */ | |
358 MediaRouter.prototype.stopObservingMediaSinks = | |
359 function(sourceUrn) { | |
360 if (!this.handlers_ || !this.handlers_.stopObservingMediaSinks) { | |
361 console.error('stopObservingMediaSinks handler not registered.'); | |
362 return; | |
363 } | |
364 this.handlers_.stopObservingMediaSinks(sourceUrn); | |
365 }; | |
366 | |
367 | |
368 /** | |
369 * Requests that |sinkId| render the media referenced by |sourceUrn|. | |
370 * @param {!string} sourceUrn | |
371 * @param {!string} sinkId | |
372 * @param {!string=} opt_presentationId | |
373 * @param {!string=} opt_origin | |
374 * @param {!number=} opt_tabId | |
375 * @return {!Promise.<!Object>} | |
haibinlu
2015/06/01 18:36:32
document that this will always resolve and never r
Kevin Marshall
2015/06/02 14:33:40
I don't think it's worth commenting, since this wo
| |
376 */ | |
377 MediaRouter.prototype.createRoute = | |
378 function(sourceUrn, sinkId, opt_presentationId, opt_origin, opt_tabId) { | |
379 if (!this.handlers_ || !this.handlers_.createRoute) { | |
380 return Promise.resolve({ | |
381 error_text: 'createRoute handler not registered.' | |
382 }); | |
383 } | |
384 return this.handlers_.createRoute( | |
385 sourceUrn, sinkId, opt_presentationId, opt_origin, opt_tabId) | |
386 .then(function(route) { | |
387 // Sink name is not used, so it is omitted here. | |
388 return {route: routeToMojo(route, "")}; | |
389 }.bind(this)) | |
390 .catch(function(err) { | |
391 return {error_text: err.message}; | |
392 }); | |
393 }; | |
394 | |
395 /** | |
396 * Join an existing route given by |presentationId| and render media | |
397 * referenced by |sourceUrn|. |origin| and |tabId| are used for checking | |
398 * origin/tab scope. | |
399 * @param {!string} sourceUrn | |
400 * @param {!string} presentationId | |
401 * @param {!string} origin | |
402 * @param {!number} tabId | |
403 * @return {!Promise.<!Object>} Resolved with the route on success, | |
404 * or with an error message on failure. | |
405 */ | |
406 MediaRouter.prototype.joinRoute = | |
407 function(sourceUrn, presentationId, origin, tabId) { | |
408 if (!this.handlers_.joinRoute) { | |
409 return Promise.resolve({error_text: 'joinRoute handler not registered.'}); | |
410 } | |
411 return this.handlers_.joinRoute(sourceUrn, presentationId, origin, tabId) | |
412 .then(function(newRoute) { | |
413 return {route: routeToMojo(newRoute)}; | |
414 }, | |
415 function(err) { | |
416 return {error_text: 'Error joining route: ' + err.message}; | |
417 }); | |
418 }; | |
419 | |
420 | |
421 /** | |
422 * Closes route specified by |routeId| | |
423 * @param {string} routeId | |
424 */ | |
425 MediaRouter.prototype.closeRoute = function(routeId) { | |
426 if (!this.handlers_ || !this.handlers_.closeRoute) { | |
427 console.error('closeRoute handler not registered.'); | |
428 return; | |
429 } | |
430 this.handlers_.closeRoute(routeId); | |
431 }; | |
432 | |
433 | |
434 /** | |
435 * Posts message to a sink connected by route with |routeId|. | |
436 * @param {string} routeId | |
437 * @param {string} message | |
438 * @param {string} extraInfoJson | |
439 */ | |
440 MediaRouter.prototype.postMessage = function( | |
441 routeId, message, extraInfoJson) { | |
442 if (!this.handlers_ || !this.handlers_.postMessage) { | |
443 console.error('postMessage handler not registered.'); | |
444 return; | |
445 } | |
446 this.handlers_.postMessage(routeId, message, JSON.parse(extraInfoJson)); | |
447 }; | |
448 | |
449 | |
450 /** | |
451 * Requests that the Provider Manager start sending information about active | |
452 * media routes to the Media Router. | |
453 */ | |
454 MediaRouter.prototype.startObservingMediaRoutes = function() { | |
455 if (!this.handlers_ || !this.handlers_.startObservingMediaRoutes) { | |
456 console.error('startObservingMediaRoutes handler not registered.'); | |
457 return; | |
458 } | |
459 | |
460 this.handlers_.startObservingMediaRoutes(); | |
461 }; | |
462 | |
463 | |
464 /** | |
465 * Requests that the Provider Manager stop sending information about active | |
466 * media routes to the Media Router. | |
467 */ | |
468 MediaRouter.prototype.stopObservingMediaRoutes = function() { | |
469 if (!this.handlers_ || !this.handlers_.stopObservingMediaRoutes) { | |
470 console.error('stopObservingMediaRoutes handler not registered.'); | |
471 return; | |
472 } | |
473 | |
474 this.handlers_.stopObservingMediaRoutes(); | |
475 }; | |
476 | |
477 mediaRouterObserver = new MediaRouterObserver(connector.bindHandleToProxy( | |
478 serviceProvider.connectToService( | |
479 mediaRouterMojom.MediaRouterObserver.name), | |
480 mediaRouterMojom.MediaRouterObserver)); | |
481 | |
482 return mediaRouterObserver; | |
483 }); | |
484 | |
OLD | NEW |