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). |
| 36 * @param {boolean=} opt_secondTry Spericy try if it's second call after |
| 37 * installation of Cast API extension. |
33 */ | 38 */ |
34 function ensureLoad(callback) { | 39 function loadCastAPI(callback, opt_secondTry) { |
35 if(!chrome.cast || !chrome.cast.isAvailable) { | 40 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 | 41 |
41 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | 42 var onError = function() { |
42 if (loaded) { | 43 script.removeEventListener('error', onError); |
43 callback(); | 44 document.body.removeChild(script); |
| 45 |
| 46 if (opt_secondTry) { |
| 47 // Shows error message and exits if it's the 2nd try. |
| 48 console.error('Google Cast API extension load failed.'); |
| 49 return; |
| 50 } |
| 51 |
| 52 // Installs the Google Cast API extension and retry loading. |
| 53 chrome.fileBrowserPrivate.installWebstoreItem( |
| 54 'mafeflapfdfljijmlienjedomfjfmhpd', |
| 55 true, // Don't use installation prompt. |
| 56 function() { |
| 57 if (chrome.runtime.lastError) { |
| 58 console.error('Google Cast API extension installation error.', |
| 59 chrome.runtime.lastError.message); |
| 60 return; |
| 61 } |
| 62 |
| 63 console.info('Google Cast API extension installed.'); |
| 64 // Loads API again. |
| 65 setTimeout(loadCastAPI.bind(null, callback, true)); |
| 66 }.wrap()); |
| 67 }.wrap(); |
| 68 |
| 69 var onLoad = function() { |
| 70 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 71 var checkTimer = setTimeout(function() { |
| 72 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
| 73 'seems not to be installed?'); |
| 74 }.wrap(), 5000); |
| 75 |
| 76 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
44 clearTimeout(checkTimer); | 77 clearTimeout(checkTimer); |
45 } else { | 78 |
46 console.error(errorInfo); | 79 if (loaded) |
47 } | 80 callback(); |
| 81 else |
| 82 console.error('Google Cast exntnsion load failed.', errorInfo); |
| 83 }.wrap(); |
| 84 } else { |
| 85 setTimeout(callback); // Runs asynchronously. |
48 } | 86 } |
49 } else { | 87 }.wrap(); |
50 setTimeout(callback); // Runs asynchronously. | 88 |
51 } | 89 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
| 90 script.addEventListener('error', onError); |
| 91 script.addEventListener('load', onLoad); |
| 92 document.body.appendChild(script); |
52 } | 93 } |
53 | 94 |
54 /** | 95 /** |
55 * Initialize Cast API. | 96 * Initialize Cast API. |
56 */ | 97 */ |
57 function initializeApi() { | 98 function initializeApi() { |
58 var onSession = function() { | 99 var onSession = function() { |
59 // TODO(yoshiki): Implement this. | 100 // TODO(yoshiki): Implement this. |
60 }; | 101 }; |
61 | 102 |
(...skipping 27 matching lines...) Expand all Loading... |
89 } | 130 } |
90 | 131 |
91 player.setCastList(receivers); | 132 player.setCastList(receivers); |
92 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 133 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
93 player.setCastList([]); | 134 player.setCastList([]); |
94 } else { | 135 } else { |
95 console.error('Unexpected response in onReceiver.', arguments); | 136 console.error('Unexpected response in onReceiver.', arguments); |
96 player.setCastList([]); | 137 player.setCastList([]); |
97 } | 138 } |
98 } | 139 } |
OLD | NEW |