Skip to content

Commit

Permalink
devtools: Change how commands are handled by the delegate.
Browse files Browse the repository at this point in the history
The DevToolsManagerDelegate is now able to send the protocol response
itself when it needs to (through the DevToolsAgentHost). So have the
delegate do that, instead of sending the response through the
DevToolsSession.

This change makes it possible, in follow up CLs, to use UberDispatcher
to handle the commands and send the response back using FrontendChannel.

BUG=758061
TBR=eseckler@ for //headless, for trivial update to devtools API change.

Change-Id: Ib0b521a24c4f4d26f8cd0fe3a6995141acd630ef
Reviewed-on: https://chromium-review.googlesource.com/677954
Commit-Queue: Sadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#503654}
  • Loading branch information
sadrulhc authored and Commit Bot committed Sep 22, 2017
1 parent e186d66 commit d3a4522
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 39 deletions.
49 changes: 37 additions & 12 deletions chrome/browser/devtools/chrome_devtools_manager_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <utility>

#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -118,6 +119,12 @@ void ToggleAdBlocking(bool enabled, content::DevToolsAgentHost* agent_host) {
}
}

std::string ToString(std::unique_ptr<base::DictionaryValue> value) {
std::string json;
base::JSONWriter::Write(*value, &json);
return json;
}

} // namespace

// static
Expand Down Expand Up @@ -347,34 +354,52 @@ void ChromeDevToolsManagerDelegate::Inspect(
DevToolsWindow::OpenDevToolsWindow(agent_host, nullptr);
}

base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand(
bool ChromeDevToolsManagerDelegate::HandleCommand(
DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command_dict) {
int id = 0;
std::string method;
base::DictionaryValue* params = nullptr;
if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, &params))
return nullptr;
return false;

// Do not actually handle the enable/disable commands, just keep track of the
// enable state.
if (method == chrome::devtools::Page::enable::kName)
if (method == chrome::devtools::Page::enable::kName) {
TogglePageEnable(true /* enable */, agent_host);
if (method == chrome::devtools::Page::disable::kName)
return false;
}

if (method == chrome::devtools::Page::disable::kName) {
TogglePageEnable(false /* enable */, agent_host);
return false;
}

auto* result = HandleBrowserCommand(id, method, params).release();
if (result)
return result;
auto result = HandleBrowserCommand(id, method, params);
if (result) {
agent_host->SendProtocolMessageToClient(session_id,
ToString(std::move(result)));
return true;
}

if (method == chrome::devtools::Page::setAdBlockingEnabled::kName)
return SetAdBlockingEnabled(agent_host, id, params).release();
if (method == chrome::devtools::Page::setAdBlockingEnabled::kName) {
result = SetAdBlockingEnabled(agent_host, id, params);
DCHECK(result);
agent_host->SendProtocolMessageToClient(session_id,
ToString(std::move(result)));
return true;
}

if (method == chrome::devtools::Target::setRemoteLocations::kName)
return SetRemoteLocations(agent_host, id, params).release();
if (method == chrome::devtools::Target::setRemoteLocations::kName) {
result = SetRemoteLocations(agent_host, id, params);
DCHECK(result);
agent_host->SendProtocolMessageToClient(session_id,
ToString(std::move(result)));
return true;
}

return nullptr;
return false;
}

