| 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 13 matching lines...) Expand all Loading... |
| 24 #include "content/browser/frame_host/frame_tree_node.h" | 24 #include "content/browser/frame_host/frame_tree_node.h" |
| 25 #include "content/browser/loader/netlog_observer.h" | 25 #include "content/browser/loader/netlog_observer.h" |
| 26 #include "content/browser/renderer_host/render_view_host_impl.h" | 26 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/content_browser_client.h" | 28 #include "content/public/browser/content_browser_client.h" |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; | 33 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; |
| 34 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; | 34 base::LazyInstance<Instances>::Leaky g_devtools_instances = |
| 35 LAZY_INSTANCE_INITIALIZER; |
| 35 | 36 |
| 36 base::LazyInstance<base::ObserverList<DevToolsAgentHostObserver>>::Leaky | 37 base::LazyInstance<base::ObserverList<DevToolsAgentHostObserver>>::Leaky |
| 37 g_observers = LAZY_INSTANCE_INITIALIZER; | 38 g_devtools_observers = LAZY_INSTANCE_INITIALIZER; |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 char DevToolsAgentHost::kTypePage[] = "page"; | 41 char DevToolsAgentHost::kTypePage[] = "page"; |
| 41 char DevToolsAgentHost::kTypeFrame[] = "iframe"; | 42 char DevToolsAgentHost::kTypeFrame[] = "iframe"; |
| 42 char DevToolsAgentHost::kTypeSharedWorker[] = "shared_worker"; | 43 char DevToolsAgentHost::kTypeSharedWorker[] = "shared_worker"; |
| 43 char DevToolsAgentHost::kTypeServiceWorker[] = "service_worker"; | 44 char DevToolsAgentHost::kTypeServiceWorker[] = "service_worker"; |
| 44 char DevToolsAgentHost::kTypeExternal[] = "external"; | 45 char DevToolsAgentHost::kTypeExternal[] = "external"; |
| 45 char DevToolsAgentHost::kTypeBrowser[] = "browser"; | 46 char DevToolsAgentHost::kTypeBrowser[] = "browser"; |
| 46 char DevToolsAgentHost::kTypeGuest[] = "webview"; | 47 char DevToolsAgentHost::kTypeGuest[] = "webview"; |
| 47 char DevToolsAgentHost::kTypeOther[] = "other"; | 48 char DevToolsAgentHost::kTypeOther[] = "other"; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 ServiceWorkerDevToolsAgentHost::List service_list; | 90 ServiceWorkerDevToolsAgentHost::List service_list; |
| 90 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list); | 91 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list); |
| 91 for (const auto& host : service_list) | 92 for (const auto& host : service_list) |
| 92 result.push_back(host); | 93 result.push_back(host); |
| 93 | 94 |
| 94 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result); | 95 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result); |
| 95 | 96 |
| 96 #if DCHECK_IS_ON() | 97 #if DCHECK_IS_ON() |
| 97 for (auto it : result) { | 98 for (auto it : result) { |
| 98 DevToolsAgentHostImpl* host = static_cast<DevToolsAgentHostImpl*>(it.get()); | 99 DevToolsAgentHostImpl* host = static_cast<DevToolsAgentHostImpl*>(it.get()); |
| 99 DCHECK(g_instances.Get().find(host->id_) != g_instances.Get().end()); | 100 DCHECK(g_devtools_instances.Get().find(host->id_) != |
| 101 g_devtools_instances.Get().end()); |
| 100 } | 102 } |
| 101 #endif | 103 #endif |
| 102 | 104 |
| 103 return result; | 105 return result; |
| 104 } | 106 } |
| 105 | 107 |
| 106 // Called on the UI thread. | 108 // Called on the UI thread. |
| 107 // static | 109 // static |
| 108 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker( | 110 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker( |
| 109 int worker_process_id, | 111 int worker_process_id, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 124 } | 126 } |
| 125 | 127 |
| 126 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { | 128 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 128 NotifyDestroyed(); | 130 NotifyDestroyed(); |
| 129 } | 131 } |
| 130 | 132 |
| 131 // static | 133 // static |
| 132 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( | 134 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( |
| 133 const std::string& id) { | 135 const std::string& id) { |
| 134 if (g_instances == NULL) | 136 if (g_devtools_instances == NULL) |
| 135 return NULL; | 137 return NULL; |
| 136 Instances::iterator it = g_instances.Get().find(id); | 138 Instances::iterator it = g_devtools_instances.Get().find(id); |
| 137 if (it == g_instances.Get().end()) | 139 if (it == g_devtools_instances.Get().end()) |
| 138 return NULL; | 140 return NULL; |
| 139 return it->second; | 141 return it->second; |
| 140 } | 142 } |
| 141 | 143 |
| 142 // static | 144 // static |
| 143 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward( | 145 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Forward( |
| 144 const std::string& id, | 146 const std::string& id, |
| 145 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) { | 147 std::unique_ptr<DevToolsExternalAgentProxyDelegate> delegate) { |
| 146 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id); | 148 scoped_refptr<DevToolsAgentHost> result = DevToolsAgentHost::GetForId(id); |
| 147 if (result) | 149 if (result) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 290 } |
| 289 | 291 |
| 290 void DevToolsAgentHostImpl::InspectElement( | 292 void DevToolsAgentHostImpl::InspectElement( |
| 291 DevToolsSession* session, | 293 DevToolsSession* session, |
| 292 int x, | 294 int x, |
| 293 int y) { | 295 int y) { |
| 294 } | 296 } |
| 295 | 297 |
| 296 // static | 298 // static |
| 297 void DevToolsAgentHost::DetachAllClients() { | 299 void DevToolsAgentHost::DetachAllClients() { |
| 298 if (g_instances == NULL) | 300 if (g_devtools_instances == NULL) |
| 299 return; | 301 return; |
| 300 | 302 |
| 301 // Make a copy, since detaching may lead to agent destruction, which | 303 // Make a copy, since detaching may lead to agent destruction, which |
| 302 // removes it from the instances. | 304 // removes it from the instances. |
| 303 Instances copy = g_instances.Get(); | 305 Instances copy = g_devtools_instances.Get(); |
| 304 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { | 306 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { |
| 305 DevToolsAgentHostImpl* agent_host = it->second; | 307 DevToolsAgentHostImpl* agent_host = it->second; |
| 306 agent_host->ForceDetachAllClients(true); | 308 agent_host->ForceDetachAllClients(true); |
| 307 } | 309 } |
| 308 } | 310 } |
| 309 | 311 |
| 310 // static | 312 // static |
| 311 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { | 313 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { |
| 312 if (observer->ShouldForceDevToolsAgentHostCreation()) { | 314 if (observer->ShouldForceDevToolsAgentHostCreation()) { |
| 313 if (!DevToolsAgentHostImpl::s_force_creation_count_) { | 315 if (!DevToolsAgentHostImpl::s_force_creation_count_) { |
| 314 // Force all agent hosts when first observer is added. | 316 // Force all agent hosts when first observer is added. |
| 315 DevToolsAgentHost::GetOrCreateAll(); | 317 DevToolsAgentHost::GetOrCreateAll(); |
| 316 } | 318 } |
| 317 DevToolsAgentHostImpl::s_force_creation_count_++; | 319 DevToolsAgentHostImpl::s_force_creation_count_++; |
| 318 } | 320 } |
| 319 | 321 |
| 320 g_observers.Get().AddObserver(observer); | 322 g_devtools_observers.Get().AddObserver(observer); |
| 321 for (const auto& id_host : g_instances.Get()) | 323 for (const auto& id_host : g_devtools_instances.Get()) |
| 322 observer->DevToolsAgentHostCreated(id_host.second); | 324 observer->DevToolsAgentHostCreated(id_host.second); |
| 323 } | 325 } |
| 324 | 326 |
| 325 // static | 327 // static |
| 326 void DevToolsAgentHost::RemoveObserver(DevToolsAgentHostObserver* observer) { | 328 void DevToolsAgentHost::RemoveObserver(DevToolsAgentHostObserver* observer) { |
| 327 if (observer->ShouldForceDevToolsAgentHostCreation()) | 329 if (observer->ShouldForceDevToolsAgentHostCreation()) |
| 328 DevToolsAgentHostImpl::s_force_creation_count_--; | 330 DevToolsAgentHostImpl::s_force_creation_count_--; |
| 329 g_observers.Get().RemoveObserver(observer); | 331 g_devtools_observers.Get().RemoveObserver(observer); |
| 330 } | 332 } |
| 331 | 333 |
| 332 // static | 334 // static |
| 333 bool DevToolsAgentHostImpl::ShouldForceCreation() { | 335 bool DevToolsAgentHostImpl::ShouldForceCreation() { |
| 334 return !!s_force_creation_count_; | 336 return !!s_force_creation_count_; |
| 335 } | 337 } |
| 336 | 338 |
| 337 void DevToolsAgentHostImpl::NotifyCreated() { | 339 void DevToolsAgentHostImpl::NotifyCreated() { |
| 338 DCHECK(g_instances.Get().find(id_) == g_instances.Get().end()); | 340 DCHECK(g_devtools_instances.Get().find(id_) == g_devtools_instances.Get().end(
)); |
| 339 g_instances.Get()[id_] = this; | 341 g_devtools_instances.Get()[id_] = this; |
| 340 for (auto& observer : g_observers.Get()) | 342 for (auto& observer : g_devtools_observers.Get()) |
| 341 observer.DevToolsAgentHostCreated(this); | 343 observer.DevToolsAgentHostCreated(this); |
| 342 } | 344 } |
| 343 | 345 |
| 344 void DevToolsAgentHostImpl::NotifyAttached() { | 346 void DevToolsAgentHostImpl::NotifyAttached() { |
| 345 if (!s_attached_count_) { | 347 if (!s_attached_count_) { |
| 346 BrowserThread::PostTask( | 348 BrowserThread::PostTask( |
| 347 BrowserThread::IO, | 349 BrowserThread::IO, |
| 348 FROM_HERE, | 350 FROM_HERE, |
| 349 base::Bind(&NetLogObserver::Attach, | 351 base::Bind(&NetLogObserver::Attach, |
| 350 GetContentClient()->browser()->GetNetLog())); | 352 GetContentClient()->browser()->GetNetLog())); |
| 351 } | 353 } |
| 352 ++s_attached_count_; | 354 ++s_attached_count_; |
| 353 | 355 |
| 354 for (auto& observer : g_observers.Get()) | 356 for (auto& observer : g_devtools_observers.Get()) |
| 355 observer.DevToolsAgentHostAttached(this); | 357 observer.DevToolsAgentHostAttached(this); |
| 356 } | 358 } |
| 357 | 359 |
| 358 void DevToolsAgentHostImpl::NotifyDetached() { | 360 void DevToolsAgentHostImpl::NotifyDetached() { |
| 359 --s_attached_count_; | 361 --s_attached_count_; |
| 360 if (!s_attached_count_) { | 362 if (!s_attached_count_) { |
| 361 BrowserThread::PostTask( | 363 BrowserThread::PostTask( |
| 362 BrowserThread::IO, | 364 BrowserThread::IO, |
| 363 FROM_HERE, | 365 FROM_HERE, |
| 364 base::Bind(&NetLogObserver::Detach)); | 366 base::Bind(&NetLogObserver::Detach)); |
| 365 } | 367 } |
| 366 | 368 |
| 367 for (auto& observer : g_observers.Get()) | 369 for (auto& observer : g_devtools_observers.Get()) |
| 368 observer.DevToolsAgentHostDetached(this); | 370 observer.DevToolsAgentHostDetached(this); |
| 369 } | 371 } |
| 370 | 372 |
| 371 void DevToolsAgentHostImpl::NotifyDestroyed() { | 373 void DevToolsAgentHostImpl::NotifyDestroyed() { |
| 372 DCHECK(g_instances.Get().find(id_) != g_instances.Get().end()); | 374 DCHECK(g_devtools_instances.Get().find(id_) != g_devtools_instances.Get().end(
)); |
| 373 for (auto& observer : g_observers.Get()) | 375 for (auto& observer : g_devtools_observers.Get()) |
| 374 observer.DevToolsAgentHostDestroyed(this); | 376 observer.DevToolsAgentHostDestroyed(this); |
| 375 g_instances.Get().erase(id_); | 377 g_devtools_instances.Get().erase(id_); |
| 376 } | 378 } |
| 377 | 379 |
| 378 // DevToolsMessageChunkProcessor ----------------------------------------------- | 380 // DevToolsMessageChunkProcessor ----------------------------------------------- |
| 379 | 381 |
| 380 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor( | 382 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor( |
| 381 const SendMessageCallback& callback) | 383 const SendMessageCallback& callback) |
| 382 : callback_(callback), | 384 : callback_(callback), |
| 383 message_buffer_size_(0), | 385 message_buffer_size_(0), |
| 384 last_call_id_(0) { | 386 last_call_id_(0) { |
| 385 } | 387 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 424 } |
| 423 | 425 |
| 424 void DevToolsMessageChunkProcessor::Reset() { | 426 void DevToolsMessageChunkProcessor::Reset() { |
| 425 message_buffer_ = std::string(); | 427 message_buffer_ = std::string(); |
| 426 message_buffer_size_ = 0; | 428 message_buffer_size_ = 0; |
| 427 state_cookie_ = std::string(); | 429 state_cookie_ = std::string(); |
| 428 last_call_id_ = 0; | 430 last_call_id_ = 0; |
| 429 } | 431 } |
| 430 | 432 |
| 431 } // namespace content | 433 } // namespace content |
| OLD | NEW |