Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1239)

Unified Diff: content/browser/devtools/devtools_agent_host_impl.cc

Issue 2874613003: [DevTools] Support multiple clients in DevToolsAgentHost (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/devtools/devtools_agent_host_impl.h ('k') | content/browser/devtools/devtools_session.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/devtools/devtools_agent_host_impl.cc
diff --git a/content/browser/devtools/devtools_agent_host_impl.cc b/content/browser/devtools/devtools_agent_host_impl.cc
index 8684e11231424c008b2939a8b787c379c255fb42..9426477f789795c1922fe47861cb77c84104f797 100644
--- a/content/browser/devtools/devtools_agent_host_impl.cc
+++ b/content/browser/devtools/devtools_agent_host_impl.cc
@@ -118,7 +118,7 @@ scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker(
}
DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id)
- : id_(id), last_session_id_(0), session_(nullptr) {
+ : id_(id), last_session_id_(0) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
@@ -148,67 +148,79 @@ scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward(
return new ForwardingAgentHost(id, std::move(delegate));
}
-bool DevToolsAgentHostImpl::InnerAttachClient(DevToolsAgentHostClient* client,
- bool force) {
- if (session_ && !force)
- return false;
-
+void DevToolsAgentHostImpl::InnerAttachClient(DevToolsAgentHostClient* client) {
scoped_refptr<DevToolsAgentHostImpl> protect(this);
- if (session_)
- ForceDetach(true);
- DCHECK(!session_);
- session_ = new DevToolsSession(this, client, ++last_session_id_);
- sessions_.insert(std::unique_ptr<DevToolsSession>(session_));
- AttachSession(session_);
- NotifyAttached();
- return true;
+ DevToolsSession* session =
+ new DevToolsSession(this, client, ++last_session_id_);
+ session_by_client_[client].reset(session);
+ session_by_id_[session->session_id()] = session;
+ AttachSession(session);
+ if (session_by_client_.size() == 1)
+ NotifyAttached();
}
bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) {
- return InnerAttachClient(client, false);
+ if (!session_by_client_.empty())
+ return false;
+ InnerAttachClient(client);
+ return true;
}
void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) {
- InnerAttachClient(client, true);
+ scoped_refptr<DevToolsAgentHostImpl> protect(this);
+ if (!session_by_client_.empty())
+ ForceDetachAllClients(true);
+ DCHECK(session_by_client_.empty());
+ InnerAttachClient(client);
+}
+
+bool DevToolsAgentHostImpl::AttachMultiClient(DevToolsAgentHostClient* client) {
+ InnerAttachClient(client);
+ return true;
}
bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) {
- if (!session_ || session_->client() != client)
+ if (session_by_client_.find(client) == session_by_client_.end())
return false;
-
scoped_refptr<DevToolsAgentHostImpl> protect(this);
- InnerDetachClient();
+ InnerDetachClient(client);
return true;
}
bool DevToolsAgentHostImpl::DispatchProtocolMessage(
DevToolsAgentHostClient* client,
const std::string& message) {
- if (!session_ || session_->client() != client)
+ auto it = session_by_client_.find(client);
+ if (it == session_by_client_.end())
return false;
- return DispatchProtocolMessage(session_, message);
+ return DispatchProtocolMessage(it->second.get(), message);
}
-void DevToolsAgentHostImpl::InnerDetachClient() {
- int session_id = session_->session_id();
- session_ = nullptr;
- sessions_.clear();
+void DevToolsAgentHostImpl::InnerDetachClient(DevToolsAgentHostClient* client) {
+ auto it = session_by_client_.find(client);
+ DCHECK(it != session_by_client_.end());
+ int session_id = it->second->session_id();
+ session_by_client_.erase(it);
+ session_by_id_.erase(session_id);
DetachSession(session_id);
- io_context_.DiscardAllStreams();
- NotifyDetached();
+ if (session_by_client_.empty()) {
+ io_context_.DiscardAllStreams();
+ NotifyDetached();
+ }
}
bool DevToolsAgentHostImpl::IsAttached() {
- return !!session_;
+ return !session_by_client_.empty();
}
void DevToolsAgentHostImpl::InspectElement(
DevToolsAgentHostClient* client,
int x,
int y) {
- if (!session_ || session_->client() != client)
- return;
- InspectElement(session_, x, y);
+ auto it = session_by_client_.find(client);
+ if (it == session_by_client_.end())
+ return;
+ InspectElement(it->second.get(), x, y);
}
std::string DevToolsAgentHostImpl::GetId() {
@@ -258,14 +270,13 @@ bool DevToolsAgentHostImpl::Inspect() {
return false;
}
-void DevToolsAgentHostImpl::ForceDetach(bool replaced) {
- if (!session_)
- return;
+void DevToolsAgentHostImpl::ForceDetachAllClients(bool replaced) {
scoped_refptr<DevToolsAgentHostImpl> protect(this);
- // Clear |client_| before notifying it.
- DevToolsAgentHostClient* client = session_->client();
- InnerDetachClient();
- client->AgentHostClosed(this, replaced);
+ while (!session_by_client_.empty()) {
+ DevToolsAgentHostClient* client = session_by_client_.begin()->first;
+ InnerDetachClient(client);
+ client->AgentHostClosed(this, replaced);
+ }
}
void DevToolsAgentHostImpl::InspectElement(
@@ -284,7 +295,7 @@ void DevToolsAgentHost::DetachAllClients() {
Instances copy = g_instances.Get();
for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
DevToolsAgentHostImpl* agent_host = it->second;
- agent_host->ForceDetach(true);
+ agent_host->ForceDetachAllClients(true);
}
}
« no previous file with comments | « content/browser/devtools/devtools_agent_host_impl.h ('k') | content/browser/devtools/devtools_session.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698