OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/debugger/devtools_manager.h" | 5 #include "content/browser/debugger/devtools_manager_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "content/browser/browsing_instance.h" | 11 #include "content/browser/browsing_instance.h" |
12 #include "content/browser/child_process_security_policy.h" | 12 #include "content/browser/child_process_security_policy.h" |
13 #include "content/browser/debugger/devtools_client_host.h" | |
14 #include "content/browser/debugger/devtools_netlog_observer.h" | 13 #include "content/browser/debugger/devtools_netlog_observer.h" |
15 #include "content/browser/debugger/render_view_devtools_agent_host.h" | 14 #include "content/browser/debugger/render_view_devtools_agent_host.h" |
16 #include "content/browser/renderer_host/render_view_host.h" | 15 #include "content/browser/renderer_host/render_view_host.h" |
17 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/devtools_client_host.h" |
| 19 #include "content/public/browser/devtools_agent_host_registry.h" |
19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
20 | 21 |
21 using content::BrowserThread; | 22 namespace content { |
22 | 23 |
23 // static | 24 // static |
24 DevToolsManager* DevToolsManager::GetInstance() { | 25 DevToolsManager* DevToolsManager::GetInstance() { |
25 return Singleton<DevToolsManager>::get(); | 26 return DevToolsManagerImpl::GetInstance(); |
26 } | 27 } |
27 | 28 |
28 DevToolsManager::DevToolsManager() | 29 // static |
| 30 DevToolsManagerImpl* DevToolsManagerImpl::GetInstance() { |
| 31 return Singleton<DevToolsManagerImpl>::get(); |
| 32 } |
| 33 |
| 34 DevToolsManagerImpl::DevToolsManagerImpl() |
29 : last_orphan_cookie_(0) { | 35 : last_orphan_cookie_(0) { |
30 } | 36 } |
31 | 37 |
32 DevToolsManager::~DevToolsManager() { | 38 DevToolsManagerImpl::~DevToolsManagerImpl() { |
33 DCHECK(agent_to_client_host_.empty()); | 39 DCHECK(agent_to_client_host_.empty()); |
34 DCHECK(client_to_agent_host_.empty()); | 40 DCHECK(client_to_agent_host_.empty()); |
35 // By the time we destroy devtools manager, all orphan client hosts should | 41 // By the time we destroy devtools manager, all orphan client hosts should |
36 // have been delelted, no need to notify them upon tab closing. | 42 // have been delelted, no need to notify them upon tab closing. |
37 DCHECK(orphan_client_hosts_.empty()); | 43 DCHECK(orphan_client_hosts_.empty()); |
38 } | 44 } |
39 | 45 |
40 DevToolsClientHost* DevToolsManager::GetDevToolsClientHostFor( | 46 DevToolsClientHost* DevToolsManagerImpl::GetDevToolsClientHostFor( |
41 RenderViewHost* inspected_rvh) { | |
42 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( | |
43 inspected_rvh); | |
44 return GetDevToolsClientHostFor(agent_host); | |
45 } | |
46 | |
47 DevToolsClientHost* DevToolsManager::GetDevToolsClientHostFor( | |
48 DevToolsAgentHost* agent_host) { | 47 DevToolsAgentHost* agent_host) { |
49 AgentToClientHostMap::iterator it = agent_to_client_host_.find(agent_host); | 48 AgentToClientHostMap::iterator it = agent_to_client_host_.find(agent_host); |
50 if (it != agent_to_client_host_.end()) | 49 if (it != agent_to_client_host_.end()) |
51 return it->second; | 50 return it->second; |
52 return NULL; | 51 return NULL; |
53 } | 52 } |
54 | 53 |
55 void DevToolsManager::RegisterDevToolsClientHostFor( | 54 void DevToolsManagerImpl::RegisterDevToolsClientHostFor( |
56 RenderViewHost* inspected_rvh, | |
57 DevToolsClientHost* client_host) { | |
58 DCHECK(!GetDevToolsClientHostFor(inspected_rvh)); | |
59 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( | |
60 inspected_rvh); | |
61 RegisterDevToolsClientHostFor(agent_host, client_host); | |
62 } | |
63 | |
64 void DevToolsManager::RegisterDevToolsClientHostFor( | |
65 DevToolsAgentHost* agent_host, | 55 DevToolsAgentHost* agent_host, |
66 DevToolsClientHost* client_host) { | 56 DevToolsClientHost* client_host) { |
67 BindClientHost(agent_host, client_host); | 57 BindClientHost(agent_host, client_host); |
68 agent_host->Attach(); | 58 agent_host->Attach(); |
69 client_host->set_close_listener(this); | |
70 } | 59 } |
71 | 60 |
72 bool DevToolsManager::ForwardToDevToolsAgent(DevToolsClientHost* from, | 61 bool DevToolsManagerImpl::DispatchOnInspectorBackend( |
73 const IPC::Message& message) { | 62 DevToolsClientHost* from, |
| 63 const std::string& message) { |
74 DevToolsAgentHost* agent_host = GetAgentHost(from); | 64 DevToolsAgentHost* agent_host = GetAgentHost(from); |
75 if (!agent_host) | 65 if (!agent_host) |
76 return false; | 66 return false; |
77 | 67 |
78 agent_host->SendMessageToAgent(new IPC::Message(message)); | 68 agent_host->DipatchOnInspectorBackend(message); |
79 return true; | 69 return true; |
80 } | 70 } |
81 | 71 |
82 void DevToolsManager::ForwardToDevToolsClient(DevToolsAgentHost* agent_host, | 72 void DevToolsManagerImpl::DispatchOnInspectorFrontend( |
83 const IPC::Message& message) { | 73 DevToolsAgentHost* agent_host, |
| 74 const std::string& message) { |
84 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); | 75 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); |
85 if (!client_host) { | 76 if (!client_host) { |
86 // Client window was closed while there were messages | 77 // Client window was closed while there were messages |
87 // being sent to it. | 78 // being sent to it. |
88 return; | 79 return; |
89 } | 80 } |
90 client_host->SendMessageToClient(message); | 81 client_host->DispatchOnInspectorFrontend(message); |
91 } | 82 } |
92 | 83 |
93 void DevToolsManager::SaveAgentRuntimeState(DevToolsAgentHost* agent_host, | 84 void DevToolsManagerImpl::SaveAgentRuntimeState(DevToolsAgentHost* agent_host, |
94 const std::string& state) { | 85 const std::string& state) { |
95 agent_runtime_states_[agent_host] = state; | 86 agent_runtime_states_[agent_host] = state; |
96 } | 87 } |
97 | 88 |
98 void DevToolsManager::ClientHostClosing(DevToolsClientHost* client_host) { | 89 void DevToolsManagerImpl::InspectElement(DevToolsAgentHost* agent_host, |
| 90 int x, int y) { |
| 91 agent_host->InspectElement(x, y); |
| 92 } |
| 93 |
| 94 void DevToolsManagerImpl::ClientHostClosing(DevToolsClientHost* client_host) { |
99 DevToolsAgentHost* agent_host = GetAgentHost(client_host); | 95 DevToolsAgentHost* agent_host = GetAgentHost(client_host); |
100 if (!agent_host) { | 96 if (!agent_host) { |
101 // It might be in the list of orphan client hosts, remove it from there. | 97 // It might be in the list of orphan client hosts, remove it from there. |
102 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin(); | 98 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin(); |
103 it != orphan_client_hosts_.end(); ++it) { | 99 it != orphan_client_hosts_.end(); ++it) { |
104 if (it->second.first == client_host) { | 100 if (it->second.first == client_host) { |
105 orphan_client_hosts_.erase(it->first); | 101 orphan_client_hosts_.erase(it->first); |
106 return; | 102 return; |
107 } | 103 } |
108 } | 104 } |
109 return; | 105 return; |
110 } | 106 } |
111 | 107 |
112 agent_host->NotifyClientClosing(); | 108 agent_host->NotifyClientClosing(); |
113 | 109 |
114 UnbindClientHost(agent_host, client_host); | 110 UnbindClientHost(agent_host, client_host); |
115 } | 111 } |
116 | 112 |
117 void DevToolsManager::AgentHostClosing(DevToolsAgentHost* agent_host) { | 113 void DevToolsManagerImpl::AgentHostClosing(DevToolsAgentHost* agent_host) { |
118 UnregisterDevToolsClientHostFor(agent_host); | 114 UnregisterDevToolsClientHostFor(agent_host); |
119 } | 115 } |
120 | 116 |
121 DevToolsAgentHost* DevToolsManager::GetAgentHost( | 117 DevToolsAgentHost* DevToolsManagerImpl::GetAgentHost( |
122 DevToolsClientHost* client_host) { | 118 DevToolsClientHost* client_host) { |
123 ClientHostToInspectedRvhMap::iterator it = | 119 ClientHostToInspectedRvhMap::iterator it = |
124 client_to_agent_host_.find(client_host); | 120 client_to_agent_host_.find(client_host); |
125 if (it != client_to_agent_host_.end()) | 121 if (it != client_to_agent_host_.end()) |
126 return it->second; | 122 return it->second; |
127 return NULL; | 123 return NULL; |
128 } | 124 } |
129 | 125 |
130 void DevToolsManager::UnregisterDevToolsClientHostFor( | 126 void DevToolsManagerImpl::UnregisterDevToolsClientHostFor( |
131 RenderViewHost* inspected_rvh) { | |
132 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( | |
133 inspected_rvh); | |
134 UnregisterDevToolsClientHostFor(agent_host); | |
135 } | |
136 | |
137 void DevToolsManager::UnregisterDevToolsClientHostFor( | |
138 DevToolsAgentHost* agent_host) { | 127 DevToolsAgentHost* agent_host) { |
139 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); | 128 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); |
140 if (!client_host) | 129 if (!client_host) |
141 return; | 130 return; |
142 UnbindClientHost(agent_host, client_host); | 131 UnbindClientHost(agent_host, client_host); |
143 client_host->InspectedTabClosing(); | 132 client_host->InspectedTabClosing(); |
144 } | 133 } |
145 | 134 |
146 void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh, | 135 void DevToolsManagerImpl::OnNavigatingToPendingEntry(RenderViewHost* rvh, |
147 RenderViewHost* dest_rvh, | 136 RenderViewHost* dest_rvh, |
148 const GURL& gurl) { | 137 const GURL& gurl) { |
149 if (rvh == dest_rvh && rvh->render_view_termination_status() == | 138 if (rvh == dest_rvh && rvh->render_view_termination_status() == |
150 base::TERMINATION_STATUS_STILL_RUNNING) | 139 base::TERMINATION_STATUS_STILL_RUNNING) |
151 return; | 140 return; |
152 int cookie = DetachClientHost(rvh); | 141 int cookie = DetachClientHost(rvh); |
153 if (cookie != -1) { | 142 if (cookie != -1) { |
154 // Navigating to URL in the inspected window. | 143 // Navigating to URL in the inspected window. |
155 AttachClientHost(cookie, dest_rvh); | 144 AttachClientHost(cookie, dest_rvh); |
156 | 145 |
157 DevToolsClientHost* client_host = GetDevToolsClientHostFor(dest_rvh); | 146 DevToolsAgentHost* dest_agent_host = |
| 147 DevToolsAgentHostRegistry::GetDevToolsAgentHost(dest_rvh); |
| 148 DevToolsClientHost* client_host = GetDevToolsClientHostFor( |
| 149 dest_agent_host); |
158 client_host->FrameNavigating(gurl.spec()); | 150 client_host->FrameNavigating(gurl.spec()); |
159 } | 151 } |
160 } | 152 } |
161 | 153 |
162 void DevToolsManager::OnCancelPendingNavigation(RenderViewHost* pending, | 154 void DevToolsManagerImpl::OnCancelPendingNavigation(RenderViewHost* pending, |
163 RenderViewHost* current) { | 155 RenderViewHost* current) { |
164 int cookie = DetachClientHost(pending); | 156 int cookie = DetachClientHost(pending); |
165 if (cookie != -1) { | 157 if (cookie != -1) { |
166 // Navigating to URL in the inspected window. | 158 // Navigating to URL in the inspected window. |
167 AttachClientHost(cookie, current); | 159 AttachClientHost(cookie, current); |
168 } | 160 } |
169 } | 161 } |
170 | 162 |
171 void DevToolsManager::TabReplaced(TabContents* old_tab, | 163 void DevToolsManagerImpl::TabReplaced(TabContents* old_tab, |
172 TabContents* new_tab) { | 164 TabContents* new_tab) { |
173 RenderViewHost* old_rvh = old_tab->render_view_host(); | 165 RenderViewHost* old_rvh = old_tab->render_view_host(); |
174 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_rvh); | 166 if (!DevToolsAgentHostRegistry::HasDevToolsAgentHost(old_rvh)) |
| 167 return; |
| 168 |
| 169 DevToolsAgentHost* old_agent_host = |
| 170 DevToolsAgentHostRegistry::GetDevToolsAgentHost(old_rvh); |
| 171 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_agent_host); |
175 if (!client_host) | 172 if (!client_host) |
176 return; // Didn't know about old_tab. | 173 return; // Didn't know about old_tab. |
177 int cookie = DetachClientHost(old_rvh); | 174 int cookie = DetachClientHost(old_rvh); |
178 if (cookie == -1) | 175 if (cookie == -1) |
179 return; // Didn't know about old_tab. | 176 return; // Didn't know about old_tab. |
180 | 177 |
181 client_host->TabReplaced(new_tab); | 178 client_host->TabReplaced(new_tab); |
182 AttachClientHost(cookie, new_tab->render_view_host()); | 179 AttachClientHost(cookie, new_tab->render_view_host()); |
183 } | 180 } |
184 | 181 |
185 int DevToolsManager::DetachClientHost(RenderViewHost* from_rvh) { | 182 int DevToolsManagerImpl::DetachClientHost(RenderViewHost* from_rvh) { |
186 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( | 183 DevToolsAgentHost* agent_host = |
187 from_rvh); | 184 DevToolsAgentHostRegistry::GetDevToolsAgentHost(from_rvh); |
188 return DetachClientHost(agent_host); | 185 return DetachClientHost(agent_host); |
189 } | 186 } |
190 | 187 |
191 int DevToolsManager::DetachClientHost(DevToolsAgentHost* agent_host) { | 188 int DevToolsManagerImpl::DetachClientHost(DevToolsAgentHost* agent_host) { |
192 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); | 189 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); |
193 if (!client_host) | 190 if (!client_host) |
194 return -1; | 191 return -1; |
195 | 192 |
196 int cookie = last_orphan_cookie_++; | 193 int cookie = last_orphan_cookie_++; |
197 orphan_client_hosts_[cookie] = | 194 orphan_client_hosts_[cookie] = |
198 std::pair<DevToolsClientHost*, std::string>( | 195 std::pair<DevToolsClientHost*, std::string>( |
199 client_host, agent_runtime_states_[agent_host]); | 196 client_host, agent_runtime_states_[agent_host]); |
200 | 197 |
201 UnbindClientHost(agent_host, client_host); | 198 UnbindClientHost(agent_host, client_host); |
202 return cookie; | 199 return cookie; |
203 } | 200 } |
204 | 201 |
205 void DevToolsManager::AttachClientHost(int client_host_cookie, | 202 void DevToolsManagerImpl::AttachClientHost(int client_host_cookie, |
206 RenderViewHost* to_rvh) { | 203 RenderViewHost* to_rvh) { |
207 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( | 204 DevToolsAgentHost* agent_host = |
208 to_rvh); | 205 DevToolsAgentHostRegistry::GetDevToolsAgentHost(to_rvh); |
209 AttachClientHost(client_host_cookie, agent_host); | 206 AttachClientHost(client_host_cookie, agent_host); |
210 } | 207 } |
211 | 208 |
212 void DevToolsManager::AttachClientHost(int client_host_cookie, | 209 void DevToolsManagerImpl::AttachClientHost(int client_host_cookie, |
213 DevToolsAgentHost* agent_host) { | 210 DevToolsAgentHost* agent_host) { |
214 OrphanClientHosts::iterator it = orphan_client_hosts_.find( | 211 OrphanClientHosts::iterator it = orphan_client_hosts_.find( |
215 client_host_cookie); | 212 client_host_cookie); |
216 if (it == orphan_client_hosts_.end()) | 213 if (it == orphan_client_hosts_.end()) |
217 return; | 214 return; |
218 | 215 |
219 DevToolsClientHost* client_host = (*it).second.first; | 216 DevToolsClientHost* client_host = (*it).second.first; |
220 const std::string& state = (*it).second.second; | 217 const std::string& state = (*it).second.second; |
221 BindClientHost(agent_host, client_host); | 218 BindClientHost(agent_host, client_host); |
222 agent_host->Reattach(state); | 219 agent_host->Reattach(state); |
223 agent_runtime_states_[agent_host] = state; | 220 agent_runtime_states_[agent_host] = state; |
224 | 221 |
225 orphan_client_hosts_.erase(it); | 222 orphan_client_hosts_.erase(it); |
226 } | 223 } |
227 | 224 |
228 void DevToolsManager::BindClientHost( | 225 void DevToolsManagerImpl::BindClientHost( |
229 DevToolsAgentHost* agent_host, | 226 DevToolsAgentHost* agent_host, |
230 DevToolsClientHost* client_host) { | 227 DevToolsClientHost* client_host) { |
231 DCHECK(agent_to_client_host_.find(agent_host) == | 228 DCHECK(agent_to_client_host_.find(agent_host) == |
232 agent_to_client_host_.end()); | 229 agent_to_client_host_.end()); |
233 DCHECK(client_to_agent_host_.find(client_host) == | 230 DCHECK(client_to_agent_host_.find(client_host) == |
234 client_to_agent_host_.end()); | 231 client_to_agent_host_.end()); |
235 | 232 |
236 if (client_to_agent_host_.empty()) { | 233 if (client_to_agent_host_.empty()) { |
237 BrowserThread::PostTask( | 234 BrowserThread::PostTask( |
238 BrowserThread::IO, | 235 BrowserThread::IO, |
239 FROM_HERE, | 236 FROM_HERE, |
240 base::Bind(&DevToolsNetLogObserver::Attach)); | 237 base::Bind(&DevToolsNetLogObserver::Attach)); |
241 } | 238 } |
242 agent_to_client_host_[agent_host] = client_host; | 239 agent_to_client_host_[agent_host] = client_host; |
243 client_to_agent_host_[client_host] = agent_host; | 240 client_to_agent_host_[client_host] = agent_host; |
244 agent_host->set_close_listener(this); | 241 agent_host->set_close_listener(this); |
245 | 242 |
246 int process_id = agent_host->GetRenderProcessId(); | 243 int process_id = agent_host->GetRenderProcessId(); |
247 if (process_id != -1) | 244 if (process_id != -1) |
248 ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies(process_id); | 245 ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies(process_id); |
249 } | 246 } |
250 | 247 |
251 void DevToolsManager::UnbindClientHost(DevToolsAgentHost* agent_host, | 248 void DevToolsManagerImpl::UnbindClientHost(DevToolsAgentHost* agent_host, |
252 DevToolsClientHost* client_host) { | 249 DevToolsClientHost* client_host) { |
253 DCHECK(agent_host); | 250 DCHECK(agent_host); |
254 DCHECK(agent_to_client_host_.find(agent_host)->second == | 251 DCHECK(agent_to_client_host_.find(agent_host)->second == |
255 client_host); | 252 client_host); |
256 DCHECK(client_to_agent_host_.find(client_host)->second == | 253 DCHECK(client_to_agent_host_.find(client_host)->second == |
257 agent_host); | 254 agent_host); |
258 agent_host->set_close_listener(NULL); | 255 agent_host->set_close_listener(NULL); |
259 | 256 |
260 agent_to_client_host_.erase(agent_host); | 257 agent_to_client_host_.erase(agent_host); |
261 client_to_agent_host_.erase(client_host); | 258 client_to_agent_host_.erase(client_host); |
262 agent_runtime_states_.erase(agent_host); | 259 agent_runtime_states_.erase(agent_host); |
(...skipping 12 matching lines...) Expand all Loading... |
275 for (AgentToClientHostMap::iterator it = agent_to_client_host_.begin(); | 272 for (AgentToClientHostMap::iterator it = agent_to_client_host_.begin(); |
276 it != agent_to_client_host_.end(); | 273 it != agent_to_client_host_.end(); |
277 ++it) { | 274 ++it) { |
278 if (it->first->GetRenderProcessId() == process_id) | 275 if (it->first->GetRenderProcessId() == process_id) |
279 return; | 276 return; |
280 } | 277 } |
281 // We've disconnected from the last renderer -> revoke cookie permissions. | 278 // We've disconnected from the last renderer -> revoke cookie permissions. |
282 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id); | 279 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id); |
283 } | 280 } |
284 | 281 |
285 void DevToolsManager::CloseAllClientHosts() { | 282 void DevToolsManagerImpl::CloseAllClientHosts() { |
286 std::vector<DevToolsAgentHost*> agents; | 283 std::vector<DevToolsAgentHost*> agents; |
287 for (AgentToClientHostMap::iterator it = | 284 for (AgentToClientHostMap::iterator it = |
288 agent_to_client_host_.begin(); | 285 agent_to_client_host_.begin(); |
289 it != agent_to_client_host_.end(); ++it) { | 286 it != agent_to_client_host_.end(); ++it) { |
290 agents.push_back(it->first); | 287 agents.push_back(it->first); |
291 } | 288 } |
292 for (std::vector<DevToolsAgentHost*>::iterator it = agents.begin(); | 289 for (std::vector<DevToolsAgentHost*>::iterator it = agents.begin(); |
293 it != agents.end(); ++it) { | 290 it != agents.end(); ++it) { |
294 UnregisterDevToolsClientHostFor(*it); | 291 UnregisterDevToolsClientHostFor(*it); |
295 } | 292 } |
296 } | 293 } |
| 294 |
| 295 } // namespace content |
OLD | NEW |