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() {} ...@@ -12,20 +12,4 @@ SettingsPageUIHandler::SettingsPageUIHandler() {}
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 } // namespace settings
...@@ -17,19 +17,6 @@ class SettingsPageUIHandler : public content::WebUIMessageHandler { ...@@ -17,19 +17,6 @@ class SettingsPageUIHandler : public content::WebUIMessageHandler {
SettingsPageUIHandler(); SettingsPageUIHandler();
~SettingsPageUIHandler() override; ~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: private:
// SettingsPageUIHandler subclasses must be JavaScript-lifecycle safe. // SettingsPageUIHandler subclasses must be JavaScript-lifecycle safe.
void OnJavascriptAllowed() override = 0; void OnJavascriptAllowed() override = 0;
......
...@@ -81,6 +81,7 @@ void SigninSupervisedUserImportHandler::AssignWebUICallbackId( ...@@ -81,6 +81,7 @@ void SigninSupervisedUserImportHandler::AssignWebUICallbackId(
CHECK_LE(1U, args->GetSize()); CHECK_LE(1U, args->GetSize());
CHECK(webui_callback_id_.empty()); CHECK(webui_callback_id_.empty());
CHECK(args->GetString(0, &webui_callback_id_)); CHECK(args->GetString(0, &webui_callback_id_));
AllowJavascript();
} }
void SigninSupervisedUserImportHandler::OpenUrlInLastActiveProfileBrowser( void SigninSupervisedUserImportHandler::OpenUrlInLastActiveProfileBrowser(
...@@ -190,9 +191,9 @@ void SigninSupervisedUserImportHandler::LoadCustodianProfileCallback( ...@@ -190,9 +191,9 @@ void SigninSupervisedUserImportHandler::LoadCustodianProfileCallback(
void SigninSupervisedUserImportHandler::RejectCallback( void SigninSupervisedUserImportHandler::RejectCallback(
const base::string16& error) { const base::string16& error) {
web_ui()->CallJavascriptFunctionUnsafe( RejectJavascriptCallback(
"cr.webUIResponse", base::StringValue(webui_callback_id_), base::StringValue(webui_callback_id_),
base::FundamentalValue(false), base::StringValue(error)); base::StringValue(error));
webui_callback_id_.clear(); webui_callback_id_.clear();
} }
...@@ -266,9 +267,9 @@ void SigninSupervisedUserImportHandler::SendExistingSupervisedUsers( ...@@ -266,9 +267,9 @@ void SigninSupervisedUserImportHandler::SendExistingSupervisedUsers(
} }
// Resolve callback with response. // Resolve callback with response.
web_ui()->CallJavascriptFunctionUnsafe( ResolveJavascriptCallback(
"cr.webUIResponse", base::StringValue(webui_callback_id_), base::StringValue(webui_callback_id_),
base::FundamentalValue(true), supervised_users); supervised_users);
webui_callback_id_.clear(); webui_callback_id_.clear();
} }
......
...@@ -502,9 +502,9 @@ void UserManagerScreenHandler::HandleAreAllProfilesLocked( ...@@ -502,9 +502,9 @@ void UserManagerScreenHandler::HandleAreAllProfilesLocked(
bool success = args->GetString(0, &webui_callback_id); bool success = args->GetString(0, &webui_callback_id);
DCHECK(success); DCHECK(success);
web_ui()->CallJavascriptFunctionUnsafe( AllowJavascript();
"cr.webUIResponse", base::StringValue(webui_callback_id), ResolveJavascriptCallback(
base::FundamentalValue(true), base::StringValue(webui_callback_id),
base::FundamentalValue(profiles::AreAllProfilesLocked())); base::FundamentalValue(profiles::AreAllProfilesLocked()));
} }
......
...@@ -73,4 +73,20 @@ void WebUIMessageHandler::RenderViewReused() { ...@@ -73,4 +73,20 @@ void WebUIMessageHandler::RenderViewReused() {
DisallowJavascript(); 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 } // namespace content
...@@ -77,6 +77,18 @@ class CONTENT_EXPORT WebUIMessageHandler { ...@@ -77,6 +77,18 @@ class CONTENT_EXPORT WebUIMessageHandler {
// to deregister or disabled observers that push JavaScript calls to the page. // to deregister or disabled observers that push JavaScript calls to the page.
virtual void OnJavascriptDisallowed() {} 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 // 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 // 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 // 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