OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * It2MeService listens to incoming connections requests from Hangouts | 7 * It2MeService listens to incoming connections requests from Hangouts |
8 * and the webapp and creates a It2MeHelperChannel between them. | 8 * and the webapp and creates a It2MeHelperChannel between them. |
9 * It supports multiple helper sessions, but only a single helpee. | 9 * It supports multiple helper sessions, but only a single helpee. |
10 */ | 10 */ |
(...skipping 17 matching lines...) Expand all Loading... |
28 /** | 28 /** |
29 * @type {Array.<remoting.It2MeHelperChannel>} | 29 * @type {Array.<remoting.It2MeHelperChannel>} |
30 * @private | 30 * @private |
31 */ | 31 */ |
32 this.helpers_ = []; | 32 this.helpers_ = []; |
33 | 33 |
34 /** @private */ | 34 /** @private */ |
35 this.helpee_ = null; | 35 this.helpee_ = null; |
36 | 36 |
37 this.onWebappConnectRef_ = this.onWebappConnect_.bind(this); | 37 this.onWebappConnectRef_ = this.onWebappConnect_.bind(this); |
38 this.onMessageExternalRef_ = this.onMessageExternal_.bind(this); | |
39 this.onConnectExternalRef_ = this.onConnectExternal_.bind(this); | 38 this.onConnectExternalRef_ = this.onConnectExternal_.bind(this); |
40 }; | 39 }; |
41 | 40 |
42 /** @enum {string} */ | 41 /** @enum {string} */ |
43 remoting.It2MeService.ConnectionTypes = { | 42 remoting.It2MeService.ConnectionTypes = { |
44 HELPER_HANGOUT: 'it2me.helper.hangout', | 43 HELPER_HANGOUT: 'it2me.helper.hangout', |
45 HELPEE_HANGOUT: 'it2me.helpee.hangout', | 44 HELPEE_HANGOUT: 'it2me.helpee.hangout', |
46 HELPER_WEBAPP: 'it2me.helper.webapp' | 45 HELPER_WEBAPP: 'it2me.helper.webapp' |
47 }; | 46 }; |
48 | 47 |
49 /** | 48 /** |
50 * Starts listening to external connection from Hangouts and the webapp. | 49 * Starts listening to external connection from Hangouts and the webapp. |
51 */ | 50 */ |
52 remoting.It2MeService.prototype.init = function() { | 51 remoting.It2MeService.prototype.init = function() { |
53 chrome.runtime.onConnect.addListener(this.onWebappConnectRef_); | 52 chrome.runtime.onConnect.addListener(this.onWebappConnectRef_); |
54 chrome.runtime.onMessageExternal.addListener(this.onMessageExternalRef_); | |
55 chrome.runtime.onConnectExternal.addListener(this.onConnectExternalRef_); | 53 chrome.runtime.onConnectExternal.addListener(this.onConnectExternalRef_); |
56 }; | 54 }; |
57 | 55 |
58 remoting.It2MeService.prototype.dispose = function() { | 56 remoting.It2MeService.prototype.dispose = function() { |
59 chrome.runtime.onConnect.removeListener(this.onWebappConnectRef_); | 57 chrome.runtime.onConnect.removeListener(this.onWebappConnectRef_); |
60 chrome.runtime.onMessageExternal.removeListener( | |
61 this.onMessageExternalRef_); | |
62 chrome.runtime.onConnectExternal.removeListener( | 58 chrome.runtime.onConnectExternal.removeListener( |
63 this.onConnectExternalRef_); | 59 this.onConnectExternalRef_); |
64 }; | 60 }; |
65 | 61 |
66 /** | 62 /** |
67 * This function is called when a runtime message is received from an external | |
68 * web page (hangout) or extension. | |
69 * | |
70 * @param {{method:string, data:Object.<string,*>}} message | |
71 * @param {chrome.runtime.MessageSender} sender | |
72 * @param {function(*):void} sendResponse | |
73 * @private | |
74 */ | |
75 remoting.It2MeService.prototype.onMessageExternal_ = | |
76 function(message, sender, sendResponse) { | |
77 try { | |
78 var method = message.method; | |
79 if (method == 'hello') { | |
80 // The hello message is used by hangouts to detect whether the app is | |
81 // installed and what features are supported. | |
82 sendResponse({ | |
83 method: 'helloResponse', | |
84 supportedFeatures: ['it2me'] | |
85 }); | |
86 return true; | |
87 } | |
88 throw new Error('Unknown method: ' + method); | |
89 } catch (e) { | |
90 var error = /** @type {Error} */ e; | |
91 console.error(error); | |
92 sendResponse({ | |
93 method: message.method + 'Response', | |
94 error: error.message | |
95 }); | |
96 } | |
97 return false; | |
98 }; | |
99 | |
100 /** | |
101 * This function is called when Hangouts connects via chrome.runtime.connect. | 63 * This function is called when Hangouts connects via chrome.runtime.connect. |
102 * Only web pages that are white-listed in the manifest are allowed to connect. | 64 * Only web pages that are white-listed in the manifest are allowed to connect. |
103 * | 65 * |
104 * @param {chrome.runtime.Port} port | 66 * @param {chrome.runtime.Port} port |
105 * @private | 67 * @private |
106 */ | 68 */ |
107 remoting.It2MeService.prototype.onConnectExternal_ = function(port) { | 69 remoting.It2MeService.prototype.onConnectExternal_ = function(port) { |
108 var ConnectionTypes = remoting.It2MeService.ConnectionTypes; | 70 var ConnectionTypes = remoting.It2MeService.ConnectionTypes; |
109 try { | 71 try { |
110 switch (port.name) { | 72 switch (port.name) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 'Cannot start a helper session while a helpee session is in process.'); | 137 'Cannot start a helper session while a helpee session is in process.'); |
176 port.disconnect(); | 138 port.disconnect(); |
177 } | 139 } |
178 | 140 |
179 console.log('Incoming helper connection from Hangouts'); | 141 console.log('Incoming helper connection from Hangouts'); |
180 var helper = new remoting.It2MeHelperChannel( | 142 var helper = new remoting.It2MeHelperChannel( |
181 this.appLauncher_, port, this.onHelperChannelDisconnected.bind(this)); | 143 this.appLauncher_, port, this.onHelperChannelDisconnected.bind(this)); |
182 helper.init(); | 144 helper.init(); |
183 this.helpers_.push(helper); | 145 this.helpers_.push(helper); |
184 }; | 146 }; |
OLD | NEW |