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