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