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 /** |
| 12 * @type {string} |
| 13 * @const |
| 14 */ |
11 var APPLICATION_ID = '4CCB98DA'; | 15 var APPLICATION_ID = '4CCB98DA'; |
12 | 16 |
13 util.addPageLoadHandler(function() { | 17 util.addPageLoadHandler(function() { |
14 initialize(); | 18 initialize(); |
15 }.wrap()); | 19 }.wrap()); |
16 | 20 |
17 /** | 21 /** |
18 * Starts initialization of cast-related feature. | 22 * Starts initialization of cast-related feature. |
19 */ | 23 */ |
20 function initialize() { | 24 function initialize() { |
(...skipping 14 matching lines...) Expand all Loading... |
35 metrics.CAST_API_EXTENSION_STATUS.SKIPPED); | 39 metrics.CAST_API_EXTENSION_STATUS.SKIPPED); |
36 } | 40 } |
37 }.wrap()); | 41 }.wrap()); |
38 } | 42 } |
39 | 43 |
40 /** | 44 /** |
41 * Loads the cast API extention. If not install, the extension is installed | 45 * Loads the cast API extention. If not install, the extension is installed |
42 * in background before load. The cast API will load the cast SDK automatically. | 46 * in background before load. The cast API will load the cast SDK automatically. |
43 * The given callback is executes after the cast SDK extension is initialized. | 47 * The given callback is executes after the cast SDK extension is initialized. |
44 * | 48 * |
45 * @param {function} callback Callback (executed asynchronously). | 49 * @param {function()} callback Callback (executed asynchronously). |
46 * @param {boolean=} opt_secondTry Specify true if it's the second call after | 50 * @param {boolean=} opt_secondTry Specify true if it's the second call after |
47 * installation of Cast API extension. | 51 * installation of Cast API extension. |
48 */ | 52 */ |
49 function loadCastAPI(callback, opt_secondTry) { | 53 function loadCastAPI(callback, opt_secondTry) { |
50 var script = document.createElement('script'); | 54 var script = document.createElement('script'); |
51 | 55 |
52 var onError = function() { | 56 var onError = function() { |
53 script.removeEventListener('error', onError); | 57 script.removeEventListener('error', onError); |
54 document.body.removeChild(script); | 58 document.body.removeChild(script); |
55 | 59 |
(...skipping 16 matching lines...) Expand all Loading... |
72 metrics.CAST_API_EXTENSION_STATUS.INSTALLATION_FAILED); | 76 metrics.CAST_API_EXTENSION_STATUS.INSTALLATION_FAILED); |
73 | 77 |
74 console.error('Google Cast API extension installation error.', | 78 console.error('Google Cast API extension installation error.', |
75 chrome.runtime.lastError.message); | 79 chrome.runtime.lastError.message); |
76 return; | 80 return; |
77 } | 81 } |
78 | 82 |
79 console.info('Google Cast API extension installed.'); | 83 console.info('Google Cast API extension installed.'); |
80 | 84 |
81 // Loads API again. | 85 // Loads API again. |
82 setTimeout(loadCastAPI.bind(null, callback, true)); | 86 setTimeout(loadCastAPI.bind(null, callback, true), 0); |
83 }.wrap()); | 87 }.wrap()); |
84 }.wrap(); | 88 }.wrap(); |
85 | 89 |
86 // Trys to load the cast API extention which is defined in manifest.json. | 90 // Trys to load the cast API extention which is defined in manifest.json. |
87 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 91 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
88 script.addEventListener('error', onError); | 92 script.addEventListener('error', onError); |
89 script.addEventListener( | 93 script.addEventListener( |
90 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); | 94 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); |
91 document.body.appendChild(script); | 95 document.body.appendChild(script); |
92 } | 96 } |
93 | 97 |
94 /** | 98 /** |
95 * Loads the cast sdk extension. | 99 * Loads the cast sdk extension. |
96 * @param {function()} callback Callback (executed asynchronously). | 100 * @param {function()} callback Callback (executed asynchronously). |
97 * @param {boolean=} opt_installationOccured True if the extension is just | 101 * @param {boolean=} opt_installationOccured True if the extension is just |
98 * installed in this window. False or null if it's already installed. | 102 * installed in this window. False or null if it's already installed. |
99 */ | 103 */ |
100 function onLoadCastExtension(callback, opt_installationOccured) { | 104 function onLoadCastExtension(callback, opt_installationOccured) { |
101 var executeCallback = function() { | 105 var executeCallback = function() { |
102 if (opt_installationOccured) { | 106 if (opt_installationOccured) { |
103 metrics.recordCastAPIExtensionStatus( | 107 metrics.recordCastAPIExtensionStatus( |
104 metrics.CAST_API_EXTENSION_STATUS.INSTALLED_AND_LOADED); | 108 metrics.CAST_API_EXTENSION_STATUS.INSTALLED_AND_LOADED); |
105 } else { | 109 } else { |
106 metrics.recordCastAPIExtensionStatus( | 110 metrics.recordCastAPIExtensionStatus( |
107 metrics.CAST_API_EXTENSION_STATUS.LOADED); | 111 metrics.CAST_API_EXTENSION_STATUS.LOADED); |
108 } | 112 } |
109 | 113 |
110 setTimeout(callback); // Runs asynchronously. | 114 setTimeout(callback, 0); // Runs asynchronously. |
111 }; | 115 }; |
112 | 116 |
113 if(!chrome.cast || !chrome.cast.isAvailable) { | 117 if(!chrome.cast || !chrome.cast.isAvailable) { |
114 var checkTimer = setTimeout(function() { | 118 var checkTimer = setTimeout(function() { |
115 console.error('Either "Google Cast API" or "Google Cast" extension ' + | 119 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
116 'seems not to be installed?'); | 120 'seems not to be installed?'); |
117 | 121 |
118 metrics.recordCastAPIExtensionStatus( | 122 metrics.recordCastAPIExtensionStatus( |
119 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); | 123 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED); |
120 }.wrap(), 5000); | 124 }.wrap(), 5000); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 }; | 161 }; |
158 | 162 |
159 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID); | 163 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID); |
160 var apiConfig = new chrome.cast.ApiConfig(sessionRequest, | 164 var apiConfig = new chrome.cast.ApiConfig(sessionRequest, |
161 onSession, | 165 onSession, |
162 onReceiver); | 166 onReceiver); |
163 chrome.cast.initialize(apiConfig, onInitSuccess, onError); | 167 chrome.cast.initialize(apiConfig, onInitSuccess, onError); |
164 } | 168 } |
165 | 169 |
166 /** | 170 /** |
| 171 * Called when receiver availability is changed. This method is also called when |
| 172 * initialization is completed. |
| 173 * |
167 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. | 174 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. |
168 * @param {Array.<Object>} receivers List of casts. | 175 * @param {Array.<Object>} receivers List of casts. |
169 */ | 176 */ |
170 function onReceiver(availability, receivers) { | 177 function onReceiver(availability, receivers) { |
171 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { | 178 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { |
172 if (!receivers) { | 179 if (!receivers) { |
173 console.error('Receiver list is empty.'); | 180 console.error('Receiver list is empty.'); |
174 receivers = []; | 181 receivers = []; |
175 } | 182 } |
176 | 183 |
177 metrics.recordNumberOfCastDevices(receivers.length); | 184 metrics.recordNumberOfCastDevices(receivers.length); |
178 player.setCastList(receivers); | 185 player.setCastList(receivers); |
179 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 186 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
180 metrics.recordNumberOfCastDevices(0); | 187 metrics.recordNumberOfCastDevices(0); |
181 player.setCastList([]); | 188 player.setCastList([]); |
182 } else { | 189 } else { |
183 console.error('Unexpected response in onReceiver.', arguments); | 190 console.error('Unexpected response in onReceiver.', arguments); |
184 player.setCastList([]); | 191 player.setCastList([]); |
185 } | 192 } |
186 } | 193 } |
OLD | NEW |