Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: extensions/renderer/resources/media_router_bindings.js

Issue 2627463003: Convert MediaRouter mojom apis to intake url::Origin objects instead of strings (Closed)
Patch Set: change media_router_bindings to handle Origin<->string conversions Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/resources/extensions_renderer_resources.grd ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var mediaRouter; 5 var mediaRouter;
6 6
7 define('media_router_bindings', [ 7 define('media_router_bindings', [
8 'content/public/renderer/frame_interfaces', 8 'content/public/renderer/frame_interfaces',
9 'chrome/browser/media/router/mojo/media_router.mojom', 9 'chrome/browser/media/router/mojo/media_router.mojom',
10 'extensions/common/mojo/keep_alive.mojom', 10 'extensions/common/mojo/keep_alive.mojom',
11 'mojo/common/time.mojom', 11 'mojo/common/time.mojom',
12 'mojo/public/js/bindings', 12 'mojo/public/js/bindings',
13 'url/mojo/origin.mojom',
13 ], function(frameInterfaces, 14 ], function(frameInterfaces,
14 mediaRouterMojom, 15 mediaRouterMojom,
15 keepAliveMojom, 16 keepAliveMojom,
16 timeMojom, 17 timeMojom,
17 bindings) { 18 bindings,
19 originMojom) {
18 'use strict'; 20 'use strict';
19 21
20 /** 22 /**
21 * Converts a media sink to a MediaSink Mojo object. 23 * Converts a media sink to a MediaSink Mojo object.
22 * @param {!MediaSink} sink A media sink. 24 * @param {!MediaSink} sink A media sink.
23 * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object. 25 * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object.
24 */ 26 */
25 function sinkToMojo_(sink) { 27 function sinkToMojo_(sink) {
26 return new mediaRouterMojom.MediaSink({ 28 return new mediaRouterMojom.MediaSink({
27 'name': sink.friendlyName, 29 'name': sink.friendlyName,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 case 'went_away': 139 case 'went_away':
138 return PresentationConnectionCloseReason.WENT_AWAY; 140 return PresentationConnectionCloseReason.WENT_AWAY;
139 default: 141 default:
140 console.error('Unknown presentation connection close reason : ' + 142 console.error('Unknown presentation connection close reason : ' +
141 reason); 143 reason);
142 return PresentationConnectionCloseReason.CONNECTION_ERROR; 144 return PresentationConnectionCloseReason.CONNECTION_ERROR;
143 } 145 }
144 } 146 }
145 147
146 /** 148 /**
149 * Converts Mojo origin to string.
150 * @param {!originMojom.Origin} Mojo origin
151 * @return {!string}
152 */
153 function mojoOriginToString_(origin) {
mark a. foltz 2017/02/02 23:05:08 Please file a crbug to remove this conversion once
steimel 2017/02/03 00:17:31 Done.
154 if (origin.unique) {
155 return '';
156 }
157 var serializedOrigin = origin.scheme + '://' + origin.host;
mark a. foltz 2017/02/02 23:05:08 This could be a one liner with an ES6 template str
steimel 2017/02/03 00:17:31 Done.
158 if (origin.port) {
159 serializedOrigin += ':' + origin.port;
160 }
161 serializedOrigin += '/';
162 return serializedOrigin;
163 }
164
165 /**
166 * Converts string to Mojo origin.
167 * @param {!string} string origin
168 * @return {!originMojom.Origin}
169 */
170 function stringToMojoOrigin_(string) {
171 try {
172 var url = new URL(string);
mark a. foltz 2017/02/02 23:05:08 Is URL defined by the bindings for mojo URL?
steimel 2017/02/03 00:17:31 This is a native js URL object, not a mojo URL obj
173 } catch (err) {
174 if (string !== '') {
mark a. foltz 2017/02/02 23:05:08 Nit: I would put this check first, then attempt to
steimel 2017/02/03 00:17:31 Done.
175 console.error('Invalid URL: ' + string);
176 }
177 return new originMojom.Origin({ unique: true });
178 }
179 var origin = new originMojom.Origin();
180 origin.scheme = url.protocol.replace(':', '');
181 origin.host = url.hostname;
182 if (url.port) {
183 origin.port = Number.parseInt(url.port);
184 }
185 return origin;
186 }
187
188 /**
147 * Parses the given route request Error object and converts it to the 189 * Parses the given route request Error object and converts it to the
148 * corresponding result code. 190 * corresponding result code.
149 * @param {!Error} error 191 * @param {!Error} error
150 * @return {!mediaRouterMojom.RouteRequestResultCode} 192 * @return {!mediaRouterMojom.RouteRequestResultCode}
151 */ 193 */
152 function getRouteRequestResultCode_(error) { 194 function getRouteRequestResultCode_(error) {
153 return error.errorCode ? error.errorCode : 195 return error.errorCode ? error.errorCode :
154 mediaRouterMojom.RouteRequestResultCode.UNKNOWN_ERROR; 196 mediaRouterMojom.RouteRequestResultCode.UNKNOWN_ERROR;
155 } 197 }
156 198
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 /** 289 /**
248 * Called by the provider manager when a sink list for a given source is 290 * Called by the provider manager when a sink list for a given source is
249 * updated. 291 * updated.
250 * @param {!string} sourceUrn 292 * @param {!string} sourceUrn
251 * @param {!Array<!MediaSink>} sinks 293 * @param {!Array<!MediaSink>} sinks
252 * @param {!Array<string>} origins 294 * @param {!Array<string>} origins
253 */ 295 */
254 MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks, 296 MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks,
255 origins) { 297 origins) {
256 this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_), 298 this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_),
257 origins); 299 origins.map(stringToMojoOrigin_));
258 }; 300 };
259 301
260 /** 302 /**
261 * Called by the provider manager when a sink is found to notify the MR of the 303 * Called by the provider manager when a sink is found to notify the MR of the
262 * sink's ID. The actual sink will be returned through the normal sink list 304 * sink's ID. The actual sink will be returned through the normal sink list
263 * update process, so this helps the MR identify the search result in the 305 * update process, so this helps the MR identify the search result in the
264 * list. 306 * list.
265 * @param {string} pseudoSinkId ID of the pseudo sink that started the 307 * @param {string} pseudoSinkId ID of the pseudo sink that started the
266 * search. 308 * search.
267 * @param {string} sinkId ID of the newly-found sink. 309 * @param {string} sinkId ID of the newly-found sink.
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 }; 607 };
566 608
567 /** 609 /**
568 * Requests that |sinkId| render the media referenced by |sourceUrn|. If the 610 * Requests that |sinkId| render the media referenced by |sourceUrn|. If the
569 * request is from the Presentation API, then origin and tabId will 611 * request is from the Presentation API, then origin and tabId will
570 * be populated. 612 * be populated.
571 * @param {!string} sourceUrn Media source to render. 613 * @param {!string} sourceUrn Media source to render.
572 * @param {!string} sinkId Media sink ID. 614 * @param {!string} sinkId Media sink ID.
573 * @param {!string} presentationId Presentation ID from the site 615 * @param {!string} presentationId Presentation ID from the site
574 * requesting presentation. TODO(mfoltz): Remove. 616 * requesting presentation. TODO(mfoltz): Remove.
575 * @param {!string} origin Origin of site requesting presentation. 617 * @param {!originMojom.Origin} origin Origin of site requesting presentation.
576 * @param {!number} tabId ID of tab requesting presentation. 618 * @param {!number} tabId ID of tab requesting presentation.
577 * @param {!TimeDelta} timeout If positive, the timeout duration for the 619 * @param {!TimeDelta} timeout If positive, the timeout duration for the
578 * request. Otherwise, the default duration will be used. 620 * request. Otherwise, the default duration will be used.
579 * @param {!boolean} incognito If true, the route is being requested by 621 * @param {!boolean} incognito If true, the route is being requested by
580 * an incognito profile. 622 * an incognito profile.
581 * @return {!Promise.<!Object>} A Promise resolving to an object describing 623 * @return {!Promise.<!Object>} A Promise resolving to an object describing
582 * the newly created media route, or rejecting with an error message on 624 * the newly created media route, or rejecting with an error message on
583 * failure. 625 * failure.
584 */ 626 */
585 MediaRouteProvider.prototype.createRoute = 627 MediaRouteProvider.prototype.createRoute =
586 function(sourceUrn, sinkId, presentationId, origin, tabId, 628 function(sourceUrn, sinkId, presentationId, origin, tabId,
587 timeout, incognito) { 629 timeout, incognito) {
588 this.handlers_.onBeforeInvokeHandler(); 630 this.handlers_.onBeforeInvokeHandler();
589 return this.handlers_.createRoute( 631 return this.handlers_.createRoute(
590 sourceUrn, sinkId, presentationId, origin, tabId, 632 sourceUrn, sinkId, presentationId, mojoOriginToString_(origin), tabId,
591 Math.floor(timeout.microseconds / 1000), incognito) 633 Math.floor(timeout.microseconds / 1000), incognito)
592 .then(function(route) { 634 .then(function(route) {
593 return toSuccessRouteResponse_(route); 635 return toSuccessRouteResponse_(route);
594 }, 636 },
595 function(err) { 637 function(err) {
596 return toErrorRouteResponse_(err); 638 return toErrorRouteResponse_(err);
597 }); 639 });
598 }; 640 };
599 641
600 /** 642 /**
601 * Handles a request via the Presentation API to join an existing route given 643 * Handles a request via the Presentation API to join an existing route given
602 * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for 644 * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for
603 * validating same-origin/tab scope. 645 * validating same-origin/tab scope.
604 * @param {!string} sourceUrn Media source to render. 646 * @param {!string} sourceUrn Media source to render.
605 * @param {!string} presentationId Presentation ID to join. 647 * @param {!string} presentationId Presentation ID to join.
606 * @param {!string} origin Origin of site requesting join. 648 * @param {!originMojom.Origin} origin Origin of site requesting join.
607 * @param {!number} tabId ID of tab requesting join. 649 * @param {!number} tabId ID of tab requesting join.
608 * @param {!TimeDelta} timeout If positive, the timeout duration for the 650 * @param {!TimeDelta} timeout If positive, the timeout duration for the
609 * request. Otherwise, the default duration will be used. 651 * request. Otherwise, the default duration will be used.
610 * @param {!boolean} incognito If true, the route is being requested by 652 * @param {!boolean} incognito If true, the route is being requested by
611 * an incognito profile. 653 * an incognito profile.
612 * @return {!Promise.<!Object>} A Promise resolving to an object describing 654 * @return {!Promise.<!Object>} A Promise resolving to an object describing
613 * the newly created media route, or rejecting with an error message on 655 * the newly created media route, or rejecting with an error message on
614 * failure. 656 * failure.
615 */ 657 */
616 MediaRouteProvider.prototype.joinRoute = 658 MediaRouteProvider.prototype.joinRoute =
617 function(sourceUrn, presentationId, origin, tabId, timeout, 659 function(sourceUrn, presentationId, origin, tabId, timeout,
618 incognito) { 660 incognito) {
619 this.handlers_.onBeforeInvokeHandler(); 661 this.handlers_.onBeforeInvokeHandler();
620 return this.handlers_.joinRoute( 662 return this.handlers_.joinRoute(
621 sourceUrn, presentationId, origin, tabId, 663 sourceUrn, presentationId, mojoOriginToString_(origin), tabId,
622 Math.floor(timeout.microseconds / 1000), incognito) 664 Math.floor(timeout.microseconds / 1000), incognito)
623 .then(function(route) { 665 .then(function(route) {
624 return toSuccessRouteResponse_(route); 666 return toSuccessRouteResponse_(route);
625 }, 667 },
626 function(err) { 668 function(err) {
627 return toErrorRouteResponse_(err); 669 return toErrorRouteResponse_(err);
628 }); 670 });
629 }; 671 };
630 672
631 /** 673 /**
632 * Handles a request via the Presentation API to join an existing route given 674 * Handles a request via the Presentation API to join an existing route given
633 * by |sourceUrn| and |routeId|. |origin| and |tabId| are used for 675 * by |sourceUrn| and |routeId|. |origin| and |tabId| are used for
634 * validating same-origin/tab scope. 676 * validating same-origin/tab scope.
635 * @param {!string} sourceUrn Media source to render. 677 * @param {!string} sourceUrn Media source to render.
636 * @param {!string} routeId Route ID to join. 678 * @param {!string} routeId Route ID to join.
637 * @param {!string} presentationId Presentation ID to join. 679 * @param {!string} presentationId Presentation ID to join.
638 * @param {!string} origin Origin of site requesting join. 680 * @param {!originMojom.Origin} origin Origin of site requesting join.
639 * @param {!number} tabId ID of tab requesting join. 681 * @param {!number} tabId ID of tab requesting join.
640 * @param {!TimeDelta} timeout If positive, the timeout duration for the 682 * @param {!TimeDelta} timeout If positive, the timeout duration for the
641 * request. Otherwise, the default duration will be used. 683 * request. Otherwise, the default duration will be used.
642 * @param {!boolean} incognito If true, the route is being requested by 684 * @param {!boolean} incognito If true, the route is being requested by
643 * an incognito profile. 685 * an incognito profile.
644 * @return {!Promise.<!Object>} A Promise resolving to an object describing 686 * @return {!Promise.<!Object>} A Promise resolving to an object describing
645 * the newly created media route, or rejecting with an error message on 687 * the newly created media route, or rejecting with an error message on
646 * failure. 688 * failure.
647 */ 689 */
648 MediaRouteProvider.prototype.connectRouteByRouteId = 690 MediaRouteProvider.prototype.connectRouteByRouteId =
649 function(sourceUrn, routeId, presentationId, origin, tabId, 691 function(sourceUrn, routeId, presentationId, origin, tabId,
650 timeout, incognito) { 692 timeout, incognito) {
651 this.handlers_.onBeforeInvokeHandler(); 693 this.handlers_.onBeforeInvokeHandler();
652 return this.handlers_.connectRouteByRouteId( 694 return this.handlers_.connectRouteByRouteId(
653 sourceUrn, routeId, presentationId, origin, tabId, 695 sourceUrn, routeId, presentationId, mojoOriginToString_(origin), tabId,
654 Math.floor(timeout.microseconds / 1000), incognito) 696 Math.floor(timeout.microseconds / 1000), incognito)
655 .then(function(route) { 697 .then(function(route) {
656 return toSuccessRouteResponse_(route); 698 return toSuccessRouteResponse_(route);
657 }, 699 },
658 function(err) { 700 function(err) {
659 return toErrorRouteResponse_(err); 701 return toErrorRouteResponse_(err);
660 }); 702 });
661 }; 703 };
662 704
663 /** 705 /**
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 return Promise.resolve({ 843 return Promise.resolve({
802 'sink_id': this.handlers_.searchSinks(sinkId, sourceUrn, searchCriteria) 844 'sink_id': this.handlers_.searchSinks(sinkId, sourceUrn, searchCriteria)
803 }); 845 });
804 }; 846 };
805 847
806 mediaRouter = new MediaRouter(new mediaRouterMojom.MediaRouterPtr( 848 mediaRouter = new MediaRouter(new mediaRouterMojom.MediaRouterPtr(
807 frameInterfaces.getInterface(mediaRouterMojom.MediaRouter.name))); 849 frameInterfaces.getInterface(mediaRouterMojom.MediaRouter.name)));
808 850
809 return mediaRouter; 851 return mediaRouter;
810 }); 852 });
OLDNEW
« no previous file with comments | « extensions/renderer/resources/extensions_renderer_resources.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698