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 window.__defineGetter__('localStorage', function() { return {}; }); | 9 window.__defineGetter__('localStorage', function() { return {}; }); |
| 10 | 10 |
| 11 var APPLICATION_ID = '4CCB98DA'; | 11 var APPLICATION_ID = '4CCB98DA'; |
| 12 | 12 |
| 13 util.addPageLoadHandler(function() { | 13 util.addPageLoadHandler(function() { |
| 14 initialize(); | 14 initialize(); |
| 15 }.wrap()); | 15 }.wrap()); |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Starts initialization of cast-related feature. | 18 * Starts initialization of cast-related feature. |
| 19 */ | 19 */ |
| 20 function initialize() { | 20 function initialize() { |
| 21 if (window.loadMockCastExtensionForTest) { | 21 if (window.loadMockCastExtensionForTest) { |
| 22 // If the test flag is set, the mock extension for test will be laoded by | 22 // If the test flag is set, the mock extension for test will be laoded by |
| 23 // the test script. Sets the handler to wait for loading. | 23 // the test script. Sets the handler to wait for loading. |
| 24 onLoadCastExtension(initializeApi); | 24 onLoadCastExtension(initializeApi); |
| 25 return; | 25 return; |
| 26 } | 26 } |
| 27 | 27 |
| 28 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { | 28 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { |
| 29 if (foundId) | 29 if (foundId) { |
| 30 loadCastAPI(initializeApi); | 30 loadCastAPI(initializeApi); |
| 31 else | 31 } else { |
| 32 console.info('No Google Cast extension is installed.'); | 32 console.info('No Google Cast extension is installed.'); |
| 33 | |
| 34 metrics.recordEnum('CastExtensionStatus', | |
| 35 metrics.CAST_EXTENSION.EXTENSION_UNAVAILABLE, | |
| 36 metrics.CAST_EXTENSION.MAX_VALUE); | |
|
Ilya Sherman
2014/12/02 23:45:10
I strongly recommend creating a wrapper function f
yoshiki
2014/12/08 02:52:29
Done.
| |
| 37 } | |
| 33 }.wrap()); | 38 }.wrap()); |
| 34 } | 39 } |
| 35 | 40 |
| 36 /** | 41 /** |
| 37 * Loads the cast API extention. If not install, the extension is installed | 42 * Loads the cast API extention. If not install, the extension is installed |
| 38 * in background before load. The cast API will load the cast SDK automatically. | 43 * in background before load. The cast API will load the cast SDK automatically. |
| 39 * The given callback is executes after the cast SDK extension is initialized. | 44 * The given callback is executes after the cast SDK extension is initialized. |
| 40 * | 45 * |
| 41 * @param {function} callback Callback (executed asynchronously). | 46 * @param {function} callback Callback (executed asynchronously). |
| 42 * @param {boolean=} opt_secondTry Spericy try if it's second call after | 47 * @param {boolean=} opt_secondTry Spericy try if it's second call after |
| 43 * installation of Cast API extension. | 48 * installation of Cast API extension. |
| 44 */ | 49 */ |
| 45 function loadCastAPI(callback, opt_secondTry) { | 50 function loadCastAPI(callback, opt_secondTry) { |
| 46 var script = document.createElement('script'); | 51 var script = document.createElement('script'); |
| 47 | 52 |
| 48 var onError = function() { | 53 var onError = function() { |
| 49 script.removeEventListener('error', onError); | 54 script.removeEventListener('error', onError); |
| 50 document.body.removeChild(script); | 55 document.body.removeChild(script); |
| 51 | 56 |
| 52 if (opt_secondTry) { | 57 if (opt_secondTry) { |
| 58 metrics.recordEnum('CastExtensionStatus', | |
| 59 metrics.CAST_EXTENSION.API_INSTALLED_BUT_LOAD_FAILED, | |
| 60 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 61 | |
| 53 // Shows error message and exits if it's the 2nd try. | 62 // Shows error message and exits if it's the 2nd try. |
| 54 console.error('Google Cast API extension load failed.'); | 63 console.error('Google Cast API extension load failed.'); |
| 55 return; | 64 return; |
| 56 } | 65 } |
| 57 | 66 |
| 58 // Installs the Google Cast API extension and retry loading. | 67 // Installs the Google Cast API extension and retry loading. |
| 59 chrome.fileManagerPrivate.installWebstoreItem( | 68 chrome.fileManagerPrivate.installWebstoreItem( |
| 60 'mafeflapfdfljijmlienjedomfjfmhpd', | 69 'mafeflapfdfljijmlienjedomfjfmhpd', |
| 61 true, // Don't use installation prompt. | 70 true, // Don't use installation prompt. |
| 62 function() { | 71 function() { |
| 63 if (chrome.runtime.lastError) { | 72 if (chrome.runtime.lastError) { |
| 73 metrics.recordEnum('CastExtensionStatus', | |
| 74 metrics.CAST_EXTENSION.API_INSTALLATION_FAILED, | |
| 75 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 76 | |
| 64 console.error('Google Cast API extension installation error.', | 77 console.error('Google Cast API extension installation error.', |
| 65 chrome.runtime.lastError.message); | 78 chrome.runtime.lastError.message); |
| 66 return; | 79 return; |
| 67 } | 80 } |
| 68 | 81 |
| 69 console.info('Google Cast API extension installed.'); | 82 console.info('Google Cast API extension installed.'); |
| 83 | |
| 70 // Loads API again. | 84 // Loads API again. |
| 71 setTimeout(loadCastAPI.bind(null, callback, true)); | 85 setTimeout(loadCastAPI.bind(null, callback, true)); |
| 72 }.wrap()); | 86 }.wrap()); |
| 73 }.wrap(); | 87 }.wrap(); |
| 74 | 88 |
| 75 // Trys to load the cast API extention which is defined in manifest.json. | 89 // Trys to load the cast API extention which is defined in manifest.json. |
| 76 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 90 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
| 77 script.addEventListener('error', onError); | 91 script.addEventListener('error', onError); |
| 78 script.addEventListener('load', onLoadCastExtension.bind(null, callback)); | 92 script.addEventListener( |
| 93 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); | |
| 79 document.body.appendChild(script); | 94 document.body.appendChild(script); |
| 80 } | 95 } |
| 81 | 96 |
| 82 /** | 97 /** |
| 83 * Loads the cast sdk extension. | 98 * Loads the cast sdk extension. |
| 84 * @param {function()} callback Callback (executed asynchronously). | 99 * @param {function()} callback Callback (executed asynchronously). |
| 100 * @param {boolean=} opt_installationOccured True if the extension is just | |
| 101 * installed in this window. False or null if it's already installed. | |
| 85 */ | 102 */ |
| 86 function onLoadCastExtension(callback) { | 103 function onLoadCastExtension(callback, opt_installationOccured) { |
| 104 var executeCallback = function() { | |
| 105 if (opt_installationOccured) { | |
| 106 metrics.recordEnum('CastExtensionStatus', | |
| 107 metrics.CAST_EXTENSION.API_INSTALLED_AND_LOADED, | |
| 108 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 109 } else { | |
| 110 metrics.recordEnum('CastExtensionStatus', | |
| 111 metrics.CAST_EXTENSION.API_AVAILABLE_AND_LOADED, | |
| 112 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 113 } | |
| 114 | |
| 115 setTimeout(callback); // Runs asynchronously. | |
| 116 }; | |
| 117 | |
| 87 if(!chrome.cast || !chrome.cast.isAvailable) { | 118 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 88 var checkTimer = setTimeout(function() { | 119 var checkTimer = setTimeout(function() { |
| 89 console.error('Either "Google Cast API" or "Google Cast" extension ' + | 120 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
| 90 'seems not to be installed?'); | 121 'seems not to be installed?'); |
| 122 | |
| 123 metrics.recordEnum('CastExtensionStatus', | |
| 124 metrics.CAST_EXTENSION.API_LOAD_FAILED, | |
| 125 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 91 }.wrap(), 5000); | 126 }.wrap(), 5000); |
| 92 | 127 |
| 93 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | 128 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
| 94 clearTimeout(checkTimer); | 129 clearTimeout(checkTimer); |
| 95 | 130 |
| 96 if (loaded) | 131 if (loaded) { |
| 97 callback(); | 132 executeCallback(); |
| 98 else | 133 } |
| 134 else { | |
| 135 metrics.recordEnum('CastExtensionStatus', | |
| 136 metrics.CAST_EXTENSION.API_LOAD_FAILED, | |
| 137 metrics.CAST_EXTENSION.MAX_VALUE); | |
| 138 | |
| 99 console.error('Google Cast extension load failed.', errorInfo); | 139 console.error('Google Cast extension load failed.', errorInfo); |
| 140 } | |
| 100 }.wrap(); | 141 }.wrap(); |
| 101 } else { | 142 } else { |
| 102 setTimeout(callback); // Runs asynchronously. | 143 // Just executes the callback since the API is already loaded. |
| 144 executeCallback(); | |
| 103 } | 145 } |
| 104 } | 146 } |
| 105 | 147 |
| 106 /** | 148 /** |
| 107 * Initialize Cast API. | 149 * Initialize Cast API. |
| 108 */ | 150 */ |
| 109 function initializeApi() { | 151 function initializeApi() { |
| 110 var onSession = function() { | 152 var onSession = function() { |
| 111 // TODO(yoshiki): Implement this. | 153 // TODO(yoshiki): Implement this. |
| 112 }; | 154 }; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 133 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. | 175 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. |
| 134 * @param {Array.<Object>} receivers List of casts. | 176 * @param {Array.<Object>} receivers List of casts. |
| 135 */ | 177 */ |
| 136 function onReceiver(availability, receivers) { | 178 function onReceiver(availability, receivers) { |
| 137 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { | 179 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { |
| 138 if (!receivers) { | 180 if (!receivers) { |
| 139 console.error('Receiver list is empty.'); | 181 console.error('Receiver list is empty.'); |
| 140 receivers = []; | 182 receivers = []; |
| 141 } | 183 } |
| 142 | 184 |
| 185 metrics.recordSmallCount('NumberOfCastDevices', receivers.length); | |
| 143 player.setCastList(receivers); | 186 player.setCastList(receivers); |
| 144 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 187 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
| 188 metrics.recordSmallCount('NumberOfCastDevices', 0); | |
| 145 player.setCastList([]); | 189 player.setCastList([]); |
| 146 } else { | 190 } else { |
| 147 console.error('Unexpected response in onReceiver.', arguments); | 191 console.error('Unexpected response in onReceiver.', arguments); |
| 148 player.setCastList([]); | 192 player.setCastList([]); |
| 149 } | 193 } |
| 150 } | 194 } |
| OLD | NEW |