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