Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: content/browser/debugger/devtools_manager_impl.cc

Issue 8549022: Define DevTools content API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments addressed Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
19 #include "content/public/browser/content_browser_client.h" 18 #include "content/public/browser/devtools/devtools_client_host.h"
19 #include "content/public/browser/devtools/devtools_agent_host_registry.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_types.h"
20 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
21 23
22 using content::BrowserThread; 24 using content::BrowserThread;
25 using content::DevToolsAgentHost;
26 using content::DevToolsAgentHostRegistry;
jam 2011/11/21 21:16:04 nit: these using statements aren't needed anymore?
yurys 2011/11/30 15:49:43 Done.
27
28 namespace content {
23 29
24 // static 30 // static
25 DevToolsManager* DevToolsManager::GetInstance() { 31 DevToolsManager* DevToolsManager::GetInstance() {
26 return content::GetContentClient()->browser()->GetDevToolsManager(); 32 return DevToolsManagerImpl::GetInstance();
27 } 33 }
28 34
29 DevToolsManager::DevToolsManager() 35 // static
30 : last_orphan_cookie_(0) { 36 DevToolsManagerImpl* DevToolsManagerImpl::GetInstance() {
37 return Singleton<DevToolsManagerImpl,
38 LeakySingletonTraits<DevToolsManagerImpl> >::get();
31 } 39 }
32 40
33 DevToolsManager::~DevToolsManager() { 41 DevToolsManagerImpl::DevToolsManagerImpl()
42 : last_orphan_cookie_(0) {
43 notification_registrar_.Add(this,
44 content::NOTIFICATION_MAIN_MESSAGE_LOOP_EXITING,
45 content::NotificationService::AllSources());
46 }
47
48 DevToolsManagerImpl::~DevToolsManagerImpl() {
34 DCHECK(agent_to_client_host_.empty()); 49 DCHECK(agent_to_client_host_.empty());
35 DCHECK(client_to_agent_host_.empty()); 50 DCHECK(client_to_agent_host_.empty());
36 // By the time we destroy devtools manager, all orphan client hosts should 51 // By the time we destroy devtools manager, all orphan client hosts should
37 // have been delelted, no need to notify them upon tab closing. 52 // have been delelted, no need to notify them upon tab closing.
38 DCHECK(orphan_client_hosts_.empty()); 53 DCHECK(orphan_client_hosts_.empty());
39 } 54 }
40 55
41 DevToolsClientHost* DevToolsManager::GetDevToolsClientHostFor( 56 DevToolsClientHost* DevToolsManagerImpl::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) { 57 DevToolsAgentHost* agent_host) {
50 AgentToClientHostMap::iterator it = agent_to_client_host_.find(agent_host); 58 AgentToClientHostMap::iterator it = agent_to_client_host_.find(agent_host);
51 if (it != agent_to_client_host_.end()) 59 if (it != agent_to_client_host_.end())
52 return it->second; 60 return it->second;
53 return NULL; 61 return NULL;
54 } 62 }
55 63
56 void DevToolsManager::RegisterDevToolsClientHostFor( 64 void DevToolsManagerImpl::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, 65 DevToolsAgentHost* agent_host,
67 DevToolsClientHost* client_host) { 66 DevToolsClientHost* client_host) {
68 BindClientHost(agent_host, client_host); 67 BindClientHost(agent_host, client_host);
69 agent_host->Attach(); 68 agent_host->Attach();
70 client_host->set_close_listener(this); 69 client_host->set_close_listener(this);
71 } 70 }
72 71
73 bool DevToolsManager::ForwardToDevToolsAgent(DevToolsClientHost* from, 72 bool DevToolsManagerImpl::ForwardToDevToolsAgent(DevToolsClientHost* from,
74 const IPC::Message& message) { 73 const IPC::Message& message) {
75 DevToolsAgentHost* agent_host = GetAgentHost(from); 74 DevToolsAgentHost* agent_host = GetAgentHost(from);
76 if (!agent_host) 75 if (!agent_host)
77 return false; 76 return false;
78 77
79 agent_host->SendMessageToAgent(new IPC::Message(message)); 78 agent_host->SendMessageToAgent(new IPC::Message(message));
80 return true; 79 return true;
81 } 80 }
82 81
83 void DevToolsManager::ForwardToDevToolsClient(DevToolsAgentHost* agent_host, 82 void DevToolsManagerImpl::ForwardToDevToolsClient(DevToolsAgentHost* agent_host,
84 const IPC::Message& message) { 83 const IPC::Message& message) {
85 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); 84 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host);
86 if (!client_host) { 85 if (!client_host) {
87 // Client window was closed while there were messages 86 // Client window was closed while there were messages
88 // being sent to it. 87 // being sent to it.
89 return; 88 return;
90 } 89 }
91 client_host->SendMessageToClient(message); 90 client_host->SendMessageToClient(message);
92 } 91 }
93 92
94 void DevToolsManager::SaveAgentRuntimeState(DevToolsAgentHost* agent_host, 93 void DevToolsManagerImpl::SaveAgentRuntimeState(DevToolsAgentHost* agent_host,
95 const std::string& state) { 94 const std::string& state) {
96 agent_runtime_states_[agent_host] = state; 95 agent_runtime_states_[agent_host] = state;
97 } 96 }
98 97
99 void DevToolsManager::ClientHostClosing(DevToolsClientHost* client_host) { 98 void DevToolsManagerImpl::ClientHostClosing(DevToolsClientHost* client_host) {
100 DevToolsAgentHost* agent_host = GetAgentHost(client_host); 99 DevToolsAgentHost* agent_host = GetAgentHost(client_host);
101 if (!agent_host) { 100 if (!agent_host) {
102 // It might be in the list of orphan client hosts, remove it from there. 101 // It might be in the list of orphan client hosts, remove it from there.
103 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin(); 102 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin();
104 it != orphan_client_hosts_.end(); ++it) { 103 it != orphan_client_hosts_.end(); ++it) {
105 if (it->second.first == client_host) { 104 if (it->second.first == client_host) {
106 orphan_client_hosts_.erase(it->first); 105 orphan_client_hosts_.erase(it->first);
107 return; 106 return;
108 } 107 }
109 } 108 }
110 return; 109 return;
111 } 110 }
112 111
113 agent_host->NotifyClientClosing(); 112 agent_host->NotifyClientClosing();
114 113
115 UnbindClientHost(agent_host, client_host); 114 UnbindClientHost(agent_host, client_host);
116 } 115 }
117 116
118 void DevToolsManager::AgentHostClosing(DevToolsAgentHost* agent_host) { 117 void DevToolsManagerImpl::AgentHostClosing(DevToolsAgentHost* agent_host) {
119 UnregisterDevToolsClientHostFor(agent_host); 118 UnregisterDevToolsClientHostFor(agent_host);
120 } 119 }
121 120
122 DevToolsAgentHost* DevToolsManager::GetAgentHost( 121 void DevToolsManagerImpl::Observe(int type,
122 const content::NotificationSource& source,
123 const content::NotificationDetails& details) {
124 delete this;
jam 2011/11/21 21:16:04 why do you care about deleting this object at shut
yurys 2011/11/30 15:49:43 Removed this method and all the stuff related to t
125 }
126
127 DevToolsAgentHost* DevToolsManagerImpl::GetAgentHost(
123 DevToolsClientHost* client_host) { 128 DevToolsClientHost* client_host) {
124 ClientHostToInspectedRvhMap::iterator it = 129 ClientHostToInspectedRvhMap::iterator it =
125 client_to_agent_host_.find(client_host); 130 client_to_agent_host_.find(client_host);
126 if (it != client_to_agent_host_.end()) 131 if (it != client_to_agent_host_.end())
127 return it->second; 132 return it->second;
128 return NULL; 133 return NULL;
129 } 134 }
130 135
131 void DevToolsManager::UnregisterDevToolsClientHostFor( 136 void DevToolsManagerImpl::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) { 137 DevToolsAgentHost* agent_host) {
140 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); 138 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host);
141 if (!client_host) 139 if (!client_host)
142 return; 140 return;
143 UnbindClientHost(agent_host, client_host); 141 UnbindClientHost(agent_host, client_host);
144 client_host->InspectedTabClosing(); 142 client_host->InspectedTabClosing();
145 } 143 }
146 144
147 void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh, 145 void DevToolsManagerImpl::OnNavigatingToPendingEntry(RenderViewHost* rvh,
148 RenderViewHost* dest_rvh, 146 RenderViewHost* dest_rvh,
149 const GURL& gurl) { 147 const GURL& gurl) {
150 if (rvh == dest_rvh && rvh->render_view_termination_status() == 148 if (rvh == dest_rvh && rvh->render_view_termination_status() ==
151 base::TERMINATION_STATUS_STILL_RUNNING) 149 base::TERMINATION_STATUS_STILL_RUNNING)
152 return; 150 return;
153 int cookie = DetachClientHost(rvh); 151 int cookie = DetachClientHost(rvh);
154 if (cookie != -1) { 152 if (cookie != -1) {
155 // Navigating to URL in the inspected window. 153 // Navigating to URL in the inspected window.
156 AttachClientHost(cookie, dest_rvh); 154 AttachClientHost(cookie, dest_rvh);
157 155
158 DevToolsClientHost* client_host = GetDevToolsClientHostFor(dest_rvh); 156 DevToolsAgentHost* dest_agent_host =
157 DevToolsAgentHostRegistry::GetDevToolsAgentHost(dest_rvh);
158 DevToolsClientHost* client_host = GetDevToolsClientHostFor(
159 dest_agent_host);
159 client_host->FrameNavigating(gurl.spec()); 160 client_host->FrameNavigating(gurl.spec());
160 } 161 }
161 } 162 }
162 163
163 void DevToolsManager::OnCancelPendingNavigation(RenderViewHost* pending, 164 void DevToolsManagerImpl::OnCancelPendingNavigation(RenderViewHost* pending,
164 RenderViewHost* current) { 165 RenderViewHost* current) {
165 int cookie = DetachClientHost(pending); 166 int cookie = DetachClientHost(pending);
166 if (cookie != -1) { 167 if (cookie != -1) {
167 // Navigating to URL in the inspected window. 168 // Navigating to URL in the inspected window.
168 AttachClientHost(cookie, current); 169 AttachClientHost(cookie, current);
169 } 170 }
170 } 171 }
171 172
172 void DevToolsManager::TabReplaced(TabContents* old_tab, 173 void DevToolsManagerImpl::TabReplaced(TabContents* old_tab,
173 TabContents* new_tab) { 174 TabContents* new_tab) {
174 RenderViewHost* old_rvh = old_tab->render_view_host(); 175 RenderViewHost* old_rvh = old_tab->render_view_host();
175 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_rvh); 176 if (!DevToolsAgentHostRegistry::HasDevToolsAgentHost(old_rvh))
177 return;
178
179 DevToolsAgentHost* old_agent_host =
180 DevToolsAgentHostRegistry::GetDevToolsAgentHost(old_rvh);
181 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_agent_host);
176 if (!client_host) 182 if (!client_host)
177 return; // Didn't know about old_tab. 183 return; // Didn't know about old_tab.
178 int cookie = DetachClientHost(old_rvh); 184 int cookie = DetachClientHost(old_rvh);
179 if (cookie == -1) 185 if (cookie == -1)
180 return; // Didn't know about old_tab. 186 return; // Didn't know about old_tab.
181 187
182 client_host->TabReplaced(new_tab); 188 client_host->TabReplaced(new_tab);
183 AttachClientHost(cookie, new_tab->render_view_host()); 189 AttachClientHost(cookie, new_tab->render_view_host());
184 } 190 }
185 191
186 int DevToolsManager::DetachClientHost(RenderViewHost* from_rvh) { 192 int DevToolsManagerImpl::DetachClientHost(RenderViewHost* from_rvh) {
187 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( 193 DevToolsAgentHost* agent_host =
188 from_rvh); 194 DevToolsAgentHostRegistry::GetDevToolsAgentHost(from_rvh);
189 return DetachClientHost(agent_host); 195 return DetachClientHost(agent_host);
190 } 196 }
191 197
192 int DevToolsManager::DetachClientHost(DevToolsAgentHost* agent_host) { 198 int DevToolsManagerImpl::DetachClientHost(DevToolsAgentHost* agent_host) {
193 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host); 199 DevToolsClientHost* client_host = GetDevToolsClientHostFor(agent_host);
194 if (!client_host) 200 if (!client_host)
195 return -1; 201 return -1;
196 202
197 int cookie = last_orphan_cookie_++; 203 int cookie = last_orphan_cookie_++;
198 orphan_client_hosts_[cookie] = 204 orphan_client_hosts_[cookie] =
199 std::pair<DevToolsClientHost*, std::string>( 205 std::pair<DevToolsClientHost*, std::string>(
200 client_host, agent_runtime_states_[agent_host]); 206 client_host, agent_runtime_states_[agent_host]);
201 207
202 UnbindClientHost(agent_host, client_host); 208 UnbindClientHost(agent_host, client_host);
203 return cookie; 209 return cookie;
204 } 210 }
205 211
206 void DevToolsManager::AttachClientHost(int client_host_cookie, 212 void DevToolsManagerImpl::AttachClientHost(int client_host_cookie,
207 RenderViewHost* to_rvh) { 213 RenderViewHost* to_rvh) {
208 DevToolsAgentHost* agent_host = RenderViewDevToolsAgentHost::FindFor( 214 DevToolsAgentHost* agent_host =
209 to_rvh); 215 DevToolsAgentHostRegistry::GetDevToolsAgentHost(to_rvh);
210 AttachClientHost(client_host_cookie, agent_host); 216 AttachClientHost(client_host_cookie, agent_host);
211 } 217 }
212 218
213 void DevToolsManager::AttachClientHost(int client_host_cookie, 219 void DevToolsManagerImpl::AttachClientHost(int client_host_cookie,
214 DevToolsAgentHost* agent_host) { 220 DevToolsAgentHost* agent_host) {
215 OrphanClientHosts::iterator it = orphan_client_hosts_.find( 221 OrphanClientHosts::iterator it = orphan_client_hosts_.find(
216 client_host_cookie); 222 client_host_cookie);
217 if (it == orphan_client_hosts_.end()) 223 if (it == orphan_client_hosts_.end())
218 return; 224 return;
219 225
220 DevToolsClientHost* client_host = (*it).second.first; 226 DevToolsClientHost* client_host = (*it).second.first;
221 const std::string& state = (*it).second.second; 227 const std::string& state = (*it).second.second;
222 BindClientHost(agent_host, client_host); 228 BindClientHost(agent_host, client_host);
223 agent_host->Reattach(state); 229 agent_host->Reattach(state);
224 agent_runtime_states_[agent_host] = state; 230 agent_runtime_states_[agent_host] = state;
225 231
226 orphan_client_hosts_.erase(it); 232 orphan_client_hosts_.erase(it);
227 } 233 }
228 234
229 void DevToolsManager::BindClientHost( 235 void DevToolsManagerImpl::BindClientHost(
230 DevToolsAgentHost* agent_host, 236 DevToolsAgentHost* agent_host,
231 DevToolsClientHost* client_host) { 237 DevToolsClientHost* client_host) {
232 DCHECK(agent_to_client_host_.find(agent_host) == 238 DCHECK(agent_to_client_host_.find(agent_host) ==
233 agent_to_client_host_.end()); 239 agent_to_client_host_.end());
234 DCHECK(client_to_agent_host_.find(client_host) == 240 DCHECK(client_to_agent_host_.find(client_host) ==
235 client_to_agent_host_.end()); 241 client_to_agent_host_.end());
236 242
237 if (client_to_agent_host_.empty()) { 243 if (client_to_agent_host_.empty()) {
238 BrowserThread::PostTask( 244 BrowserThread::PostTask(
239 BrowserThread::IO, 245 BrowserThread::IO,
240 FROM_HERE, 246 FROM_HERE,
241 base::Bind(&DevToolsNetLogObserver::Attach)); 247 base::Bind(&DevToolsNetLogObserver::Attach));
242 } 248 }
243 agent_to_client_host_[agent_host] = client_host; 249 agent_to_client_host_[agent_host] = client_host;
244 client_to_agent_host_[client_host] = agent_host; 250 client_to_agent_host_[client_host] = agent_host;
245 agent_host->set_close_listener(this); 251 agent_host->set_close_listener(this);
246 252
247 int process_id = agent_host->GetRenderProcessId(); 253 int process_id = agent_host->GetRenderProcessId();
248 if (process_id != -1) 254 if (process_id != -1)
249 ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies(process_id); 255 ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies(process_id);
250 } 256 }
251 257
252 void DevToolsManager::UnbindClientHost(DevToolsAgentHost* agent_host, 258 void DevToolsManagerImpl::UnbindClientHost(DevToolsAgentHost* agent_host,
253 DevToolsClientHost* client_host) { 259 DevToolsClientHost* client_host) {
254 DCHECK(agent_host); 260 DCHECK(agent_host);
255 DCHECK(agent_to_client_host_.find(agent_host)->second == 261 DCHECK(agent_to_client_host_.find(agent_host)->second ==
256 client_host); 262 client_host);
257 DCHECK(client_to_agent_host_.find(client_host)->second == 263 DCHECK(client_to_agent_host_.find(client_host)->second ==
258 agent_host); 264 agent_host);
259 agent_host->set_close_listener(NULL); 265 agent_host->set_close_listener(NULL);
260 266
261 agent_to_client_host_.erase(agent_host); 267 agent_to_client_host_.erase(agent_host);
262 client_to_agent_host_.erase(client_host); 268 client_to_agent_host_.erase(client_host);
(...skipping 13 matching lines...) Expand all
276 for (AgentToClientHostMap::iterator it = agent_to_client_host_.begin(); 282 for (AgentToClientHostMap::iterator it = agent_to_client_host_.begin();
277 it != agent_to_client_host_.end(); 283 it != agent_to_client_host_.end();
278 ++it) { 284 ++it) {
279 if (it->first->GetRenderProcessId() == process_id) 285 if (it->first->GetRenderProcessId() == process_id)
280 return; 286 return;
281 } 287 }
282 // We've disconnected from the last renderer -> revoke cookie permissions. 288 // We've disconnected from the last renderer -> revoke cookie permissions.
283 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id); 289 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id);
284 } 290 }
285 291
286 void DevToolsManager::CloseAllClientHosts() { 292 void DevToolsManagerImpl::CloseAllClientHosts() {
287 std::vector<DevToolsAgentHost*> agents; 293 std::vector<DevToolsAgentHost*> agents;
288 for (AgentToClientHostMap::iterator it = 294 for (AgentToClientHostMap::iterator it =
289 agent_to_client_host_.begin(); 295 agent_to_client_host_.begin();
290 it != agent_to_client_host_.end(); ++it) { 296 it != agent_to_client_host_.end(); ++it) {
291 agents.push_back(it->first); 297 agents.push_back(it->first);
292 } 298 }
293 for (std::vector<DevToolsAgentHost*>::iterator it = agents.begin(); 299 for (std::vector<DevToolsAgentHost*>::iterator it = agents.begin();
294 it != agents.end(); ++it) { 300 it != agents.end(); ++it) {
295 UnregisterDevToolsClientHostFor(*it); 301 UnregisterDevToolsClientHostFor(*it);
296 } 302 }
297 } 303 }
304
305 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698