Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / webapp / base / js / protocol_extension_manager.js
blob216c1ce0290e24cd834a94de7b70a48e4d6496c2
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 || {};
8 (function() {
10 'use strict';
12 /**
13 * @param {function(string, string)} sendExtensionMessage Callback used to
14 * send an extension message to the plugin.
15 * @constructor
16 * @implements {base.Disposable}
18 remoting.ProtocolExtensionManager = function(sendExtensionMessage) {
19 /** @private */
20 this.sendExtensionMessage_ = sendExtensionMessage;
21 /** @private {Object<string,remoting.ProtocolExtension>} */
22 this.protocolExtensions_ = {};
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
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>} */
42 var started = {};
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;
53 /**
54 * @param {remoting.ProtocolExtension} extension
55 * @return {boolean} true if the extension is successfully registered.
57 remoting.ProtocolExtensionManager.prototype.register =
58 function(extension) {
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_) {
64 console.error(
65 'Attempt to register multiple extensions of the same type: ', type);
66 return false;
70 // Register the extension for each type.
71 for (var i=0, len=types.length; i < len; i++) {
72 var type = types[i];
73 this.protocolExtensions_[type] = extension;
76 // Start the extension.
77 if (this.protocolExtensionsStarted_) {
78 extension.startExtension(this.sendExtensionMessage_);
81 return true;
84 /**
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);
95 return true;
98 var message = base.jsonParseSafe(data);
99 if (typeof message != 'object') {
100 console.error('Error parsing extension json data: ' + data);
101 return false;
104 if (type in this.protocolExtensions_) {
105 /** @type {remoting.ProtocolExtension} */
106 var extension = this.protocolExtensions_[type];
107 var handled = false;
108 try {
109 handled = extension.onExtensionMessage(type, message);
110 } catch (/** @type {*} */ err) {
111 console.error('Failed to process protocol extension ' + type +
112 ' message: ' + err);
114 if (handled) {
115 return true;
119 return false;
122 })();