Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_host_daemon_facade.js
blobf404df60305e22f98cc74357df2e0fab1b3ec923
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 /**
6  * @fileoverview A mock version of HostDaemonFacade.  Internally all
7  * delays are implemented with promises, so SpyPromise can be used to
8  * wait out delays.
9  *
10  * By default, every method fails.  Methods can be individually set to
11  * pass specific values to their onDone arguments by setting member
12  * variables of the mock object.
13  *
14  * When methods fail, they set the detail field of the remoting.Error
15  * object to the name of the method that failed.
16  */
18 /** @suppress {duplicate} */
19 var remoting = remoting || {};
21 (function() {
23 'use strict';
25 /**
26  * By default, all methods fail.
27  * @constructor
28  */
29 remoting.MockHostDaemonFacade = function() {
30   /** @type {Array<remoting.HostController.Feature>} */
31   this.features = [];
33   /** @type {?string} */
34   this.hostName = null;
36   /**
37    * A function to generate a fake PIN hash given a host ID and a PIN.
38    * @type {?function(string,string):string}
39    */
40   this.pinHashFunc = null;
42   /** @type {?string} */
43   this.privateKey = null;
45   /** @type {?string} */
46   this.publicKey = null;
48   /** @type {Object} */
49   this.daemonConfig = null;
51   /** @type {?string} */
52   this.daemonVersion = null;
54   /** @type {?boolean} */
55   this.consentSupported = null;
57   /** @type {?boolean} */
58   this.consentAllowed = null;
60   /** @type {?boolean} */
61   this.consentSetByPolicy = null;
63   /** @type {?remoting.HostController.AsyncResult} */
64   this.startDaemonResult = null;
66   /** @type {?remoting.HostController.AsyncResult} */
67   this.stopDaemonResult = null;
69   /** @type {?remoting.HostController.State} */
70   this.daemonState = null;
72   /** @type {?remoting.HostController.AsyncResult} */
73   this.updateDaemonConfigResult = null;
75   /** @type {Array<remoting.PairedClient>} */
76   this.pairedClients = null;
78   /** @type {?string} */
79   this.hostClientId = null;
81   /** @type {?string} */
82   this.userEmail = null;
84   /** @type {?string} */
85   this.refreshToken = null;
88 /**
89  * @param {remoting.HostController.Feature} feature
90  * @return {!Promise<boolean>}
91  */
92 remoting.MockHostDaemonFacade.prototype.hasFeature = function(feature) {
93   var that = this;
94   return Promise.resolve().then(function() {
95     return that.features.indexOf(feature) >= 0;
96   });
99 /**
100  * @return {!Promise<string>}
101  */
102 remoting.MockHostDaemonFacade.prototype.getHostName = function() {
103   var that = this;
104   return Promise.resolve().then(function() {
105     if (that.hostName === null) {
106       throw remoting.Error.unexpected('getHostName');
107     } else {
108       return that.hostName;
109     }
110   });
114  * @param {string} hostId
115  * @param {string} pin
116  * @return {!Promise<string>}
117  */
118 remoting.MockHostDaemonFacade.prototype.getPinHash = function(hostId, pin) {
119   var that = this;
120   return Promise.resolve().then(function() {
121     if (that.pinHashFunc === null) {
122       throw remoting.Error.unexpected('getPinHash');
123     } else {
124       return that.pinHashFunc(hostId, pin);
125     }
126   });
130  * @return {!Promise<{privateKey:string, publicKey:string}>}
131  */
132 remoting.MockHostDaemonFacade.prototype.generateKeyPair = function() {
133   var that = this;
134   return Promise.resolve().then(function() {
135     if (that.privateKey === null || that.publicKey === null) {
136       throw remoting.Error.unexpected('generateKeyPair');
137     } else {
138       return {
139         privateKey: that.privateKey,
140         publicKey: that.publicKey
141       };
142     }
143   });
147  * @param {Object} config The new config parameters.
148  * @return {!Promise<remoting.HostController.AsyncResult>}
149  */
150 remoting.MockHostDaemonFacade.prototype.updateDaemonConfig = function(config) {
151   var that = this;
152   return Promise.resolve().then(function() {
153     if (that.daemonConfig === null ||
154         that.updateDaemonConfigResult === null ||
155         'host_id' in config ||
156         'xmpp_login' in config) {
157       throw remoting.Error.unexpected('updateDaemonConfig');
158     } else if (that.updateDaemonConfigResult !=
159                remoting.HostController.AsyncResult.OK) {
160       return that.updateDaemonConfigResult;
161     } else {
162       base.mix(that.daemonConfig, config);
163       return remoting.HostController.AsyncResult.OK;
164     }
165   });
169  * @return {!Promise<Object>}
170  */
171 remoting.MockHostDaemonFacade.prototype.getDaemonConfig = function() {
172   var that = this;
173   return Promise.resolve().then(function() {
174     if (that.daemonConfig === null) {
175       throw remoting.Error.unexpected('getDaemonConfig');
176     } else {
177       return that.daemonConfig;
178     }
179   });
183  * @return {!Promise<string>}
184  */
185 remoting.MockHostDaemonFacade.prototype.getDaemonVersion = function() {
186   var that = this;
187   return Promise.resolve().then(function() {
188     if (that.daemonVersion === null) {
189       throw remoting.Error.unexpected('getDaemonVersion');
190     } else {
191       return that.daemonVersion;
192     }
193   });
197  * @return {!Promise<remoting.UsageStatsConsent>}
198  */
199 remoting.MockHostDaemonFacade.prototype.getUsageStatsConsent = function() {
200   var that = this;
201   return Promise.resolve().then(function() {
202     if (that.consentSupported === null ||
203         that.consentAllowed === null ||
204         that.consentSetByPolicy === null) {
205       throw remoting.Error.unexpected('getUsageStatsConsent');
206     } else {
207       return {
208         supported: that.consentSupported,
209         allowed: that.consentAllowed,
210         setByPolicy: that.consentSetByPolicy
211       };
212     }
213   });
217  * @param {Object} config
218  * @param {boolean} consent Consent to report crash dumps.
219  * @return {!Promise<remoting.HostController.AsyncResult>}
220  */
221 remoting.MockHostDaemonFacade.prototype.startDaemon =
222     function(config, consent) {
223   var that = this;
224   return Promise.resolve().then(function() {
225     if (that.startDaemonResult === null) {
226       throw remoting.Error.unexpected('startDaemon');
227     } else {
228       return that.startDaemonResult;
229     }
230   });
234  * @return {!Promise<remoting.HostController.AsyncResult>}
235  */
236 remoting.MockHostDaemonFacade.prototype.stopDaemon =
237     function() {
238   var that = this;
239   return Promise.resolve().then(function() {
240     if (that.stopDaemonResult === null) {
241       throw remoting.Error.unexpected('stopDaemon');
242     } else {
243       return that.stopDaemonResult;
244     }
245   });
249  * @return {!Promise<remoting.HostController.State>}
250  */
251 remoting.MockHostDaemonFacade.prototype.getDaemonState =
252     function() {
253   var that = this;
254   return Promise.resolve().then(function() {
255     if (that.daemonState === null) {
256       throw remoting.Error.unexpected('getDaemonState');
257     } else {
258       return that.daemonState;
259     }
260   });
264  * @return {!Promise<Array<remoting.PairedClient>>}
265  */
266 remoting.MockHostDaemonFacade.prototype.getPairedClients =
267     function() {
268   var that = this;
269   return Promise.resolve().then(function() {
270     if (that.pairedClients === null) {
271       throw remoting.Error.unexpected('getPairedClients');
272     } else {
273       return that.pairedClients;
274     }
275   });
279  * @return {!Promise<boolean>}
280  */
281 remoting.MockHostDaemonFacade.prototype.clearPairedClients = function() {
282   var that = this;
283   return Promise.resolve().then(function() {
284     if (that.pairedClients === null) {
285       throw remoting.Error.unexpected('clearPairedClients');
286     } else {
287       that.pairedClients = [];
288       return true;  // TODO(jrw): Not always correct.
289     }
290   });
294  * @param {string} client
295  * @return {!Promise<boolean>}
296  */
297 remoting.MockHostDaemonFacade.prototype.deletePairedClient = function(client) {
298   var that = this;
299   return Promise.resolve().then(function() {
300     if (that.pairedClients === null) {
301       throw remoting.Error.unexpected('deletePairedClient');
302     } else {
303       that.pairedClients = that.pairedClients.filter(function(c) {
304         return c['clientId'] != client;
305       });
306       return true;  // TODO(jrw):  Not always correct.
307     }
308   });
312  * @return {!Promise<string>}
313  */
314 remoting.MockHostDaemonFacade.prototype.getHostClientId = function() {
315   var that = this;
316   return Promise.resolve().then(function() {
317     if (that.hostClientId === null) {
318       throw remoting.Error.unexpected('getHostClientId');
319     } else {
320       return that.hostClientId;
321     }
322   });
326  * @param {string} authorizationCode
327  * @return {!Promise<{userEmail:string, refreshToken:string}>}
328  */
329 remoting.MockHostDaemonFacade.prototype.getCredentialsFromAuthCode =
330     function(authorizationCode) {
331   var that = this;
332   return Promise.resolve().then(function() {
333     if (that.userEmail === null || that.refreshToken === null) {
334       throw remoting.Error.unexpected('getCredentialsFromAuthCode');
335     } else {
336       return {
337         userEmail: that.userEmail,
338         refreshToken: that.refreshToken
339       };
340     }
341   });
344 })();