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_manager.h" | 5 #include "content/browser/devtools/devtools_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/browser/devtools/devtools_agent_host_impl.h" | 10 #include "content/browser/devtools/devtools_agent_host_impl.h" |
11 #include "content/browser/devtools/devtools_http_handler.h" | 11 #include "content/browser/devtools/devtools_http_handler.h" |
12 #include "content/browser/loader/netlog_observer.h" | 12 #include "content/browser/loader/netlog_observer.h" |
13 #include "content/browser/web_contents/web_contents_impl.h" | |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/browser/devtools_socket_factory.h" | 16 #include "content/public/browser/devtools_socket_factory.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // static | 20 // static |
20 void DevToolsAgentHost::StartRemoteDebuggingServer( | 21 void DevToolsAgentHost::StartRemoteDebuggingServer( |
21 std::unique_ptr<DevToolsSocketFactory> server_socket_factory, | 22 std::unique_ptr<DevToolsSocketFactory> server_socket_factory, |
22 const std::string& frontend_url, | 23 const std::string& frontend_url, |
23 const base::FilePath& active_port_output_directory, | 24 const base::FilePath& active_port_output_directory, |
24 const base::FilePath& debug_frontend_dir, | 25 const base::FilePath& debug_frontend_dir, |
25 const std::string& product_name, | 26 const std::string& product_name, |
26 const std::string& user_agent) { | 27 const std::string& user_agent) { |
27 DevToolsManager* manager = DevToolsManager::GetInstance(); | 28 DevToolsManager* manager = DevToolsManager::GetInstance(); |
28 if (!manager->delegate()) | 29 if (!manager->GetDelegate()) |
29 return; | 30 return; |
30 manager->SetHttpHandler(base::WrapUnique(new DevToolsHttpHandler( | 31 manager->SetHttpHandler(base::WrapUnique(new DevToolsHttpHandler( |
31 manager->delegate(), std::move(server_socket_factory), frontend_url, | 32 manager->GetDelegate(), std::move(server_socket_factory), frontend_url, |
32 active_port_output_directory, debug_frontend_dir, product_name, | 33 active_port_output_directory, debug_frontend_dir, product_name, |
33 user_agent))); | 34 user_agent))); |
34 } | 35 } |
35 | 36 |
36 // static | 37 // static |
37 void DevToolsAgentHost::StopRemoteDebuggingServer() { | 38 void DevToolsAgentHost::StopRemoteDebuggingServer() { |
38 DevToolsManager* manager = DevToolsManager::GetInstance(); | 39 DevToolsManager* manager = DevToolsManager::GetInstance(); |
39 manager->SetHttpHandler(nullptr); | 40 manager->SetHttpHandler(nullptr); |
40 } | 41 } |
41 | 42 |
42 // static | 43 // static |
43 DevToolsManager* DevToolsManager::GetInstance() { | 44 DevToolsManager* DevToolsManager::GetInstance() { |
44 return base::Singleton<DevToolsManager>::get(); | 45 return base::Singleton<DevToolsManager>::get(); |
45 } | 46 } |
46 | 47 |
47 DevToolsManager::DevToolsManager() | 48 DevToolsManager::DevToolsManager() |
48 : delegate_(GetContentClient()->browser()->GetDevToolsManagerDelegate()), | 49 : delegate_initialized_(false), |
49 attached_hosts_count_(0) { | 50 attached_hosts_count_(0) { |
50 } | 51 } |
51 | 52 |
52 DevToolsManager::~DevToolsManager() { | 53 DevToolsManager::~DevToolsManager() { |
54 for (DevToolsAgentHostImpl* agent_host : agent_hosts_) { | |
pfeldman
2016/10/12 23:11:18
How can this happen?
dgozman
2016/10/12 23:23:12
Service workers shutdown later (I think on IO) tha
pfeldman
2016/10/13 17:58:10
Could you mention it the comments here? Also, what
| |
55 for (auto& observer : observers_) | |
56 observer.AgentHostDestroyed(agent_host); | |
57 } | |
58 agent_hosts_.clear(); | |
53 DCHECK(!attached_hosts_count_); | 59 DCHECK(!attached_hosts_count_); |
54 } | 60 } |
55 | 61 |
62 DevToolsManagerDelegate* DevToolsManager::GetDelegate() { | |
63 if (!delegate_initialized_) { | |
pfeldman
2016/10/12 23:11:18
What is wrong with !delegate_ ?
dgozman
2016/10/12 23:23:12
Client can pass a null one, I don't want to ask fo
| |
64 delegate_initialized_ = true; | |
65 delegate_.reset( | |
66 GetContentClient()->browser()->GetDevToolsManagerDelegate()); | |
67 } | |
68 return delegate_.get(); | |
69 } | |
70 | |
71 void DevToolsManager::AddObserver(Observer* observer) { | |
72 if (!observers_.might_have_observers()) { | |
73 // Force all agent hosts when first observer is added. | |
74 DevToolsAgentHost::GetOrCreateAll(); | |
75 } | |
76 observers_.AddObserver(observer); | |
77 for (DevToolsAgentHostImpl* agent_host : agent_hosts_) | |
78 observer->AgentHostCreated(agent_host); | |
79 } | |
80 | |
81 void DevToolsManager::RemoveObserver(Observer* observer) { | |
82 observers_.RemoveObserver(observer); | |
83 } | |
84 | |
85 void DevToolsManager::WebContentsCreated(WebContentsImpl* web_contents) { | |
86 if (observers_.might_have_observers()) { | |
87 // Force agent host. | |
88 DevToolsAgentHost::GetOrCreateFor(web_contents); | |
89 } | |
90 } | |
91 | |
92 void DevToolsManager::AgentHostCreated(DevToolsAgentHostImpl* agent_host) { | |
93 agent_hosts_.insert(agent_host); | |
94 for (auto& observer : observers_) | |
95 observer.AgentHostCreated(agent_host); | |
96 } | |
97 | |
98 void DevToolsManager::AgentHostDestroyed(DevToolsAgentHostImpl* agent_host) { | |
99 for (auto& observer : observers_) | |
100 observer.AgentHostDestroyed(agent_host); | |
101 agent_hosts_.erase(agent_host); | |
102 } | |
103 | |
56 void DevToolsManager::AgentHostStateChanged( | 104 void DevToolsManager::AgentHostStateChanged( |
57 DevToolsAgentHostImpl* agent_host, bool attached) { | 105 DevToolsAgentHostImpl* agent_host, bool attached) { |
58 if (attached) { | 106 if (attached) { |
59 if (!attached_hosts_count_) { | 107 if (!attached_hosts_count_) { |
60 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
61 BrowserThread::IO, | 109 BrowserThread::IO, |
62 FROM_HERE, | 110 FROM_HERE, |
63 base::Bind(&NetLogObserver::Attach, | 111 base::Bind(&NetLogObserver::Attach, |
64 GetContentClient()->browser()->GetNetLog())); | 112 GetContentClient()->browser()->GetNetLog())); |
65 } | 113 } |
66 ++attached_hosts_count_; | 114 ++attached_hosts_count_; |
67 } else { | 115 } else { |
68 --attached_hosts_count_; | 116 --attached_hosts_count_; |
69 if (!attached_hosts_count_) { | 117 if (!attached_hosts_count_) { |
70 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
71 BrowserThread::IO, | 119 BrowserThread::IO, |
72 FROM_HERE, | 120 FROM_HERE, |
73 base::Bind(&NetLogObserver::Detach)); | 121 base::Bind(&NetLogObserver::Detach)); |
74 } | 122 } |
75 } | 123 } |
76 } | 124 } |
77 | 125 |
78 void DevToolsManager::SetHttpHandler( | 126 void DevToolsManager::SetHttpHandler( |
79 std::unique_ptr<DevToolsHttpHandler> http_handler) { | 127 std::unique_ptr<DevToolsHttpHandler> http_handler) { |
80 http_handler_ = std::move(http_handler); | 128 http_handler_ = std::move(http_handler); |
81 } | 129 } |
82 | 130 |
83 } // namespace content | 131 } // namespace content |
OLD | NEW |