Index: ui/file_manager/video_player/js/cast/caster.js |
diff --git a/ui/file_manager/video_player/js/cast/caster.js b/ui/file_manager/video_player/js/cast/caster.js |
index 6ff0831c63a3b11b60dddca5fa81ccc6cd21002a..86e800b151ae2cde62e3b372aeb896ae4aa2cfef 100644 |
--- a/ui/file_manager/video_player/js/cast/caster.js |
+++ b/ui/file_manager/video_player/js/cast/caster.js |
@@ -4,11 +4,11 @@ |
'use strict'; |
-/** |
- * The instance of cast api. |
- * @type {cast.ExtensionApi} |
- */ |
-var castApi = null; |
+// This hack prevents a bug on the cast extension. |
+// TODO(yoshiki): Remove this once the cast extension supports Chrome apps. |
+// Although localStorage in Chrome app is not supported, but it's used in the |
+// cast extension. This line prevents an exception on using localStorage. |
+window.__defineGetter__('localStorage', function() { return {}; }); |
/** |
* @type {string} |
@@ -16,37 +16,75 @@ var castApi = null; |
*/ |
var CAST_COMMAND_LINE_FLAG = 'enable-video-player-chromecast-support'; |
+// THIS IS A TEST APP. |
+// TODO(yoshiki): Fix this before launch. |
+var APPLICATION_ID = '214CC863'; |
+ |
chrome.commandLinePrivate.hasSwitch(CAST_COMMAND_LINE_FLAG, function(result) { |
if (!result) |
return; |
- CastExtensionDiscoverer.findInstalledExtension(onCastExtensionFound); |
+ ensureLoad(initializeApi); |
}); |
-function onCastExtensionFound(extensionId) { |
- if (!extensionId) { |
- console.info('Cast extention is not found.'); |
- return; |
+/** |
+ * Executes the given callback after the cast extension is initialized. |
+ * @param {function} callback Callback (executed asynchronously). |
+ */ |
+function ensureLoad(callback) { |
+ if(!chrome.cast || !chrome.cast.isAvailable) { |
+ var checkTimer = setTimeout(function() { |
+ console.error('Either "Google Cast API" or "Google Cast" extension ' + |
+ 'seems not to be installed?'); |
+ }, 5000); |
+ |
+ window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
+ if (loaded) { |
+ callback(); |
+ clearTimeout(chcekTimer); |
+ } else { |
+ console.error(errorInfo); |
+ } |
+ } |
+ } else { |
+ setTimeout(callback); // Runs asynchronously. |
} |
+} |
- var api = document.createElement('script'); |
- api.src = 'chrome-extension://' + extensionId + '/api_script.js'; |
- api.onload = function() { |
- initializeCast(extensionId); |
+function initializeApi() { |
hirono
2014/07/23 04:37:31
nit: missing jsdoc.
yoshiki
2014/07/23 05:07:32
Done.
|
+ var onSession = function() { |
+ // TODO(yoshiki): Implement this. |
}; |
- api.onerror = function() { |
- console.error('api_script.js load failed.'); |
+ |
+ var onInitSuccess = function() { |
+ // TODO(yoshiki): Implement this. |
}; |
- document.body.appendChild(api); |
-}; |
-function initializeCast(extensionId) { |
- loadCastExtensionApi(); |
+ /** |
+ * @param {chrome.cast.Error} error |
+ */ |
+ var onError = function(error) { |
+ console.error('Error on Cast initialization.', error); |
+ }; |
- castApi = new cast.ExtensionApi(extensionId); |
- castApi.addReceiverListener('ChromeCast', onReceiverUpdate); |
+ var sessionRequest = new chrome.cast.SessionRequest(APPLICATION_ID); |
+ var apiConfig = new chrome.cast.ApiConfig(sessionRequest, |
+ onSession, |
+ onReceiver); |
+ chrome.cast.initialize(apiConfig, onInitSuccess, onError); |
} |
-function onReceiverUpdate(receivers) { |
- player.setCastList(receivers); |
+/** |
+ * @param {chrome.cast.ReceiverAvailability} availability Availability of casts. |
+ * @param {Array.<Object>} receivers List of casts. |
+ */ |
+function onReceiver(availability, receivers) { |
+ if (availability === chrome.cast.ReceiverAvailability.AVAILABLE) { |
+ player.setCastList(receivers); |
+ } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
+ player.setCastList([]); |
+ } else { |
+ console.error('Unexpected response in onReceiver.', arguments); |
+ player.setCastList([]); |
+ } |
} |