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.
5 /** @suppress {duplicate} */
6 var remoting
= remoting
|| {};
13 * @param {function(string, string)} sendExtensionMessage Callback used to
14 * send an extension message to the plugin.
16 * @implements {base.Disposable}
18 remoting
.ProtocolExtensionManager = function(sendExtensionMessage
) {
20 this.sendExtensionMessage_
= sendExtensionMessage
;
21 /** @private {Object<remoting.ProtocolExtension>} */
22 this.protocolExtensions_
= {};
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.
30 this.protocolExtensionsStarted_
= false;
33 remoting
.ProtocolExtensionManager
.prototype.dispose = function() {
34 this.sendExtensionMessage_
= base
.doNothing
;
35 this.protocolExtensions_
= {};
38 /** Called by the plugin when the session is connected */
39 remoting
.ProtocolExtensionManager
.prototype.start = function() {
40 console
.assert(!this.protocolExtensionsStarted_
,
41 'Duplicate start() invocation.');
42 /** @type {Object<remoting.ProtocolExtension, boolean>} */
44 for (var type
in this.protocolExtensions_
) {
45 var extension
= this.protocolExtensions_
[type
];
46 if (!(extension
in started
)) {
47 extension
.startExtension(this.sendExtensionMessage_
);
48 started
[extension
] = true;
51 this.protocolExtensionsStarted_
= true;
55 * @param {remoting.ProtocolExtension} extension
56 * @return {boolean} true if the extension is successfully registered.
58 remoting
.ProtocolExtensionManager
.prototype.register
=
60 var types
= extension
.getExtensionTypes();
62 // Make sure we don't have an extension of that type already registered.
63 for (var i
=0, len
=types
.length
; i
< len
; i
++) {
64 if (types
[i
] in this.protocolExtensions_
) {
66 'Attempt to register multiple extensions of the same type: ', type
);
71 // Register the extension for each type.
72 for (var i
=0, len
=types
.length
; i
< len
; i
++) {
74 this.protocolExtensions_
[type
] = extension
;
77 // Start the extension.
78 if (this.protocolExtensionsStarted_
) {
79 extension
.startExtension(this.sendExtensionMessage_
);
86 * Called when an extension message needs to be handled.
88 * @param {string} type The type of the extension message.
89 * @param {string} data The payload of the extension message.
90 * @return {boolean} Return true if the extension message was recognized.
92 remoting
.ProtocolExtensionManager
.prototype.onProtocolExtensionMessage
=
93 function(type
, data
) {
94 if (type
== 'test-echo-reply') {
95 console
.log('Got echo reply: ' + data
);
99 var message
= base
.jsonParseSafe(data
);
100 if (typeof message
!= 'object') {
101 console
.error('Error parsing extension json data: ' + data
);
105 if (type
in this.protocolExtensions_
) {
106 /** @type {remoting.ProtocolExtension} */
107 var extension
= this.protocolExtensions_
[type
];
110 handled
= extension
.onExtensionMessage(type
, message
);
111 } catch (/** @type {*} */ err
) {
112 console
.error('Failed to process protocol extension ' + type
+