Commit d4f8a728 authored by Benedikt Meurer's avatar Benedikt Meurer Committed by Commit Bot

[devtools] Introduce and consistently use `CallClientMethod` helper.

This refactors the `CallClientFunction` helper method into a new helper
method `CallClientMethod`, which takes separate object and method name,
and the arguments (as base::Values), with the intention of sending that
information to the renderer in a second step, instead of generating a
string with all the above embedded in it, which is then send to V8 as a
new script.

The motivation is that sending every single protocol message as a new
script causes a lot of churn on the renderer, not only regarding the
runtime performance, but also trashing the memory and resulting in
fragmentation that can be avoided.

Drive-by-refactoring: Reduce friction around the variable number of
parameters passed to `CallClientMethod` and avoid the need to pass
base::Value as pointers. This improves code readability a bit.

Bug: chromium:1029427
Change-Id: I5ac578e2071d7ba63b9b42dff7e3b137a9908043
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2042713
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Auto-Submit: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739363}
parent 00e3f38e
...@@ -80,10 +80,11 @@ class DevToolsUIBindings : public DevToolsEmbedderMessageDispatcher::Delegate, ...@@ -80,10 +80,11 @@ class DevToolsUIBindings : public DevToolsEmbedderMessageDispatcher::Delegate,
// Takes ownership over the |delegate|. // Takes ownership over the |delegate|.
void SetDelegate(Delegate* delegate); void SetDelegate(Delegate* delegate);
void CallClientFunction(const std::string& function_name, void CallClientMethod(const std::string& object_name,
const base::Value* arg1, const std::string& method_name,
const base::Value* arg2, const base::Value& arg1 = {},
const base::Value* arg3); const base::Value& arg2 = {},
const base::Value& arg3 = {});
void AttachTo(const scoped_refptr<content::DevToolsAgentHost>& agent_host); void AttachTo(const scoped_refptr<content::DevToolsAgentHost>& agent_host);
void Detach(); void Detach();
bool IsAttachedTo(content::DevToolsAgentHost* agent_host); bool IsAttachedTo(content::DevToolsAgentHost* agent_host);
......
...@@ -315,8 +315,8 @@ bool DevToolsEventForwarder::ForwardEvent( ...@@ -315,8 +315,8 @@ bool DevToolsEventForwarder::ForwardEvent(
static_cast<ui::DomCode>(event.dom_code))); static_cast<ui::DomCode>(event.dom_code)));
event_data.SetIntKey("keyCode", key_code); event_data.SetIntKey("keyCode", key_code);
event_data.SetIntKey("modifiers", modifiers); event_data.SetIntKey("modifiers", modifiers);
devtools_window_->bindings_->CallClientFunction( devtools_window_->bindings_->CallClientMethod(
"DevToolsAPI.keyEventUnhandled", &event_data, NULL, NULL); "DevToolsAPI", "keyEventUnhandled", event_data);
return true; return true;
} }
...@@ -773,8 +773,7 @@ void DevToolsWindow::UpdateInspectedWebContents( ...@@ -773,8 +773,7 @@ void DevToolsWindow::UpdateInspectedWebContents(
std::make_unique<ObserverWithAccessor>(new_web_contents); std::make_unique<ObserverWithAccessor>(new_web_contents);
bindings_->AttachTo( bindings_->AttachTo(
content::DevToolsAgentHost::GetOrCreateFor(new_web_contents)); content::DevToolsAgentHost::GetOrCreateFor(new_web_contents));
bindings_->CallClientFunction("DevToolsAPI.reattachMainTarget", nullptr, bindings_->CallClientMethod("DevToolsAPI", "reattachMainTarget");
nullptr, nullptr);
} }
void DevToolsWindow::ScheduleShow(const DevToolsToggleAction& action) { void DevToolsWindow::ScheduleShow(const DevToolsToggleAction& action) {
...@@ -1427,8 +1426,7 @@ void DevToolsWindow::ColorPickedInEyeDropper(int r, int g, int b, int a) { ...@@ -1427,8 +1426,7 @@ void DevToolsWindow::ColorPickedInEyeDropper(int r, int g, int b, int a) {
color.SetInteger("g", g); color.SetInteger("g", g);
color.SetInteger("b", b); color.SetInteger("b", b);
color.SetInteger("a", a); color.SetInteger("a", a);
bindings_->CallClientFunction("DevToolsAPI.eyeDropperPickedColor", &color, bindings_->CallClientMethod("DevToolsAPI", "eyeDropperPickedColor", color);
nullptr, nullptr);
} }
void DevToolsWindow::InspectedContentsClosing() { void DevToolsWindow::InspectedContentsClosing() {
...@@ -1491,9 +1489,9 @@ void DevToolsWindow::OnLoadCompleted() { ...@@ -1491,9 +1489,9 @@ void DevToolsWindow::OnLoadCompleted() {
sessions::SessionTabHelper* session_tab_helper = sessions::SessionTabHelper* session_tab_helper =
sessions::SessionTabHelper::FromWebContents(inspected_web_contents); sessions::SessionTabHelper::FromWebContents(inspected_web_contents);
if (session_tab_helper) { if (session_tab_helper) {
base::Value tabId(session_tab_helper->session_id().id()); bindings_->CallClientMethod(
bindings_->CallClientFunction("DevToolsAPI.setInspectedTabId", "DevToolsAPI", "setInspectedTabId",
&tabId, NULL, NULL); base::Value(session_tab_helper->session_id().id()));
} }
} }
...@@ -1560,8 +1558,7 @@ BrowserWindow* DevToolsWindow::GetInspectedBrowserWindow() { ...@@ -1560,8 +1558,7 @@ BrowserWindow* DevToolsWindow::GetInspectedBrowserWindow() {
void DevToolsWindow::DoAction(const DevToolsToggleAction& action) { void DevToolsWindow::DoAction(const DevToolsToggleAction& action) {
switch (action.type()) { switch (action.type()) {
case DevToolsToggleAction::kInspect: case DevToolsToggleAction::kInspect:
bindings_->CallClientFunction("DevToolsAPI.enterInspectElementMode", NULL, bindings_->CallClientMethod("DevToolsAPI", "enterInspectElementMode");
NULL, NULL);
break; break;
case DevToolsToggleAction::kShowElementsPanel: case DevToolsToggleAction::kShowElementsPanel:
...@@ -1576,11 +1573,10 @@ void DevToolsWindow::DoAction(const DevToolsToggleAction& action) { ...@@ -1576,11 +1573,10 @@ void DevToolsWindow::DoAction(const DevToolsToggleAction& action) {
const DevToolsToggleAction::RevealParams* params = const DevToolsToggleAction::RevealParams* params =
action.params(); action.params();
CHECK(params); CHECK(params);
base::Value url_value(params->url); bindings_->CallClientMethod(
base::Value line_value(static_cast<int>(params->line_number)); "DevToolsAPI", "revealSourceLine", base::Value(params->url),
base::Value column_value(static_cast<int>(params->column_number)); base::Value(static_cast<int>(params->line_number)),
bindings_->CallClientFunction("DevToolsAPI.revealSourceLine", base::Value(static_cast<int>(params->column_number)));
&url_value, &line_value, &column_value);
break; break;
} }
default: default:
...@@ -1635,9 +1631,8 @@ bool DevToolsWindow::ReloadInspectedWebContents(bool bypass_cache) { ...@@ -1635,9 +1631,8 @@ bool DevToolsWindow::ReloadInspectedWebContents(bool bypass_cache) {
WebContents* wc = GetInspectedWebContents(); WebContents* wc = GetInspectedWebContents();
if (!wc || wc->GetCrashedStatus() != base::TERMINATION_STATUS_STILL_RUNNING) if (!wc || wc->GetCrashedStatus() != base::TERMINATION_STATUS_STILL_RUNNING)
return false; return false;
base::Value bypass_cache_value(bypass_cache); bindings_->CallClientMethod("DevToolsAPI", "reloadInspectedPage",
bindings_->CallClientFunction("DevToolsAPI.reloadInspectedPage", base::Value(bypass_cache));
&bypass_cache_value, nullptr, nullptr);
return true; return true;
} }
......
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