summaryrefslogtreecommitdiffstats
path: root/remoting
diff options
context:
space:
mode:
authorjrw <jrw@chromium.org>2015-04-21 14:53:01 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-21 21:53:19 +0000
commite975b852fae409dc396ac17a31c13d9e06e6a98a (patch)
tree9f2f84a4a02cbd8bd465b765e3a731984a2cda88 /remoting
parent14a0d5bba738da6d7ab4e0b08d7c4bb3ec2b1341 (diff)
downloadchromium_src-e975b852fae409dc396ac17a31c13d9e06e6a98a.zip
chromium_src-e975b852fae409dc396ac17a31c13d9e06e6a98a.tar.gz
chromium_src-e975b852fae409dc396ac17a31c13d9e06e6a98a.tar.bz2
Converted HostListApi to use a promises instead of callbacks.
BUG=471928 Review URL: https://codereview.chromium.org/1096633002 Cr-Commit-Position: refs/heads/master@{#326131}
Diffstat (limited to 'remoting')
-rw-r--r--remoting/webapp/crd/js/host_list.js40
-rw-r--r--remoting/webapp/crd/js/host_list_api.js15
-rw-r--r--remoting/webapp/crd/js/host_list_api_impl.js80
-rw-r--r--remoting/webapp/crd/js/mock_host_list_api.js88
4 files changed, 93 insertions, 130 deletions
diff --git a/remoting/webapp/crd/js/host_list.js b/remoting/webapp/crd/js/host_list.js
index 3b130ec..1678464 100644
--- a/remoting/webapp/crd/js/host_list.js
+++ b/remoting/webapp/crd/js/host_list.js
@@ -48,7 +48,7 @@ remoting.HostList = function(table, noHosts, errorMsg, errorButton,
/** @private {HTMLElement} */
this.loadingIndicator_ = loadingIndicator;
/** @private */
- this.onError_ = onError;
+ this.onError_ = remoting.Error.handler(onError);
/** @private */
this.handleConnect_ = handleConnect;
@@ -152,8 +152,11 @@ remoting.HostList.prototype.refresh = function(onDone) {
that.lastError_ = error;
onDone(false);
};
- remoting.hostListApi.get(this.onHostListResponse_.bind(this, onDone),
- onError);
+ remoting.hostListApi.get().then(function(hosts) {
+ onDone(that.onHostListResponse_(hosts));
+ }).catch(
+ remoting.Error.handler(onError)
+ );
};
/**
@@ -161,18 +164,17 @@ remoting.HostList.prototype.refresh = function(onDone) {
* include a JSON-encoded list of host descriptions, which we display if we're
* able to successfully parse it.
*
- * @param {function(boolean):void} onDone The callback passed to |refresh|.
* @param {Array<remoting.Host>} hosts The list of hosts for the user.
- * @return {void} Nothing.
+ * @return {boolean}
* @private
*/
-remoting.HostList.prototype.onHostListResponse_ = function(onDone, hosts) {
+remoting.HostList.prototype.onHostListResponse_ = function(hosts) {
this.lastError_ = remoting.Error.none();
this.hosts_ = hosts;
this.sortHosts_();
this.save_();
this.loadingIndicator_.classList.remove('loading');
- onDone(true);
+ return true;
};
/**
@@ -304,8 +306,8 @@ remoting.HostList.prototype.deleteHost_ = function(hostTableEntry) {
if (index != -1) {
this.hostTableEntries_.splice(index, 1);
}
- remoting.hostListApi.remove(hostTableEntry.host.hostId, base.doNothing,
- this.onError_);
+ remoting.hostListApi.remove(hostTableEntry.host.hostId).
+ catch(this.onError_);
};
/**
@@ -324,9 +326,8 @@ remoting.HostList.prototype.renameHost = function(hostTableEntry) {
remoting.hostListApi.put(hostTableEntry.host.hostId,
hostTableEntry.host.hostName,
- hostTableEntry.host.publicKey,
- function() {},
- this.onError_);
+ hostTableEntry.host.publicKey).
+ catch(this.onError_);
};
/**
@@ -347,13 +348,14 @@ remoting.HostList.prototype.unregisterHostById = function(hostId, opt_onDone) {
return;
}
- var onRemoved = function() {
- that.refresh(function() {
- that.display();
- onDone();
- });
- };
- remoting.hostListApi.remove(hostId, onRemoved, this.onError_);
+ remoting.hostListApi.remove(hostId).
+ then(function() {
+ that.refresh(function() {
+ that.display();
+ onDone();
+ });
+ }).
+ catch(this.onError_);
};
/**
diff --git a/remoting/webapp/crd/js/host_list_api.js b/remoting/webapp/crd/js/host_list_api.js
index acdff97..9b63ccb 100644
--- a/remoting/webapp/crd/js/host_list_api.js
+++ b/remoting/webapp/crd/js/host_list_api.js
@@ -28,31 +28,28 @@ remoting.HostListApi.prototype.register;
/**
* Fetch the list of hosts for a user.
*
- * @param {function(Array<remoting.Host>):void} onDone
- * @param {function(!remoting.Error):void} onError
+ * @return {!Promise<!Array<!remoting.Host>>}
*/
-remoting.HostListApi.prototype.get = function(onDone, onError) {
+remoting.HostListApi.prototype.get = function() {
};
/**
* Update the information for a host.
*
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
* @param {string} hostId
* @param {string} hostName
* @param {string} hostPublicKey
+ * @return {!Promise<void>}
*/
remoting.HostListApi.prototype.put =
- function(hostId, hostName, hostPublicKey, onDone, onError) {
+ function(hostId, hostName, hostPublicKey) {
};
/**
* Delete a host.
*
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
* @param {string} hostId
+ * @return {!Promise<void>}
*/
-remoting.HostListApi.prototype.remove = function(hostId, onDone, onError) {
+remoting.HostListApi.prototype.remove = function(hostId) {
};
diff --git a/remoting/webapp/crd/js/host_list_api_impl.js b/remoting/webapp/crd/js/host_list_api_impl.js
index 55b4868..9cfff10 100644
--- a/remoting/webapp/crd/js/host_list_api_impl.js
+++ b/remoting/webapp/crd/js/host_list_api_impl.js
@@ -56,39 +56,22 @@ remoting.HostListApiImpl.prototype.register = function(
});
};
-/**
- * Fetch the list of hosts for a user.
- *
- * @param {function(Array<remoting.Host>):void} onDone
- * @param {function(!remoting.Error):void} onError
- */
-remoting.HostListApiImpl.prototype.get = function(onDone, onError) {
- /** @type {function(!remoting.Xhr.Response):void} */
- var parseHostListResponse =
- this.parseHostListResponse_.bind(this, onDone, onError);
- /** @param {string} token */
- var onToken = function(token) {
- new remoting.Xhr({
- method: 'GET',
- url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
- oauthToken: token
- }).start().then(parseHostListResponse);
- };
- remoting.identity.getToken().then(onToken, remoting.Error.handler(onError));
+/** @override */
+remoting.HostListApiImpl.prototype.get = function() {
+ var that = this;
+ return new remoting.Xhr({
+ method: 'GET',
+ url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
+ useIdentity: true
+ }).start().then(function(/** !remoting.Xhr.Response */ response) {
+ return that.parseHostListResponse_(response);
+ });
};
-/**
- * Update the information for a host.
- *
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
- * @param {string} hostId
- * @param {string} hostName
- * @param {string} hostPublicKey
- */
+/** @override */
remoting.HostListApiImpl.prototype.put =
- function(hostId, hostName, hostPublicKey, onDone, onError) {
- new remoting.Xhr({
+ function(hostId, hostName, hostPublicKey) {
+ return new remoting.Xhr({
method: 'PUT',
url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
jsonContent: {
@@ -99,23 +82,17 @@ remoting.HostListApiImpl.prototype.put =
}
},
useIdentity: true
- }).start().then(remoting.HostListApiImpl.defaultResponse_(onDone, onError));
+ }).start().then(remoting.HostListApiImpl.defaultResponse_());
};
-/**
- * Delete a host.
- *
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
- * @param {string} hostId
- */
-remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) {
- new remoting.Xhr({
+/** @override */
+remoting.HostListApiImpl.prototype.remove = function(hostId) {
+ return new remoting.Xhr({
method: 'DELETE',
url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
useIdentity: true
}).start().then(remoting.HostListApiImpl.defaultResponse_(
- onDone, onError, [remoting.Error.Tag.NOT_FOUND]));
+ [remoting.Error.Tag.NOT_FOUND]));
};
/**
@@ -123,19 +100,17 @@ remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) {
* include a JSON-encoded list of host descriptions, which is parsed and
* passed to the callback.
*
- * @param {function(Array<remoting.Host>):void} onDone
- * @param {function(!remoting.Error):void} onError
* @param {!remoting.Xhr.Response} response
+ * @return {!Array<!remoting.Host>}
* @private
*/
-remoting.HostListApiImpl.prototype.parseHostListResponse_ =
- function(onDone, onError, response) {
+remoting.HostListApiImpl.prototype.parseHostListResponse_ = function(response) {
if (response.status == 200) {
var obj = /** @type {{data: {items: Array}}} */
(base.jsonParseSafe(response.getText()));
if (!obj || !obj.data) {
console.error('Invalid "hosts" response from server.');
- onError(remoting.Error.unexpected());
+ throw remoting.Error.unexpected();
} else {
var items = obj.data.items || [];
var hosts = items.map(
@@ -153,38 +128,33 @@ remoting.HostListApiImpl.prototype.parseHostListResponse_ =
base.getStringAttr(item, 'hostOfflineReason', '');
return host;
});
- onDone(hosts);
+ return hosts;
}
} else {
- onError(remoting.Error.fromHttpStatus(response.status));
+ throw remoting.Error.fromHttpStatus(response.status);
}
};
/**
* Generic success/failure response proxy.
*
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
* @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
* @return {function(!remoting.Xhr.Response):void}
* @private
*/
-remoting.HostListApiImpl.defaultResponse_ = function(
- onDone, onError, opt_ignoreErrors) {
+remoting.HostListApiImpl.defaultResponse_ = function(opt_ignoreErrors) {
/** @param {!remoting.Xhr.Response} response */
var result = function(response) {
var error = remoting.Error.fromHttpStatus(response.status);
if (error.isNone()) {
- onDone();
return;
}
if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) {
- onDone();
return;
}
- onError(error);
+ throw error;
};
return result;
};
diff --git a/remoting/webapp/crd/js/mock_host_list_api.js b/remoting/webapp/crd/js/mock_host_list_api.js
index 7d1516c..b21462d 100644
--- a/remoting/webapp/crd/js/mock_host_list_api.js
+++ b/remoting/webapp/crd/js/mock_host_list_api.js
@@ -58,64 +58,58 @@ remoting.MockHostListApi.prototype.register = function(
}
};
-/**
- * @param {function(Array<remoting.Host>):void} onDone
- * @param {function(!remoting.Error):void} onError
- */
-remoting.MockHostListApi.prototype.get = function(onDone, onError) {
- remoting.mockIdentity.validateTokenAndCall(onDone, onError, [this.hosts]);
+/** @override */
+remoting.MockHostListApi.prototype.get = function() {
+ var that = this;
+ new Promise(function(resolve, reject) {
+ remoting.mockIdentity.validateTokenAndCall(
+ resolve, remoting.Error.handler(reject), [that.hosts]);
+ });
};
-/**
- * @param {string} hostId
- * @param {string} hostName
- * @param {string} hostPublicKey
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
- */
+/** @override */
remoting.MockHostListApi.prototype.put =
- function(hostId, hostName, hostPublicKey, onDone, onError) {
+ function(hostId, hostName, hostPublicKey) {
/** @type {remoting.MockHostListApi} */
var that = this;
- var onTokenValid = function() {
- for (var i = 0; i < that.hosts.length; ++i) {
- var host = that.hosts[i];
- if (host.hostId == hostId) {
- host.hostName = hostName;
- host.hostPublicKey = hostPublicKey;
- onDone();
- return;
+ return new Promise(function(resolve, reject) {
+ var onTokenValid = function() {
+ for (var i = 0; i < that.hosts.length; ++i) {
+ var host = that.hosts[i];
+ if (host.hostId == hostId) {
+ host.hostName = hostName;
+ host.hostPublicKey = hostPublicKey;
+ resolve(undefined);
+ return;
+ }
}
- }
- console.error('PUT request for unknown host: ' + hostId +
- ' (' + hostName + ')');
- onError(remoting.Error.unexpected());
- };
- remoting.mockIdentity.validateTokenAndCall(onTokenValid, onError, []);
+ console.error('PUT request for unknown host: ' + hostId +
+ ' (' + hostName + ')');
+ reject(remoting.Error.unexpected());
+ };
+ remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
+ });
};
-/**
- * @param {string} hostId
- * @param {function():void} onDone
- * @param {function(!remoting.Error):void} onError
- */
-remoting.MockHostListApi.prototype.remove =
- function(hostId, onDone, onError) {
+/** @override */
+remoting.MockHostListApi.prototype.remove = function(hostId) {
/** @type {remoting.MockHostListApi} */
var that = this;
- var onTokenValid = function() {
- for (var i = 0; i < that.hosts.length; ++i) {
- var host = that.hosts[i];
- if (host.hostId == hostId) {
- that.hosts.splice(i, 1);
- onDone();
- return;
+ return new Promise(function(resolve, reject) {
+ var onTokenValid = function() {
+ for (var i = 0; i < that.hosts.length; ++i) {
+ var host = that.hosts[i];
+ if (host.hostId == hostId) {
+ that.hosts.splice(i, 1);
+ resolve(undefined);
+ return;
+ }
}
- }
- console.error('DELETE request for unknown host: ' + hostId);
- onError(remoting.Error.unexpected());
- };
- remoting.mockIdentity.validateTokenAndCall(onTokenValid, onError, []);
+ console.error('DELETE request for unknown host: ' + hostId);
+ reject(remoting.Error.unexpected());
+ };
+ remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
+ });
};
/**