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 24 matching lines...) Expand all Loading... |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 char DevToolsAgentHost::kTypePage[] = "page"; | 37 char DevToolsAgentHost::kTypePage[] = "page"; |
38 char DevToolsAgentHost::kTypeFrame[] = "iframe"; | 38 char DevToolsAgentHost::kTypeFrame[] = "iframe"; |
39 char DevToolsAgentHost::kTypeSharedWorker[] = "shared_worker"; | 39 char DevToolsAgentHost::kTypeSharedWorker[] = "shared_worker"; |
40 char DevToolsAgentHost::kTypeServiceWorker[] = "service_worker"; | 40 char DevToolsAgentHost::kTypeServiceWorker[] = "service_worker"; |
41 char DevToolsAgentHost::kTypeExternal[] = "external"; | 41 char DevToolsAgentHost::kTypeExternal[] = "external"; |
42 char DevToolsAgentHost::kTypeBrowser[] = "browser"; | 42 char DevToolsAgentHost::kTypeBrowser[] = "browser"; |
43 char DevToolsAgentHost::kTypeOther[] = "other"; | 43 char DevToolsAgentHost::kTypeOther[] = "other"; |
44 int DevToolsAgentHostImpl::s_attached_count_ = 0; | 44 int DevToolsAgentHostImpl::s_attached_count_ = 0; |
| 45 int DevToolsAgentHostImpl::s_force_creation_count_ = 0; |
45 | 46 |
46 // static | 47 // static |
47 std::string DevToolsAgentHost::GetProtocolVersion() { | 48 std::string DevToolsAgentHost::GetProtocolVersion() { |
48 return std::string(devtools::kProtocolVersion); | 49 return std::string(devtools::kProtocolVersion); |
49 } | 50 } |
50 | 51 |
51 // static | 52 // static |
52 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string& version) { | 53 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string& version) { |
53 return devtools::IsSupportedProtocolVersion(version); | 54 return devtools::IsSupportedProtocolVersion(version); |
54 } | 55 } |
55 | 56 |
56 // static | 57 // static |
57 DevToolsAgentHost::List DevToolsAgentHost::GetOrCreateAll() { | 58 DevToolsAgentHost::List DevToolsAgentHost::GetOrCreateAll() { |
58 List result; | 59 List result; |
59 SharedWorkerDevToolsAgentHost::List shared_list; | 60 SharedWorkerDevToolsAgentHost::List shared_list; |
60 SharedWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&shared_list); | 61 SharedWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&shared_list); |
61 for (const auto& host : shared_list) | 62 for (const auto& host : shared_list) |
62 result.push_back(host); | 63 result.push_back(host); |
63 | 64 |
64 ServiceWorkerDevToolsAgentHost::List service_list; | 65 ServiceWorkerDevToolsAgentHost::List service_list; |
65 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list); | 66 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list); |
66 for (const auto& host : service_list) | 67 for (const auto& host : service_list) |
67 result.push_back(host); | 68 result.push_back(host); |
68 | 69 |
69 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result); | 70 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result); |
| 71 |
| 72 #if DCHECK_IS_ON() |
| 73 for (auto it : result) { |
| 74 DevToolsAgentHostImpl* host = static_cast<DevToolsAgentHostImpl*>(it.get()); |
| 75 DCHECK(g_instances.Get().find(host->id_) != g_instances.Get().end()); |
| 76 } |
| 77 #endif |
| 78 |
70 return result; | 79 return result; |
71 } | 80 } |
72 | 81 |
73 // static | 82 // static |
74 void DevToolsAgentHost::DiscoverAllHosts(const DiscoveryCallback& callback) { | 83 void DevToolsAgentHost::DiscoverAllHosts(const DiscoveryCallback& callback) { |
75 DevToolsManager* manager = DevToolsManager::GetInstance(); | 84 DevToolsManager* manager = DevToolsManager::GetInstance(); |
76 if (!manager->delegate() || !manager->delegate()->DiscoverTargets(callback)) | 85 if (!manager->delegate() || !manager->delegate()->DiscoverTargets(callback)) |
77 callback.Run(DevToolsAgentHost::GetOrCreateAll()); | 86 callback.Run(DevToolsAgentHost::GetOrCreateAll()); |
78 } | 87 } |
79 | 88 |
(...skipping 10 matching lines...) Expand all Loading... |
90 } | 99 } |
91 return ServiceWorkerDevToolsManager::GetInstance() | 100 return ServiceWorkerDevToolsManager::GetInstance() |
92 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); | 101 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); |
93 } | 102 } |
94 | 103 |
95 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id) | 104 DevToolsAgentHostImpl::DevToolsAgentHostImpl(const std::string& id) |
96 : id_(id), | 105 : id_(id), |
97 session_id_(0), | 106 session_id_(0), |
98 client_(NULL) { | 107 client_(NULL) { |
99 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
100 DCHECK(g_instances.Get().find(id_) == g_instances.Get().end()); | |
101 g_instances.Get()[id_] = this; | |
102 } | 109 } |
103 | 110 |
104 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { | 111 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { |
105 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
106 g_instances.Get().erase(g_instances.Get().find(id_)); | 113 NotifyDestroyed(); |
107 } | 114 } |
108 | 115 |
109 // static | 116 // static |
110 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( | 117 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( |
111 const std::string& id) { | 118 const std::string& id) { |
112 if (g_instances == NULL) | 119 if (g_instances == NULL) |
113 return NULL; | 120 return NULL; |
114 Instances::iterator it = g_instances.Get().find(id); | 121 Instances::iterator it = g_instances.Get().find(id); |
115 if (it == g_instances.Get().end()) | 122 if (it == g_instances.Get().end()) |
116 return NULL; | 123 return NULL; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 DevToolsAgentHostClient* client = agent_host->client_; | 294 DevToolsAgentHostClient* client = agent_host->client_; |
288 agent_host->client_ = NULL; | 295 agent_host->client_ = NULL; |
289 client->AgentHostClosed(agent_host, true); | 296 client->AgentHostClosed(agent_host, true); |
290 agent_host->InnerDetach(); | 297 agent_host->InnerDetach(); |
291 } | 298 } |
292 } | 299 } |
293 } | 300 } |
294 | 301 |
295 // static | 302 // static |
296 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { | 303 void DevToolsAgentHost::AddObserver(DevToolsAgentHostObserver* observer) { |
| 304 if (observer->ShouldForceDevToolsAgentHostCreation()) { |
| 305 if (!DevToolsAgentHostImpl::s_force_creation_count_) { |
| 306 // Force all agent hosts when first observer is added. |
| 307 DevToolsAgentHost::GetOrCreateAll(); |
| 308 } |
| 309 DevToolsAgentHostImpl::s_force_creation_count_++; |
| 310 } |
| 311 |
297 g_observers.Get().AddObserver(observer); | 312 g_observers.Get().AddObserver(observer); |
| 313 for (const auto& id_host : g_instances.Get()) |
| 314 observer->DevToolsAgentHostCreated(id_host.second); |
298 } | 315 } |
299 | 316 |
300 // static | 317 // static |
301 void DevToolsAgentHost::RemoveObserver(DevToolsAgentHostObserver* observer) { | 318 void DevToolsAgentHost::RemoveObserver(DevToolsAgentHostObserver* observer) { |
| 319 if (observer->ShouldForceDevToolsAgentHostCreation()) |
| 320 DevToolsAgentHostImpl::s_force_creation_count_--; |
302 g_observers.Get().RemoveObserver(observer); | 321 g_observers.Get().RemoveObserver(observer); |
303 } | 322 } |
304 | 323 |
| 324 // static |
| 325 bool DevToolsAgentHostImpl::ShouldForceCreation() { |
| 326 return !!s_force_creation_count_; |
| 327 } |
| 328 |
| 329 void DevToolsAgentHostImpl::NotifyCreated() { |
| 330 DCHECK(g_instances.Get().find(id_) == g_instances.Get().end()); |
| 331 g_instances.Get()[id_] = this; |
| 332 for (auto& observer : g_observers.Get()) |
| 333 observer.DevToolsAgentHostCreated(this); |
| 334 } |
| 335 |
305 void DevToolsAgentHostImpl::NotifyAttached() { | 336 void DevToolsAgentHostImpl::NotifyAttached() { |
306 if (!s_attached_count_) { | 337 if (!s_attached_count_) { |
307 BrowserThread::PostTask( | 338 BrowserThread::PostTask( |
308 BrowserThread::IO, | 339 BrowserThread::IO, |
309 FROM_HERE, | 340 FROM_HERE, |
310 base::Bind(&NetLogObserver::Attach, | 341 base::Bind(&NetLogObserver::Attach, |
311 GetContentClient()->browser()->GetNetLog())); | 342 GetContentClient()->browser()->GetNetLog())); |
312 } | 343 } |
313 ++s_attached_count_; | 344 ++s_attached_count_; |
314 | 345 |
315 for (auto& observer : g_observers.Get()) | 346 for (auto& observer : g_observers.Get()) |
316 observer.DevToolsAgentHostAttached(this); | 347 observer.DevToolsAgentHostAttached(this); |
317 } | 348 } |
318 | 349 |
319 void DevToolsAgentHostImpl::NotifyDetached() { | 350 void DevToolsAgentHostImpl::NotifyDetached() { |
320 --s_attached_count_; | 351 --s_attached_count_; |
321 if (!s_attached_count_) { | 352 if (!s_attached_count_) { |
322 BrowserThread::PostTask( | 353 BrowserThread::PostTask( |
323 BrowserThread::IO, | 354 BrowserThread::IO, |
324 FROM_HERE, | 355 FROM_HERE, |
325 base::Bind(&NetLogObserver::Detach)); | 356 base::Bind(&NetLogObserver::Detach)); |
326 } | 357 } |
327 | 358 |
328 for (auto& observer : g_observers.Get()) | 359 for (auto& observer : g_observers.Get()) |
329 observer.DevToolsAgentHostDetached(this); | 360 observer.DevToolsAgentHostDetached(this); |
330 } | 361 } |
331 | 362 |
| 363 void DevToolsAgentHostImpl::NotifyDestroyed() { |
| 364 DCHECK(g_instances.Get().find(id_) != g_instances.Get().end()); |
| 365 for (auto& observer : g_observers.Get()) |
| 366 observer.DevToolsAgentHostDestroyed(this); |
| 367 g_instances.Get().erase(id_); |
| 368 } |
| 369 |
332 // DevToolsMessageChunkProcessor ----------------------------------------------- | 370 // DevToolsMessageChunkProcessor ----------------------------------------------- |
333 | 371 |
334 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor( | 372 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor( |
335 const SendMessageCallback& callback) | 373 const SendMessageCallback& callback) |
336 : callback_(callback), | 374 : callback_(callback), |
337 message_buffer_size_(0), | 375 message_buffer_size_(0), |
338 last_call_id_(0) { | 376 last_call_id_(0) { |
339 } | 377 } |
340 | 378 |
341 DevToolsMessageChunkProcessor::~DevToolsMessageChunkProcessor() { | 379 DevToolsMessageChunkProcessor::~DevToolsMessageChunkProcessor() { |
(...skipping 27 matching lines...) Expand all Loading... |
369 if (message_buffer_.size() != message_buffer_size_) | 407 if (message_buffer_.size() != message_buffer_size_) |
370 return false; | 408 return false; |
371 callback_.Run(chunk.session_id, message_buffer_); | 409 callback_.Run(chunk.session_id, message_buffer_); |
372 message_buffer_ = std::string(); | 410 message_buffer_ = std::string(); |
373 message_buffer_size_ = 0; | 411 message_buffer_size_ = 0; |
374 } | 412 } |
375 return true; | 413 return true; |
376 } | 414 } |
377 | 415 |
378 } // namespace content | 416 } // namespace content |
OLD | NEW |