| 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 /** | 5 /** |
| 6 * @fileoverview Description of this file. | 6 * @fileoverview Description of this file. |
| 7 * Class handling interaction with the cast extension session of the Chromoting | 7 * Class handling interaction with the cast extension session of the Chromoting |
| 8 * host. It receives and sends extension messages from/to the host through | 8 * host. It receives and sends extension messages from/to the host through |
| 9 * the client session. It uses the Google Cast Chrome Sender API library to | 9 * the client session. It uses the Google Cast Chrome Sender API library to |
| 10 * interact with nearby Cast receivers. | 10 * interact with nearby Cast receivers. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 /** @private {string} */ | 34 /** @private {string} */ |
| 35 this.kApplicationId_ = "8A1211E3"; | 35 this.kApplicationId_ = "8A1211E3"; |
| 36 | 36 |
| 37 /** @private {Array<Object>} */ | 37 /** @private {Array<Object>} */ |
| 38 this.messageQueue_ = []; | 38 this.messageQueue_ = []; |
| 39 | 39 |
| 40 /** @private {?function(string,string)} */ | 40 /** @private {?function(string,string)} */ |
| 41 this.sendMessageToHostCallback_ = null; | 41 this.sendMessageToHostCallback_ = null; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 /** @return {string} */ | 44 /** @private {string} */ |
| 45 remoting.CastExtensionHandler.prototype.getType = function() { | 45 remoting.CastExtensionHandler.EXTENSION_TYPE = 'cast_message'; |
| 46 return 'cast_message'; | 46 |
| 47 /** @return {Array<string>} */ |
| 48 remoting.CastExtensionHandler.prototype.getExtensionTypes = function() { |
| 49 return [remoting.CastExtensionHandler.EXTENSION_TYPE]; |
| 47 }; | 50 }; |
| 48 | 51 |
| 49 /** | 52 /** |
| 50 * The id of the script node. | 53 * The id of the script node. |
| 51 * @private {string} | 54 * @private {string} |
| 52 */ | 55 */ |
| 53 remoting.CastExtensionHandler.prototype.SCRIPT_NODE_ID_ = 'cast-script-node'; | 56 remoting.CastExtensionHandler.prototype.SCRIPT_NODE_ID_ = 'cast-script-node'; |
| 54 | 57 |
| 55 /** | 58 /** |
| 56 * Attempts to load the Google Cast Chrome Sender API libary. | 59 * Attempts to load the Google Cast Chrome Sender API libary. |
| 57 * | 60 * |
| 58 * @param {function(string,string)} sendMessageToHost Callback to send a message | 61 * @param {function(string,string)} sendMessageToHost Callback to send a message |
| 59 * to the host. | 62 * to the host. |
| 60 */ | 63 */ |
| 61 remoting.CastExtensionHandler.prototype.start = function(sendMessageToHost) { | 64 remoting.CastExtensionHandler.prototype.startExtension = |
| 65 function(sendMessageToHost) { |
| 62 this.sendMessageToHostCallback_ = sendMessageToHost; | 66 this.sendMessageToHostCallback_ = sendMessageToHost; |
| 63 | 67 |
| 64 var node = document.getElementById(this.SCRIPT_NODE_ID_); | 68 var node = document.getElementById(this.SCRIPT_NODE_ID_); |
| 65 if (node) { | 69 if (node) { |
| 66 console.error( | 70 console.error( |
| 67 'Multiple calls to CastExtensionHandler.start_ not expected.'); | 71 'Multiple calls to CastExtensionHandler.start_ not expected.'); |
| 68 return; | 72 return; |
| 69 } | 73 } |
| 70 | 74 |
| 71 // Create a script node to load the Cast Sender API. | 75 // Create a script node to load the Cast Sender API. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 }; | 87 }; |
| 84 var onLoadError = function(event) { | 88 var onLoadError = function(event) { |
| 85 console.error("Failed to load Chrome Cast Sender API."); | 89 console.error("Failed to load Chrome Cast Sender API."); |
| 86 } | 90 } |
| 87 node.addEventListener('load', onLoad, false); | 91 node.addEventListener('load', onLoad, false); |
| 88 node.addEventListener('error', onLoadError, false); | 92 node.addEventListener('error', onLoadError, false); |
| 89 }; | 93 }; |
| 90 | 94 |
| 91 /** | 95 /** |
| 92 * Process Cast Extension Messages from the Chromoting host. | 96 * Process Cast Extension Messages from the Chromoting host. |
| 93 * @param {string} msgString The extension message's data. | 97 * |
| 98 * @param {string} type The message type. |
| 99 * @param {Object} message The parsed extension message data. |
| 100 * @return {boolean} True if the extension message was handled. |
| 94 */ | 101 */ |
| 95 remoting.CastExtensionHandler.prototype.onMessage = function(msgString) { | 102 remoting.CastExtensionHandler.prototype.onExtensionMessage = |
| 96 var message = getJsonObjectFromString(msgString); | 103 function(type, message) { |
| 97 | |
| 98 // Save messages to send after a session is established. | 104 // Save messages to send after a session is established. |
| 99 this.messageQueue_.push(message); | 105 this.messageQueue_.push(message); |
| 100 // Trigger the sending of pending messages, followed by the one just | 106 // Trigger the sending of pending messages, followed by the one just |
| 101 // received. | 107 // received. |
| 102 if (this.session_) { | 108 if (this.session_) { |
| 103 this.sendPendingMessages_(); | 109 this.sendPendingMessages_(); |
| 104 } | 110 } |
| 111 return true; |
| 105 }; | 112 }; |
| 106 | 113 |
| 107 /** | 114 /** |
| 108 * Send cast-extension messages through the host via the plugin. | 115 * Send cast-extension messages through the host via the plugin. |
| 109 * @param {Object} data The JSON response to be sent to the host. The | 116 * @param {Object} data The JSON response to be sent to the host. The |
| 110 * response object must contain the appropriate keys. | 117 * response object must contain the appropriate keys. |
| 111 * @private | 118 * @private |
| 112 */ | 119 */ |
| 113 remoting.CastExtensionHandler.prototype.sendMessageToHost_ = function(data) { | 120 remoting.CastExtensionHandler.prototype.sendMessageToHost_ = function(data) { |
| 114 this.sendMessageToHostCallback_(this.getType(), JSON.stringify(data)); | 121 this.sendMessageToHostCallback_(remoting.CastExtensionHandler.EXTENSION_TYPE, |
| 122 JSON.stringify(data)); |
| 115 }; | 123 }; |
| 116 | 124 |
| 117 /** | 125 /** |
| 118 * Send pending messages from the host to the receiver app. | 126 * Send pending messages from the host to the receiver app. |
| 119 * @private | 127 * @private |
| 120 */ | 128 */ |
| 121 remoting.CastExtensionHandler.prototype.sendPendingMessages_ = function() { | 129 remoting.CastExtensionHandler.prototype.sendPendingMessages_ = function() { |
| 122 var len = this.messageQueue_.length; | 130 var len = this.messageQueue_.length; |
| 123 for(var i = 0; i<len; i++) { | 131 for(var i = 0; i<len; i++) { |
| 124 this.session_.sendMessage(this.kCastNamespace_, | 132 this.session_.sendMessage(this.kCastNamespace_, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 }; | 346 }; |
| 339 | 347 |
| 340 /** | 348 /** |
| 341 * Listener invoked when we fail to stop the receiver application. | 349 * Listener invoked when we fail to stop the receiver application. |
| 342 * | 350 * |
| 343 * @param {chrome.cast.Error} error The error code. | 351 * @param {chrome.cast.Error} error The error code. |
| 344 */ | 352 */ |
| 345 remoting.CastExtensionHandler.prototype.onStopAppError = function(error) { | 353 remoting.CastExtensionHandler.prototype.onStopAppError = function(error) { |
| 346 console.error('Error Stopping App: ', error); | 354 console.error('Error Stopping App: ', error); |
| 347 }; | 355 }; |
| OLD | NEW |