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

Side by Side Diff: content/browser/devtools/devtools_agent_host_impl.cc

Issue 2590293003: [DevTools] Rework DevToolsSession interaction with domain handlers. (Closed)
Patch Set: Created 4 years 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 ->GetDevToolsAgentHostForWorker(worker_process_id, 99 ->GetDevToolsAgentHostForWorker(worker_process_id,
100 worker_route_id)) { 100 worker_route_id)) {
101 return host; 101 return host;
102 } 102 }
103 return ServiceWorkerDevToolsManager::GetInstance() 103 return ServiceWorkerDevToolsManager::GetInstance()
104 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); 104 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id);
105 } 105 }
106 106
107 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id) 107 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id)
108 : id_(id), 108 : id_(id),
109 last_session_id_(0), 109 last_session_id_(0) {
110 client_(NULL) {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI); 110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
112 } 111 }
113 112
114 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { 113 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() {
115 DCHECK_CURRENTLY_ON(BrowserThread::UI); 114 DCHECK_CURRENTLY_ON(BrowserThread::UI);
116 NotifyDestroyed(); 115 NotifyDestroyed();
117 } 116 }
118 117
119 // static 118 // static
120 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( 119 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId(
121 const std::string& id) { 120 const std::string& id) {
122 if (g_instances == NULL) 121 if (g_instances == NULL)
123 return NULL; 122 return NULL;
124 Instances::iterator it = g_instances.Get().find(id); 123 Instances::iterator it = g_instances.Get().find(id);
125 if (it == g_instances.Get().end()) 124 if (it == g_instances.Get().end())
126 return NULL; 125 return NULL;
127 return it->second; 126 return it->second;
128 } 127 }
129 128
130 // static 129 // static
131 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward( 130 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward(
132 const std::string& id, 131 const std::string& id,
133 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) { 132 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) {
134 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id); 133 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id);
135 if (result) 134 if (result)
136 return result; 135 return result;
137 return new ForwardingAgentHost(id, std::move(delegate)); 136 return new ForwardingAgentHost(id, std::move(delegate));
138 } 137 }
139 138
140 bool DevToolsAgentHostImpl::InnerAttach(DevToolsAgentHostClient* client, 139 bool DevToolsAgentHostImpl::InnerAttach(DevToolsAgentHostClient* client,
caseq 2016/12/20 23:13:19 Let's rename to InnerAttachClient
dgozman 2016/12/21 00:29:40 Done.
141 bool force) { 140 bool force) {
142 if (client_ && !force) 141 if (session_ && !force)
143 return false; 142 return false;
144 143
145 scoped_refptr<DevToolsAgentHostImpl> protect(this); 144 scoped_refptr<DevToolsAgentHostImpl> protect(this);
146 if (client_) { 145 if (session_) {
147 client_->AgentHostClosed(this, true); 146 session_->client()->AgentHostClosed(this, true);
148 InnerDetach(); 147 InnerDetach();
149 } 148 }
150 client_ = client; 149 session_.reset(new DevToolsSession(this, client, ++last_session_id_));
151 session_.reset(new DevToolsSession(this, ++last_session_id_)); 150 Attach(session_.get());
caseq 2016/12/20 23:13:19 AttachSession?
dgozman 2016/12/21 00:29:40 Done.
152 Attach(); 151 session_->Attach();
153 NotifyAttached(); 152 NotifyAttached();
154 return true; 153 return true;
155 } 154 }
156 155
157 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { 156 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) {
158 return InnerAttach(client, false); 157 return InnerAttach(client, false);
159 } 158 }
160 159
161 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) { 160 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) {
162 InnerAttach(client, true); 161 InnerAttach(client, true);
163 } 162 }
164 163
165 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) { 164 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) {
166 if (!client_ || client_ != client) 165 if (!session_ || session_->client() != client)
167 return false; 166 return false;
168 167
169 scoped_refptr<DevToolsAgentHostImpl> protect(this); 168 scoped_refptr<DevToolsAgentHostImpl> protect(this);
170 client_ = NULL;
171 InnerDetach(); 169 InnerDetach();
172 return true; 170 return true;
173 } 171 }
174 172
175 bool DevToolsAgentHostImpl::DispatchProtocolMessage( 173 bool DevToolsAgentHostImpl::DispatchProtocolMessage(
176 DevToolsAgentHostClient* client, 174 DevToolsAgentHostClient* client,
177 const std::string& message) { 175 const std::string& message) {
178 if (!client_ || client_ != client) 176 if (!session_ || session_->client() != client)
179 return false; 177 return false;
180 return DispatchProtocolMessage(message); 178 return DispatchProtocolMessage(session_.get(), message);
181 } 179 }
182 180
183 void DevToolsAgentHostImpl::InnerDetach() { 181 void DevToolsAgentHostImpl::InnerDetach() {
184 session_->ResetDispatcher(); 182 session_->Detach();
185 Detach(); 183 Detach(session_.get());
186 io_context_.DiscardAllStreams(); 184 io_context_.DiscardAllStreams();
187 session_.reset(); 185 session_.reset();
188 NotifyDetached(); 186 NotifyDetached();
189 } 187 }
190 188
191 bool DevToolsAgentHostImpl::IsAttached() { 189 bool DevToolsAgentHostImpl::IsAttached() {
192 return !!client_; 190 return !!session_;
193 } 191 }
194 192
195 void DevToolsAgentHostImpl::InspectElement( 193 void DevToolsAgentHostImpl::InspectElement(
196 DevToolsAgentHostClient* client, 194 DevToolsAgentHostClient* client,
197 int x, 195 int x,
198 int y) { 196 int y) {
199 if (!client_ || client_ != client) 197 if (!session_ || session_->client() != client)
200 return; 198 return;
201 InspectElement(x, y); 199 InspectElement(session_.get(), x, y);
202 } 200 }
203 201
204 std::string DevToolsAgentHostImpl::GetId() { 202 std::string DevToolsAgentHostImpl::GetId() {
205 return id_; 203 return id_;
206 } 204 }
207 205
208 std::string DevToolsAgentHostImpl::GetParentId() { 206 std::string DevToolsAgentHostImpl::GetParentId() {
209 return ""; 207 return "";
210 } 208 }
211 209
(...skipping 30 matching lines...) Expand all
242 bool DevToolsAgentHostImpl::Inspect() { 240 bool DevToolsAgentHostImpl::Inspect() {
243 DevToolsManager* manager = DevToolsManager::GetInstance(); 241 DevToolsManager* manager = DevToolsManager::GetInstance();
244 if (manager->delegate()) { 242 if (manager->delegate()) {
245 manager->delegate()->Inspect(this); 243 manager->delegate()->Inspect(this);
246 return true; 244 return true;
247 } 245 }
248 return false; 246 return false;
249 } 247 }
250 248
251 void DevToolsAgentHostImpl::HostClosed() { 249 void DevToolsAgentHostImpl::HostClosed() {
252 if (!client_) 250 if (!session_)
253 return; 251 return;
254 252
255 scoped_refptr<DevToolsAgentHostImpl> protect(this); 253 scoped_refptr<DevToolsAgentHostImpl> protect(this);
256 // Clear |client_| before notifying it. 254 // Clear |client_| before notifying it.
257 DevToolsAgentHostClient* client = client_; 255 DevToolsAgentHostClient* client = session_->client();
258 client_ = NULL; 256 InnerDetach();
259 client->AgentHostClosed(this, false); 257 client->AgentHostClosed(this, false);
260 InnerDetach();
261 } 258 }
262 259
263 void DevToolsAgentHostImpl::InspectElement(int x, int y) { 260 void DevToolsAgentHostImpl::InspectElement(
261 DevToolsSession* session,
262 int x,
263 int y) {
264 } 264 }
265 265
266 void DevToolsAgentHostImpl::SendMessageToClient(int session_id, 266 void DevToolsAgentHostImpl::SendMessageToClient(int session_id,
267 const std::string& message) { 267 const std::string& message) {
268 if (!client_)
269 return;
270 // Filter any messages from previous sessions. 268 // Filter any messages from previous sessions.
271 if (!session_ || session_id != session_->session_id()) 269 if (!session_ || session_id != session_->session_id())
272 return; 270 return;
273 client_->DispatchProtocolMessage(this, message); 271 session_->client()->DispatchProtocolMessage(this, message);
274 } 272 }
275 273
276 // static 274 // static
277 void DevToolsAgentHost::DetachAllClients() { 275 void DevToolsAgentHost::DetachAllClients() {
278 if (g_instances == NULL) 276 if (g_instances == NULL)
279 return; 277 return;
280 278
281 // Make a copy, since detaching may lead to agent destruction, which 279 // Make a copy, since detaching may lead to agent destruction, which
282 // removes it from the instances. 280 // removes it from the instances.
283 Instances copy = g_instances.Get(); 281 Instances copy = g_instances.Get();
284 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { 282 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
285 DevToolsAgentHostImpl* agent_host = it->second; 283 DevToolsAgentHostImpl* agent_host = it->second;
286 if (agent_host->client_) { 284 if (agent_host->session_) {
287 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); 285 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host);
288 // Clear |client_| before notifying it. 286 // Clear |client_| before notifying it.
289 DevToolsAgentHostClient* client = agent_host->client_; 287 DevToolsAgentHostClient* client = agent_host->session_->client();
290 agent_host->client_ = NULL; 288 agent_host->InnerDetach();
291 client->AgentHostClosed(agent_host, true); 289 client->AgentHostClosed(agent_host, true);
292 agent_host->InnerDetach();
293 } 290 }
294 } 291 }
295 } 292 }
296 293
297 // static 294 // static
298 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { 295 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) {
299 if (observer->ShouldForceDevToolsAgentHostCreation()) { 296 if (observer->ShouldForceDevToolsAgentHostCreation()) {
300 if (!DevToolsAgentHostImpl::s_force_creation_count_) { 297 if (!DevToolsAgentHostImpl::s_force_creation_count_) {
301 // Force all agent hosts when first observer is added. 298 // Force all agent hosts when first observer is added.
302 DevToolsAgentHost::GetOrCreateAll(); 299 DevToolsAgentHost::GetOrCreateAll();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 if (message_buffer_.size() != message_buffer_size_) 399 if (message_buffer_.size() != message_buffer_size_)
403 return false; 400 return false;
404 callback_.Run(chunk.session_id, message_buffer_); 401 callback_.Run(chunk.session_id, message_buffer_);
405 message_buffer_ = std::string(); 402 message_buffer_ = std::string();
406 message_buffer_size_ = 0; 403 message_buffer_size_ = 0;
407 } 404 }
408 return true; 405 return true;
409 } 406 }
410 407
411 } // namespace content 408 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698