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.
7 // This hack prevents a bug on the cast extension.
8 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps.
9 // Although localStorage in Chrome app is not supported, but it's used in the
10 // cast extension. This line prevents an exception on using localStorage.
11 window.__defineGetter__('localStorage', function() { return {}; });
13 // THIS IS A TEST APP.
14 // TODO(yoshiki): Fix this before launch.
15 var APPLICATION_ID = '214CC863';
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.');
41 * 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.
43 * The given callback is executes after the cast SDK extension is initialized.
45 * @param {function} callback Callback (executed asynchronously).
46 * @param {boolean=} opt_secondTry Spericy try if it's second call after
47 * installation of Cast API extension.
49 function loadCastAPI(callback, opt_secondTry) {
50 var script = document.createElement('script');
52 var onError = function() {
53 script.removeEventListener('error', onError);
54 document.body.removeChild(script);
57 // Shows error message and exits if it's the 2nd try.
58 console.error('Google Cast API extension load failed.');
62 // Installs the Google Cast API extension and retry loading.
63 chrome.fileManagerPrivate.installWebstoreItem(
64 'mafeflapfdfljijmlienjedomfjfmhpd',
65 true, // Don't use installation prompt.
67 if (chrome.runtime.lastError) {
68 console.error('Google Cast API extension installation error.',
69 chrome.runtime.lastError.message);
73 console.info('Google Cast API extension installed.');
75 setTimeout(loadCastAPI.bind(null, callback, true));
79 // Trys to load the cast API extention which is defined in manifest.json.
80 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js';
81 script.addEventListener('error', onError);
82 script.addEventListener('load', onLoadCastExtension.bind(null, callback));
83 document.body.appendChild(script);
87 * Loads the cast sdk extension.
88 * @param {function()} callback Callback (executed asynchronously).
90 function onLoadCastExtension(callback) {
91 if(!chrome.cast || !chrome.cast.isAvailable) {
92 var checkTimer = setTimeout(function() {
93 console.error('Either "Google Cast API" or "Google Cast" extension ' +
94 'seems not to be installed?');
97 window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
98 clearTimeout(checkTimer);
103 console.error('Google Cast extension load failed.', errorInfo);
106 setTimeout(callback); // Runs asynchronously.
111 * Initialize Cast API.
113 function initializeApi() {
114 var onSession = function() {
115 // TODO(yoshiki): Implement this.
118 var onInitSuccess = function() {
119 // TODO(yoshiki): Implement this.
123 * @param {chrome.cast.Error} error
125 var onError = function(error) {
126 console.error('Error on Cast initialization.', error);
129 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID);
130 var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
133 chrome.cast.initialize(apiConfig, onInitSuccess, onError);
137 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts.
138 * @param {Array.<Object>} receivers List of casts.
140 function onReceiver(availability, receivers) {
141 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) {
143 console.error('Receiver list is empty.');
147 player.setCastList(receivers);
148 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) {
149 player.setCastList([]);
151 console.error('Unexpected response in onReceiver.', arguments);
152 player.setCastList([]);