Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: remoting/webapp/base/js/protocol_extension_manager.js

Issue 1067133002: Move ProtocolExtensionManager from SessionConnector into its own class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and Reviewer's feedback Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)} sendExtensionMessage Callback used to
14 * send an extension message to the plugin.
15 * @constructor
16 * @implements {base.Disposable}
17 */
18 remoting.ProtocolExtensionManager = function(sendExtensionMessage) {
19 /** @private */
20 this.sendExtensionMessage_ = sendExtensionMessage;
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 * @return {boolean} true if the extension is successfully registered.
50 */
51 remoting.ProtocolExtensionManager.prototype.register =
52 function(extension) {
53 var types = extension.getExtensionTypes();
54
55 // Make sure we don't have an extension of that type already registered.
56 for (var i=0, len=types.length; i < len; i++) {
57 if (types[i] in this.protocolExtensions_) {
58 console.error(
59 'Attempt to register multiple extensions of the same type: ', type);
60 return false;
61 }
62 }
63
64 for (var i=0, len=types.length; i < len; i++) {
65 var type = types[i];
66 this.protocolExtensions_[type] = extension;
67 if (this.protocolExtensionsStarted_) {
68 this.startExtension_(type);
69 }
70 }
71 return true;
72 };
73
74 /**
75 * @param {string} type
76 * @private
77 */
78 remoting.ProtocolExtensionManager.prototype.startExtension_ =
79 function(type) {
80 var extension = this.protocolExtensions_[type];
81 extension.startExtension(this.sendExtensionMessage_);
82 };
83
84 /**
85 * Called when an extension message needs to be handled.
86 *
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.
90 */
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;
96 }
97
98 var message = base.jsonParseSafe(data);
99 if (typeof message != 'object') {
100 console.error('Error parsing extension json data: ' + data);
101 return false;
102 }
103
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);
113 }
114 if (handled) {
115 return true;
116 }
117 }
118
119 return false;
120 };
121
122 })();
OLDNEW
« no previous file with comments | « remoting/webapp/app_remoting/js/app_remoting.js ('k') | remoting/webapp/base/js/protocol_extension_manager_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698