Chromium Code Reviews| Index: ui/file_manager/video_player/js/cast/load_cast_extension_api.js |
| diff --git a/ui/file_manager/video_player/js/cast/load_cast_extension_api.js b/ui/file_manager/video_player/js/cast/load_cast_extension_api.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8b7979f121c5fc20820089376daa8d5d0178861d |
| --- /dev/null |
| +++ b/ui/file_manager/video_player/js/cast/load_cast_extension_api.js |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Defines the cast.ExtensionApi class. This class inherits cast.Api and must |
| + * be decraled after the parent is loaded. So we introduce this method to |
| + * decrale it with delay. |
| + */ |
| +function loadCastExtensionApi() { |
| + if (!cast) { |
| + console.error('"cast" namespace is not defined.'); |
| + return; |
| + } |
| + |
| + /** |
| + * @constructor |
| + * @param {string} castExtensionId Extension ID of cast extension. |
| + * @extends {cast.Api} |
| + */ |
| + cast.ExtensionApi = function(castExtensionId) { |
| + this.castExtensionId_ = castExtensionId; |
| + cast.Client.clientId_ = chrome.runtime.id; |
| + |
| + cast.Api.call(this); |
| + }; |
| + |
| + cast.ExtensionApi.prototype.__proto__ = cast.Api.prototype; |
| + |
| + /** |
| + * @override |
| + */ |
| + cast.ExtensionApi.prototype.init = function() { |
| + chrome.runtime.onMessageExternal.addListener( |
| + this.onMessageExternal_.bind(this)); |
| + this.sendRequest(cast.AppRequestType.REGISTER_CLIENT, {}); |
| + cast.isAvailable = true; |
| + }; |
| + |
| + /** |
| + * @override |
| + */ |
| + cast.ExtensionApi.prototype.onDisconnect = function() {}; |
| + |
| + /** |
| + * @override |
| + */ |
| + cast.ExtensionApi.prototype.sendRequest = function(requestType, request) { |
| + if (!this.castExtensionId_) { |
| + return null; |
|
fukino
2014/07/11 04:35:51
nit: Chromium coding style says "Do not use braces
|
| + } |
| + |
| + var appRequest = new cast.Message(cast.Client.getClientId(), |
| + cast.NAME, cast.Client.getNextSeq(), requestType, request); |
| + |
| + // Use response callback for message ACK to detect extension unload. |
| + var responseCallback = function() { |
| + if (chrome.runtime.lastError) { |
| + // Unregister the cast extension. |
| + this.castExtensionId_ = ''; |
| + cast.isAvailable = false; |
| + this.onDisconnect(); |
| + } |
| + }.bind(this); |
| + |
| + chrome.runtime.sendMessage(this.castExtensionId_, |
| + appRequest, |
| + responseCallback); |
| + return appRequest; |
| + }; |
| + |
| + /** |
| + * @param {string} message |
| + * @param {function(boolean)} sender |
| + * @param {function(boolean)} sendResponse |
| + * @private |
| + */ |
| + cast.ExtensionApi.prototype.onMessageExternal_ = function(message, |
| + sender, sendResponse) { |
| + if (!this.castExtensionId_ || sender.id != this.castExtensionId_) { |
| + return; |
|
fukino
2014/07/11 04:35:50
ditto
|
| + } |
| + |
| + if (!message) { |
| + // No content. |
| + return; |
|
fukino
2014/07/11 04:35:50
ditto
|
| + } |
| + |
| + this.processCastMessage(/** @type {cast.Message} */(message)); |
| + }; |
| +} |