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 {}; } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 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 onLoadCastExtension(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 loadCastAPI(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 | 39 |
| 40 metrics.recordCastAPIExtensionStatus( | 40 metrics.recordCastAPIExtensionStatus( |
|
yoshiki
2016/04/26 03:02:52
I think This metrics should no longer be necessary
fukino
2016/04/26 03:59:07
Done. (Removed)
| |
| 41 metrics.CAST_API_EXTENSION_STATUS.SKIPPED); | 41 metrics.CAST_API_EXTENSION_STATUS.SKIPPED); |
| 42 } | 42 } |
| 43 }.wrap()); | 43 }.wrap()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Loads the cast API extention. If not install, the extension is installed | 47 * Loads the cast API extention. |
| 48 * in background before load. The cast API will load the cast SDK automatically. | 48 * The cast API will load the cast SDK automatically. The given callback is |
|
yoshiki
2016/04/26 03:02:52
nit: update the comment.
fukino
2016/04/26 03:59:07
Done.
| |
| 49 * The given callback is executes after the cast SDK extension is initialized. | 49 * executes after the cast SDK extension is initialized. |
| 50 * | 50 * |
| 51 * @param {string} extensionId ID of the extension to be loaded. | |
| 51 * @param {function()} callback Callback (executed asynchronously). | 52 * @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 */ | 53 */ |
| 55 function loadCastAPI(callback, opt_secondTry) { | 54 function loadCastAPI(extensionId, callback) { |
|
yoshiki
2016/04/26 03:02:52
Please rename this, since now this loads the googl
fukino
2016/04/26 03:59:08
Done.
I renamed it to loadCastSDK(), as it loads G
| |
| 56 var script = document.createElement('script'); | 55 var script = document.createElement('script'); |
| 57 | 56 |
| 58 var onError = function() { | 57 var onError = function() { |
| 59 script.removeEventListener('error', onError); | 58 script.removeEventListener('error', onError); |
| 60 document.body.removeChild(script); | 59 document.body.removeChild(script); |
| 61 | 60 |
| 62 if (opt_secondTry) { | 61 metrics.recordCastAPIExtensionStatus( |
| 63 metrics.recordCastAPIExtensionStatus( | 62 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); |
| 64 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); | |
| 65 | 63 |
| 66 // Shows error message and exits if it's the 2nd try. | 64 // Shows error message and exits if it's the 2nd try. |
|
yoshiki
2016/04/26 03:02:52
nit: please update the comment.
fukino
2016/04/26 03:59:07
Removed this comment.
| |
| 67 console.error('Google Cast API extension load failed.'); | 65 console.error('Google Cast API extension load failed.'); |
| 68 return; | 66 } |
|
yoshiki
2016/04/26 03:02:52
nit: need .wrap() for consistency?
yoshiki
2016/04/26 03:02:52
nit: semicolon.
fukino
2016/04/26 03:59:07
Done.
fukino
2016/04/26 03:59:07
Done.
| |
| 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(); | |
| 91 | 67 |
| 92 // Trys to load the cast API extention which is defined in manifest.json. | 68 // Trys to load the cast API extention which is defined in manifest.json. |
|
yoshiki
2016/04/26 03:02:52
nit: please fix the comment
fukino
2016/04/26 03:59:07
Done.
| |
| 93 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 69 window.chrome['cast'] = window.chrome['cast'] || {}; |
| 70 window.chrome['cast']['extensionId'] = extensionId; | |
| 71 script.src = 'chrome-extension://' + extensionId + '/cast_sender.js'; | |
| 94 script.addEventListener('error', onError); | 72 script.addEventListener('error', onError); |
| 95 script.addEventListener( | 73 script.addEventListener( |
| 96 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); | 74 'load', onLoadCastExtension.bind(null, callback)); |
| 97 document.body.appendChild(script); | 75 document.body.appendChild(script); |
| 98 } | 76 } |
| 99 | 77 |
| 100 /** | 78 /** |
| 101 * Loads the cast sdk extension. | 79 * Loads the cast sdk extension. |
| 102 * @param {function()} callback Callback (executed asynchronously). | 80 * @param {function()} callback Callback (executed asynchronously). |
| 103 * @param {boolean=} opt_installationOccured True if the extension is just | |
| 104 * installed in this window. False or null if it's already installed. | |
| 105 */ | 81 */ |
| 106 function onLoadCastExtension(callback, opt_installationOccured) { | 82 function onLoadCastExtension(callback) { |
| 107 var executeCallback = function() { | 83 var executeCallback = function() { |
| 108 if (opt_installationOccured) { | 84 metrics.recordCastAPIExtensionStatus( |
| 109 metrics.recordCastAPIExtensionStatus( | 85 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 | 86 |
| 116 setTimeout(callback, 0); // Runs asynchronously. | 87 setTimeout(callback, 0); // Runs asynchronously. |
| 117 }; | 88 }; |
| 118 | 89 |
| 119 if(!chrome.cast || !chrome.cast.isAvailable) { | 90 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 120 var checkTimer = setTimeout(function() { | 91 var checkTimer = setTimeout(function() { |
| 121 console.error('Either "Google Cast API" or "Google Cast" extension ' + | 92 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
| 122 'seems not to be installed?'); | 93 'seems not to be installed?'); |
| 123 | 94 |
| 124 metrics.recordCastAPIExtensionStatus( | 95 metrics.recordCastAPIExtensionStatus( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 metrics.recordNumberOfCastDevices(receivers.length); | 157 metrics.recordNumberOfCastDevices(receivers.length); |
| 187 player.setCastList(receivers); | 158 player.setCastList(receivers); |
| 188 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 159 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
| 189 metrics.recordNumberOfCastDevices(0); | 160 metrics.recordNumberOfCastDevices(0); |
| 190 player.setCastList([]); | 161 player.setCastList([]); |
| 191 } else { | 162 } else { |
| 192 console.error('Unexpected response in onReceiver.', arguments); | 163 console.error('Unexpected response in onReceiver.', arguments); |
| 193 player.setCastList([]); | 164 player.setCastList([]); |
| 194 } | 165 } |
| 195 } | 166 } |
| OLD | NEW |