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.recordCastExtensionStatus( |
| 35 metrics.CAST_EXTENSION.EXTENSION_UNAVAILABLE); |
| 36 } |
33 }.wrap()); | 37 }.wrap()); |
34 } | 38 } |
35 | 39 |
36 /** | 40 /** |
37 * Loads the cast API extention. If not install, the extension is installed | 41 * 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. | 42 * 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. | 43 * The given callback is executes after the cast SDK extension is initialized. |
40 * | 44 * |
41 * @param {function} callback Callback (executed asynchronously). | 45 * @param {function} callback Callback (executed asynchronously). |
42 * @param {boolean=} opt_secondTry Spericy try if it's second call after | 46 * @param {boolean=} opt_secondTry Spericy try if it's second call after |
43 * installation of Cast API extension. | 47 * installation of Cast API extension. |
44 */ | 48 */ |
45 function loadCastAPI(callback, opt_secondTry) { | 49 function loadCastAPI(callback, opt_secondTry) { |
46 var script = document.createElement('script'); | 50 var script = document.createElement('script'); |
47 | 51 |
48 var onError = function() { | 52 var onError = function() { |
49 script.removeEventListener('error', onError); | 53 script.removeEventListener('error', onError); |
50 document.body.removeChild(script); | 54 document.body.removeChild(script); |
51 | 55 |
52 if (opt_secondTry) { | 56 if (opt_secondTry) { |
| 57 metrics.recordCastExtensionStatus( |
| 58 metrics.CAST_EXTENSION.API_INSTALLED_BUT_LOAD_FAILED); |
| 59 |
53 // Shows error message and exits if it's the 2nd try. | 60 // Shows error message and exits if it's the 2nd try. |
54 console.error('Google Cast API extension load failed.'); | 61 console.error('Google Cast API extension load failed.'); |
55 return; | 62 return; |
56 } | 63 } |
57 | 64 |
58 // Installs the Google Cast API extension and retry loading. | 65 // Installs the Google Cast API extension and retry loading. |
59 chrome.fileManagerPrivate.installWebstoreItem( | 66 chrome.fileManagerPrivate.installWebstoreItem( |
60 'mafeflapfdfljijmlienjedomfjfmhpd', | 67 'mafeflapfdfljijmlienjedomfjfmhpd', |
61 true, // Don't use installation prompt. | 68 true, // Don't use installation prompt. |
62 function() { | 69 function() { |
63 if (chrome.runtime.lastError) { | 70 if (chrome.runtime.lastError) { |
| 71 metrics.recordCastExtensionStatus( |
| 72 metrics.CAST_EXTENSION.API_INSTALLATION_FAILED); |
| 73 |
64 console.error('Google Cast API extension installation error.', | 74 console.error('Google Cast API extension installation error.', |
65 chrome.runtime.lastError.message); | 75 chrome.runtime.lastError.message); |
66 return; | 76 return; |
67 } | 77 } |
68 | 78 |
69 console.info('Google Cast API extension installed.'); | 79 console.info('Google Cast API extension installed.'); |
| 80 |
70 // Loads API again. | 81 // Loads API again. |
71 setTimeout(loadCastAPI.bind(null, callback, true)); | 82 setTimeout(loadCastAPI.bind(null, callback, true)); |
72 }.wrap()); | 83 }.wrap()); |
73 }.wrap(); | 84 }.wrap(); |
74 | 85 |
75 // Trys to load the cast API extention which is defined in manifest.json. | 86 // Trys to load the cast API extention which is defined in manifest.json. |
76 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 87 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
77 script.addEventListener('error', onError); | 88 script.addEventListener('error', onError); |
78 script.addEventListener('load', onLoadCastExtension.bind(null, callback)); | 89 script.addEventListener( |
| 90 'load', onLoadCastExtension.bind(null, callback, opt_secondTry)); |
79 document.body.appendChild(script); | 91 document.body.appendChild(script); |
80 } | 92 } |
81 | 93 |
82 /** | 94 /** |
83 * Loads the cast sdk extension. | 95 * Loads the cast sdk extension. |
84 * @param {function()} callback Callback (executed asynchronously). | 96 * @param {function()} callback Callback (executed asynchronously). |
| 97 * @param {boolean=} opt_installationOccured True if the extension is just |
| 98 * installed in this window. False or null if it's already installed. |
85 */ | 99 */ |
86 function onLoadCastExtension(callback) { | 100 function onLoadCastExtension(callback, opt_installationOccured) { |
| 101 var executeCallback = function() { |
| 102 if (opt_installationOccured) { |
| 103 metrics.recordCastExtensionStatus( |
| 104 metrics.CAST_EXTENSION.API_INSTALLED_AND_LOADED); |
| 105 } else { |
| 106 metrics.recordCastExtensionStatus( |
| 107 metrics.CAST_EXTENSION.API_AVAILABLE_AND_LOADED); |
| 108 } |
| 109 |
| 110 setTimeout(callback); // Runs asynchronously. |
| 111 }; |
| 112 |
87 if(!chrome.cast || !chrome.cast.isAvailable) { | 113 if(!chrome.cast || !chrome.cast.isAvailable) { |
88 var checkTimer = setTimeout(function() { | 114 var checkTimer = setTimeout(function() { |
89 console.error('Either "Google Cast API" or "Google Cast" extension ' + | 115 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
90 'seems not to be installed?'); | 116 'seems not to be installed?'); |
| 117 |
| 118 metrics.recordCastExtensionStatus( |
| 119 metrics.CAST_EXTENSION.API_LOAD_FAILED); |
91 }.wrap(), 5000); | 120 }.wrap(), 5000); |
92 | 121 |
93 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | 122 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
94 clearTimeout(checkTimer); | 123 clearTimeout(checkTimer); |
95 | 124 |
96 if (loaded) | 125 if (loaded) { |
97 callback(); | 126 executeCallback(); |
98 else | 127 } else { |
| 128 metrics.recordCastExtensionStatus( |
| 129 metrics.CAST_EXTENSION.API_LOAD_FAILED); |
| 130 |
99 console.error('Google Cast extension load failed.', errorInfo); | 131 console.error('Google Cast extension load failed.', errorInfo); |
| 132 } |
100 }.wrap(); | 133 }.wrap(); |
101 } else { | 134 } else { |
102 setTimeout(callback); // Runs asynchronously. | 135 // Just executes the callback since the API is already loaded. |
| 136 executeCallback(); |
103 } | 137 } |
104 } | 138 } |
105 | 139 |
106 /** | 140 /** |
107 * Initialize Cast API. | 141 * Initialize Cast API. |
108 */ | 142 */ |
109 function initializeApi() { | 143 function initializeApi() { |
110 var onSession = function() { | 144 var onSession = function() { |
111 // TODO(yoshiki): Implement this. | 145 // TODO(yoshiki): Implement this. |
112 }; | 146 }; |
(...skipping 20 matching lines...) Expand all Loading... |
133 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. | 167 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. |
134 * @param {Array.<Object>} receivers List of casts. | 168 * @param {Array.<Object>} receivers List of casts. |
135 */ | 169 */ |
136 function onReceiver(availability, receivers) { | 170 function onReceiver(availability, receivers) { |
137 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { | 171 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { |
138 if (!receivers) { | 172 if (!receivers) { |
139 console.error('Receiver list is empty.'); | 173 console.error('Receiver list is empty.'); |
140 receivers = []; | 174 receivers = []; |
141 } | 175 } |
142 | 176 |
| 177 metrics.recordNumberOfCastDevices(receivers.length); |
143 player.setCastList(receivers); | 178 player.setCastList(receivers); |
144 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 179 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
| 180 metrics.recordNumberOfCastDevices(0); |
145 player.setCastList([]); | 181 player.setCastList([]); |
146 } else { | 182 } else { |
147 console.error('Unexpected response in onReceiver.', arguments); | 183 console.error('Unexpected response in onReceiver.', arguments); |
148 player.setCastList([]); | 184 player.setCastList([]); |
149 } | 185 } |
150 } | 186 } |
OLD | NEW |