Commit 4d3274ee authored by dpapad's avatar dpapad Committed by Commit bot

WebUI: Move {Resolve|Reject}JavascriptCallback helpers in WebUIMessageHandler.

cr.sendWithPromise() is gaining traction outside of MD Settings, and the
C++ helpers to resolve/reject a JS Promise are more useful in the
superclass WebUIMessageHandler instead of SettingsPageUIHandler.

Review-Url: https://codereview.chromium.org/2040473002
Cr-Commit-Position: refs/heads/master@{#398166}
parent d46e5ff0
......@@ -12,20 +12,4 @@ SettingsPageUIHandler::SettingsPageUIHandler() {}
SettingsPageUIHandler::~SettingsPageUIHandler() {}
void SettingsPageUIHandler::ResolveJavascriptCallback(
const base::Value& callback_id,
const base::Value& response) {
// cr.webUIResponse is a global JS function exposed from cr.js.
CallJavascriptFunction("cr.webUIResponse", callback_id,
base::FundamentalValue(true), response);
}
void SettingsPageUIHandler::RejectJavascriptCallback(
const base::Value& callback_id,
const base::Value& response) {
// cr.webUIResponse is a global JS function exposed from cr.js.
CallJavascriptFunction("cr.webUIResponse", callback_id,
base::FundamentalValue(false), response);
}
} // namespace settings
......@@ -17,19 +17,6 @@ class SettingsPageUIHandler : public content::WebUIMessageHandler {
SettingsPageUIHandler();
~SettingsPageUIHandler() override;
protected:
// Helper method for responding to JS requests initiated with
// cr.sendWithPromise(), for the case where the returned promise should be
// resolved (request succeeded).
void ResolveJavascriptCallback(const base::Value& callback_id,
const base::Value& response);
// Helper method for responding to JS requests initiated with
// cr.sendWithPromise(), for the case where the returned promise should be
// rejected (request failed).
void RejectJavascriptCallback(const base::Value& callback_id,
const base::Value& response);
private:
// SettingsPageUIHandler subclasses must be JavaScript-lifecycle safe.
void OnJavascriptAllowed() override = 0;
......
......@@ -81,6 +81,7 @@ void SigninSupervisedUserImportHandler::AssignWebUICallbackId(
CHECK_LE(1U, args->GetSize());
CHECK(webui_callback_id_.empty());
CHECK(args->GetString(0, &webui_callback_id_));
AllowJavascript();
}
void SigninSupervisedUserImportHandler::OpenUrlInLastActiveProfileBrowser(
......@@ -190,9 +191,9 @@ void SigninSupervisedUserImportHandler::LoadCustodianProfileCallback(
void SigninSupervisedUserImportHandler::RejectCallback(
const base::string16& error) {
web_ui()->CallJavascriptFunctionUnsafe(
"cr.webUIResponse", base::StringValue(webui_callback_id_),
base::FundamentalValue(false), base::StringValue(error));
RejectJavascriptCallback(
base::StringValue(webui_callback_id_),
base::StringValue(error));
webui_callback_id_.clear();
}
......@@ -266,9 +267,9 @@ void SigninSupervisedUserImportHandler::SendExistingSupervisedUsers(
}
// Resolve callback with response.
web_ui()->CallJavascriptFunctionUnsafe(
"cr.webUIResponse", base::StringValue(webui_callback_id_),
base::FundamentalValue(true), supervised_users);
ResolveJavascriptCallback(
base::StringValue(webui_callback_id_),
supervised_users);
webui_callback_id_.clear();
}
......
......@@ -502,9 +502,9 @@ void UserManagerScreenHandler::HandleAreAllProfilesLocked(
bool success = args->GetString(0, &webui_callback_id);
DCHECK(success);
web_ui()->CallJavascriptFunctionUnsafe(
"cr.webUIResponse", base::StringValue(webui_callback_id),
base::FundamentalValue(true),
AllowJavascript();
ResolveJavascriptCallback(
base::StringValue(webui_callback_id),
base::FundamentalValue(profiles::AreAllProfilesLocked()));
}
......
......@@ -73,4 +73,20 @@ void WebUIMessageHandler::RenderViewReused() {
DisallowJavascript();
}
void WebUIMessageHandler::ResolveJavascriptCallback(
const base::Value& callback_id,
const base::Value& response) {
// cr.webUIResponse is a global JS function exposed from cr.js.
CallJavascriptFunction("cr.webUIResponse", callback_id,
base::FundamentalValue(true), response);
}
void WebUIMessageHandler::RejectJavascriptCallback(
const base::Value& callback_id,
const base::Value& response) {
// cr.webUIResponse is a global JS function exposed from cr.js.
CallJavascriptFunction("cr.webUIResponse", callback_id,
base::FundamentalValue(false), response);
}
} // namespace content
......@@ -77,6 +77,18 @@ class CONTENT_EXPORT WebUIMessageHandler {
// to deregister or disabled observers that push JavaScript calls to the page.
virtual void OnJavascriptDisallowed() {}
// Helper method for responding to Javascript requests initiated with
// cr.sendWithPromise() (defined in cr.js) for the case where the returned
// promise should be resolved (request succeeded).
void ResolveJavascriptCallback(const base::Value& callback_id,
const base::Value& response);
// Helper method for responding to Javascript requests initiated with
// cr.sendWithPromise() (defined in cr.js), for the case where the returned
// promise should be rejected (request failed).
void RejectJavascriptCallback(const base::Value& callback_id,
const base::Value& response);
// Call a Javascript function by sending its name and arguments down to
// the renderer. This is asynchronous; there's no way to get the result
// of the call, and should be thought of more like sending a message to
......
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