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

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

Issue 7322004: DevTools: devtools manager should know nothing about DevToolsWindow (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 5 months 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.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/auto_reset.h"
10 #include "base/message_loop.h" 9 #include "base/message_loop.h"
11 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/io_thread.h" 11 #include "chrome/browser/io_thread.h"
13 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
16 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
17 #include "content/browser/browsing_instance.h" 16 #include "content/browser/browsing_instance.h"
18 #include "content/browser/child_process_security_policy.h" 17 #include "content/browser/child_process_security_policy.h"
19 #include "content/browser/debugger/devtools_client_host.h" 18 #include "content/browser/debugger/devtools_client_host.h"
20 #include "content/browser/debugger/devtools_netlog_observer.h" 19 #include "content/browser/debugger/devtools_netlog_observer.h"
21 #include "content/browser/debugger/devtools_window.h"
22 #include "content/browser/renderer_host/render_view_host.h" 20 #include "content/browser/renderer_host/render_view_host.h"
23 #include "content/browser/site_instance.h" 21 #include "content/browser/site_instance.h"
24 #include "content/common/devtools_messages.h" 22 #include "content/common/devtools_messages.h"
25 #include "content/common/notification_service.h" 23 #include "content/common/notification_service.h"
26 #include "googleurl/src/gurl.h" 24 #include "googleurl/src/gurl.h"
25 #include "devtools_manager.h"
27 26
28 // static 27 // static
29 DevToolsManager* DevToolsManager::GetInstance() { 28 DevToolsManager* DevToolsManager::GetInstance() {
30 // http://crbug.com/47806 this method may be called when BrowserProcess 29 // http://crbug.com/47806 this method may be called when BrowserProcess
31 // has already been destroyed. 30 // has already been destroyed.
32 if (!g_browser_process) 31 if (!g_browser_process)
33 return NULL; 32 return NULL;
34 return g_browser_process->devtools_manager(); 33 return g_browser_process->devtools_manager();
35 } 34 }
36 35
37 // static 36 // static
38 void DevToolsManager::RegisterUserPrefs(PrefService* prefs) { 37 void DevToolsManager::RegisterUserPrefs(PrefService* prefs) {
39 prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked, 38 prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked,
40 true, 39 true,
41 PrefService::UNSYNCABLE_PREF); 40 PrefService::UNSYNCABLE_PREF);
42 } 41 }
43 42
44 DevToolsManager::DevToolsManager() 43 DevToolsManager::DevToolsManager()
45 : inspected_rvh_for_reopen_(NULL), 44 : last_orphan_cookie_(0) {
46 in_initial_show_(false),
47 last_orphan_cookie_(0) {
48 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, 45 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED,
49 NotificationService::AllSources()); 46 NotificationService::AllSources());
50 } 47 }
51 48
52 DevToolsManager::~DevToolsManager() { 49 DevToolsManager::~DevToolsManager() {
53 DCHECK(inspected_rvh_to_client_host_.empty()); 50 DCHECK(inspected_rvh_to_client_host_.empty());
54 DCHECK(client_host_to_inspected_rvh_.empty()); 51 DCHECK(client_host_to_inspected_rvh_.empty());
55 // By the time we destroy devtools manager, all orphan client hosts should 52 // By the time we destroy devtools manager, all orphan client hosts should
56 // have been delelted, no need to notify them upon tab closing. 53 // have been delelted, no need to notify them upon tab closing.
57 DCHECK(orphan_client_hosts_.empty()); 54 DCHECK(orphan_client_hosts_.empty());
(...skipping 12 matching lines...) Expand all
70 RenderViewHost* inspected_rvh, 67 RenderViewHost* inspected_rvh,
71 DevToolsClientHost* client_host) { 68 DevToolsClientHost* client_host) {
72 DCHECK(!GetDevToolsClientHostFor(inspected_rvh)); 69 DCHECK(!GetDevToolsClientHostFor(inspected_rvh));
73 70
74 DevToolsRuntimeProperties initial_properties; 71 DevToolsRuntimeProperties initial_properties;
75 BindClientHost(inspected_rvh, client_host, initial_properties); 72 BindClientHost(inspected_rvh, client_host, initial_properties);
76 client_host->set_close_listener(this); 73 client_host->set_close_listener(this);
77 SendAttachToAgent(inspected_rvh); 74 SendAttachToAgent(inspected_rvh);
78 } 75 }
79 76
80 void DevToolsManager::ForwardToDevToolsAgent(
81 RenderViewHost* client_rvh,
82 const IPC::Message& message) {
83 DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
84 if (client_host)
85 ForwardToDevToolsAgent(client_host, message);
86 }
87
88 void DevToolsManager::ForwardToDevToolsAgent(DevToolsClientHost* from, 77 void DevToolsManager::ForwardToDevToolsAgent(DevToolsClientHost* from,
89 const IPC::Message& message) { 78 const IPC::Message& message) {
90 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(from); 79 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(from);
91 if (!inspected_rvh) { 80 if (!inspected_rvh) {
92 // TODO(yurys): notify client that the agent is no longer available 81 // TODO(yurys): notify client that the agent is no longer available
93 NOTREACHED(); 82 NOTREACHED();
94 return; 83 return;
95 } 84 }
96 85
97 IPC::Message* m = new IPC::Message(message); 86 IPC::Message* m = new IPC::Message(message);
98 m->set_routing_id(inspected_rvh->routing_id()); 87 m->set_routing_id(inspected_rvh->routing_id());
99 inspected_rvh->Send(m); 88 inspected_rvh->Send(m);
100 } 89 }
101 90
102 void DevToolsManager::ForwardToDevToolsClient(RenderViewHost* inspected_rvh, 91 void DevToolsManager::ForwardToDevToolsClient(RenderViewHost* inspected_rvh,
103 const IPC::Message& message) { 92 const IPC::Message& message) {
104 DevToolsClientHost* client_host = GetDevToolsClientHostFor(inspected_rvh); 93 DevToolsClientHost* client_host = GetDevToolsClientHostFor(inspected_rvh);
105 if (!client_host) { 94 if (!client_host) {
106 // Client window was closed while there were messages 95 // Client window was closed while there were messages
107 // being sent to it. 96 // being sent to it.
108 return; 97 return;
109 } 98 }
110 client_host->SendMessageToClient(message); 99 client_host->SendMessageToClient(message);
111 } 100 }
112 101
113 void DevToolsManager::ActivateWindow(RenderViewHost* client_rvh) { 102 void DevToolsManager::CloseClient(DevToolsClientHost* client_host) {
114 DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh); 103 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(client_host);
115 if (!client_host) 104 DCHECK(inspected_rvh);
116 return; 105 UnregisterDevToolsClientHostFor(inspected_rvh);
117
118 DevToolsWindow* window = client_host->AsDevToolsWindow();
119 DCHECK(window);
120 window->Activate();
121 }
122
123 void DevToolsManager::CloseWindow(RenderViewHost* client_rvh) {
124 DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
125 if (client_host) {
126 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(client_host);
127 DCHECK(inspected_rvh);
128 UnregisterDevToolsClientHostFor(inspected_rvh);
129 }
130 }
131
132 void DevToolsManager::RequestDockWindow(RenderViewHost* client_rvh) {
133 ReopenWindow(client_rvh, true);
134 }
135
136 void DevToolsManager::RequestUndockWindow(RenderViewHost* client_rvh) {
137 ReopenWindow(client_rvh, false);
138 }
139
140 void DevToolsManager::OpenDevToolsWindow(RenderViewHost* inspected_rvh) {
141 ToggleDevToolsWindow(
142 inspected_rvh,
143 true,
144 DEVTOOLS_TOGGLE_ACTION_NONE);
145 }
146
147 void DevToolsManager::ToggleDevToolsWindow(
148 RenderViewHost* inspected_rvh,
149 DevToolsToggleAction action) {
150 ToggleDevToolsWindow(inspected_rvh, false, action);
151 } 106 }
152 107
153 void DevToolsManager::RuntimePropertyChanged(RenderViewHost* inspected_rvh, 108 void DevToolsManager::RuntimePropertyChanged(RenderViewHost* inspected_rvh,
154 const std::string& name, 109 const std::string& name,
155 const std::string& value) { 110 const std::string& value) {
156 RuntimePropertiesMap::iterator it = 111 RuntimePropertiesMap::iterator it =
157 runtime_properties_map_.find(inspected_rvh); 112 runtime_properties_map_.find(inspected_rvh);
158 if (it == runtime_properties_map_.end()) { 113 if (it == runtime_properties_map_.end()) {
159 std::pair<RenderViewHost*, DevToolsRuntimeProperties> value( 114 std::pair<RenderViewHost*, DevToolsRuntimeProperties> value(
160 inspected_rvh, 115 inspected_rvh,
161 DevToolsRuntimeProperties()); 116 DevToolsRuntimeProperties());
162 it = runtime_properties_map_.insert(value).first; 117 it = runtime_properties_map_.insert(value).first;
163 } 118 }
164 it->second[name] = value; 119 it->second[name] = value;
165 } 120 }
166 121
167 void DevToolsManager::InspectElement(RenderViewHost* inspected_rvh, 122 void DevToolsManager::SendInspectElement(RenderViewHost* inspected_rvh,
168 int x, 123 int x,
169 int y) { 124 int y) {
170 IPC::Message* m = new DevToolsAgentMsg_InspectElement(x, y); 125 IPC::Message* m = new DevToolsAgentMsg_InspectElement(x, y);
171 m->set_routing_id(inspected_rvh->routing_id()); 126 m->set_routing_id(inspected_rvh->routing_id());
172 inspected_rvh->Send(m); 127 inspected_rvh->Send(m);
173 // TODO(loislo): we should initiate DevTools window opening from within
174 // renderer. Otherwise, we still can hit a race condition here.
175 OpenDevToolsWindow(inspected_rvh);
176 } 128 }
177 129
178 void DevToolsManager::ClientHostClosing(DevToolsClientHost* host) { 130 void DevToolsManager::ClientHostClosing(DevToolsClientHost* host) {
179 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(host); 131 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(host);
180 if (!inspected_rvh) { 132 if (!inspected_rvh) {
181 // It might be in the list of orphan client hosts, remove it from there. 133 // It might be in the list of orphan client hosts, remove it from there.
182 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin(); 134 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin();
183 it != orphan_client_hosts_.end(); ++it) { 135 it != orphan_client_hosts_.end(); ++it) {
184 if (it->second.first == host) { 136 if (it->second.first == host) {
185 orphan_client_hosts_.erase(it->first); 137 orphan_client_hosts_.erase(it->first);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh); 170 DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh);
219 if (!host) 171 if (!host)
220 return; 172 return;
221 UnbindClientHost(inspected_rvh, host); 173 UnbindClientHost(inspected_rvh, host);
222 host->InspectedTabClosing(); 174 host->InspectedTabClosing();
223 } 175 }
224 176
225 void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh, 177 void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh,
226 RenderViewHost* dest_rvh, 178 RenderViewHost* dest_rvh,
227 const GURL& gurl) { 179 const GURL& gurl) {
228 if (in_initial_show_) {
229 // Mute this even in case it is caused by the initial show routines.
230 return;
231 }
232
233 int cookie = DetachClientHost(rvh); 180 int cookie = DetachClientHost(rvh);
234 if (cookie != -1) { 181 if (cookie != -1) {
235 // Navigating to URL in the inspected window. 182 // Navigating to URL in the inspected window.
236 AttachClientHost(cookie, dest_rvh); 183 AttachClientHost(cookie, dest_rvh);
237 184
238 DevToolsClientHost* client_host = GetDevToolsClientHostFor(dest_rvh); 185 DevToolsClientHost* client_host = GetDevToolsClientHostFor(dest_rvh);
239 client_host->FrameNavigating(gurl.spec()); 186 client_host->FrameNavigating(gurl.spec());
240
241 return;
242 }
243
244 // Iterate over client hosts and if there is one that has render view host
245 // changing, reopen entire client window (this must be caused by the user
246 // manually refreshing its content).
247 for (ClientHostToInspectedRvhMap::iterator it =
248 client_host_to_inspected_rvh_.begin();
249 it != client_host_to_inspected_rvh_.end(); ++it) {
250 DevToolsWindow* window = it->first->AsDevToolsWindow();
251 if (window && window->GetRenderViewHost() == rvh) {
252 inspected_rvh_for_reopen_ = it->second;
253 MessageLoop::current()->PostTask(FROM_HERE,
254 NewRunnableMethod(this,
255 &DevToolsManager::ForceReopenWindow));
256 return;
257 }
258 } 187 }
259 } 188 }
260 189
261 void DevToolsManager::TabReplaced(TabContentsWrapper* old_tab, 190 void DevToolsManager::TabReplaced(TabContentsWrapper* old_tab,
262 TabContentsWrapper* new_tab) { 191 TabContentsWrapper* new_tab) {
263 RenderViewHost* old_rvh = old_tab->tab_contents()->render_view_host(); 192 RenderViewHost* old_rvh = old_tab->tab_contents()->render_view_host();
264 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_rvh); 193 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_rvh);
265 if (!client_host) 194 if (!client_host)
266 return; // Didn't know about old_tab. 195 return; // Didn't know about old_tab.
267 int cookie = DetachClientHost(old_rvh); 196 int cookie = DetachClientHost(old_rvh);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 } 248 }
320 249
321 void DevToolsManager::SendDetachToAgent(RenderViewHost* inspected_rvh) { 250 void DevToolsManager::SendDetachToAgent(RenderViewHost* inspected_rvh) {
322 if (inspected_rvh) { 251 if (inspected_rvh) {
323 IPC::Message* m = new DevToolsAgentMsg_Detach(); 252 IPC::Message* m = new DevToolsAgentMsg_Detach();
324 m->set_routing_id(inspected_rvh->routing_id()); 253 m->set_routing_id(inspected_rvh->routing_id());
325 inspected_rvh->Send(m); 254 inspected_rvh->Send(m);
326 } 255 }
327 } 256 }
328 257
329 void DevToolsManager::ForceReopenWindow() {
330 if (inspected_rvh_for_reopen_) {
331 RenderViewHost* inspected_rvn = inspected_rvh_for_reopen_;
332 UnregisterDevToolsClientHostFor(inspected_rvn);
333 OpenDevToolsWindow(inspected_rvn);
334 }
335 }
336
337 DevToolsClientHost* DevToolsManager::FindOwnerDevToolsClientHost(
338 RenderViewHost* client_rvh) {
339 for (InspectedRvhToClientHostMap::iterator it =
340 inspected_rvh_to_client_host_.begin();
341 it != inspected_rvh_to_client_host_.end();
342 ++it) {
343 DevToolsWindow* win = it->second->AsDevToolsWindow();
344 if (!win)
345 continue;
346 if (client_rvh == win->GetRenderViewHost())
347 return it->second;
348 }
349 return NULL;
350 }
351
352 void DevToolsManager::ReopenWindow(RenderViewHost* client_rvh, bool docked) {
353 DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
354 if (!client_host)
355 return;
356 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(client_host);
357 DCHECK(inspected_rvh);
358 inspected_rvh->process()->profile()->GetPrefs()->SetBoolean(
359 prefs::kDevToolsOpenDocked, docked);
360
361 DevToolsWindow* window = client_host->AsDevToolsWindow();
362 DCHECK(window);
363 window->SetDocked(docked);
364 }
365
366 void DevToolsManager::ToggleDevToolsWindow(
367 RenderViewHost* inspected_rvh,
368 bool force_open,
369 DevToolsToggleAction action) {
370 bool do_open = force_open;
371 DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh);
372
373 if (host != NULL && host->AsDevToolsWindow() == NULL) {
374 // Break remote debugging / extension debugging session.
375 UnregisterDevToolsClientHostFor(inspected_rvh);
376 host = NULL;
377 }
378
379 if (!host) {
380 bool docked = inspected_rvh->process()->profile()->GetPrefs()->
381 GetBoolean(prefs::kDevToolsOpenDocked);
382 host = new DevToolsWindow(
383 inspected_rvh->site_instance()->browsing_instance()->profile(),
384 inspected_rvh,
385 docked);
386 RegisterDevToolsClientHostFor(inspected_rvh, host);
387 do_open = true;
388 }
389
390 DevToolsWindow* window = host->AsDevToolsWindow();
391 // If window is docked and visible, we hide it on toggle. If window is
392 // undocked, we show (activate) it.
393 if (!window->is_docked() || do_open) {
394 AutoReset<bool> auto_reset_in_initial_show(&in_initial_show_, true);
395 window->Show(action);
396 } else {
397 UnregisterDevToolsClientHostFor(inspected_rvh);
398 }
399 }
400
401 void DevToolsManager::BindClientHost( 258 void DevToolsManager::BindClientHost(
402 RenderViewHost* inspected_rvh, 259 RenderViewHost* inspected_rvh,
403 DevToolsClientHost* client_host, 260 DevToolsClientHost* client_host,
404 const DevToolsRuntimeProperties& runtime_properties) { 261 const DevToolsRuntimeProperties& runtime_properties) {
405 DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh) == 262 DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh) ==
406 inspected_rvh_to_client_host_.end()); 263 inspected_rvh_to_client_host_.end());
407 DCHECK(client_host_to_inspected_rvh_.find(client_host) == 264 DCHECK(client_host_to_inspected_rvh_.find(client_host) ==
408 client_host_to_inspected_rvh_.end()); 265 client_host_to_inspected_rvh_.end());
409 266
410 if (client_host_to_inspected_rvh_.empty()) { 267 if (client_host_to_inspected_rvh_.empty()) {
(...skipping 19 matching lines...) Expand all
430 client_host_to_inspected_rvh_.erase(client_host); 287 client_host_to_inspected_rvh_.erase(client_host);
431 runtime_properties_map_.erase(inspected_rvh); 288 runtime_properties_map_.erase(inspected_rvh);
432 289
433 if (client_host_to_inspected_rvh_.empty()) { 290 if (client_host_to_inspected_rvh_.empty()) {
434 BrowserThread::PostTask( 291 BrowserThread::PostTask(
435 BrowserThread::IO, 292 BrowserThread::IO,
436 FROM_HERE, 293 FROM_HERE,
437 NewRunnableFunction(&DevToolsNetLogObserver::Detach)); 294 NewRunnableFunction(&DevToolsNetLogObserver::Detach));
438 } 295 }
439 SendDetachToAgent(inspected_rvh); 296 SendDetachToAgent(inspected_rvh);
440 if (inspected_rvh_for_reopen_ == inspected_rvh)
441 inspected_rvh_for_reopen_ = NULL;
442 297
443 int process_id = inspected_rvh->process()->id(); 298 int process_id = inspected_rvh->process()->id();
444 for (InspectedRvhToClientHostMap::iterator it = 299 for (InspectedRvhToClientHostMap::iterator it =
445 inspected_rvh_to_client_host_.begin(); 300 inspected_rvh_to_client_host_.begin();
446 it != inspected_rvh_to_client_host_.end(); 301 it != inspected_rvh_to_client_host_.end();
447 ++it) { 302 ++it) {
448 if (it->first->process()->id() == process_id) 303 if (it->first->process()->id() == process_id)
449 return; 304 return;
450 } 305 }
451 // We've disconnected from the last renderer -> revoke cookie permissions. 306 // We've disconnected from the last renderer -> revoke cookie permissions.
452 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id); 307 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id);
453 } 308 }
454 309
455 void DevToolsManager::CloseAllClientHosts() { 310 void DevToolsManager::CloseAllClientHosts() {
456 std::vector<RenderViewHost*> rhvs; 311 std::vector<RenderViewHost*> rhvs;
457 for (InspectedRvhToClientHostMap::iterator it = 312 for (InspectedRvhToClientHostMap::iterator it =
458 inspected_rvh_to_client_host_.begin(); 313 inspected_rvh_to_client_host_.begin();
459 it != inspected_rvh_to_client_host_.end(); ++it) { 314 it != inspected_rvh_to_client_host_.end(); ++it) {
460 rhvs.push_back(it->first); 315 rhvs.push_back(it->first);
461 } 316 }
462 for (std::vector<RenderViewHost*>::iterator it = rhvs.begin(); 317 for (std::vector<RenderViewHost*>::iterator it = rhvs.begin();
463 it != rhvs.end(); ++it) { 318 it != rhvs.end(); ++it) {
464 UnregisterDevToolsClientHostFor(*it); 319 UnregisterDevToolsClientHostFor(*it);
465 } 320 }
466 } 321 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698