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 function initializeApi() { |
hirono
2014/07/23 04:37:31
nit: missing jsdoc.
yoshiki
2014/07/23 05:07:32
Done.
| |
51 player.setCastList(receivers); | 55 var onSession = function() { |
56 // TODO(yoshiki): Implement this. | |
57 }; | |
58 | |
59 var onInitSuccess = function() { | |
60 // TODO(yoshiki): Implement this. | |
61 }; | |
62 | |
63 /** | |
64 * @param {chrome.cast.Error} error | |
65 */ | |
66 var onError = function(error) { | |
67 console.error('Error on Cast initialization.', error); | |
68 }; | |
69 | |
70 var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID); | |
71 var apiConfig = new chrome.cast.ApiConfig(sessionRequest, | |
72 onSession, | |
73 onReceiver); | |
74 chrome.cast.initialize(apiConfig, onInitSuccess, onError); | |
52 } | 75 } |
76 | |
77 /** | |
78 * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. | |
79 * @param {Array.<Object>} receivers List of casts. | |
80 */ | |
81 function onReceiver(availability, receivers) { | |
82 if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { | |
83 player.setCastList(receivers); | |
84 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | |
85 player.setCastList([]); | |
86 } else { | |
87 console.error('Unexpected response in onReceiver.', arguments); | |
88 player.setCastList([]); | |
89 } | |
90 } | |
OLD | NEW |