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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // This hack prevents a bug on the cast extension. | 7 // This hack prevents a bug on the cast extension. |
8 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. | 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 | 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. | 10 // cast extension. This line prevents an exception on using localStorage. |
11 window.__defineGetter__('localStorage', function() { return {}; }); | 11 window.__defineGetter__('localStorage', function() { return {}; }); |
12 | 12 |
13 /** | 13 /** |
14 * @type {string} | 14 * @type {string} |
15 * @const | 15 * @const |
16 */ | 16 */ |
17 var CAST_COMMAND_LINE_FLAG = 'enable-video-player-chromecast-support'; | 17 var CAST_COMMAND_LINE_FLAG = 'enable-video-player-chromecast-support'; |
18 | 18 |
19 // THIS IS A TEST APP. | 19 // THIS IS A TEST APP. |
20 // TODO(yoshiki): Fix this before launch. | 20 // TODO(yoshiki): Fix this before launch. |
21 var APPLICATION_ID = '214CC863'; | 21 var APPLICATION_ID = '214CC863'; |
22 | 22 |
23 chrome.commandLinePrivate.hasSwitch(CAST_COMMAND_LINE_FLAG, function(result) { | 23 chrome.commandLinePrivate.hasSwitch(CAST_COMMAND_LINE_FLAG, function(result) { |
24 if (!result) | 24 if (!result) |
25 return; | 25 return; |
26 | 26 |
27 ensureLoad(initializeApi); | 27 // TODO(yoshiki): Check if the Google Cast extension is installed or not. |
28 // If not installed, we should skip all cast-related functionality. | |
29 | |
30 loadCastAPI(initializeApi); | |
28 }); | 31 }); |
29 | 32 |
30 /** | 33 /** |
31 * Executes the given callback after the cast extension is initialized. | 34 * Executes the given callback after the cast extension is initialized. |
32 * @param {function} callback Callback (executed asynchronously). | 35 * @param {function} callback Callback (executed asynchronously). |
hirono
2014/08/01 09:58:08
nit: jsdoc for opt_secondTry
yoshiki
2014/08/01 11:46:41
Done.
| |
33 */ | 36 */ |
34 function ensureLoad(callback) { | 37 function loadCastAPI(callback, opt_secondTry) { |
35 if(!chrome.cast || !chrome.cast.isAvailable) { | 38 var script = document.createElement('script'); |
36 var checkTimer = setTimeout(function() { | |
37 console.error('Either "Google Cast API" or "Google Cast" extension ' + | |
38 'seems not to be installed?'); | |
39 }, 5000); | |
40 | 39 |
41 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | 40 var onError = function() { |
42 if (loaded) { | 41 script.removeEventListener('error', onError); |
43 callback(); | 42 document.body.removeChild(script); |
43 | |
44 if (opt_secondTry) { | |
45 // Shows error message and exits if it's the 2nd try. | |
46 console.error('Google Cast API extension load failed.'); | |
47 return; | |
48 } | |
49 | |
50 // Installs the Google Cast API extension and retry loading. | |
51 chrome.fileBrowserPrivate.installWebstoreItem( | |
52 'mafeflapfdfljijmlienjedomfjfmhpd', | |
53 true, // Don't use installation prompt. | |
54 function() { | |
55 if (chrome.runtime.lastError) { | |
56 console.error('Google Cast API extension installation error.', | |
57 chrome.runtime.lastError.message); | |
58 return; | |
59 } | |
60 | |
61 console.info('Google Cast API extension installed.'); | |
62 // Loads API again. | |
63 setTimeout(loadCastAPI.bind(null, callback, true)); | |
64 }.wrap()); | |
65 }.wrap(); | |
66 | |
67 var onLoad = function() { | |
68 if(!chrome.cast || !chrome.cast.isAvailable) { | |
69 var checkTimer = setTimeout(function() { | |
70 console.error('Either "Google Cast API" or "Google Cast" extension ' + | |
71 'seems not to be installed?'); | |
72 }.wrap(), 5000); | |
73 | |
74 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | |
44 clearTimeout(checkTimer); | 75 clearTimeout(checkTimer); |
45 } else { | 76 |
46 console.error(errorInfo); | 77 if (loaded) |
47 } | 78 callback(); |
79 else | |
80 console.error('Google Cast exntnsion load failed.', errorInfo); | |
81 }.wrap(); | |
82 } else { | |
83 setTimeout(callback); // Runs asynchronously. | |
48 } | 84 } |
49 } else { | 85 }.wrap(); |
50 setTimeout(callback); // Runs asynchronously. | 86 |
51 } | 87 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
88 script.addEventListener('error', onError); | |
89 script.addEventListener('load', onLoad); | |
90 document.body.appendChild(script); | |
52 } | 91 } |
53 | 92 |
54 /** | 93 /** |
55 * Initialize Cast API. | 94 * Initialize Cast API. |
56 */ | 95 */ |
57 function initializeApi() { | 96 function initializeApi() { |
58 var onSession = function() { | 97 var onSession = function() { |
59 // TODO(yoshiki): Implement this. | 98 // TODO(yoshiki): Implement this. |
60 }; | 99 }; |
61 | 100 |
(...skipping 27 matching lines...) Expand all Loading... | |
89 } | 128 } |
90 | 129 |
91 player.setCastList(receivers); | 130 player.setCastList(receivers); |
92 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 131 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
93 player.setCastList([]); | 132 player.setCastList([]); |
94 } else { | 133 } else { |
95 console.error('Unexpected response in onReceiver.', arguments); | 134 console.error('Unexpected response in onReceiver.', arguments); |
96 player.setCastList([]); | 135 player.setCastList([]); |
97 } | 136 } |
98 } | 137 } |
OLD | NEW |