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<string,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 base
.debug
.assert(!this.protocolExtensionsStarted_
);
41 /** @type {Object<remoting.ProtocolExtension, boolean>} */
43 for (var type
in this.protocolExtensions_
) {
44 var extension
= this.protocolExtensions_
[type
];
45 if (!(extension
in started
)) {
46 extension
.startExtension(this.sendExtensionMessage_
);
47 started
[extension
] = true;
50 this.protocolExtensionsStarted_
= true;
54 * @param {remoting.ProtocolExtension} extension
55 * @return {boolean} true if the extension is successfully registered.
57 remoting
.ProtocolExtensionManager
.prototype.register
=
59 var types
= extension
.getExtensionTypes();
61 // Make sure we don't have an extension of that type already registered.
62 for (var i
=0, len
=types
.length
; i
< len
; i
++) {
63 if (types
[i
] in this.protocolExtensions_
) {
65 'Attempt to register multiple extensions of the same type: ', type
);
70 // Register the extension for each type.
71 for (var i
=0, len
=types
.length
; i
< len
; i
++) {
73 this.protocolExtensions_
[type
] = extension
;
76 // Start the extension.
77 if (this.protocolExtensionsStarted_
) {
78 extension
.startExtension(this.sendExtensionMessage_
);
85 * Called when an extension message needs to be handled.
87 * @param {string} type The type of the extension message.
88 * @param {string} data The payload of the extension message.
89 * @return {boolean} Return true if the extension message was recognized.
91 remoting
.ProtocolExtensionManager
.prototype.onProtocolExtensionMessage
=
92 function(type
, data
) {
93 if (type
== 'test-echo-reply') {
94 console
.log('Got echo reply: ' + data
);
98 var message
= base
.jsonParseSafe(data
);
99 if (typeof message
!= 'object') {
100 console
.error('Error parsing extension json data: ' + data
);
104 if (type
in this.protocolExtensions_
) {
105 /** @type {remoting.ProtocolExtension} */
106 var extension
= this.protocolExtensions_
[type
];
109 handled
= extension
.onExtensionMessage(type
, message
);
110 } catch (/** @type {*} */ err
) {
111 console
.error('Failed to process protocol extension ' + type
+