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