Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / host_list_api_gcd_impl.js
blob2e6e508c9fdfd5be6f681328837bc7924c060beb
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 * REST API for host-list management.
8 */
10 /** @suppress {duplicate} */
11 var remoting = remoting || {};
13 (function() {
15 'use strict';
17 /**
18 * @constructor
19 * @implements {remoting.HostListApi}
21 remoting.HostListApiGcdImpl = function() {
22 this.gcd_ = new remoting.gcd.Client({
23 apiKey: remoting.settings.GOOGLE_API_KEY
24 });
27 /** @override */
28 remoting.HostListApiGcdImpl.prototype.register = function(
29 newHostId, hostName, publicKey, hostClientId) {
30 var self = this;
31 var deviceDraft = {
32 channel: {
33 supportedType: 'xmpp'
35 deviceKind: 'vendor',
36 name: newHostId,
37 displayName: hostName,
38 state: {
39 'publicKey': publicKey
43 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ (
44 this.gcd_.insertRegistrationTicket().
45 then(function(ticket) {
46 return self.gcd_.patchRegistrationTicket(
47 ticket.id, deviceDraft, hostClientId);
48 }).
49 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) {
50 return self.gcd_.finalizeRegistrationTicket(ticket.id);
51 }).
52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) {
53 return {
54 authCode: ticket.robotAccountAuthorizationCode,
55 email: ticket.robotAccountEmail,
56 gcdId: ticket.deviceId
58 }).
59 catch(function(error) {
60 console.error('Error registering device with GCD: ' + error);
61 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED);
62 }));
65 /** @override */
66 remoting.HostListApiGcdImpl.prototype.get = function() {
67 return this.gcd_.listDevices().
68 then(function(devices) {
69 var hosts = [];
70 devices.forEach(function(device) {
71 try {
72 hosts.push(deviceToHost(device));
73 } catch (/** @type {*} */ error) {
74 console.warn('Invalid device spec:', error);
76 });
77 return hosts;
78 });
81 /** @override */
82 remoting.HostListApiGcdImpl.prototype.put =
83 function(hostId, hostName, hostPublicKey) {
84 // TODO(jrw)
85 throw new Error('Not implemented');
88 /** @override */
89 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) {
90 var that = this;
91 return this.gcd_.listDevices(hostId).then(function(devices) {
92 var gcdId = null;
93 for (var i = 0; i < devices.length; i++) {
94 var device = devices[i];
95 // The "name" field in GCD holds what Chromoting considers to be
96 // the host ID.
97 if (device.name == hostId) {
98 gcdId = device.id;
101 if (gcdId == null) {
102 return false;
103 } else {
104 return that.gcd_.deleteDevice(gcdId);
109 /** @override */
110 remoting.HostListApiGcdImpl.prototype.getSupportHost = function(supportId) {
111 console.error('getSupportHost not supported by HostListApiGclImpl');
112 return Promise.reject(remoting.Error.unexpected());
116 * Converts a GCD device description to a Host object.
117 * @param {!Object} device
118 * @return {!remoting.Host}
120 function deviceToHost(device) {
121 var statusMap = {
122 'online': 'ONLINE',
123 'offline': 'OFFLINE'
125 var hostId = base.getStringAttr(device, 'name');
126 var host = new remoting.Host(hostId);
127 host.hostName = base.getStringAttr(device, 'displayName');
128 host.status = base.getStringAttr(
129 statusMap, base.getStringAttr(device, 'connectionStatus'));
130 var state = base.getObjectAttr(device, 'state', {});
131 host.publicKey = base.getStringAttr(state, 'publicKey');
132 host.jabberId = base.getStringAttr(state, 'jabberId', '');
133 host.hostVersion = base.getStringAttr(state, 'hostVersion', '');
134 var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0);
135 if (creationTimeMs) {
136 host.createdTime = new Date(creationTimeMs).toISOString();
138 var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0);
139 if (lastUpdateTimeMs) {
140 host.updatedTime = new Date(lastUpdateTimeMs).toISOString();
142 return host;
145 })();