OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** @suppress {duplicate} */ |
| 6 var remoting = remoting || {}; |
| 7 |
| 8 (function() { |
| 9 |
| 10 'use strict'; |
| 11 |
| 12 /** |
| 13 * @param {function(string, string)} sendClientMessageCallback Callback used to |
| 14 * send a client message to the plugin. |
| 15 * @constructor |
| 16 * @implements {base.Disposable} |
| 17 */ |
| 18 remoting.ProtocolExtensionManager = function(sendClientMessageCallback) { |
| 19 /** @private */ |
| 20 this.sendExtensionMessage_ = sendClientMessageCallback; |
| 21 /** @private {Object<string,remoting.ProtocolExtension>} */ |
| 22 this.protocolExtensions_ = {}; |
| 23 |
| 24 /** |
| 25 * True once a session has been created and we've started the extensions. |
| 26 * This is used to immediately start any extensions that are registered |
| 27 * after the CONNECTED state change. |
| 28 * @private |
| 29 */ |
| 30 this.protocolExtensionsStarted_ = false; |
| 31 }; |
| 32 |
| 33 remoting.ProtocolExtensionManager.prototype.dispose = function() { |
| 34 this.sendExtensionMessage_ = base.doNothing; |
| 35 this.protocolExtensions_ = {}; |
| 36 }; |
| 37 |
| 38 /** Called by the plugin when the session is connected */ |
| 39 remoting.ProtocolExtensionManager.prototype.start = function() { |
| 40 base.debug.assert(!this.protocolExtensionsStarted_); |
| 41 for (var type in this.protocolExtensions_) { |
| 42 this.startExtension_(type); |
| 43 } |
| 44 this.protocolExtensionsStarted_ = true; |
| 45 }; |
| 46 |
| 47 /** |
| 48 * @param {remoting.ProtocolExtension} extension |
| 49 */ |
| 50 remoting.ProtocolExtensionManager.prototype.register = |
| 51 function(extension) { |
| 52 var types = extension.getExtensionTypes(); |
| 53 |
| 54 // Make sure we don't have an extension of that type already registered. |
| 55 for (var i=0, len=types.length; i < len; i++) { |
| 56 if (types[i] in this.protocolExtensions_) { |
| 57 console.error( |
| 58 'Attempt to register multiple extensions of the same type: ', type); |
| 59 return; |
| 60 } |
| 61 } |
| 62 |
| 63 for (var i=0, len=types.length; i < len; i++) { |
| 64 var type = types[i]; |
| 65 this.protocolExtensions_[type] = extension; |
| 66 if (this.protocolExtensionsStarted_) { |
| 67 this.startExtension_(type); |
| 68 } |
| 69 } |
| 70 }; |
| 71 |
| 72 /** |
| 73 * @param {string} type |
| 74 * @private |
| 75 */ |
| 76 remoting.ProtocolExtensionManager.prototype.startExtension_ = |
| 77 function(type) { |
| 78 var extension = this.protocolExtensions_[type]; |
| 79 extension.startExtension(this.sendExtensionMessage_); |
| 80 }; |
| 81 |
| 82 /** |
| 83 * Called when an extension message needs to be handled. |
| 84 * |
| 85 * @param {string} type The type of the extension message. |
| 86 * @param {string} data The payload of the extension message. |
| 87 * @return {boolean} Return true if the extension message was recognized. |
| 88 */ |
| 89 remoting.ProtocolExtensionManager.prototype.onProtocolExtensionMessage = |
| 90 function(type, data) { |
| 91 if (type == 'test-echo-reply') { |
| 92 console.log('Got echo reply: ' + data); |
| 93 return true; |
| 94 } |
| 95 |
| 96 var message = base.jsonParseSafe(data); |
| 97 if (typeof message != 'object') { |
| 98 console.error('Error parsing extension json data: ' + data); |
| 99 return false; |
| 100 } |
| 101 |
| 102 if (type in this.protocolExtensions_) { |
| 103 /** @type {remoting.ProtocolExtension} */ |
| 104 var extension = this.protocolExtensions_[type]; |
| 105 var handled = false; |
| 106 try { |
| 107 handled = extension.onExtensionMessage(type, message); |
| 108 } catch (/** @type {*} */ err) { |
| 109 console.error('Failed to process protocol extension ' + type + |
| 110 ' message: ' + err); |
| 111 } |
| 112 if (handled) { |
| 113 return true; |
| 114 } |
| 115 } |
| 116 |
| 117 return false; |
| 118 }; |
| 119 |
| 120 })(); |
OLD | NEW |