| 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 "chrome/browser/debugger/devtools_manager.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/auto_reset.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/debugger/devtools_client_host.h" | |
| 13 #include "chrome/browser/debugger/devtools_netlog_observer.h" | |
| 14 #include "chrome/browser/debugger/devtools_window.h" | |
| 15 #include "chrome/browser/io_thread.h" | |
| 16 #include "chrome/browser/prefs/pref_service.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "content/browser/browsing_instance.h" | |
| 21 #include "content/browser/child_process_security_policy.h" | |
| 22 #include "content/browser/renderer_host/render_view_host.h" | |
| 23 #include "content/browser/site_instance.h" | |
| 24 #include "content/common/devtools_messages.h" | |
| 25 #include "content/common/notification_service.h" | |
| 26 #include "googleurl/src/gurl.h" | |
| 27 | |
| 28 // static | |
| 29 DevToolsManager* DevToolsManager::GetInstance() { | |
| 30 // http://crbug.com/47806 this method may be called when BrowserProcess | |
| 31 // has already been destroyed. | |
| 32 if (!g_browser_process) | |
| 33 return NULL; | |
| 34 return g_browser_process->devtools_manager(); | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 void DevToolsManager::RegisterUserPrefs(PrefService* prefs) { | |
| 39 prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked, | |
| 40 true, | |
| 41 PrefService::UNSYNCABLE_PREF); | |
| 42 } | |
| 43 | |
| 44 DevToolsManager::DevToolsManager() | |
| 45 : inspected_rvh_for_reopen_(NULL), | |
| 46 in_initial_show_(false), | |
| 47 last_orphan_cookie_(0) { | |
| 48 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, | |
| 49 NotificationService::AllSources()); | |
| 50 } | |
| 51 | |
| 52 DevToolsManager::~DevToolsManager() { | |
| 53 DCHECK(inspected_rvh_to_client_host_.empty()); | |
| 54 DCHECK(client_host_to_inspected_rvh_.empty()); | |
| 55 // By the time we destroy devtools manager, all orphan client hosts should | |
| 56 // have been delelted, no need to notify them upon tab closing. | |
| 57 DCHECK(orphan_client_hosts_.empty()); | |
| 58 } | |
| 59 | |
| 60 DevToolsClientHost* DevToolsManager::GetDevToolsClientHostFor( | |
| 61 RenderViewHost* inspected_rvh) { | |
| 62 InspectedRvhToClientHostMap::iterator it = | |
| 63 inspected_rvh_to_client_host_.find(inspected_rvh); | |
| 64 if (it != inspected_rvh_to_client_host_.end()) | |
| 65 return it->second; | |
| 66 return NULL; | |
| 67 } | |
| 68 | |
| 69 void DevToolsManager::RegisterDevToolsClientHostFor( | |
| 70 RenderViewHost* inspected_rvh, | |
| 71 DevToolsClientHost* client_host) { | |
| 72 DCHECK(!GetDevToolsClientHostFor(inspected_rvh)); | |
| 73 | |
| 74 DevToolsRuntimeProperties initial_properties; | |
| 75 BindClientHost(inspected_rvh, client_host, initial_properties); | |
| 76 client_host->set_close_listener(this); | |
| 77 SendAttachToAgent(inspected_rvh); | |
| 78 } | |
| 79 | |
| 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, | |
| 89 const IPC::Message& message) { | |
| 90 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(from); | |
| 91 if (!inspected_rvh) { | |
| 92 // TODO(yurys): notify client that the agent is no longer available | |
| 93 NOTREACHED(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 IPC::Message* m = new IPC::Message(message); | |
| 98 m->set_routing_id(inspected_rvh->routing_id()); | |
| 99 inspected_rvh->Send(m); | |
| 100 } | |
| 101 | |
| 102 void DevToolsManager::ForwardToDevToolsClient(RenderViewHost* inspected_rvh, | |
| 103 const IPC::Message& message) { | |
| 104 DevToolsClientHost* client_host = GetDevToolsClientHostFor(inspected_rvh); | |
| 105 if (!client_host) { | |
| 106 // Client window was closed while there were messages | |
| 107 // being sent to it. | |
| 108 return; | |
| 109 } | |
| 110 client_host->SendMessageToClient(message); | |
| 111 } | |
| 112 | |
| 113 void DevToolsManager::ActivateWindow(RenderViewHost* client_rvh) { | |
| 114 DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh); | |
| 115 if (!client_host) | |
| 116 return; | |
| 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 } | |
| 152 | |
| 153 void DevToolsManager::RuntimePropertyChanged(RenderViewHost* inspected_rvh, | |
| 154 const std::string& name, | |
| 155 const std::string& value) { | |
| 156 RuntimePropertiesMap::iterator it = | |
| 157 runtime_properties_map_.find(inspected_rvh); | |
| 158 if (it == runtime_properties_map_.end()) { | |
| 159 std::pair<RenderViewHost*, DevToolsRuntimeProperties> value( | |
| 160 inspected_rvh, | |
| 161 DevToolsRuntimeProperties()); | |
| 162 it = runtime_properties_map_.insert(value).first; | |
| 163 } | |
| 164 it->second[name] = value; | |
| 165 } | |
| 166 | |
| 167 void DevToolsManager::InspectElement(RenderViewHost* inspected_rvh, | |
| 168 int x, | |
| 169 int y) { | |
| 170 IPC::Message* m = new DevToolsAgentMsg_InspectElement(x, y); | |
| 171 m->set_routing_id(inspected_rvh->routing_id()); | |
| 172 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 } | |
| 177 | |
| 178 void DevToolsManager::ClientHostClosing(DevToolsClientHost* host) { | |
| 179 RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(host); | |
| 180 if (!inspected_rvh) { | |
| 181 // It might be in the list of orphan client hosts, remove it from there. | |
| 182 for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin(); | |
| 183 it != orphan_client_hosts_.end(); ++it) { | |
| 184 if (it->second.first == host) { | |
| 185 orphan_client_hosts_.erase(it->first); | |
| 186 return; | |
| 187 } | |
| 188 } | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 NotificationService::current()->Notify( | |
| 193 NotificationType::DEVTOOLS_WINDOW_CLOSING, | |
| 194 Source<Profile>(inspected_rvh->site_instance()->GetProcess()->profile()), | |
| 195 Details<RenderViewHost>(inspected_rvh)); | |
| 196 | |
| 197 UnbindClientHost(inspected_rvh, host); | |
| 198 } | |
| 199 | |
| 200 void DevToolsManager::Observe(NotificationType type, | |
| 201 const NotificationSource& source, | |
| 202 const NotificationDetails& details) { | |
| 203 DCHECK(type == NotificationType::RENDER_VIEW_HOST_DELETED); | |
| 204 UnregisterDevToolsClientHostFor(Source<RenderViewHost>(source).ptr()); | |
| 205 } | |
| 206 | |
| 207 RenderViewHost* DevToolsManager::GetInspectedRenderViewHost( | |
| 208 DevToolsClientHost* client_host) { | |
| 209 ClientHostToInspectedRvhMap::iterator it = | |
| 210 client_host_to_inspected_rvh_.find(client_host); | |
| 211 if (it != client_host_to_inspected_rvh_.end()) | |
| 212 return it->second; | |
| 213 return NULL; | |
| 214 } | |
| 215 | |
| 216 void DevToolsManager::UnregisterDevToolsClientHostFor( | |
| 217 RenderViewHost* inspected_rvh) { | |
| 218 DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh); | |
| 219 if (!host) | |
| 220 return; | |
| 221 UnbindClientHost(inspected_rvh, host); | |
| 222 host->InspectedTabClosing(); | |
| 223 } | |
| 224 | |
| 225 void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh, | |
| 226 RenderViewHost* dest_rvh, | |
| 227 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); | |
| 234 if (cookie != -1) { | |
| 235 // Navigating to URL in the inspected window. | |
| 236 AttachClientHost(cookie, dest_rvh); | |
| 237 | |
| 238 DevToolsClientHost* client_host = GetDevToolsClientHostFor(dest_rvh); | |
| 239 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 } | |
| 259 } | |
| 260 | |
| 261 void DevToolsManager::TabReplaced(TabContentsWrapper* old_tab, | |
| 262 TabContentsWrapper* new_tab) { | |
| 263 RenderViewHost* old_rvh = old_tab->tab_contents()->render_view_host(); | |
| 264 DevToolsClientHost* client_host = GetDevToolsClientHostFor(old_rvh); | |
| 265 if (!client_host) | |
| 266 return; // Didn't know about old_tab. | |
| 267 int cookie = DetachClientHost(old_rvh); | |
| 268 if (cookie == -1) | |
| 269 return; // Didn't know about old_tab. | |
| 270 | |
| 271 client_host->TabReplaced(new_tab); | |
| 272 AttachClientHost(cookie, new_tab->tab_contents()->render_view_host()); | |
| 273 } | |
| 274 | |
| 275 int DevToolsManager::DetachClientHost(RenderViewHost* from_rvh) { | |
| 276 DevToolsClientHost* client_host = GetDevToolsClientHostFor(from_rvh); | |
| 277 if (!client_host) | |
| 278 return -1; | |
| 279 | |
| 280 int cookie = last_orphan_cookie_++; | |
| 281 orphan_client_hosts_[cookie] = | |
| 282 std::pair<DevToolsClientHost*, DevToolsRuntimeProperties>( | |
| 283 client_host, runtime_properties_map_[from_rvh]); | |
| 284 | |
| 285 UnbindClientHost(from_rvh, client_host); | |
| 286 return cookie; | |
| 287 } | |
| 288 | |
| 289 void DevToolsManager::AttachClientHost(int client_host_cookie, | |
| 290 RenderViewHost* to_rvh) { | |
| 291 OrphanClientHosts::iterator it = orphan_client_hosts_.find( | |
| 292 client_host_cookie); | |
| 293 if (it == orphan_client_hosts_.end()) | |
| 294 return; | |
| 295 | |
| 296 DevToolsClientHost* client_host = (*it).second.first; | |
| 297 BindClientHost(to_rvh, client_host, (*it).second.second); | |
| 298 SendAttachToAgent(to_rvh); | |
| 299 | |
| 300 orphan_client_hosts_.erase(client_host_cookie); | |
| 301 } | |
| 302 | |
| 303 void DevToolsManager::SendAttachToAgent(RenderViewHost* inspected_rvh) { | |
| 304 if (inspected_rvh) { | |
| 305 ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies( | |
| 306 inspected_rvh->process()->id()); | |
| 307 | |
| 308 DevToolsRuntimeProperties properties; | |
| 309 RuntimePropertiesMap::iterator it = | |
| 310 runtime_properties_map_.find(inspected_rvh); | |
| 311 if (it != runtime_properties_map_.end()) { | |
| 312 properties = DevToolsRuntimeProperties(it->second.begin(), | |
| 313 it->second.end()); | |
| 314 } | |
| 315 IPC::Message* m = new DevToolsAgentMsg_Attach(properties); | |
| 316 m->set_routing_id(inspected_rvh->routing_id()); | |
| 317 inspected_rvh->Send(m); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 void DevToolsManager::SendDetachToAgent(RenderViewHost* inspected_rvh) { | |
| 322 if (inspected_rvh) { | |
| 323 IPC::Message* m = new DevToolsAgentMsg_Detach(); | |
| 324 m->set_routing_id(inspected_rvh->routing_id()); | |
| 325 inspected_rvh->Send(m); | |
| 326 } | |
| 327 } | |
| 328 | |
| 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( | |
| 402 RenderViewHost* inspected_rvh, | |
| 403 DevToolsClientHost* client_host, | |
| 404 const DevToolsRuntimeProperties& runtime_properties) { | |
| 405 DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh) == | |
| 406 inspected_rvh_to_client_host_.end()); | |
| 407 DCHECK(client_host_to_inspected_rvh_.find(client_host) == | |
| 408 client_host_to_inspected_rvh_.end()); | |
| 409 | |
| 410 if (client_host_to_inspected_rvh_.empty()) { | |
| 411 BrowserThread::PostTask( | |
| 412 BrowserThread::IO, | |
| 413 FROM_HERE, | |
| 414 NewRunnableFunction(&DevToolsNetLogObserver::Attach, | |
| 415 g_browser_process->io_thread())); | |
| 416 } | |
| 417 inspected_rvh_to_client_host_[inspected_rvh] = client_host; | |
| 418 client_host_to_inspected_rvh_[client_host] = inspected_rvh; | |
| 419 runtime_properties_map_[inspected_rvh] = runtime_properties; | |
| 420 } | |
| 421 | |
| 422 void DevToolsManager::UnbindClientHost(RenderViewHost* inspected_rvh, | |
| 423 DevToolsClientHost* client_host) { | |
| 424 DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh)->second == | |
| 425 client_host); | |
| 426 DCHECK(client_host_to_inspected_rvh_.find(client_host)->second == | |
| 427 inspected_rvh); | |
| 428 | |
| 429 inspected_rvh_to_client_host_.erase(inspected_rvh); | |
| 430 client_host_to_inspected_rvh_.erase(client_host); | |
| 431 runtime_properties_map_.erase(inspected_rvh); | |
| 432 | |
| 433 if (client_host_to_inspected_rvh_.empty()) { | |
| 434 BrowserThread::PostTask( | |
| 435 BrowserThread::IO, | |
| 436 FROM_HERE, | |
| 437 NewRunnableFunction(&DevToolsNetLogObserver::Detach)); | |
| 438 } | |
| 439 SendDetachToAgent(inspected_rvh); | |
| 440 if (inspected_rvh_for_reopen_ == inspected_rvh) | |
| 441 inspected_rvh_for_reopen_ = NULL; | |
| 442 | |
| 443 int process_id = inspected_rvh->process()->id(); | |
| 444 for (InspectedRvhToClientHostMap::iterator it = | |
| 445 inspected_rvh_to_client_host_.begin(); | |
| 446 it != inspected_rvh_to_client_host_.end(); | |
| 447 ++it) { | |
| 448 if (it->first->process()->id() == process_id) | |
| 449 return; | |
| 450 } | |
| 451 // We've disconnected from the last renderer -> revoke cookie permissions. | |
| 452 ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id); | |
| 453 } | |
| 454 | |
| 455 void DevToolsManager::CloseAllClientHosts() { | |
| 456 std::vector<RenderViewHost*> rhvs; | |
| 457 for (InspectedRvhToClientHostMap::iterator it = | |
| 458 inspected_rvh_to_client_host_.begin(); | |
| 459 it != inspected_rvh_to_client_host_.end(); ++it) { | |
| 460 rhvs.push_back(it->first); | |
| 461 } | |
| 462 for (std::vector<RenderViewHost*>::iterator it = rhvs.begin(); | |
| 463 it != rhvs.end(); ++it) { | |
| 464 UnregisterDevToolsClientHostFor(*it); | |
| 465 } | |
| 466 } | |
| OLD | NEW |