std::string ChromeDevToolsManagerDelegate::GetTargetType(
Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/devtools/chrome_devtools_manager_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ class ChromeDevToolsManagerDelegate :

// content::DevToolsManagerDelegate implementation.
void Inspect(content::DevToolsAgentHost* agent_host) override;
base::DictionaryValue* HandleCommand(
content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command_dict) override;
bool HandleCommand(content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command_dict) override;
std::string GetTargetType(content::WebContents* web_contents) override;
std::string GetTargetTitle(content::WebContents* web_contents) override;
scoped_refptr<content::DevToolsAgentHost> CreateNewTarget(
Expand Down
8 changes: 3 additions & 5 deletions content/browser/devtools/devtools_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ protocol::Response::Status DevToolsSession::Dispatch(
if (value && value->IsType(base::Value::Type::DICTIONARY) && delegate) {
base::DictionaryValue* dict_value =
static_cast<base::DictionaryValue*>(value.get());
std::unique_ptr<base::DictionaryValue> response(
delegate->HandleCommand(agent_host_, session_id_, dict_value));
if (response) {
SendResponse(std::move(response));

if (delegate->HandleCommand(agent_host_, session_id_, dict_value))
return protocol::Response::kSuccess;
}

if (delegate->HandleAsyncCommand(agent_host_, session_id_, dict_value,
base::Bind(&DevToolsSession::SendResponse,
weak_factory_.GetWeakPtr()))) {
Expand Down
9 changes: 4 additions & 5 deletions content/public/browser/devtools_manager_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ void DevToolsManagerDelegate::SessionDestroyed(
content::DevToolsAgentHost* agent_host,
int session_id) {}

base::DictionaryValue* DevToolsManagerDelegate::HandleCommand(
DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command) {
return nullptr;
bool DevToolsManagerDelegate::HandleCommand(DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command) {
return false;
}

bool DevToolsManagerDelegate::HandleAsyncCommand(
Expand Down
8 changes: 4 additions & 4 deletions content/public/browser/devtools_manager_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class CONTENT_EXPORT DevToolsManagerDelegate {
virtual void SessionDestroyed(content::DevToolsAgentHost* agent_host,
int session_id);

// Result ownership is passed to the caller.
virtual base::DictionaryValue* HandleCommand(DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command);
// Returns true if the command has been handled, false otherwise.
virtual bool HandleCommand(DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command);

using CommandCallback =
base::Callback<void(std::unique_ptr<base::DictionaryValue> response)>;
Expand Down
23 changes: 17 additions & 6 deletions headless/lib/browser/headless_devtools_manager_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <utility>

#include "base/json/json_writer.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h"
Expand Down Expand Up @@ -100,6 +101,12 @@ void PDFCreated(
}
#endif

std::string ToString(std::unique_ptr<base::DictionaryValue> value) {
std::string json;
base::JSONWriter::Write(*value, &json);
return json;
}

} // namespace

#if BUILDFLAG(ENABLE_BASIC_PRINTING)
Expand Down Expand Up @@ -199,33 +206,37 @@ HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate(

HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {}

base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand(
bool HeadlessDevToolsManagerDelegate::HandleCommand(
content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

if (!browser_)
return nullptr;
return false;

int id;
std::string method;
if (!command->GetInteger("id", &id) || !command->GetString("method", &method))
return nullptr;
return false;

auto find_it = command_map_.find(method);
if (find_it == command_map_.end())
return nullptr;
return false;

// Handle Browser domain commands only from Browser DevToolsAgentHost.
if (method.find("Browser.") == 0 &&
agent_host->GetType() != content::DevToolsAgentHost::kTypeBrowser)
return nullptr;
return false;

const base::DictionaryValue* params = nullptr;
command->GetDictionary("params", &params);
auto cmd_result = find_it->second.Run(id, params);
return cmd_result.release();
if (!cmd_result)
return false;
agent_host->SendProtocolMessageToClient(session_id,
ToString(std::move(cmd_result)));
return true;
}

bool HeadlessDevToolsManagerDelegate::HandleAsyncCommand(
Expand Down
6 changes: 3 additions & 3 deletions headless/lib/browser/headless_devtools_manager_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class HeadlessDevToolsManagerDelegate
~HeadlessDevToolsManagerDelegate() override;

// DevToolsManagerDelegate implementation:
base::DictionaryValue* HandleCommand(content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command) override;
bool HandleCommand(content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command) override;
bool HandleAsyncCommand(content::DevToolsAgentHost* agent_host,
int session_id,
base::DictionaryValue* command,
Expand Down

0 comments on commit d3a4522

Please sign in to comment.