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.
7 * Mock implementation of remoting.HostList
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
17 * @implements {remoting.HostListApi}
19 remoting.MockHostListApi = function() {
21 * The auth code value for the |register| method to return, or null
25 this.authCodeFromRegister = null;
28 * The email value for the |register| method to return, or null if
32 this.emailFromRegister = null;
34 /** @type {Array<remoting.Host>} */
37 'hostName': 'Online host',
38 'hostId': 'online-host-id',
40 'jabberId': 'online-jid',
41 'publicKey': 'online-public-key',
42 'tokenUrlPatterns': [],
43 'updatedTime': new Date().toISOString()
46 'hostName': 'Offline host',
47 'hostId': 'offline-host-id',
49 'jabberId': 'offline-jid',
50 'publicKey': 'offline-public-key',
51 'tokenUrlPatterns': [],
52 'updatedTime': new Date(1970, 1, 1).toISOString()
58 remoting.MockHostListApi.prototype.register = function(
59 newHostId, hostName, publicKey, hostClientId) {
60 if (this.authCodeFromRegister === null || this.emailFromRegister === null) {
61 return Promise.reject(
63 remoting.Error.Tag.REGISTRATION_FAILED,
64 'MockHostListApi.register'));
66 return Promise.resolve({
67 authCode: this.authCodeFromRegister,
68 email: this.emailFromRegister
74 remoting.MockHostListApi.prototype.get = function() {
76 return new Promise(function(resolve, reject) {
77 remoting.mockIdentity.validateTokenAndCall(
78 resolve, remoting.Error.handler(reject), [that.hosts]);
84 * @param {string} hostId
85 * @param {string} hostName
86 * @param {string} hostPublicKey
88 remoting.MockHostListApi.prototype.put =
89 function(hostId, hostName, hostPublicKey) {
90 /** @type {remoting.MockHostListApi} */
92 return new Promise(function(resolve, reject) {
93 var onTokenValid = function() {
94 for (var i = 0; i < that.hosts.length; ++i) {
95 /** type {remoting.Host} */
96 var host = that.hosts[i];
97 if (host.hostId == hostId) {
98 host.hostName = hostName;
99 host.hostPublicKey = hostPublicKey;
104 console.error('PUT request for unknown host: ' + hostId +
105 ' (' + hostName + ')');
106 reject(remoting.Error.unexpected());
108 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
114 * @param {string} hostId
116 remoting.MockHostListApi.prototype.remove = function(hostId) {
117 /** @type {remoting.MockHostListApi} */
119 return new Promise(function(resolve, reject) {
120 var onTokenValid = function() {
121 for (var i = 0; i < that.hosts.length; ++i) {
122 var host = that.hosts[i];
123 if (host.hostId == hostId) {
124 that.hosts.splice(i, 1);
129 console.error('DELETE request for unknown host: ' + hostId);
130 reject(remoting.Error.unexpected());
132 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
137 remoting.MockHostListApi.prototype.getSupportHost = function(supportId) {
139 return new Promise(function(resolve, reject) {
140 remoting.mockIdentity.validateTokenAndCall(
141 resolve, remoting.Error.handler(reject), [that.hosts[0]]);
147 * @param {boolean} active
149 remoting.MockHostListApi.setActive = function(active) {
150 remoting.HostListApi.setInstance(
151 active ? new remoting.MockHostListApi() : null);