Commit b60b1f03 authored by dpapad's avatar dpapad Committed by Commit bot

MD Settings: Add helper method for responding to cr.sendWithPromise.

 - Stop passing the 'cr.webUIResponse' string from JS to C++ and again back to JS.
 - Restricting C++ response to JS to a single argument, since Promises
   can only be resolved with a single argument (this is also more consistent
   with Mojo-based communication).
 - Adding additional test cases.

BUG=580633

Review URL: https://codereview.chromium.org/1635673003

Cr-Commit-Position: refs/heads/master@{#371726}
parent c235c91d
...@@ -124,9 +124,8 @@ Polymer({ ...@@ -124,9 +124,8 @@ Polymer({
/** @override */ /** @override */
attached: function() { attached: function() {
// Query the initial state. // Query the initial state.
cr.sendWithPromise('getResetThemeEnabled').then(function(response) { cr.sendWithPromise('getResetThemeEnabled').then(
this.setResetThemeEnabled(response[0]); this.setResetThemeEnabled.bind(this));
}.bind(this));
// Set up the change event listener. // Set up the change event listener.
cr.addWebUIListener('reset-theme-enabled-changed', cr.addWebUIListener('reset-theme-enabled-changed',
......
...@@ -71,15 +71,12 @@ base::FundamentalValue AppearanceHandler::QueryResetThemeEnabledState() { ...@@ -71,15 +71,12 @@ base::FundamentalValue AppearanceHandler::QueryResetThemeEnabledState() {
} }
void AppearanceHandler::GetResetThemeEnabled(const base::ListValue* args) { void AppearanceHandler::GetResetThemeEnabled(const base::ListValue* args) {
CHECK_EQ(2U, args->GetSize()); CHECK_EQ(1U, args->GetSize());
const base::Value* callback_id;
std::string callbackFn; CHECK(args->Get(0, &callback_id));
CHECK(args->GetString(0, &callbackFn));
const base::Value* callbackId;
CHECK(args->Get(1, &callbackId));
base::FundamentalValue enabled(QueryResetThemeEnabledState()); base::FundamentalValue enabled(QueryResetThemeEnabledState());
web_ui()->CallJavascriptFunction(callbackFn, *callbackId, enabled); CallJavascriptCallback(*callback_id, enabled);
} }
} // namespace settings } // namespace settings
...@@ -40,6 +40,12 @@ SettingsPageUIHandler::SettingsPageUIHandler() { ...@@ -40,6 +40,12 @@ SettingsPageUIHandler::SettingsPageUIHandler() {
SettingsPageUIHandler::~SettingsPageUIHandler() { SettingsPageUIHandler::~SettingsPageUIHandler() {
} }
void SettingsPageUIHandler::CallJavascriptCallback(
const base::Value& callback_id, const base::Value& response) {
// cr.webUIResponse is a global JS function exposed from cr.js.
web_ui()->CallJavascriptFunction("cr.webUIResponse", callback_id, response);
}
MdSettingsUI::MdSettingsUI(content::WebUI* web_ui) MdSettingsUI::MdSettingsUI(content::WebUI* web_ui)
: content::WebUIController(web_ui) { : content::WebUIController(web_ui) {
Profile* profile = Profile::FromWebUI(web_ui); Profile* profile = Profile::FromWebUI(web_ui);
......
...@@ -25,6 +25,12 @@ class SettingsPageUIHandler : public content::WebUIMessageHandler { ...@@ -25,6 +25,12 @@ class SettingsPageUIHandler : public content::WebUIMessageHandler {
// WebUIMessageHandler implementation. // WebUIMessageHandler implementation.
void RegisterMessages() override {} void RegisterMessages() override {}
protected:
// Helper method for responding to JS requests initiated with
// cr.sendWithPromise().
void CallJavascriptCallback(const base::Value& callback_id,
const base::Value& response);
private: private:
DISALLOW_COPY_AND_ASSIGN(SettingsPageUIHandler); DISALLOW_COPY_AND_ASSIGN(SettingsPageUIHandler);
}; };
......
...@@ -56,22 +56,39 @@ TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() { ...@@ -56,22 +56,39 @@ TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() {
setup(function() { setup(function() {
// Simulate a WebUI handler that echoes back all parameters passed to it. // Simulate a WebUI handler that echoes back all parameters passed to it.
whenChromeSendCalled(CHROME_SEND_NAME).then(function(args) { whenChromeSendCalled(CHROME_SEND_NAME).then(function(args) {
assertEquals('cr.webUIResponse', args[0]); cr.webUIResponse.apply(null, args);
var globalCallbackArgs = args.slice(1);
cr.webUIResponse.apply(null, globalCallbackArgs);
}); });
}); });
test('sendWithPromise_MultipleArgs', function() { test('sendWithPromise_ResponseObject', function() {
return cr.sendWithPromise(CHROME_SEND_NAME, 'foo', 'bar').then( var expectedResponse = {'foo': 'bar'};
return cr.sendWithPromise(CHROME_SEND_NAME, expectedResponse).then(
function(response) { function(response) {
assertEquals(['foo', 'bar'].join(), response.join()); assertEquals(
JSON.stringify(expectedResponse), JSON.stringify(response));
}); });
}); });
test('sendWithPromise_NoArgs', function() { test('sendWithPromise_ResponseArray', function() {
var expectedResponse = ['foo', 'bar'];
return cr.sendWithPromise(CHROME_SEND_NAME, expectedResponse).then(
function(response) {
assertEquals(
JSON.stringify(expectedResponse), JSON.stringify(response));
});
});
test('sendWithPromise_ResponsePrimitive', function() {
var expectedResponse = 1234;
return cr.sendWithPromise(CHROME_SEND_NAME, expectedResponse).then(
function(response) {
assertEquals(expectedResponse, response);
});
});
test('sendWithPromise_ResponseVoid', function() {
return cr.sendWithPromise(CHROME_SEND_NAME).then(function(response) { return cr.sendWithPromise(CHROME_SEND_NAME).then(function(response) {
assertEquals([].join(), response.join()); assertEquals(undefined, response);
}); });
}); });
}); });
......
...@@ -331,15 +331,12 @@ var cr = function() { ...@@ -331,15 +331,12 @@ var cr = function() {
* supply any number of other arguments that will be included in the response. * supply any number of other arguments that will be included in the response.
* @param {string} id The unique ID identifying the Promise this response is * @param {string} id The unique ID identifying the Promise this response is
* tied to. * tied to.
* @param {...*} var_args Variable number of arguments to be included in the * @param {*} response The response as sent from C++.
* response.
*/ */
function webUIResponse(id, var_args) { function webUIResponse(id, response) {
var resolverFn = chromeSendResolverMap[id]; var resolverFn = chromeSendResolverMap[id];
delete chromeSendResolverMap[id]; delete chromeSendResolverMap[id];
// Promise#resolve accepts only one value, therefore wrapping all arguments resolverFn(response);
// passed from C++ to JS in an array.
resolverFn(Array.prototype.slice.call(arguments, 1));
} }
/** /**
...@@ -355,7 +352,7 @@ var cr = function() { ...@@ -355,7 +352,7 @@ var cr = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var id = methodName + '_' + createUid(); var id = methodName + '_' + createUid();
chromeSendResolverMap[id] = resolve; chromeSendResolverMap[id] = resolve;
chrome.send(methodName, ['cr.webUIResponse', id].concat(args)); chrome.send(methodName, [id].concat(args));
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment