| OLD | NEW |
| 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', | |
| 14 ], function(frameInterfaces, | 13 ], function(frameInterfaces, |
| 15 mediaRouterMojom, | 14 mediaRouterMojom, |
| 16 keepAliveMojom, | 15 keepAliveMojom, |
| 17 timeMojom, | 16 timeMojom, |
| 18 bindings, | 17 bindings) { |
| 19 originMojom) { | |
| 20 'use strict'; | 18 'use strict'; |
| 21 | 19 |
| 22 /** | 20 /** |
| 23 * Converts a media sink to a MediaSink Mojo object. | 21 * Converts a media sink to a MediaSink Mojo object. |
| 24 * @param {!MediaSink} sink A media sink. | 22 * @param {!MediaSink} sink A media sink. |
| 25 * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object. | 23 * @return {!mediaRouterMojom.MediaSink} A Mojo MediaSink object. |
| 26 */ | 24 */ |
| 27 function sinkToMojo_(sink) { | 25 function sinkToMojo_(sink) { |
| 28 return new mediaRouterMojom.MediaSink({ | 26 return new mediaRouterMojom.MediaSink({ |
| 29 'name': sink.friendlyName, | 27 'name': sink.friendlyName, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 return PresentationConnectionCloseReason.CLOSED; | 136 return PresentationConnectionCloseReason.CLOSED; |
| 139 case 'went_away': | 137 case 'went_away': |
| 140 return PresentationConnectionCloseReason.WENT_AWAY; | 138 return PresentationConnectionCloseReason.WENT_AWAY; |
| 141 default: | 139 default: |
| 142 console.error('Unknown presentation connection close reason : ' + | 140 console.error('Unknown presentation connection close reason : ' + |
| 143 reason); | 141 reason); |
| 144 return PresentationConnectionCloseReason.CONNECTION_ERROR; | 142 return PresentationConnectionCloseReason.CONNECTION_ERROR; |
| 145 } | 143 } |
| 146 } | 144 } |
| 147 | 145 |
| 148 // TODO(crbug.com/688177): remove this conversion. | |
| 149 /** | |
| 150 * Converts Mojo origin to string. | |
| 151 * @param {!originMojom.Origin} Mojo origin | |
| 152 * @return {string} | |
| 153 */ | |
| 154 function mojoOriginToString_(origin) { | |
| 155 return origin.unique ? '' : | |
| 156 `${origin.scheme}:\/\/${origin.host}` + | |
| 157 `${origin.port ? `:${origin.port}` : ''}/` | |
| 158 } | |
| 159 | |
| 160 // TODO(crbug.com/688177): remove this conversion. | |
| 161 /** | |
| 162 * Converts string to Mojo origin. | |
| 163 * @param {string} origin | |
| 164 * @return {!originMojom.Origin} | |
| 165 */ | |
| 166 function stringToMojoOrigin_(origin) { | |
| 167 var url = new URL(origin); | |
| 168 var mojoOrigin = {}; | |
| 169 mojoOrigin.scheme = url.protocol.replace(':', ''); | |
| 170 mojoOrigin.host = url.hostname; | |
| 171 var port = url.port ? Number.parseInt(url.port) : 0; | |
| 172 switch (mojoOrigin.scheme) { | |
| 173 case 'http': | |
| 174 mojoOrigin.port = port || 80; | |
| 175 break; | |
| 176 case 'https': | |
| 177 mojoOrigin.port = port || 443; | |
| 178 break; | |
| 179 default: | |
| 180 throw new Error('Scheme must be http or https'); | |
| 181 } | |
| 182 return new originMojom.Origin(mojoOrigin); | |
| 183 } | |
| 184 | |
| 185 /** | 146 /** |
| 186 * Parses the given route request Error object and converts it to the | 147 * Parses the given route request Error object and converts it to the |
| 187 * corresponding result code. | 148 * corresponding result code. |
| 188 * @param {!Error} error | 149 * @param {!Error} error |
| 189 * @return {!mediaRouterMojom.RouteRequestResultCode} | 150 * @return {!mediaRouterMojom.RouteRequestResultCode} |
| 190 */ | 151 */ |
| 191 function getRouteRequestResultCode_(error) { | 152 function getRouteRequestResultCode_(error) { |
| 192 return error.errorCode ? error.errorCode : | 153 return error.errorCode ? error.errorCode : |
| 193 mediaRouterMojom.RouteRequestResultCode.UNKNOWN_ERROR; | 154 mediaRouterMojom.RouteRequestResultCode.UNKNOWN_ERROR; |
| 194 } | 155 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 /** | 247 /** |
| 287 * Called by the provider manager when a sink list for a given source is | 248 * Called by the provider manager when a sink list for a given source is |
| 288 * updated. | 249 * updated. |
| 289 * @param {!string} sourceUrn | 250 * @param {!string} sourceUrn |
| 290 * @param {!Array<!MediaSink>} sinks | 251 * @param {!Array<!MediaSink>} sinks |
| 291 * @param {!Array<string>} origins | 252 * @param {!Array<string>} origins |
| 292 */ | 253 */ |
| 293 MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks, | 254 MediaRouter.prototype.onSinksReceived = function(sourceUrn, sinks, |
| 294 origins) { | 255 origins) { |
| 295 this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_), | 256 this.service_.onSinksReceived(sourceUrn, sinks.map(sinkToMojo_), |
| 296 origins.map(stringToMojoOrigin_)); | 257 origins); |
| 297 }; | 258 }; |
| 298 | 259 |
| 299 /** | 260 /** |
| 300 * Called by the provider manager when a sink is found to notify the MR of the | 261 * Called by the provider manager when a sink is found to notify the MR of the |
| 301 * sink's ID. The actual sink will be returned through the normal sink list | 262 * sink's ID. The actual sink will be returned through the normal sink list |
| 302 * update process, so this helps the MR identify the search result in the | 263 * update process, so this helps the MR identify the search result in the |
| 303 * list. | 264 * list. |
| 304 * @param {string} pseudoSinkId ID of the pseudo sink that started the | 265 * @param {string} pseudoSinkId ID of the pseudo sink that started the |
| 305 * search. | 266 * search. |
| 306 * @param {string} sinkId ID of the newly-found sink. | 267 * @param {string} sinkId ID of the newly-found sink. |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 }; | 565 }; |
| 605 | 566 |
| 606 /** | 567 /** |
| 607 * Requests that |sinkId| render the media referenced by |sourceUrn|. If the | 568 * Requests that |sinkId| render the media referenced by |sourceUrn|. If the |
| 608 * request is from the Presentation API, then origin and tabId will | 569 * request is from the Presentation API, then origin and tabId will |
| 609 * be populated. | 570 * be populated. |
| 610 * @param {!string} sourceUrn Media source to render. | 571 * @param {!string} sourceUrn Media source to render. |
| 611 * @param {!string} sinkId Media sink ID. | 572 * @param {!string} sinkId Media sink ID. |
| 612 * @param {!string} presentationId Presentation ID from the site | 573 * @param {!string} presentationId Presentation ID from the site |
| 613 * requesting presentation. TODO(mfoltz): Remove. | 574 * requesting presentation. TODO(mfoltz): Remove. |
| 614 * @param {!originMojom.Origin} origin Origin of site requesting presentation. | 575 * @param {!string} origin Origin of site requesting presentation. |
| 615 * @param {!number} tabId ID of tab requesting presentation. | 576 * @param {!number} tabId ID of tab requesting presentation. |
| 616 * @param {!TimeDelta} timeout If positive, the timeout duration for the | 577 * @param {!TimeDelta} timeout If positive, the timeout duration for the |
| 617 * request. Otherwise, the default duration will be used. | 578 * request. Otherwise, the default duration will be used. |
| 618 * @param {!boolean} incognito If true, the route is being requested by | 579 * @param {!boolean} incognito If true, the route is being requested by |
| 619 * an incognito profile. | 580 * an incognito profile. |
| 620 * @return {!Promise.<!Object>} A Promise resolving to an object describing | 581 * @return {!Promise.<!Object>} A Promise resolving to an object describing |
| 621 * the newly created media route, or rejecting with an error message on | 582 * the newly created media route, or rejecting with an error message on |
| 622 * failure. | 583 * failure. |
| 623 */ | 584 */ |
| 624 MediaRouteProvider.prototype.createRoute = | 585 MediaRouteProvider.prototype.createRoute = |
| 625 function(sourceUrn, sinkId, presentationId, origin, tabId, | 586 function(sourceUrn, sinkId, presentationId, origin, tabId, |
| 626 timeout, incognito) { | 587 timeout, incognito) { |
| 627 this.handlers_.onBeforeInvokeHandler(); | 588 this.handlers_.onBeforeInvokeHandler(); |
| 628 return this.handlers_.createRoute( | 589 return this.handlers_.createRoute( |
| 629 sourceUrn, sinkId, presentationId, mojoOriginToString_(origin), tabId, | 590 sourceUrn, sinkId, presentationId, origin, tabId, |
| 630 Math.floor(timeout.microseconds / 1000), incognito) | 591 Math.floor(timeout.microseconds / 1000), incognito) |
| 631 .then(function(route) { | 592 .then(function(route) { |
| 632 return toSuccessRouteResponse_(route); | 593 return toSuccessRouteResponse_(route); |
| 633 }, | 594 }, |
| 634 function(err) { | 595 function(err) { |
| 635 return toErrorRouteResponse_(err); | 596 return toErrorRouteResponse_(err); |
| 636 }); | 597 }); |
| 637 }; | 598 }; |
| 638 | 599 |
| 639 /** | 600 /** |
| 640 * Handles a request via the Presentation API to join an existing route given | 601 * Handles a request via the Presentation API to join an existing route given |
| 641 * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for | 602 * by |sourceUrn| and |presentationId|. |origin| and |tabId| are used for |
| 642 * validating same-origin/tab scope. | 603 * validating same-origin/tab scope. |
| 643 * @param {!string} sourceUrn Media source to render. | 604 * @param {!string} sourceUrn Media source to render. |
| 644 * @param {!string} presentationId Presentation ID to join. | 605 * @param {!string} presentationId Presentation ID to join. |
| 645 * @param {!originMojom.Origin} origin Origin of site requesting join. | 606 * @param {!string} origin Origin of site requesting join. |
| 646 * @param {!number} tabId ID of tab requesting join. | 607 * @param {!number} tabId ID of tab requesting join. |
| 647 * @param {!TimeDelta} timeout If positive, the timeout duration for the | 608 * @param {!TimeDelta} timeout If positive, the timeout duration for the |
| 648 * request. Otherwise, the default duration will be used. | 609 * request. Otherwise, the default duration will be used. |
| 649 * @param {!boolean} incognito If true, the route is being requested by | 610 * @param {!boolean} incognito If true, the route is being requested by |
| 650 * an incognito profile. | 611 * an incognito profile. |
| 651 * @return {!Promise.<!Object>} A Promise resolving to an object describing | 612 * @return {!Promise.<!Object>} A Promise resolving to an object describing |
| 652 * the newly created media route, or rejecting with an error message on | 613 * the newly created media route, or rejecting with an error message on |
| 653 * failure. | 614 * failure. |
| 654 */ | 615 */ |
| 655 MediaRouteProvider.prototype.joinRoute = | 616 MediaRouteProvider.prototype.joinRoute = |
| 656 function(sourceUrn, presentationId, origin, tabId, timeout, | 617 function(sourceUrn, presentationId, origin, tabId, timeout, |
| 657 incognito) { | 618 incognito) { |
| 658 this.handlers_.onBeforeInvokeHandler(); | 619 this.handlers_.onBeforeInvokeHandler(); |
| 659 return this.handlers_.joinRoute( | 620 return this.handlers_.joinRoute( |
| 660 sourceUrn, presentationId, mojoOriginToString_(origin), tabId, | 621 sourceUrn, presentationId, origin, tabId, |
| 661 Math.floor(timeout.microseconds / 1000), incognito) | 622 Math.floor(timeout.microseconds / 1000), incognito) |
| 662 .then(function(route) { | 623 .then(function(route) { |
| 663 return toSuccessRouteResponse_(route); | 624 return toSuccessRouteResponse_(route); |
| 664 }, | 625 }, |
| 665 function(err) { | 626 function(err) { |
| 666 return toErrorRouteResponse_(err); | 627 return toErrorRouteResponse_(err); |
| 667 }); | 628 }); |
| 668 }; | 629 }; |
| 669 | 630 |
| 670 /** | 631 /** |
| 671 * Handles a request via the Presentation API to join an existing route given | 632 * Handles a request via the Presentation API to join an existing route given |
| 672 * by |sourceUrn| and |routeId|. |origin| and |tabId| are used for | 633 * by |sourceUrn| and |routeId|. |origin| and |tabId| are used for |
| 673 * validating same-origin/tab scope. | 634 * validating same-origin/tab scope. |
| 674 * @param {!string} sourceUrn Media source to render. | 635 * @param {!string} sourceUrn Media source to render. |
| 675 * @param {!string} routeId Route ID to join. | 636 * @param {!string} routeId Route ID to join. |
| 676 * @param {!string} presentationId Presentation ID to join. | 637 * @param {!string} presentationId Presentation ID to join. |
| 677 * @param {!originMojom.Origin} origin Origin of site requesting join. | 638 * @param {!string} origin Origin of site requesting join. |
| 678 * @param {!number} tabId ID of tab requesting join. | 639 * @param {!number} tabId ID of tab requesting join. |
| 679 * @param {!TimeDelta} timeout If positive, the timeout duration for the | 640 * @param {!TimeDelta} timeout If positive, the timeout duration for the |
| 680 * request. Otherwise, the default duration will be used. | 641 * request. Otherwise, the default duration will be used. |
| 681 * @param {!boolean} incognito If true, the route is being requested by | 642 * @param {!boolean} incognito If true, the route is being requested by |
| 682 * an incognito profile. | 643 * an incognito profile. |
| 683 * @return {!Promise.<!Object>} A Promise resolving to an object describing | 644 * @return {!Promise.<!Object>} A Promise resolving to an object describing |
| 684 * the newly created media route, or rejecting with an error message on | 645 * the newly created media route, or rejecting with an error message on |
| 685 * failure. | 646 * failure. |
| 686 */ | 647 */ |
| 687 MediaRouteProvider.prototype.connectRouteByRouteId = | 648 MediaRouteProvider.prototype.connectRouteByRouteId = |
| 688 function(sourceUrn, routeId, presentationId, origin, tabId, | 649 function(sourceUrn, routeId, presentationId, origin, tabId, |
| 689 timeout, incognito) { | 650 timeout, incognito) { |
| 690 this.handlers_.onBeforeInvokeHandler(); | 651 this.handlers_.onBeforeInvokeHandler(); |
| 691 return this.handlers_.connectRouteByRouteId( | 652 return this.handlers_.connectRouteByRouteId( |
| 692 sourceUrn, routeId, presentationId, mojoOriginToString_(origin), tabId, | 653 sourceUrn, routeId, presentationId, origin, tabId, |
| 693 Math.floor(timeout.microseconds / 1000), incognito) | 654 Math.floor(timeout.microseconds / 1000), incognito) |
| 694 .then(function(route) { | 655 .then(function(route) { |
| 695 return toSuccessRouteResponse_(route); | 656 return toSuccessRouteResponse_(route); |
| 696 }, | 657 }, |
| 697 function(err) { | 658 function(err) { |
| 698 return toErrorRouteResponse_(err); | 659 return toErrorRouteResponse_(err); |
| 699 }); | 660 }); |
| 700 }; | 661 }; |
| 701 | 662 |
| 702 /** | 663 /** |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 return Promise.resolve({ | 801 return Promise.resolve({ |
| 841 'sink_id': this.handlers_.searchSinks(sinkId, sourceUrn, searchCriteria) | 802 'sink_id': this.handlers_.searchSinks(sinkId, sourceUrn, searchCriteria) |
| 842 }); | 803 }); |
| 843 }; | 804 }; |
| 844 | 805 |
| 845 mediaRouter = new MediaRouter(new mediaRouterMojom.MediaRouterPtr( | 806 mediaRouter = new MediaRouter(new mediaRouterMojom.MediaRouterPtr( |
| 846 frameInterfaces.getInterface(mediaRouterMojom.MediaRouter.name))); | 807 frameInterfaces.getInterface(mediaRouterMojom.MediaRouter.name))); |
| 847 | 808 |
| 848 return mediaRouter; | 809 return mediaRouter; |
| 849 }); | 810 }); |
| OLD | NEW |