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 * Mock implementation of remoting.HostList | 7 * Mock implementation of remoting.HostList |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 'updatedTime': new Date(1970, 1, 1).toISOString() | 45 'updatedTime': new Date(1970, 1, 1).toISOString() |
46 } | 46 } |
47 ]; | 47 ]; |
48 }; | 48 }; |
49 | 49 |
50 /** @override */ | 50 /** @override */ |
51 remoting.MockHostListApi.prototype.register = function( | 51 remoting.MockHostListApi.prototype.register = function( |
52 newHostId, hostName, publicKey, hostClientId) { | 52 newHostId, hostName, publicKey, hostClientId) { |
53 if (this.registerResult === null) { | 53 if (this.registerResult === null) { |
54 return Promise.reject( | 54 return Promise.reject( |
55 new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED)); | 55 new remoting.Error( |
| 56 remoting.Error.Tag.REGISTRATION_FAILED, |
| 57 'MockHostListApi.register')); |
56 } else { | 58 } else { |
57 return Promise.resolve(this.registerResult); | 59 return Promise.resolve(this.registerResult); |
58 } | 60 } |
59 }; | 61 }; |
60 | 62 |
61 /** @override */ | 63 /** @override */ |
62 remoting.MockHostListApi.prototype.get = function() { | 64 remoting.MockHostListApi.prototype.get = function() { |
63 var that = this; | 65 var that = this; |
64 new Promise(function(resolve, reject) { | 66 return new Promise(function(resolve, reject) { |
65 remoting.mockIdentity.validateTokenAndCall( | 67 remoting.mockIdentity.validateTokenAndCall( |
66 resolve, remoting.Error.handler(reject), [that.hosts]); | 68 resolve, remoting.Error.handler(reject), [that.hosts]); |
67 }); | 69 }); |
68 }; | 70 }; |
69 | 71 |
70 /** @override */ | 72 /** |
| 73 * @override |
| 74 * @param {string} hostId |
| 75 * @param {string} hostName |
| 76 * @param {string} hostPublicKey |
| 77 */ |
71 remoting.MockHostListApi.prototype.put = | 78 remoting.MockHostListApi.prototype.put = |
72 function(hostId, hostName, hostPublicKey) { | 79 function(hostId, hostName, hostPublicKey) { |
73 /** @type {remoting.MockHostListApi} */ | 80 /** @type {remoting.MockHostListApi} */ |
74 var that = this; | 81 var that = this; |
75 return new Promise(function(resolve, reject) { | 82 return new Promise(function(resolve, reject) { |
76 var onTokenValid = function() { | 83 var onTokenValid = function() { |
77 for (var i = 0; i < that.hosts.length; ++i) { | 84 for (var i = 0; i < that.hosts.length; ++i) { |
78 var host = that.hosts[i]; | 85 var host = that.hosts[i]; |
79 if (host.hostId == hostId) { | 86 if (host.hostId == hostId) { |
80 host.hostName = hostName; | 87 host.hostName = hostName; |
81 host.hostPublicKey = hostPublicKey; | 88 host.hostPublicKey = hostPublicKey; |
82 resolve(undefined); | 89 resolve(undefined); |
83 return; | 90 return; |
84 } | 91 } |
85 } | 92 } |
86 console.error('PUT request for unknown host: ' + hostId + | 93 console.error('PUT request for unknown host: ' + hostId + |
87 ' (' + hostName + ')'); | 94 ' (' + hostName + ')'); |
88 reject(remoting.Error.unexpected()); | 95 reject(remoting.Error.unexpected()); |
89 }; | 96 }; |
90 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); | 97 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); |
91 }); | 98 }); |
92 }; | 99 }; |
93 | 100 |
94 /** @override */ | 101 /** |
| 102 * @override |
| 103 * @param {string} hostId |
| 104 */ |
95 remoting.MockHostListApi.prototype.remove = function(hostId) { | 105 remoting.MockHostListApi.prototype.remove = function(hostId) { |
96 /** @type {remoting.MockHostListApi} */ | 106 /** @type {remoting.MockHostListApi} */ |
97 var that = this; | 107 var that = this; |
98 return new Promise(function(resolve, reject) { | 108 return new Promise(function(resolve, reject) { |
99 var onTokenValid = function() { | 109 var onTokenValid = function() { |
100 for (var i = 0; i < that.hosts.length; ++i) { | 110 for (var i = 0; i < that.hosts.length; ++i) { |
101 var host = that.hosts[i]; | 111 var host = that.hosts[i]; |
102 if (host.hostId == hostId) { | 112 if (host.hostId == hostId) { |
103 that.hosts.splice(i, 1); | 113 that.hosts.splice(i, 1); |
104 resolve(undefined); | 114 resolve(undefined); |
105 return; | 115 return; |
106 } | 116 } |
107 } | 117 } |
108 console.error('DELETE request for unknown host: ' + hostId); | 118 console.error('DELETE request for unknown host: ' + hostId); |
109 reject(remoting.Error.unexpected()); | 119 reject(remoting.Error.unexpected()); |
110 }; | 120 }; |
111 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); | 121 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); |
112 }); | 122 }); |
113 }; | 123 }; |
114 | 124 |
115 /** | 125 /** |
116 * @param {boolean} active | 126 * @param {boolean} active |
117 */ | 127 */ |
118 remoting.MockHostListApi.setActive = function(active) { | 128 remoting.MockHostListApi.setActive = function(active) { |
119 remoting.hostListApi = active ? new remoting.MockHostListApi() | 129 remoting.HostListApi.setInstance( |
120 : new remoting.HostListApiImpl(); | 130 active ? new remoting.MockHostListApi() : null); |
121 }; | 131 }; |
OLD | NEW |