Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / gcd_client.js
blob35076cd3df67f99947d4decdc02b7089656e1b72
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
7 * Client for the GCD REST API.
8 * TODO: Add link to GCD docs.
9 */
11 /** @suppress {duplicate} */
12 var remoting = remoting || {};
14 /**
15 * Namespace for GCD definitions
16 * @type {Object}
18 remoting.gcd = remoting.gcd || {};
20 /**
21 * @typedef {{
22 * id: string,
23 * robotAccountEmail: string,
24 * robotAccountAuthorizationCode: string,
25 * deviceId: string,
26 * deviceDraft: Object
27 * }}
29 remoting.gcd.RegistrationTicket;
31 /**
32 * TODO: Flesh out with typical fields.
33 * @typedef {{
34 * id:string,
35 * name:string
36 * }}
38 remoting.gcd.Device;
40 /**
41 * @typedef {!Object}
43 remoting.gcd.DevicePatch;
45 /**
46 * @typedef {{
47 * devices: (Array<remoting.gcd.Device>|undefined)
48 * }}
50 remoting.gcd.DeviceListResponse;
52 (function() {
53 'use strict';
55 /**
56 * Interprets an HTTP response as a JSON object with a specific value
57 * in the 'kind' field.
58 * @param {remoting.Xhr.Response} response
59 * @param {string} expectedKind
60 * @return {!Object}
61 * @throws {remoting.Error}
63 var responseAsObject = function(response, expectedKind) {
64 if (typeof response.getJson() != 'object') {
65 console.error(
66 'invalid response; expected object, got:', response.getJson());
67 throw remoting.Error.unexpected();
69 var obj = base.assertObject(response.getJson());
70 var kind = base.getStringAttr(obj, 'kind');
71 if (kind != expectedKind) {
72 console.error(
73 'invalid resonse kind; expected ' + expectedKind + ', got ' + kind);
74 throw remoting.Error.unexpected();
76 return obj;
79 /**
80 * Interprets an HTTP response as containing a GCD registration ticket.
81 * @param {remoting.Xhr.Response} response
82 * @return {!remoting.gcd.RegistrationTicket}
83 * @throws {remoting.Error}
85 var responseAsGcdRegistrationTicket = function(response) {
86 return /** @type {!remoting.gcd.RegistrationTicket} */ (
87 responseAsObject(
88 response, 'clouddevices#registrationTicket'));
91 /**
92 * Interprets an HTTP response as containing a GCD device defintion.
93 * @param {remoting.Xhr.Response} response
94 * @return {!remoting.gcd.Device}
95 * @throws {remoting.Error}
97 var responseAsGcdDevice = function(response) {
98 return /** @type {!remoting.gcd.Device} */ (
99 responseAsObject(response, 'clouddevices#device'));
103 * Interprets an HTTP response as containing a GCD device list.
104 * @param {remoting.Xhr.Response} response
105 * @return {!remoting.gcd.DeviceListResponse}
106 * @throws {remoting.Error}
108 var responseAsGcdDeviceListResponse = function(response) {
109 return /** @type {!remoting.gcd.DeviceListResponse} */ (
110 responseAsObject(response, 'clouddevices#devicesListResponse'));
114 * Creates a new client using a specific API key, and optionall a
115 * specific base URL, and OAuth2 client ID.
116 * @param {{
117 * apiKey: string,
118 * apiBaseUrl: (string|undefined)
119 * }} options
120 * @constructor
122 remoting.gcd.Client = function(options) {
123 /** @const */
124 this.apiKey_ = options.apiKey;
125 /** @const */
126 this.apiBaseUrl_ = options.apiBaseUrl ||
127 'https://www.googleapis.com/clouddevices/v1';
131 * Creates a new registration ticket.
132 * TODO: Add link to GCD docs.
133 * @return {!Promise<remoting.gcd.RegistrationTicket>}
135 remoting.gcd.Client.prototype.insertRegistrationTicket = function() {
136 return new remoting.Xhr({
137 method: 'POST',
138 url: this.apiBaseUrl_ + '/registrationTickets',
139 jsonContent: { 'userEmail': 'me' },
140 useIdentity: true,
141 acceptJson: true
142 }).start().then(function(/** remoting.Xhr.Response */ response) {
143 if (response.isError()) {
144 console.error('error creating registration ticket');
145 throw remoting.Error.unexpected();
147 return responseAsGcdRegistrationTicket(response);
152 * Updates an existing registration ticket using patch semantics.
153 * TODO: Add link to GCD docs.
154 * @param {string} ticketId
155 * @param {!Object<string,*>} deviceDraft
156 * @param {string} oauthClientId
157 * @return {!Promise<remoting.gcd.RegistrationTicket>}
159 remoting.gcd.Client.prototype.patchRegistrationTicket = function(
160 ticketId, deviceDraft, oauthClientId) {
161 return new remoting.Xhr({
162 method: 'PATCH',
163 url: this.apiBaseUrl_ + '/registrationTickets/' +
164 encodeURIComponent(ticketId),
165 urlParams: {
166 'key': this.apiKey_
168 jsonContent: {
169 'deviceDraft': deviceDraft,
170 'oauthClientId': oauthClientId
172 acceptJson: true
173 }).start().then(function(response) {
174 if (response.isError()) {
175 console.error('error patching registration ticket');
176 throw remoting.Error.unexpected();
178 return responseAsGcdRegistrationTicket(response);
183 * Finalizes device registration and returns its credentials.
184 * TODO: Add link to GCD docs.
185 * @param {string} ticketId
186 * @return {!Promise<remoting.gcd.RegistrationTicket>}
188 remoting.gcd.Client.prototype.finalizeRegistrationTicket = function(ticketId) {
189 return new remoting.Xhr({
190 method: 'POST',
191 url: this.apiBaseUrl_ + '/registrationTickets/' +
192 encodeURIComponent(ticketId) + '/finalize',
193 urlParams: {
194 'key': this.apiKey_
196 acceptJson: true
197 }).start().then(function(response) {
198 if (response.isError()) {
199 console.error('error finalizing registration ticket');
200 throw remoting.Error.unexpected();
202 return responseAsGcdRegistrationTicket(response);
207 * Lists devices user has access to.
208 * TODO: Add link to GCD docs.
209 * @param {string=} opt_nameSubstring If present, the list of devices
210 * is filtered by GCD such that every device returned contains
211 * this string as as a substring of its |name| or |displayName|.
212 * @return {!Promise<!Array<remoting.gcd.Device>>}
214 remoting.gcd.Client.prototype.listDevices = function(opt_nameSubstring) {
215 return new remoting.Xhr({
216 method: 'GET',
217 url: this.apiBaseUrl_ + '/devices',
218 urlParams: {
219 nameSubstring: opt_nameSubstring || null
221 useIdentity: true,
222 acceptJson: true
223 }).start().then(function(response) {
224 if (response.isError()) {
225 console.error('error getting device list');
226 throw remoting.Error.unexpected();
228 var hosts = responseAsGcdDeviceListResponse(response);
229 return hosts.devices || [];
234 * Deletes a device from the system.
235 * TODO: Add link to GCD docs.
236 * @param {string} deviceId
237 * @return {!Promise<boolean>} Promise that resolves to true if the
238 * device was deleted, false if there was no such device ID.
240 remoting.gcd.Client.prototype.deleteDevice = function(deviceId) {
241 return new remoting.Xhr({
242 method: 'DELETE',
243 url: this.apiBaseUrl_ + '/devices/' + deviceId,
244 useIdentity: true
245 }).start().then(function(response) {
246 if (response.status == 404) {
247 return false;
249 if (response.isError()) {
250 console.error('error deleting device');
251 throw remoting.Error.unexpected();
253 return true;
258 * Updates a device data using patch semantics.
259 * TODO: Add link to GCD docs.
260 * @param {string} deviceId
261 * @param {!Object<string,*>} patch
262 * @return {!Promise<remoting.gcd.Device>}
264 remoting.gcd.Client.prototype.patchDevice = function(deviceId, patch) {
265 return new remoting.Xhr({
266 method: 'PATCH',
267 url: this.apiBaseUrl_ + '/devices/' + deviceId,
268 jsonContent: patch,
269 useIdentity: true,
270 acceptJson: true
271 }).start().then(function(response) {
272 if (response.isError()) {
273 console.error('error patching device');
274 throw remoting.Error.unexpected();
276 return responseAsGcdDevice(response);
280 })();