Commit decbccdb authored by Andrey Lushnikov's avatar Andrey Lushnikov Committed by Commit Bot

DevTools: pass parsed message as unique_ptr instead of raw pointer

This will allow devtools_session to own parsed message and
bind it as a part of a callback.

R=dgozman

Bug: 817413
Change-Id: I48dc7a12741902603e22233cdd26b6bb97ecf453
Reviewed-on: https://chromium-review.googlesource.com/1145954
Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577328}
parent ee2be2e0
......@@ -111,14 +111,15 @@ void BrowserDevToolsAgentHost::Reload() {
bool BrowserDevToolsAgentHost::DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message,
base::DictionaryValue* parsed_message) {
std::unique_ptr<base::DictionaryValue> parsed_message) {
auto it = target_registries_.find(client);
if (it != target_registries_.end() &&
it->second->DispatchMessageOnAgentHost(message, parsed_message)) {
it->second->CanDispatchMessageOnAgentHost(parsed_message.get())) {
it->second->DispatchMessageOnAgentHost(message, std::move(parsed_message));
return true;
}
return DevToolsAgentHostImpl::DispatchProtocolMessage(client, message,
parsed_message);
return DevToolsAgentHostImpl::DispatchProtocolMessage(
client, message, std::move(parsed_message));
}
} // content
......@@ -25,9 +25,10 @@ class BrowserDevToolsAgentHost : public DevToolsAgentHostImpl {
bool AttachSession(DevToolsSession* session,
TargetRegistry* registry) override;
void DetachSession(DevToolsSession* session) override;
bool DispatchProtocolMessage(DevToolsAgentHostClient* client,
const std::string& message,
base::DictionaryValue* parsed_message) override;
bool DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message) override;
// DevToolsAgentHost implementation.
std::string GetType() override;
......
......@@ -231,20 +231,18 @@ bool DevToolsAgentHostImpl::DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message) {
std::unique_ptr<base::Value> value = base::JSONReader::Read(message);
if (value && !value->is_dict())
value.reset();
return DispatchProtocolMessage(
client, message, static_cast<base::DictionaryValue*>(value.get()));
return DispatchProtocolMessage(client, message,
base::DictionaryValue::From(std::move(value)));
}
bool DevToolsAgentHostImpl::DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message,
base::DictionaryValue* parsed_message) {
std::unique_ptr<base::DictionaryValue> parsed_message) {
DevToolsSession* session = SessionByClient(client);
if (!session)
return false;
session->DispatchProtocolMessage(message, parsed_message);
session->DispatchProtocolMessage(message, std::move(parsed_message));
return true;
}
......
......@@ -70,9 +70,10 @@ class CONTENT_EXPORT DevToolsAgentHostImpl : public DevToolsAgentHost {
TargetRegistry* registry);
virtual void DetachSession(DevToolsSession* session);
virtual bool DispatchProtocolMessage(DevToolsAgentHostClient* client,
const std::string& message,
base::DictionaryValue* parsed_message);
virtual bool DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void NotifyCreated();
void NotifyNavigated();
......
......@@ -125,19 +125,19 @@ void DevToolsSession::MojoConnectionDestroyed() {
void DevToolsSession::DispatchProtocolMessage(
const std::string& message,
base::DictionaryValue* parsed_message) {
std::unique_ptr<base::DictionaryValue> parsed_message) {
DevToolsManagerDelegate* delegate =
DevToolsManager::GetInstance()->delegate();
if (delegate && parsed_message &&
delegate->HandleCommand(agent_host_, client_, parsed_message)) {
delegate->HandleCommand(agent_host_, client_, parsed_message.get())) {
return;
}
int call_id;
std::string method;
if (dispatcher_->dispatch(protocol::toProtocolValue(parsed_message, 1000),
&call_id,
&method) != protocol::Response::kFallThrough) {
if (dispatcher_->dispatch(
protocol::toProtocolValue(parsed_message.get(), 1000), &call_id,
&method) != protocol::Response::kFallThrough) {
return;
}
......
......@@ -43,8 +43,9 @@ class DevToolsSession : public protocol::FrontendChannel,
void SetRenderer(int process_host_id, RenderFrameHostImpl* frame_host);
void AttachToAgent(const blink::mojom::DevToolsAgentAssociatedPtr& agent);
void DispatchProtocolMessage(const std::string& message,
base::DictionaryValue* parsed_message);
void DispatchProtocolMessage(
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void SuspendSendingMessagesToAgent();
void ResumeSendingMessagesToAgent();
......
......@@ -24,20 +24,28 @@ void TargetRegistry::DetachSubtargetSession(const std::string& session_id) {
sessions_.erase(session_id);
}
bool TargetRegistry::DispatchMessageOnAgentHost(
const std::string& message,
bool TargetRegistry::CanDispatchMessageOnAgentHost(
base::DictionaryValue* parsed_message) {
return parsed_message->FindKeyOfType("sessionId",
base::DictionaryValue::Type::STRING);
}
void TargetRegistry::DispatchMessageOnAgentHost(
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message) {
std::string session_id;
if (!parsed_message->GetString("sessionId", &session_id))
return false;
bool result = parsed_message->GetString("sessionId", &session_id);
DCHECK(result);
auto it = sessions_.find(session_id);
if (it == sessions_.end()) {
LOG(ERROR) << "Unknown session " << session_id;
return true;
return;
}
scoped_refptr<DevToolsAgentHostImpl> agent_host = it->second.first;
DevToolsAgentHostClient* client = it->second.second;
return agent_host->DispatchProtocolMessage(client, message, parsed_message);
agent_host->DispatchProtocolMessage(client, message,
std::move(parsed_message));
}
void TargetRegistry::SendMessageToClient(const std::string& session_id,
......
......@@ -25,8 +25,10 @@ class TargetRegistry {
DevToolsAgentHostImpl* agent_host,
DevToolsAgentHostClient* client);
void DetachSubtargetSession(const std::string& session_id);
bool DispatchMessageOnAgentHost(const std::string& message,
base::DictionaryValue* parsed_message);
bool CanDispatchMessageOnAgentHost(base::DictionaryValue* parsed_message);
void DispatchMessageOnAgentHost(
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void SendMessageToClient(const std::string& session_id,
const std::string& message);
......
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