Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Side by Side Diff: ui/webui/resources/js/cr.js

Issue 1635673003: MD Settings: Add helper method for responding to cr.sendWithPromise. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/test/data/webui/webui_resource_async_browsertest.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 * The global object. 6 * The global object.
7 * @type {!Object} 7 * @type {!Object}
8 * @const 8 * @const
9 */ 9 */
10 var global = this; 10 var global = this;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 /** 324 /**
325 * The named method the WebUI handler calls directly in response to a 325 * The named method the WebUI handler calls directly in response to a
326 * chrome.send call that expects a response. The handler requires no knowledge 326 * chrome.send call that expects a response. The handler requires no knowledge
327 * of the specific name of this method, as the name is passed to the handler 327 * of the specific name of this method, as the name is passed to the handler
328 * as the first argument in the arguments list of chrome.send. The handler 328 * as the first argument in the arguments list of chrome.send. The handler
329 * must pass the ID, also sent via the chrome.send arguments list, as the 329 * must pass the ID, also sent via the chrome.send arguments list, as the
330 * first argument of the JS invocation; additionally, the handler may 330 * first argument of the JS invocation; additionally, the handler may
331 * supply any number of other arguments that will be included in the response. 331 * supply any number of other arguments that will be included in the response.
332 * @param {string} id The unique ID identifying the Promise this response is 332 * @param {string} id The unique ID identifying the Promise this response is
333 * tied to. 333 * tied to.
334 * @param {...*} var_args Variable number of arguments to be included in the 334 * @param {*} response The response as sent from C++.
335 * response.
336 */ 335 */
337 function webUIResponse(id, var_args) { 336 function webUIResponse(id, response) {
338 var resolverFn = chromeSendResolverMap[id]; 337 var resolverFn = chromeSendResolverMap[id];
339 delete chromeSendResolverMap[id]; 338 delete chromeSendResolverMap[id];
340 // Promise#resolve accepts only one value, therefore wrapping all arguments 339 resolverFn(response);
341 // passed from C++ to JS in an array.
342 resolverFn(Array.prototype.slice.call(arguments, 1));
343 } 340 }
344 341
345 /** 342 /**
346 * A variation of chrome.send, suitable for messages that expect a single 343 * A variation of chrome.send, suitable for messages that expect a single
347 * response from C++. 344 * response from C++.
348 * @param {string} methodName The name of the WebUI handler API. 345 * @param {string} methodName The name of the WebUI handler API.
349 * @param {...*} var_args Varibale number of arguments to be forwarded to the 346 * @param {...*} var_args Varibale number of arguments to be forwarded to the
350 * C++ call. 347 * C++ call.
351 * @return {!Promise} 348 * @return {!Promise}
352 */ 349 */
353 function sendWithPromise(methodName, var_args) { 350 function sendWithPromise(methodName, var_args) {
354 var args = Array.prototype.slice.call(arguments, 1); 351 var args = Array.prototype.slice.call(arguments, 1);
355 return new Promise(function(resolve, reject) { 352 return new Promise(function(resolve, reject) {
356 var id = methodName + '_' + createUid(); 353 var id = methodName + '_' + createUid();
357 chromeSendResolverMap[id] = resolve; 354 chromeSendResolverMap[id] = resolve;
358 chrome.send(methodName, ['cr.webUIResponse', id].concat(args)); 355 chrome.send(methodName, [id].concat(args));
359 }); 356 });
360 } 357 }
361 358
362 /** 359 /**
363 * A registry of callbacks keyed by event name. Used by addWebUIListener to 360 * A registry of callbacks keyed by event name. Used by addWebUIListener to
364 * register listeners. 361 * register listeners.
365 * @type {!Object<Array<Function>>} 362 * @type {!Object<Array<Function>>}
366 */ 363 */
367 var webUIListenerMap = Object.create(null); 364 var webUIListenerMap = Object.create(null);
368 365
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 get isChromeOS() { 425 get isChromeOS() {
429 return /CrOS/.test(navigator.userAgent); 426 return /CrOS/.test(navigator.userAgent);
430 }, 427 },
431 428
432 /** Whether this is on vanilla Linux (not chromeOS). */ 429 /** Whether this is on vanilla Linux (not chromeOS). */
433 get isLinux() { 430 get isLinux() {
434 return /Linux/.test(navigator.userAgent); 431 return /Linux/.test(navigator.userAgent);
435 }, 432 },
436 }; 433 };
437 }(); 434 }();
OLDNEW
« no previous file with comments | « chrome/test/data/webui/webui_resource_async_browsertest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698