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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/devtools/devtools_agent_host_impl.h" 5 #include "content/browser/devtools/devtools_agent_host_impl.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 SharedWorkerDevToolsManager::GetInstance() 111 SharedWorkerDevToolsManager::GetInstance()
112 ->GetDevToolsAgentHostForWorker(worker_process_id, 112 ->GetDevToolsAgentHostForWorker(worker_process_id,
113 worker_route_id)) { 113 worker_route_id)) {
114 return host; 114 return host;
115 } 115 }
116 return ServiceWorkerDevToolsManager::GetInstance() 116 return ServiceWorkerDevToolsManager::GetInstance()
117 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); 117 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id);
118 } 118 }
119 119
120 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id) 120 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id)
121 : id_(id), last_session_id_(0), session_(nullptr) { 121 : id_(id), last_session_id_(0) {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI); 122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
123 } 123 }
124 124
125 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { 125 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI); 126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 NotifyDestroyed(); 127 NotifyDestroyed();
128 } 128 }
129 129
130 // static 130 // static
131 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( 131 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId(
132 const std::string& id) { 132 const std::string& id) {
133 if (g_instances == NULL) 133 if (g_instances == NULL)
134 return NULL; 134 return NULL;
135 Instances::iterator it = g_instances.Get().find(id); 135 Instances::iterator it = g_instances.Get().find(id);
136 if (it == g_instances.Get().end()) 136 if (it == g_instances.Get().end())
137 return NULL; 137 return NULL;
138 return it->second; 138 return it->second;
139 } 139 }
140 140
141 // static 141 // static
142 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward( 142 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward(
143 const std::string& id, 143 const std::string& id,
144 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) { 144 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) {
145 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id); 145 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id);
146 if (result) 146 if (result)
147 return result; 147 return result;
148 return new ForwardingAgentHost(id, std::move(delegate)); 148 return new ForwardingAgentHost(id, std::move(delegate));
149 } 149 }
150 150
151 bool DevToolsAgentHostImpl::InnerAttachClient(DevToolsAgentHostClient* client, 151 void DevToolsAgentHostImpl::InnerAttachClient(DevToolsAgentHostClient* client) {
152 bool force) { 152 scoped_refptr<DevToolsAgentHostImpl> protect(this);
153 if (session_ && !force) 153 DevToolsSession* session =
154 new DevToolsSession(this, client, ++last_session_id_);
155 session_by_client_[client].reset(session);
156 session_by_id_[session->session_id()] = session;
157 AttachSession(session);
158 if (session_by_client_.size() == 1)
159 NotifyAttached();
160 }
161
162 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) {
163 if (!session_by_client_.empty())
154 return false; 164 return false;
155 165 InnerAttachClient(client);
156 scoped_refptr<DevToolsAgentHostImpl> protect(this);
157 if (session_)
158 ForceDetach(true);
159 DCHECK(!session_);
160 session_ = new DevToolsSession(this, client, ++last_session_id_);
161 sessions_.insert(std::unique_ptr<DevToolsSession>(session_));
162 AttachSession(session_);
163 NotifyAttached();
164 return true; 166 return true;
165 } 167 }
166 168
167 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { 169 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) {
168 return InnerAttachClient(client, false); 170 scoped_refptr<DevToolsAgentHostImpl> protect(this);
171 if (!session_by_client_.empty())
172 ForceDetachAllClients(true);
173 DCHECK(session_by_client_.empty());
174 InnerAttachClient(client);
169 } 175 }
170 176
171 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) { 177 bool DevToolsAgentHostImpl::AttachMultiClient(DevToolsAgentHostClient* client) {
172 InnerAttachClient(client, true); 178 InnerAttachClient(client);
179 return true;
173 } 180 }
174 181
175 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) { 182 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) {
176 if (!session_ || session_->client() != client) 183 if (session_by_client_.find(client) == session_by_client_.end())
177 return false; 184 return false;
178
179 scoped_refptr<DevToolsAgentHostImpl> protect(this); 185 scoped_refptr<DevToolsAgentHostImpl> protect(this);
180 InnerDetachClient(); 186 InnerDetachClient(client);
181 return true; 187 return true;
182 } 188 }
183 189
184 bool DevToolsAgentHostImpl::DispatchProtocolMessage( 190 bool DevToolsAgentHostImpl::DispatchProtocolMessage(
185 DevToolsAgentHostClient* client, 191 DevToolsAgentHostClient* client,
186 const std::string& message) { 192 const std::string& message) {
187 if (!session_ || session_->client() != client) 193 auto it = session_by_client_.find(client);
194 if (it == session_by_client_.end())
188 return false; 195 return false;
189 return DispatchProtocolMessage(session_, message); 196 return DispatchProtocolMessage(it->second.get(), message);
190 } 197 }
191 198
192 void DevToolsAgentHostImpl::InnerDetachClient() { 199 void DevToolsAgentHostImpl::InnerDetachClient(DevToolsAgentHostClient* client) {
193 int session_id = session_->session_id(); 200 auto it = session_by_client_.find(client);
194 session_ = nullptr; 201 DCHECK(it != session_by_client_.end());
195 sessions_.clear(); 202 int session_id = it->second->session_id();
203 session_by_client_.erase(it);
204 session_by_id_.erase(session_id);
196 DetachSession(session_id); 205 DetachSession(session_id);
197 io_context_.DiscardAllStreams(); 206 if (session_by_client_.empty()) {
198 NotifyDetached(); 207 io_context_.DiscardAllStreams();
208 NotifyDetached();
209 }
199 } 210 }
200 211
201 bool DevToolsAgentHostImpl::IsAttached() { 212 bool DevToolsAgentHostImpl::IsAttached() {
202 return !!session_; 213 return !session_by_client_.empty();
203 } 214 }
204 215
205 void DevToolsAgentHostImpl::InspectElement( 216 void DevToolsAgentHostImpl::InspectElement(
206 DevToolsAgentHostClient* client, 217 DevToolsAgentHostClient* client,
207 int x, 218 int x,
208 int y) { 219 int y) {
209 if (!session_ || session_->client() != client) 220 auto it = session_by_client_.find(client);
210 return; 221 if (it == session_by_client_.end())
211 InspectElement(session_, x, y); 222 return;
223 InspectElement(it->second.get(), x, y);
212 } 224 }
213 225
214 std::string DevToolsAgentHostImpl::GetId() { 226 std::string DevToolsAgentHostImpl::GetId() {
215 return id_; 227 return id_;
216 } 228 }
217 229
218 std::string DevToolsAgentHostImpl::GetParentId() { 230 std::string DevToolsAgentHostImpl::GetParentId() {
219 return ""; 231 return "";
220 } 232 }
221 233
(...skipping 29 matching lines...) Expand all
251 263
252 bool DevToolsAgentHostImpl::Inspect() { 264 bool DevToolsAgentHostImpl::Inspect() {
253 DevToolsManager* manager = DevToolsManager::GetInstance(); 265 DevToolsManager* manager = DevToolsManager::GetInstance();
254 if (manager->delegate()) { 266 if (manager->delegate()) {
255 manager->delegate()->Inspect(this); 267 manager->delegate()->Inspect(this);
256 return true; 268 return true;
257 } 269 }
258 return false; 270 return false;
259 } 271 }
260 272
261 void DevToolsAgentHostImpl::ForceDetach(bool replaced) { 273 void DevToolsAgentHostImpl::ForceDetachAllClients(bool replaced) {
262 if (!session_)
263 return;
264 scoped_refptr<DevToolsAgentHostImpl> protect(this); 274 scoped_refptr<DevToolsAgentHostImpl> protect(this);
265 // Clear |client_| before notifying it. 275 while (!session_by_client_.empty()) {
266 DevToolsAgentHostClient* client = session_->client(); 276 DevToolsAgentHostClient* client = session_by_client_.begin()->first;
267 InnerDetachClient(); 277 InnerDetachClient(client);
268 client->AgentHostClosed(this, replaced); 278 client->AgentHostClosed(this, replaced);
279 }
269 } 280 }
270 281
271 void DevToolsAgentHostImpl::InspectElement( 282 void DevToolsAgentHostImpl::InspectElement(
272 DevToolsSession* session, 283 DevToolsSession* session,
273 int x, 284 int x,
274 int y) { 285 int y) {
275 } 286 }
276 287
277 // static 288 // static
278 void DevToolsAgentHost::DetachAllClients() { 289 void DevToolsAgentHost::DetachAllClients() {
279 if (g_instances == NULL) 290 if (g_instances == NULL)
280 return; 291 return;
281 292
282 // Make a copy, since detaching may lead to agent destruction, which 293 // Make a copy, since detaching may lead to agent destruction, which
283 // removes it from the instances. 294 // removes it from the instances.
284 Instances copy = g_instances.Get(); 295 Instances copy = g_instances.Get();
285 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { 296 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
286 DevToolsAgentHostImpl* agent_host = it->second; 297 DevToolsAgentHostImpl* agent_host = it->second;
287 agent_host->ForceDetach(true); 298 agent_host->ForceDetachAllClients(true);
288 } 299 }
289 } 300 }
290 301
291 // static 302 // static
292 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { 303 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) {
293 if (observer->ShouldForceDevToolsAgentHostCreation()) { 304 if (observer->ShouldForceDevToolsAgentHostCreation()) {
294 if (!DevToolsAgentHostImpl::s_force_creation_count_) { 305 if (!DevToolsAgentHostImpl::s_force_creation_count_) {
295 // Force all agent hosts when first observer is added. 306 // Force all agent hosts when first observer is added.
296 DevToolsAgentHost::GetOrCreateAll(); 307 DevToolsAgentHost::GetOrCreateAll();
297 } 308 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 if (message_buffer_.size() != message_buffer_size_) 407 if (message_buffer_.size() != message_buffer_size_)
397 return false; 408 return false;
398 callback_.Run(chunk.session_id, message_buffer_); 409 callback_.Run(chunk.session_id, message_buffer_);
399 message_buffer_ = std::string(); 410 message_buffer_ = std::string();
400 message_buffer_size_ = 0; 411 message_buffer_size_ = 0;
401 } 412 }
402 return true; 413 return true;
403 } 414 }
404 415
405 } // namespace content 416 } // namespace content
OLDNEW
« 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