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() { ...@@ -111,14 +111,15 @@ void BrowserDevToolsAgentHost::Reload() {
bool BrowserDevToolsAgentHost::DispatchProtocolMessage( bool BrowserDevToolsAgentHost::DispatchProtocolMessage(
DevToolsAgentHostClient* client, DevToolsAgentHostClient* client,
const std::string& message, const std::string& message,
base::DictionaryValue* parsed_message) { std::unique_ptr<base::DictionaryValue> parsed_message) {
auto it = target_registries_.find(client); auto it = target_registries_.find(client);
if (it != target_registries_.end() && 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 true;
} }
return DevToolsAgentHostImpl::DispatchProtocolMessage(client, message, return DevToolsAgentHostImpl::DispatchProtocolMessage(
parsed_message); client, message, std::move(parsed_message));
} }
} // content } // content
...@@ -25,9 +25,10 @@ class BrowserDevToolsAgentHost : public DevToolsAgentHostImpl { ...@@ -25,9 +25,10 @@ class BrowserDevToolsAgentHost : public DevToolsAgentHostImpl {
bool AttachSession(DevToolsSession* session, bool AttachSession(DevToolsSession* session,
TargetRegistry* registry) override; TargetRegistry* registry) override;
void DetachSession(DevToolsSession* session) override; void DetachSession(DevToolsSession* session) override;
bool DispatchProtocolMessage(DevToolsAgentHostClient* client, bool DispatchProtocolMessage(
const std::string& message, DevToolsAgentHostClient* client,
base::DictionaryValue* parsed_message) override; const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message) override;
// DevToolsAgentHost implementation. // DevToolsAgentHost implementation.
std::string GetType() override; std::string GetType() override;
......
...@@ -231,20 +231,18 @@ bool DevToolsAgentHostImpl::DispatchProtocolMessage( ...@@ -231,20 +231,18 @@ bool DevToolsAgentHostImpl::DispatchProtocolMessage(
DevToolsAgentHostClient* client, DevToolsAgentHostClient* client,
const std::string& message) { const std::string& message) {
std::unique_ptr<base::Value> value = base::JSONReader::Read(message); std::unique_ptr<base::Value> value = base::JSONReader::Read(message);
if (value && !value->is_dict()) return DispatchProtocolMessage(client, message,
value.reset(); base::DictionaryValue::From(std::move(value)));
return DispatchProtocolMessage(
client, message, static_cast<base::DictionaryValue*>(value.get()));
} }
bool DevToolsAgentHostImpl::DispatchProtocolMessage( bool DevToolsAgentHostImpl::DispatchProtocolMessage(
DevToolsAgentHostClient* client, DevToolsAgentHostClient* client,
const std::string& message, const std::string& message,
base::DictionaryValue* parsed_message) { std::unique_ptr<base::DictionaryValue> parsed_message) {
DevToolsSession* session = SessionByClient(client); DevToolsSession* session = SessionByClient(client);
if (!session) if (!session)
return false; return false;
session->DispatchProtocolMessage(message, parsed_message); session->DispatchProtocolMessage(message, std::move(parsed_message));
return true; return true;
} }
......
...@@ -70,9 +70,10 @@ class CONTENT_EXPORT DevToolsAgentHostImpl : public DevToolsAgentHost { ...@@ -70,9 +70,10 @@ class CONTENT_EXPORT DevToolsAgentHostImpl : public DevToolsAgentHost {
TargetRegistry* registry); TargetRegistry* registry);
virtual void DetachSession(DevToolsSession* session); virtual void DetachSession(DevToolsSession* session);
virtual bool DispatchProtocolMessage(DevToolsAgentHostClient* client, virtual bool DispatchProtocolMessage(
const std::string& message, DevToolsAgentHostClient* client,
base::DictionaryValue* parsed_message); const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void NotifyCreated(); void NotifyCreated();
void NotifyNavigated(); void NotifyNavigated();
......
...@@ -125,19 +125,19 @@ void DevToolsSession::MojoConnectionDestroyed() { ...@@ -125,19 +125,19 @@ void DevToolsSession::MojoConnectionDestroyed() {
void DevToolsSession::DispatchProtocolMessage( void DevToolsSession::DispatchProtocolMessage(
const std::string& message, const std::string& message,
base::DictionaryValue* parsed_message) { std::unique_ptr<base::DictionaryValue> parsed_message) {
DevToolsManagerDelegate* delegate = DevToolsManagerDelegate* delegate =
DevToolsManager::GetInstance()->delegate(); DevToolsManager::GetInstance()->delegate();
if (delegate && parsed_message && if (delegate && parsed_message &&
delegate->HandleCommand(agent_host_, client_, parsed_message)) { delegate->HandleCommand(agent_host_, client_, parsed_message.get())) {
return; return;
} }
int call_id; int call_id;
std::string method; std::string method;
if (dispatcher_->dispatch(protocol::toProtocolValue(parsed_message, 1000), if (dispatcher_->dispatch(
&call_id, protocol::toProtocolValue(parsed_message.get(), 1000), &call_id,
&method) != protocol::Response::kFallThrough) { &method) != protocol::Response::kFallThrough) {
return; return;
} }
......
...@@ -43,8 +43,9 @@ class DevToolsSession : public protocol::FrontendChannel, ...@@ -43,8 +43,9 @@ class DevToolsSession : public protocol::FrontendChannel,
void SetRenderer(int process_host_id, RenderFrameHostImpl* frame_host); void SetRenderer(int process_host_id, RenderFrameHostImpl* frame_host);
void AttachToAgent(const blink::mojom::DevToolsAgentAssociatedPtr& agent); void AttachToAgent(const blink::mojom::DevToolsAgentAssociatedPtr& agent);
void DispatchProtocolMessage(const std::string& message, void DispatchProtocolMessage(
base::DictionaryValue* parsed_message); const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void SuspendSendingMessagesToAgent(); void SuspendSendingMessagesToAgent();
void ResumeSendingMessagesToAgent(); void ResumeSendingMessagesToAgent();
......
...@@ -24,20 +24,28 @@ void TargetRegistry::DetachSubtargetSession(const std::string& session_id) { ...@@ -24,20 +24,28 @@ void TargetRegistry::DetachSubtargetSession(const std::string& session_id) {
sessions_.erase(session_id); sessions_.erase(session_id);
} }
bool TargetRegistry::DispatchMessageOnAgentHost( bool TargetRegistry::CanDispatchMessageOnAgentHost(
const std::string& message,
base::DictionaryValue* parsed_message) { 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; std::string session_id;
if (!parsed_message->GetString("sessionId", &session_id)) bool result = parsed_message->GetString("sessionId", &session_id);
return false; DCHECK(result);
auto it = sessions_.find(session_id); auto it = sessions_.find(session_id);
if (it == sessions_.end()) { if (it == sessions_.end()) {
LOG(ERROR) << "Unknown session " << session_id; LOG(ERROR) << "Unknown session " << session_id;
return true; return;
} }
scoped_refptr<DevToolsAgentHostImpl> agent_host = it->second.first; scoped_refptr<DevToolsAgentHostImpl> agent_host = it->second.first;
DevToolsAgentHostClient* client = it->second.second; 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, void TargetRegistry::SendMessageToClient(const std::string& session_id,
......
...@@ -25,8 +25,10 @@ class TargetRegistry { ...@@ -25,8 +25,10 @@ class TargetRegistry {
DevToolsAgentHostImpl* agent_host, DevToolsAgentHostImpl* agent_host,
DevToolsAgentHostClient* client); DevToolsAgentHostClient* client);
void DetachSubtargetSession(const std::string& session_id); void DetachSubtargetSession(const std::string& session_id);
bool DispatchMessageOnAgentHost(const std::string& message, bool CanDispatchMessageOnAgentHost(base::DictionaryValue* parsed_message);
base::DictionaryValue* parsed_message); void DispatchMessageOnAgentHost(
const std::string& message,
std::unique_ptr<base::DictionaryValue> parsed_message);
void SendMessageToClient(const std::string& session_id, void SendMessageToClient(const std::string& session_id,
const std::string& message); 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