Fix the //sandbox/mac build when using a modern (10.7+) SDK.
[chromium-blink-merge.git] / remoting / webapp / it2me_host_facade.js
blobe83e22560e73e44df86205ebe9a6705f419fdf40
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
3 // found in the LICENSE file.
5 /**
6  * @fileoverview
7  * Class to communicate with the It2me Host component via Native Messaging.
8  */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16  * @constructor
17  */
18 remoting.It2MeHostFacade = function() {
19   /**
20    * @type {number}
21    * @private
22    */
23   this.nextId_ = 0;
25   /**
26    * @type {?chrome.extension.Port}
27    * @private
28    */
29   this.port_ = null;
31   /**
32    * @type {string}
33    * @private
34    */
35   this.accessCode_ = '';
37   /**
38    * @type {number}
39    * @private
40    */
41   this.accessCodeLifetime_ = 0;
43   /**
44    * @type {string}
45    * @private
46    */
47   this.clientId_ = '';
49   /**
50    * @type {boolean}
51    * @private
52    */
53   this.initialized_ = false;
55   /**
56    * @type {function():void}
57    * @private
58    */
59   this.onInitialized_ = function() {};
61   /**
62    * Called if Native Messaging host has failed to start.
63    * @private
64    * */
65   this.onInitializationFailed_ = function() {};
67   /**
68    * Called if the It2Me Native Messaging host sends a malformed message:
69    * missing required attributes, attributes with incorrect types, etc.
70    * @param {remoting.Error} error
71    * @private
72    */
73   this.onError_ = function(error) {};
75   /**
76    * @type {function(remoting.HostSession.State):void}
77    * @private
78    */
79   this.onStateChanged_ = function() {};
81   /**
82    * @type {function(boolean):void}
83    * @private
84    */
85   this.onNatPolicyChanged_ = function() {};
88 /**
89  * Sets up connection to the Native Messaging host process and exchanges
90  * 'hello' messages. If Native Messaging is not supported or if the it2me
91  * native messaging host is not installed, onInitializationFailed is invoked.
92  * Otherwise, onInitialized is invoked.
93  *
94  * @param {function():void} onInitialized Called after successful
95  *     initialization.
96  * @param {function():void} onInitializationFailed Called if cannot connect to
97  *     the native messaging host.
98  * @return {void}
99  */
100 remoting.It2MeHostFacade.prototype.initialize =
101     function(onInitialized, onInitializationFailed) {
102   this.onInitialized_ = onInitialized;
103   this.onInitializationFailed_ = onInitializationFailed;
105   try {
106     this.port_ = chrome.runtime.connectNative(
107         'com.google.chrome.remote_assistance');
108     this.port_.onMessage.addListener(this.onIncomingMessage_.bind(this));
109     this.port_.onDisconnect.addListener(this.onHostDisconnect_.bind(this));
110     this.port_.postMessage({type: 'hello'});
111   } catch (err) {
112     console.log('Native Messaging initialization failed: ',
113                 /** @type {*} */ (err));
114     onInitializationFailed();
115     return;
116   }
120  * @param {string} email The user's email address.
121  * @param {string} authServiceWithToken Concatenation of the auth service
122  *     (e.g. oauth2) and the access token.
123  * @param {function(remoting.HostSession.State):void} onStateChanged Callback to
124  *     invoke when the host state changes.
125  * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when
126  *     the nat traversal policy changes.
127  * @param {function(string):void} logDebugInfo Callback allowing the plugin
128  *     to log messages to the debug log.
129  * @param {string} xmppServerAddress XMPP server host name (or IP address) and
130  *     port.
131  * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
132  *     XMPP server
133  * @param {string} directoryBotJid XMPP JID for the remoting directory server
134  *     bot.
135  * @param {function(remoting.Error):void} onError Callback to invoke in case of
136  *     an error.
137  * @return {void}
138  */
139 remoting.It2MeHostFacade.prototype.connect =
140     function(email, authServiceWithToken, onStateChanged, onNatPolicyChanged,
141              logDebugInfo, xmppServerAddress, xmppServerUseTls, directoryBotJid,
142              onError) {
143   if (!this.port_) {
144     console.error(
145         'remoting.It2MeHostFacade.connect() without initialization.');
146     onError(remoting.Error.UNEXPECTED);
147     return;
148   }
150   this.onStateChanged_ = onStateChanged;
151   this.onNatPolicyChanged_ = onNatPolicyChanged;
152   this.onErrorHandler_ = onError;
153   this.port_.postMessage({
154     type: 'connect',
155     userName: email,
156     authServiceWithToken: authServiceWithToken,
157     xmppServerAddress: xmppServerAddress,
158     xmppServerUseTls: xmppServerUseTls,
159     directoryBotJid: directoryBotJid
160   });
164  * @return {void}
165  */
166 remoting.It2MeHostFacade.prototype.disconnect = function() {
167   if (this.port_)
168     this.port_.postMessage({type: 'disconnect'});
172  * @return {string}
173  */
174 remoting.It2MeHostFacade.prototype.getAccessCode = function() {
175   return this.accessCode_
179  * @return {number}
180  */
181 remoting.It2MeHostFacade.prototype.getAccessCodeLifetime = function() {
182   return this.accessCodeLifetime_
186  * @return {string}
187  */
188 remoting.It2MeHostFacade.prototype.getClient = function() {
189   return this.clientId_;
193  * Handler for incoming messages.
195  * @param {Object} message The received message.
196  * @return {void}
197  * @private
198  */
199 remoting.It2MeHostFacade.prototype.onIncomingMessage_ =
200     function(message) {
201   var type = getStringAttr(message, 'type');
203   switch (type) {
204     case 'helloResponse':
205       var version = getStringAttr(message, 'version');
206       console.log('Host version: ', version);
207       this.initialized_ = true;
208       // A "hello" request is sent immediately after the native messaging host
209       // is started. We can proceed to the next task once we receive the
210       // "helloReponse".
211       this.onInitialized_();
212       break;
214     case 'connectResponse':
215       console.log('connectResponse received');
216       // Response to the "connect" request. No action is needed until we
217       // receive the corresponding "hostStateChanged" message.
218       break;
220     case 'disconnectResponse':
221       console.log('disconnectResponse received');
222       // Response to the "disconnect" request. No action is needed until we
223       // receive the corresponding "hostStateChanged" message.
224       break;
226     case 'hostStateChanged':
227       var stateString = getStringAttr(message, 'state');
228       console.log('hostStateChanged received: ', stateString);
229       var state = remoting.HostSession.State.fromString(stateString);
231       switch (state) {
232         case remoting.HostSession.State.RECEIVED_ACCESS_CODE:
233           var accessCode = getStringAttr(message, 'accessCode');
234           var accessCodeLifetime = getNumberAttr(message, 'accessCodeLifetime');
235           this.onReceivedAccessCode_(accessCode, accessCodeLifetime);
236           break;
238         case remoting.HostSession.State.CONNECTED:
239           var client = getStringAttr(message, 'client');
240           this.onConnected_(client);
241           break;
242       }
243       this.onStateChanged_(state);
244       break;
246     case 'natPolicyChanged':
247       var natTraversalEnabled = getBooleanAttr(message, 'natTraversalEnabled');
248       this.onNatPolicyChanged_(natTraversalEnabled);
249       break;
251     case 'error':
252       console.error(getStringAttr(message, 'description'));
253       this.onError_(remoting.Error.UNEXPECTED);
254       break;
256     default:
257       throw 'Unexpected native message: ' + message;
258   }
262  * @param {string} accessCode
263  * @param {number} accessCodeLifetime
264  * @return {void}
265  * @private
266  */
267 remoting.It2MeHostFacade.prototype.onReceivedAccessCode_ =
268     function(accessCode, accessCodeLifetime) {
269   this.accessCode_ = accessCode;
270   this.accessCodeLifetime_ = accessCodeLifetime;
274  * @param {string} clientId
275  * @return {void}
276  * @private
277  */
278 remoting.It2MeHostFacade.prototype.onConnected_ = function(clientId) {
279   this.clientId_ = clientId;
283  * @return {void}
284  * @private
285  */
286 remoting.It2MeHostFacade.prototype.onHostDisconnect_ = function() {
287   if (!this.initialized_) {
288     // If the host is disconnected before it is initialized, it probably means
289     // the host is not propertly installed (or not installed at all).
290     // E.g., if the host manifest is not present we get "Specified native
291     // messaging host not found" error. If the host manifest is present but
292     // the host binary cannot be found we get the "Native host has exited"
293     // error.
294     console.log('Native Messaging initialization failed: ' +
295                 chrome.runtime.lastError.message);
296     this.onInitializationFailed_();
297   } else {
298     console.error('Native Messaging port disconnected.');
299     this.port_ = null;
300     this.onError_(remoting.Error.UNEXPECTED);
301   }