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 * REST API for host-list management. | 7 * REST API for host-list management. |
8 */ | 8 */ |
9 | 9 |
10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 }). | 51 }). |
52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { | 52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { |
53 return ticket.robotAccountAuthorizationCode; | 53 return ticket.robotAccountAuthorizationCode; |
54 }). | 54 }). |
55 catch(function(error) { | 55 catch(function(error) { |
56 console.error('Error registering device with GCD: ' + error); | 56 console.error('Error registering device with GCD: ' + error); |
57 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | 57 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); |
58 })); | 58 })); |
59 }; | 59 }; |
60 | 60 |
61 /** @override */ | 61 /** @override */ |
Jamie
2015/04/27 22:44:06
@return {Promise<Array<remoting.Host>>}?
John Williams
2015/04/27 23:05:42
I generally don't like to repeat the type declarat
| |
62 remoting.HostListApiGcdImpl.prototype.get = function() { | 62 remoting.HostListApiGcdImpl.prototype.get = function() { |
63 // TODO(jrw) | 63 return this.gcd_.listDevices(). |
64 this.gcd_.listDevices(); | 64 then(function(devices) { |
65 return Promise.resolve([]); | 65 var hosts = []; |
66 devices.forEach(function(device) { | |
67 try { | |
68 hosts.push(remoting.HostListApiGcdImpl.deviceToHost(device)); | |
69 } catch (/** @type {*} */ error) { | |
70 console.warn('Invalid device spec:', error); | |
71 } | |
72 }); | |
73 return hosts; | |
74 }); | |
66 }; | 75 }; |
67 | 76 |
68 /** @override */ | 77 /** @override */ |
69 remoting.HostListApiGcdImpl.prototype.put = | 78 remoting.HostListApiGcdImpl.prototype.put = |
70 function(hostId, hostName, hostPublicKey) { | 79 function(hostId, hostName, hostPublicKey) { |
71 // TODO(jrw) | 80 // TODO(jrw) |
72 throw new Error('Not implemented'); | 81 throw new Error('Not implemented'); |
73 }; | 82 }; |
74 | 83 |
75 /** @override */ | 84 /** @override */ |
76 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) { | 85 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) { |
77 // TODO(jrw) | 86 var that = this; |
78 throw new Error('Not implemented'); | 87 return this.gcd_.listDevices(hostId).then(function(devices) { |
Jamie
2015/04/27 22:44:06
You're passing a hostId to a function expecting a
John Williams
2015/04/27 23:05:42
It's imposed by GCD. We could use GCD's own "id"
| |
88 var gcdId = null; | |
89 for (var i = 0; i < devices.length; i++) { | |
90 var device = devices[i]; | |
91 if (device.name == hostId) { | |
92 gcdId = device.id; | |
93 } | |
94 } | |
95 if (gcdId == null) { | |
96 return false; | |
97 } else { | |
98 return that.gcd_.deleteDevice(gcdId); | |
99 } | |
100 }); | |
101 }; | |
102 | |
103 /** | |
104 * @param {!Object} device | |
105 * @return {!remoting.Host} | |
106 */ | |
107 remoting.HostListApiGcdImpl.deviceToHost = function(device) { | |
108 var statusMap = { | |
109 'online': 'ONLINE', | |
110 'offline': 'OFFLINE' | |
111 }; | |
112 var hostId = base.getStringAttr(device, 'name'); | |
113 var host = new remoting.Host(hostId); | |
114 host.hostName = base.getStringAttr(device, 'displayName'); | |
115 host.status = base.getStringAttr( | |
116 statusMap, base.getStringAttr(device, 'connectionStatus')); | |
117 var state = base.getObjectAttr(device, 'state', {}); | |
118 host.publicKey = base.getStringAttr(state, 'publicKey'); | |
119 host.jabberId = base.getStringAttr(state, 'jabberId', ''); | |
120 host.hostVersion = base.getStringAttr(state, 'hostVersion', ''); | |
121 var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0); | |
122 if (creationTimeMs) { | |
123 host.createdTime = new Date(creationTimeMs).toISOString(); | |
124 } | |
125 var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0); | |
126 if (lastUpdateTimeMs) { | |
127 host.updatedTime = new Date(lastUpdateTimeMs).toISOString(); | |
128 } | |
129 return host; | |
79 }; | 130 }; |
80 | 131 |
81 })(); | 132 })(); |
OLD | NEW |