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 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** @constructor */ | 15 /** |
16 remoting.HostListApi = function() { | 16 * @constructor |
| 17 * @implements {remoting.HostListApi} |
| 18 */ |
| 19 remoting.HostListApiImpl = function() { |
17 }; | 20 }; |
18 | 21 |
19 /** | 22 /** |
20 * Fetch the list of hosts for a user. | 23 * Fetch the list of hosts for a user. |
21 * | 24 * |
22 * @param {function(Array.<remoting.Host>):void} onDone | 25 * @param {function(Array.<remoting.Host>):void} onDone |
23 * @param {function(remoting.Error):void} onError | 26 * @param {function(remoting.Error):void} onError |
24 */ | 27 */ |
25 remoting.HostListApi.prototype.get = function(onDone, onError) { | 28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { |
26 /** @type {function(XMLHttpRequest):void} */ | 29 /** @type {function(XMLHttpRequest):void} */ |
27 var parseHostListResponse = | 30 var parseHostListResponse = |
28 this.parseHostListResponse_.bind(this, onDone, onError) | 31 this.parseHostListResponse_.bind(this, onDone, onError) |
29 /** @param {string} token */ | 32 /** @param {string} token */ |
30 var onToken = function(token) { | 33 var onToken = function(token) { |
31 var headers = { 'Authorization': 'OAuth ' + token }; | 34 var headers = { 'Authorization': 'OAuth ' + token }; |
32 remoting.xhr.get(remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 35 remoting.xhr.get(remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
33 parseHostListResponse, '', headers); | 36 parseHostListResponse, '', headers); |
34 }; | 37 }; |
35 remoting.identity.callWithToken(onToken, onError); | 38 remoting.identity.callWithToken(onToken, onError); |
36 }; | 39 }; |
37 | 40 |
38 /** | 41 /** |
39 * Update the information for a host. | 42 * Update the information for a host. |
40 * | 43 * |
41 * @param {function():void} onDone | 44 * @param {function():void} onDone |
42 * @param {function(remoting.Error):void} onError | 45 * @param {function(remoting.Error):void} onError |
43 * @param {string} hostId | 46 * @param {string} hostId |
44 * @param {string} hostName | 47 * @param {string} hostName |
45 * @param {string} hostPublicKey | 48 * @param {string} hostPublicKey |
46 */ | 49 */ |
47 remoting.HostListApi.prototype.put = | 50 remoting.HostListApiImpl.prototype.put = |
48 function(hostId, hostName, hostPublicKey, onDone, onError) { | 51 function(hostId, hostName, hostPublicKey, onDone, onError) { |
49 /** @param {string} token */ | 52 /** @param {string} token */ |
50 var onToken = function(token) { | 53 var onToken = function(token) { |
51 var headers = { | 54 var headers = { |
52 'Authorization': 'OAuth ' + token, | 55 'Authorization': 'OAuth ' + token, |
53 'Content-type' : 'application/json; charset=UTF-8' | 56 'Content-type' : 'application/json; charset=UTF-8' |
54 }; | 57 }; |
55 var newHostDetails = { | 58 var newHostDetails = { |
56 'data': { | 59 'data': { |
57 'hostId': hostId, | 60 'hostId': hostId, |
(...skipping 10 matching lines...) Expand all Loading... |
68 remoting.identity.callWithToken(onToken, onError); | 71 remoting.identity.callWithToken(onToken, onError); |
69 }; | 72 }; |
70 | 73 |
71 /** | 74 /** |
72 * Delete a host. | 75 * Delete a host. |
73 * | 76 * |
74 * @param {function():void} onDone | 77 * @param {function():void} onDone |
75 * @param {function(remoting.Error):void} onError | 78 * @param {function(remoting.Error):void} onError |
76 * @param {string} hostId | 79 * @param {string} hostId |
77 */ | 80 */ |
78 remoting.HostListApi.prototype.remove = function(hostId, onDone, onError) { | 81 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
79 /** @param {string} token */ | 82 /** @param {string} token */ |
80 var onToken = function(token) { | 83 var onToken = function(token) { |
81 var headers = { 'Authorization': 'OAuth ' + token }; | 84 var headers = { 'Authorization': 'OAuth ' + token }; |
82 remoting.xhr.remove( | 85 remoting.xhr.remove( |
83 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 86 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
84 remoting.xhr.defaultResponse(onDone, onError), | 87 remoting.xhr.defaultResponse(onDone, onError), |
85 '', headers); | 88 '', headers); |
86 }; | 89 }; |
87 remoting.identity.callWithToken(onToken, onError); | 90 remoting.identity.callWithToken(onToken, onError); |
88 }; | 91 }; |
89 | 92 |
90 /** | 93 /** |
91 * Handle the results of the host list request. A success response will | 94 * Handle the results of the host list request. A success response will |
92 * include a JSON-encoded list of host descriptions, which is parsed and | 95 * include a JSON-encoded list of host descriptions, which is parsed and |
93 * passed to the callback. | 96 * passed to the callback. |
94 * | 97 * |
95 * @param {function(Array.<remoting.Host>):void} onDone | 98 * @param {function(Array.<remoting.Host>):void} onDone |
96 * @param {function(remoting.Error):void} onError | 99 * @param {function(remoting.Error):void} onError |
97 * @param {XMLHttpRequest} xhr | 100 * @param {XMLHttpRequest} xhr |
98 * @private | 101 * @private |
99 */ | 102 */ |
100 remoting.HostListApi.prototype.parseHostListResponse_ = | 103 remoting.HostListApiImpl.prototype.parseHostListResponse_ = |
101 function(onDone, onError, xhr) { | 104 function(onDone, onError, xhr) { |
102 if (xhr.status == 200) { | 105 if (xhr.status == 200) { |
103 var response = /** @type {{data: {items: Array}}} */ | 106 var response = /** @type {{data: {items: Array}}} */ |
104 (base.jsonParseSafe(xhr.responseText)); | 107 (base.jsonParseSafe(xhr.responseText)); |
105 if (!response || !response.data) { | 108 if (!response || !response.data) { |
106 console.error('Invalid "hosts" response from server.'); | 109 console.error('Invalid "hosts" response from server.'); |
107 onError(remoting.Error.UNEXPECTED); | 110 onError(remoting.Error.UNEXPECTED); |
108 } else { | 111 } else { |
109 var hosts = response.data.items || []; | 112 var hosts = response.data.items || []; |
110 onDone(hosts); | 113 onDone(hosts); |
111 } | 114 } |
112 } else { | 115 } else { |
113 onError(remoting.Error.fromHttpError(xhr.status)); | 116 onError(remoting.Error.fromHttpError(xhr.status)); |
114 } | 117 } |
115 }; | 118 }; |
116 | 119 |
117 /** @type {remoting.HostListApi} */ | 120 /** @type {remoting.HostListApi} */ |
118 remoting.hostListApi = new remoting.HostListApi(); | 121 remoting.hostListApi = new remoting.HostListApiImpl(); |
OLD | NEW |