Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // This hack prevents a bug on the cast extension. | 5 // This hack prevents a bug on the cast extension. |
| 6 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. | 6 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. |
| 7 // Although localStorage in Chrome app is not supported, but it's used in the | 7 // Although localStorage in Chrome app is not supported, but it's used in the |
| 8 // cast extension. This line prevents an exception on using localStorage. | 8 // cast extension. This line prevents an exception on using localStorage. |
| 9 Object.defineProperty(window, 'localStorage', { | 9 Object.defineProperty(window, 'localStorage', { |
| 10 get: function() { return {}; } | 10 get: function() { return {}; } |
| 11 }); | 11 }); |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * @type {string} | 14 * @type {string} |
| 15 * @const | 15 * @const |
| 16 */ | 16 */ |
| 17 var APPLICATION_ID = '4CCB98DA'; | 17 var APPLICATION_ID = '4CCB98DA'; |
| 18 | 18 |
| 19 util.addPageLoadHandler(function() { | 19 util.addPageLoadHandler(function() { |
| 20 initialize(); | 20 initialize(); |
| 21 }.wrap()); | 21 }.wrap()); |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Starts initialization of cast-related feature. | 24 * Starts initialization of cast-related feature. |
| 25 */ | 25 */ |
| 26 function initialize() { | 26 function initialize() { |
| 27 if (window.loadMockCastExtensionForTest) { | 27 if (window.loadMockCastExtensionForTest) { |
| 28 // If the test flag is set, the mock extension for test will be laoded by | 28 // If the test flag is set, the mock extension for test will be laoded by |
| 29 // the test script. Sets the handler to wait for loading. | 29 // the test script. Sets the handler to wait for loading. |
| 30 onLoadCastExtension(initializeApi); | 30 onLoadCastSDK(initializeApi); |
| 31 return; | 31 return; |
| 32 } | 32 } |
| 33 | 33 |
| 34 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { | 34 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { |
| 35 if (foundId) { | 35 if (foundId) { |
| 36 loadCastAPI(initializeApi); | 36 loadCastSDK(foundId, initializeApi); |
| 37 } else { | 37 } else { |
| 38 console.info('No Google Cast extension is installed.'); | 38 console.info('No Google Cast extension is installed.'); |
| 39 | |
| 40 metrics.recordCastAPIExtensionStatus( | |
| 41 metrics.CAST_API_EXTENSION_STATUS.SKIPPED); | |
| 42 } | 39 } |
| 43 }.wrap()); | 40 }.wrap()); |
| 44 } | 41 } |
| 45 | 42 |
| 46 /** | 43 /** |
| 47 * Loads the cast API extention. If not install, the extension is installed | 44 * Loads the Google Cast Sender SDK from the given cast extension. |
| 48 * in background before load. The cast API will load the cast SDK automatically. | 45 * The given callback is executes after the cast SDK is loaded. |
| 49 * The given callback is executes after the cast SDK extension is initialized. | |
| 50 * | 46 * |
| 47 * @param {string} extensionId ID of the extension to be loaded. | |
| 51 * @param {function()} callback Callback (executed asynchronously). | 48 * @param {function()} callback Callback (executed asynchronously). |
| 52 * @param {boolean=} opt_secondTry Specify true if it's the second call after | |
| 53 * installation of Cast API extension. | |
| 54 */ | 49 */ |
| 55 function loadCastAPI(callback, opt_secondTry) { | 50 function loadCastSDK(extensionId, callback) { |
| 56 var script = document.createElement('script'); | 51 var script = document.createElement('script'); |
| 57 | 52 |
| 58 var onError = function() { | 53 var onError = function() { |
| 59 script.removeEventListener('error', onError); | 54 script.removeEventListener('error', onError); |
| 60 document.body.removeChild(script); | 55 document.body.removeChild(script); |
| 61 | 56 |
| 62 if (opt_secondTry) { | 57 metrics.recordCastAPIExtensionStatus( |
|
yoshiki
2016/04/26 05:01:16
Please remove this as well. I think we can remove
fukino
2016/04/26 06:36:17
I removed all the metrics about CAST_API_EXTENSION
| |
| 63 metrics.recordCastAPIExtensionStatus( | 58 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); |
| 64 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); | |
| 65 | 59 |
| 66 // Shows error message and exits if it's the 2nd try. | 60 console.error('Google Cast API extension load failed.'); |
| 67 console.error('Google Cast API extension load failed.'); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 // Installs the Google Cast API extension and retry loading. | |
| 72 chrome.webstoreWidgetPrivate.installWebstoreItem( | |
| 73 'mafeflapfdfljijmlienjedomfjfmhpd', | |
| 74 true, // Don't use installation prompt. | |
| 75 function() { | |
| 76 if (chrome.runtime.lastError) { | |
| 77 metrics.recordCastAPIExtensionStatus( | |
| 78 metrics.CAST_API_EXTENSION_STATUS.INSTALLATION_FAILED); | |
| 79 | |
| 80 console.error('Google Cast API extension installation error.', | |
| 81 chrome.runtime.lastError.message); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 console.info('Google Cast API extension installed.'); | |
| 86 | |
| 87 // Loads API again. | |
| 88 setTimeout(loadCastAPI.bind(null, callback, true), 0); | |
| 89 }.wrap()); | |
| 90 }.wrap(); | 61 }.wrap(); |
| 91 | 62 |
| 92 // Trys to load the cast API extention which is defined in manifest.json. | 63 // Load the Cast Sender SDK provided by the given Cast extension. |
| 93 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 64 window.chrome['cast'] = window.chrome['cast'] || {}; |
| 65 window.chrome['cast']['extensionId'] = extensionId; | |
|
yoshiki
2016/04/26 05:04:30
I can't file the usage of this? Is this necessary?
fukino
2016/04/26 06:36:17
Added a comment.
Legacy cast extension relies on t
| |
| 66 script.src = 'chrome-extension://' + extensionId + '/cast_sender.js'; | |
| 94 script.addEventListener('error', onError); | 67 script.addEventListener('error', onError); |
| 95 script.addEventListener( | 68 script.addEventListener( |
|
yoshiki
2016/04/26 05:01:16
nit: no need for new line?
fukino
2016/04/26 06:36:17
Done.
| |
| 96 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); | 69 'load', onLoadCastSDK.bind(null, callback)); |
| 97 document.body.appendChild(script); | 70 document.body.appendChild(script); |
| 98 } | 71 } |
| 99 | 72 |
| 100 /** | 73 /** |
| 101 * Loads the cast sdk extension. | 74 * Handles load event of Cast SDK and make sure the Cast API is available. |
| 102 * @param {function()} callback Callback (executed asynchronously). | 75 * @param {function()} callback Callback which is called when the Caset Sender |
| 103 * @param {boolean=} opt_installationOccured True if the extension is just | 76 * API is ready for use. |
| 104 * installed in this window. False or null if it's already installed. | |
| 105 */ | 77 */ |
| 106 function onLoadCastExtension(callback, opt_installationOccured) { | 78 function onLoadCastSDK(callback) { |
| 107 var executeCallback = function() { | 79 var executeCallback = function() { |
| 108 if (opt_installationOccured) { | 80 metrics.recordCastAPIExtensionStatus( |
|
yoshiki
2016/04/26 05:01:16
ditto
fukino
2016/04/26 06:36:17
Done.
| |
| 109 metrics.recordCastAPIExtensionStatus( | 81 metrics.CAST_API_EXTENSION_STATUS.LOADED); |
| 110 metrics.CAST_API_EXTENSION_STATUS.INSTALLED_AND_LOADED); | |
| 111 } else { | |
| 112 metrics.recordCastAPIExtensionStatus( | |
| 113 metrics.CAST_API_EXTENSION_STATUS.LOADED); | |
| 114 } | |
| 115 | 82 |
| 116 setTimeout(callback, 0); // Runs asynchronously. | 83 setTimeout(callback, 0); // Runs asynchronously. |
| 117 }; | 84 }; |
| 118 | 85 |
| 119 if(!chrome.cast || !chrome.cast.isAvailable) { | 86 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 120 var checkTimer = setTimeout(function() { | 87 var checkTimer = setTimeout(function() { |
| 121 console.error('Either "Google Cast API" or "Google Cast" extension ' + | 88 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
| 122 'seems not to be installed?'); | 89 'seems not to be installed?'); |
| 123 | 90 |
| 124 metrics.recordCastAPIExtensionStatus( | 91 metrics.recordCastAPIExtensionStatus( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 metrics.recordNumberOfCastDevices(receivers.length); | 153 metrics.recordNumberOfCastDevices(receivers.length); |
| 187 player.setCastList(receivers); | 154 player.setCastList(receivers); |
| 188 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 155 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
| 189 metrics.recordNumberOfCastDevices(0); | 156 metrics.recordNumberOfCastDevices(0); |
| 190 player.setCastList([]); | 157 player.setCastList([]); |
| 191 } else { | 158 } else { |
| 192 console.error('Unexpected response in onReceiver.', arguments); | 159 console.error('Unexpected response in onReceiver.', arguments); |
| 193 player.setCastList([]); | 160 player.setCastList([]); |
| 194 } | 161 } |
| 195 } | 162 } |
| OLD | NEW |