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 /** | 7 // This hack prevents a bug on the cast extension. |
8 * The instance of cast api. | 8 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. |
9 * @type {cast.ExtensionApi} | 9 // Although localStorage in Chrome app is not supported, but it's used in the |
10 */ | 10 // cast extension. This line prevents an exception on using localStorage. |
11 var castApi = null; | 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. |
| 20 // TODO(yoshiki): Fix this before launch. |
| 21 var APPLICATION_ID = '214CC863'; |
| 22 |
19 chrome.commandLinePrivate.hasSwitch(CAST_COMMAND_LINE_FLAG, function(result) { | 23 chrome.commandLinePrivate.hasSwitch(CAST_COMMAND_LINE_FLAG, function(result) { |
20 if (!result) | 24 if (!result) |
21 return; | 25 return; |
22 | 26 |
23 CastExtensionDiscoverer.findInstalledExtension(onCastExtensionFound); | 27 ensureLoad(initializeApi); |
24 }); | 28 }); |
25 | 29 |
26 function onCastExtensionFound(extensionId) { | 30 /** |
27 if (!extensionId) { | 31 * Executes the given callback after the cast extension is initialized. |
28 console.info('Cast extention is not found.'); | 32 * @param {function} callback Callback (executed asynchronously). |
29 return; | 33 */ |
| 34 function ensureLoad(callback) { |
| 35 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 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 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
| 42 if (loaded) { |
| 43 callback(); |
| 44 clearTimeout(chcekTimer); |
| 45 } else { |
| 46 console.error(errorInfo); |
| 47 } |
| 48 } |
| 49 } else { |
| 50 setTimeout(callback); // Runs asynchronously. |
30 } | 51 } |
31 | |
32 var api = document.createElement('script'); | |
33 api.src = 'chrome-extension://' + extensionId + '/api_script.js'; | |
34 api.onload = function() { | |
35 initializeCast(extensionId); | |
36 }; | |
37 api.onerror = function() { | |
38 console.error('api_script.js load failed.'); | |
39 }; | |
40 document.body.appendChild(api); | |
41 }; | |
42 | |
43 function initializeCast(extensionId) { | |
44 loadCastExtensionApi(); | |
45 | |
46 castApi = new cast.ExtensionApi(extensionId); | |
47 castApi.addReceiverListener('ChromeCast', onReceiverUpdate); | |
48 } | 52 } |
49 | 53 |
50 function onReceiverUpdate(receivers) { | 54 /** |
51 player.setCastList(receivers); | 55 * Initialize Cast API. |
| 56 */ |
| 57 function initializeApi() { |
| 58 var onSession = function() { |
| 59 // TODO(yoshiki): Implement this. |
| 60 }; |
| 61 |
| 62 var onInitSuccess = function() { |
| 63 // TODO(yoshiki): Implement this. |
| 64 }; |
| 65 |
| 66 /** |
| 67 * @param {chrome.cast.Error} error |
| 68 */ |
| 69 var onError = function(error) { |
| 70 console.error('Error on Cast initialization.', error); |
| 71 }; |
| 72 |
| 73 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID); |
| 74 var apiConfig = new chrome.cast.ApiConfig(sessionRequest, |
| 75 onSession, |
| 76 onReceiver); |
| 77 chrome.cast.initialize(apiConfig, onInitSuccess, onError); |
52 } | 78 } |
| 79 |
| 80 /** |
| 81 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. |
| 82 * @param {Array.<Object>} receivers List of casts. |
| 83 */ |
| 84 function onReceiver(availability, receivers) { |
| 85 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { |
| 86 player.setCastList(receivers); |
| 87 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
| 88 player.setCastList([]); |
| 89 } else { |
| 90 console.error('Unexpected response in onReceiver.', arguments); |
| 91 player.setCastList([]); |
| 92 } |
| 93 } |
OLD | NEW |