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 {}; });
11 var APPLICATION_ID = '4CCB98DA';
13 util.addPageLoadHandler(function() {
18 * Starts initialization of cast-related feature.
20 function initialize() {
21 if (window.loadMockCastExtensionForTest) {
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.
24 onLoadCastExtension(initializeApi);
28 CastExtensionDiscoverer.findInstalledExtension(function(foundId) {
30 loadCastAPI(initializeApi);
32 console.info('No Google Cast extension is installed.');
37 * 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.
39 * The given callback is executes after the cast SDK extension is initialized.
41 * @param {function} callback Callback (executed asynchronously).
42 * @param {boolean=} opt_secondTry Spericy try if it's second call after
43 * installation of Cast API extension.
45 function loadCastAPI(callback, opt_secondTry) {
46 var script = document.createElement('script');
48 var onError = function() {
49 script.removeEventListener('error', onError);
50 document.body.removeChild(script);
53 // Shows error message and exits if it's the 2nd try.
54 console.error('Google Cast API extension load failed.');
58 // Installs the Google Cast API extension and retry loading.
59 chrome.fileManagerPrivate.installWebstoreItem(
60 'mafeflapfdfljijmlienjedomfjfmhpd',
61 true, // Don't use installation prompt.
63 if (chrome.runtime.lastError) {
64 console.error('Google Cast API extension installation error.',
65 chrome.runtime.lastError.message);
69 console.info('Google Cast API extension installed.');
71 setTimeout(loadCastAPI.bind(null, callback, true));
75 // Trys to load the cast API extention which is defined in manifest.json.
76 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js';
77 script.addEventListener('error', onError);
78 script.addEventListener('load', onLoadCastExtension.bind(null, callback));
79 document.body.appendChild(script);
83 * Loads the cast sdk extension.
84 * @param {function()} callback Callback (executed asynchronously).
86 function onLoadCastExtension(callback) {
87 if(!chrome.cast || !chrome.cast.isAvailable) {
88 var checkTimer = setTimeout(function() {
89 console.error('Either "Google Cast API" or "Google Cast" extension ' +
90 'seems not to be installed?');
93 window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
94 clearTimeout(checkTimer);
99 console.error('Google Cast extension load failed.', errorInfo);
102 setTimeout(callback); // Runs asynchronously.
107 * Initialize Cast API.
109 function initializeApi() {
110 var onSession = function() {
111 // TODO(yoshiki): Implement this.
114 var onInitSuccess = function() {
115 // TODO(yoshiki): Implement this.
119 * @param {chrome.cast.Error} error
121 var onError = function(error) {
122 console.error('Error on Cast initialization.', error);
125 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID);
126 var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
129 chrome.cast.initialize(apiConfig, onInitSuccess, onError);
133 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts.
134 * @param {Array.<Object>} receivers List of casts.
136 function onReceiver(availability, receivers) {
137 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) {
139 console.error('Receiver list is empty.');
143 player.setCastList(receivers);
144 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) {
145 player.setCastList([]);
147 console.error('Unexpected response in onReceiver.', arguments);
148 player.setCastList([]);