OLD | NEW |
---|---|
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 Loading... | |
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::InnerAttachClient(DevToolsAgentHostClient* client, |
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 InnerDetachClient(); |
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 AttachSession(session_.get()); |
152 Attach(); | |
153 NotifyAttached(); | 151 NotifyAttached(); |
154 return true; | 152 return true; |
155 } | 153 } |
156 | 154 |
157 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { | 155 bool DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { |
158 return InnerAttach(client, false); | 156 return InnerAttachClient(client, false); |
159 } | 157 } |
160 | 158 |
161 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) { | 159 void DevToolsAgentHostImpl::ForceAttachClient(DevToolsAgentHostClient* client) { |
162 InnerAttach(client, true); | 160 InnerAttachClient(client, true); |
163 } | 161 } |
164 | 162 |
165 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) { | 163 bool DevToolsAgentHostImpl::DetachClient(DevToolsAgentHostClient* client) { |
166 if (!client_ || client_ != client) | 164 if (!session_ || session_->client() != client) |
167 return false; | 165 return false; |
168 | 166 |
169 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 167 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
170 client_ = NULL; | 168 InnerDetachClient(); |
171 InnerDetach(); | |
172 return true; | 169 return true; |
173 } | 170 } |
174 | 171 |
175 bool DevToolsAgentHostImpl::DispatchProtocolMessage( | 172 bool DevToolsAgentHostImpl::DispatchProtocolMessage( |
176 DevToolsAgentHostClient* client, | 173 DevToolsAgentHostClient* client, |
177 const std::string& message) { | 174 const std::string& message) { |
178 if (!client_ || client_ != client) | 175 if (!session_ || session_->client() != client) |
179 return false; | 176 return false; |
180 return DispatchProtocolMessage(message); | 177 return DispatchProtocolMessage(session_.get(), message); |
181 } | 178 } |
182 | 179 |
183 void DevToolsAgentHostImpl::InnerDetach() { | 180 void DevToolsAgentHostImpl::InnerDetachClient() { |
184 session_->ResetDispatcher(); | 181 int session_id = session_->session_id(); |
185 Detach(); | 182 session_.reset(); |
183 DetachSession(session_id); | |
186 io_context_.DiscardAllStreams(); | 184 io_context_.DiscardAllStreams(); |
187 session_.reset(); | |
188 NotifyDetached(); | 185 NotifyDetached(); |
189 } | 186 } |
190 | 187 |
191 bool DevToolsAgentHostImpl::IsAttached() { | 188 bool DevToolsAgentHostImpl::IsAttached() { |
192 return !!client_; | 189 return !!session_; |
193 } | 190 } |
194 | 191 |
195 void DevToolsAgentHostImpl::InspectElement( | 192 void DevToolsAgentHostImpl::InspectElement( |
196 DevToolsAgentHostClient* client, | 193 DevToolsAgentHostClient* client, |
197 int x, | 194 int x, |
198 int y) { | 195 int y) { |
199 if (!client_ || client_ != client) | 196 if (!session_ || session_->client() != client) |
200 return; | 197 return; |
201 InspectElement(x, y); | 198 InspectElement(session_.get(), x, y); |
202 } | 199 } |
203 | 200 |
204 std::string DevToolsAgentHostImpl::GetId() { | 201 std::string DevToolsAgentHostImpl::GetId() { |
205 return id_; | 202 return id_; |
206 } | 203 } |
207 | 204 |
208 std::string DevToolsAgentHostImpl::GetParentId() { | 205 std::string DevToolsAgentHostImpl::GetParentId() { |
209 return ""; | 206 return ""; |
210 } | 207 } |
211 | 208 |
(...skipping 30 matching lines...) Expand all Loading... | |
242 bool DevToolsAgentHostImpl::Inspect() { | 239 bool DevToolsAgentHostImpl::Inspect() { |
243 DevToolsManager* manager = DevToolsManager::GetInstance(); | 240 DevToolsManager* manager = DevToolsManager::GetInstance(); |
244 if (manager->delegate()) { | 241 if (manager->delegate()) { |
245 manager->delegate()->Inspect(this); | 242 manager->delegate()->Inspect(this); |
246 return true; | 243 return true; |
247 } | 244 } |
248 return false; | 245 return false; |
249 } | 246 } |
250 | 247 |
251 void DevToolsAgentHostImpl::HostClosed() { | 248 void DevToolsAgentHostImpl::HostClosed() { |
252 if (!client_) | 249 if (!session_) |
253 return; | 250 return; |
254 | 251 |
255 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 252 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
256 // Clear |client_| before notifying it. | 253 // Clear |client_| before notifying it. |
257 DevToolsAgentHostClient* client = client_; | 254 DevToolsAgentHostClient* client = session_->client(); |
258 client_ = NULL; | 255 InnerDetachClient(); |
259 client->AgentHostClosed(this, false); | 256 client->AgentHostClosed(this, false); |
260 InnerDetach(); | |
261 } | 257 } |
262 | 258 |
263 void DevToolsAgentHostImpl::InspectElement(int x, int y) { | 259 void DevToolsAgentHostImpl::InspectElement( |
260 DevToolsSession* session, | |
261 int x, | |
262 int y) { | |
264 } | 263 } |
265 | 264 |
266 void DevToolsAgentHostImpl::SendMessageToClient(int session_id, | 265 void DevToolsAgentHostImpl::SendMessageToClient(int session_id, |
267 const std::string& message) { | 266 const std::string& message) { |
268 if (!client_) | |
269 return; | |
270 // Filter any messages from previous sessions. | 267 // Filter any messages from previous sessions. |
271 if (!session_ || session_id != session_->session_id()) | 268 if (!session_ || session_id != session_->session_id()) |
272 return; | 269 return; |
273 client_->DispatchProtocolMessage(this, message); | 270 session_->client()->DispatchProtocolMessage(this, message); |
274 } | 271 } |
275 | 272 |
276 // static | 273 // static |
277 void DevToolsAgentHost::DetachAllClients() { | 274 void DevToolsAgentHost::DetachAllClients() { |
278 if (g_instances == NULL) | 275 if (g_instances == NULL) |
279 return; | 276 return; |
280 | 277 |
281 // Make a copy, since detaching may lead to agent destruction, which | 278 // Make a copy, since detaching may lead to agent destruction, which |
282 // removes it from the instances. | 279 // removes it from the instances. |
283 Instances copy = g_instances.Get(); | 280 Instances copy = g_instances.Get(); |
284 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { | 281 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { |
285 DevToolsAgentHostImpl* agent_host = it->second; | 282 DevToolsAgentHostImpl* agent_host = it->second; |
286 if (agent_host->client_) { | 283 if (agent_host->session_) { |
caseq
2016/12/21 23:17:27
nit: parametrize HostClosed() with bool and call i
dgozman
2016/12/22 06:26:13
Done.
| |
287 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); | 284 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); |
288 // Clear |client_| before notifying it. | 285 // Clear |client_| before notifying it. |
289 DevToolsAgentHostClient* client = agent_host->client_; | 286 DevToolsAgentHostClient* client = agent_host->session_->client(); |
290 agent_host->client_ = NULL; | 287 agent_host->InnerDetachClient(); |
291 client->AgentHostClosed(agent_host, true); | 288 client->AgentHostClosed(agent_host, true); |
292 agent_host->InnerDetach(); | |
293 } | 289 } |
294 } | 290 } |
295 } | 291 } |
296 | 292 |
297 // static | 293 // static |
298 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { | 294 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { |
299 if (observer->ShouldForceDevToolsAgentHostCreation()) { | 295 if (observer->ShouldForceDevToolsAgentHostCreation()) { |
300 if (!DevToolsAgentHostImpl::s_force_creation_count_) { | 296 if (!DevToolsAgentHostImpl::s_force_creation_count_) { |
301 // Force all agent hosts when first observer is added. | 297 // Force all agent hosts when first observer is added. |
302 DevToolsAgentHost::GetOrCreateAll(); | 298 DevToolsAgentHost::GetOrCreateAll(); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 if (message_buffer_.size() != message_buffer_size_) | 398 if (message_buffer_.size() != message_buffer_size_) |
403 return false; | 399 return false; |
404 callback_.Run(chunk.session_id, message_buffer_); | 400 callback_.Run(chunk.session_id, message_buffer_); |
405 message_buffer_ = std::string(); | 401 message_buffer_ = std::string(); |
406 message_buffer_size_ = 0; | 402 message_buffer_size_ = 0; |
407 } | 403 } |
408 return true; | 404 return true; |
409 } | 405 } |
410 | 406 |
411 } // namespace content | 407 } // namespace content |
OLD | NEW |