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
3 // found in the LICENSE file.
5 // This hack prevents a bug on the cast extension.
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
8 // cast extension. This line prevents an exception on using localStorage.
9 window.__defineGetter__('localStorage', function() { return {}; });
15 var APPLICATION_ID = '4CCB98DA';
17 util.addPageLoadHandler(function() {
22 * Starts initialization of cast-related feature.
24 function initialize() {
25 if (window.loadMockCastExtensionForTest) {
26 // If the test flag is set, the mock extension for test will be laoded by
27 // the test script. Sets the handler to wait for loading.
28 onLoadCastExtension(initializeApi);
32 CastExtensionDiscoverer.findInstalledExtension(function(foundId) {
34 loadCastAPI(initializeApi);
36 console.info('No Google Cast extension is installed.');
38 metrics.recordCastAPIExtensionStatus(
39 metrics.CAST_API_EXTENSION_STATUS.SKIPPED);
45 * Loads the cast API extention. If not install, the extension is installed
46 * in background before load. The cast API will load the cast SDK automatically.
47 * The given callback is executes after the cast SDK extension is initialized.
49 * @param {function()} callback Callback (executed asynchronously).
50 * @param {boolean=} opt_secondTry Specify true if it's the second call after
51 * installation of Cast API extension.
53 function loadCastAPI(callback, opt_secondTry) {
54 var script = document.createElement('script');
56 var onError = function() {
57 script.removeEventListener('error', onError);
58 document.body.removeChild(script);
61 metrics.recordCastAPIExtensionStatus(
62 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
64 // Shows error message and exits if it's the 2nd try.
65 console.error('Google Cast API extension load failed.');
69 // Installs the Google Cast API extension and retry loading.
70 chrome.fileManagerPrivate.installWebstoreItem(
71 'mafeflapfdfljijmlienjedomfjfmhpd',
72 true, // Don't use installation prompt.
74 if (chrome.runtime.lastError) {
75 metrics.recordCastAPIExtensionStatus(
76 metrics.CAST_API_EXTENSION_STATUS.INSTALLATION_FAILED);
78 console.error('Google Cast API extension installation error.',
79 chrome.runtime.lastError.message);
83 console.info('Google Cast API extension installed.');
86 setTimeout(loadCastAPI.bind(null, callback, true), 0);
90 // Trys to load the cast API extention which is defined in manifest.json.
91 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js';
92 script.addEventListener('error', onError);
93 script.addEventListener(
94 'load', onLoadCastExtension.bind(null, callback, opt_secondTry));
95 document.body.appendChild(script);
99 * Loads the cast sdk extension.
100 * @param {function()} callback Callback (executed asynchronously).
101 * @param {boolean=} opt_installationOccured True if the extension is just
102 * installed in this window. False or null if it's already installed.
104 function onLoadCastExtension(callback, opt_installationOccured) {
105 var executeCallback = function() {
106 if (opt_installationOccured) {
107 metrics.recordCastAPIExtensionStatus(
108 metrics.CAST_API_EXTENSION_STATUS.INSTALLED_AND_LOADED);
110 metrics.recordCastAPIExtensionStatus(
111 metrics.CAST_API_EXTENSION_STATUS.LOADED);
114 setTimeout(callback, 0); // Runs asynchronously.
117 if(!chrome.cast || !chrome.cast.isAvailable) {
118 var checkTimer = setTimeout(function() {
119 console.error('Either "Google Cast API" or "Google Cast" extension ' +
120 'seems not to be installed?');
122 metrics.recordCastAPIExtensionStatus(
123 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
126 window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
127 clearTimeout(checkTimer);
132 metrics.recordCastAPIExtensionStatus(
133 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
135 console.error('Google Cast extension load failed.', errorInfo);
139 // Just executes the callback since the API is already loaded.
145 * Initialize Cast API.
147 function initializeApi() {
148 var onSession = function() {
149 // TODO(yoshiki): Implement this.
152 var onInitSuccess = function() {
153 // TODO(yoshiki): Implement this.
157 * @param {chrome.cast.Error} error
159 var onError = function(error) {
160 console.error('Error on Cast initialization.', error);
163 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID);
164 var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
167 chrome.cast.initialize(apiConfig, onInitSuccess, onError);
171 * Called when receiver availability is changed. This method is also called when
172 * initialization is completed.
174 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts.
175 * @param {Array.<Object>} receivers List of casts.
177 function onReceiver(availability, receivers) {
178 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) {
180 console.error('Receiver list is empty.');
184 metrics.recordNumberOfCastDevices(receivers.length);
185 player.setCastList(receivers);
186 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) {
187 metrics.recordNumberOfCastDevices(0);
188 player.setCastList([]);
190 console.error('Unexpected response in onReceiver.', arguments);
191 player.setCastList([]);