OLD | NEW |
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 <algorithm> |
| 6 |
5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
6 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
7 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
8 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
9 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/sessions/restore_tab_helper.h" | 18 #include "chrome/browser/sessions/restore_tab_helper.h" |
17 #include "chrome/browser/tabs/tab_strip_model.h" | 19 #include "chrome/browser/tabs/tab_strip_model.h" |
18 #include "chrome/browser/themes/theme_service.h" | 20 #include "chrome/browser/themes/theme_service.h" |
19 #include "chrome/browser/themes/theme_service_factory.h" | 21 #include "chrome/browser/themes/theme_service_factory.h" |
20 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_list.h" | 23 #include "chrome/browser/ui/browser_list.h" |
22 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 25 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
24 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/render_messages.h" | 27 #include "chrome/common/render_messages.h" |
26 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 29 #include "content/browser/browsing_instance.h" |
27 #include "content/browser/debugger/devtools_manager.h" | 30 #include "content/browser/debugger/devtools_manager.h" |
28 #include "content/browser/debugger/devtools_window.h" | 31 #include "content/browser/debugger/devtools_window.h" |
29 #include "content/browser/in_process_webkit/session_storage_namespace.h" | 32 #include "content/browser/in_process_webkit/session_storage_namespace.h" |
30 #include "content/browser/load_notification_details.h" | 33 #include "content/browser/load_notification_details.h" |
31 #include "content/browser/renderer_host/render_view_host.h" | 34 #include "content/browser/renderer_host/render_view_host.h" |
32 #include "content/browser/tab_contents/navigation_controller.h" | 35 #include "content/browser/tab_contents/navigation_controller.h" |
33 #include "content/browser/tab_contents/navigation_entry.h" | 36 #include "content/browser/tab_contents/navigation_entry.h" |
34 #include "content/browser/tab_contents/tab_contents.h" | 37 #include "content/browser/tab_contents/tab_contents.h" |
35 #include "content/browser/tab_contents/tab_contents_view.h" | 38 #include "content/browser/tab_contents/tab_contents_view.h" |
36 #include "content/common/bindings_policy.h" | 39 #include "content/common/bindings_policy.h" |
37 #include "content/common/notification_service.h" | 40 #include "content/common/notification_service.h" |
38 #include "grit/generated_resources.h" | 41 #include "grit/generated_resources.h" |
39 | 42 |
40 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp"; | 43 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp"; |
41 | 44 |
| 45 DevToolsWindow::DevToolsWindowList DevToolsWindow::instances_; |
| 46 |
42 // static | 47 // static |
43 TabContentsWrapper* DevToolsWindow::GetDevToolsContents( | 48 TabContentsWrapper* DevToolsWindow::GetDevToolsContents( |
44 TabContents* inspected_tab) { | 49 TabContents* inspected_tab) { |
45 if (!inspected_tab) { | 50 if (!inspected_tab) { |
46 return NULL; | 51 return NULL; |
47 } | 52 } |
48 | 53 |
49 if (!DevToolsManager::GetInstance()) | 54 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 55 if (!manager) |
50 return NULL; // Happens only in tests. | 56 return NULL; // Happens only in tests. |
51 | 57 |
52 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> | 58 DevToolsClientHost* client_host = manager-> |
53 GetDevToolsClientHostFor(inspected_tab->render_view_host()); | 59 GetDevToolsClientHostFor(inspected_tab->render_view_host()); |
54 if (!client_host) { | 60 DevToolsWindow* window = AsDevToolsWindow(client_host); |
55 return NULL; | |
56 } | |
57 | |
58 DevToolsWindow* window = client_host->AsDevToolsWindow(); | |
59 if (!window || !window->is_docked()) { | 61 if (!window || !window->is_docked()) { |
60 return NULL; | 62 return NULL; |
61 } | 63 } |
62 return window->tab_contents(); | 64 return window->tab_contents(); |
63 } | 65 } |
64 | 66 |
| 67 // static |
| 68 DevToolsWindow* DevToolsWindow::FindDevToolsWindow( |
| 69 RenderViewHost* window_rvh) { |
| 70 for (DevToolsWindowList::iterator it = instances_.begin(); |
| 71 it != instances_.end(); ++it) { |
| 72 if ((*it)->GetRenderViewHost() == window_rvh) |
| 73 return *it; |
| 74 } |
| 75 return NULL; |
| 76 } |
| 77 |
| 78 // static |
| 79 void DevToolsWindow::OpenDevToolsWindow(RenderViewHost* inspected_rvh) { |
| 80 ToggleDevToolsWindow(inspected_rvh, true, DEVTOOLS_TOGGLE_ACTION_NONE); |
| 81 } |
| 82 |
| 83 // static |
| 84 void DevToolsWindow::ToggleDevToolsWindow(RenderViewHost* inspected_rvh, |
| 85 DevToolsToggleAction action) { |
| 86 ToggleDevToolsWindow(inspected_rvh, false, action); |
| 87 } |
| 88 |
| 89 void DevToolsWindow::InspectElement(RenderViewHost* inspected_rvh, |
| 90 int x, |
| 91 int y) { |
| 92 DevToolsManager::GetInstance()->SendInspectElement(inspected_rvh, x, y); |
| 93 // TODO(loislo): we should initiate DevTools window opening from within |
| 94 // renderer. Otherwise, we still can hit a race condition here. |
| 95 OpenDevToolsWindow(inspected_rvh); |
| 96 } |
| 97 |
| 98 |
65 DevToolsWindow::DevToolsWindow(Profile* profile, | 99 DevToolsWindow::DevToolsWindow(Profile* profile, |
66 RenderViewHost* inspected_rvh, | 100 RenderViewHost* inspected_rvh, |
67 bool docked) | 101 bool docked) |
68 : profile_(profile), | 102 : profile_(profile), |
69 inspected_tab_(NULL), | 103 inspected_tab_(NULL), |
70 browser_(NULL), | 104 browser_(NULL), |
71 docked_(docked), | 105 docked_(docked), |
72 is_loaded_(false), | 106 is_loaded_(false), |
73 action_on_load_(DEVTOOLS_TOGGLE_ACTION_NONE) { | 107 action_on_load_(DEVTOOLS_TOGGLE_ACTION_NONE) { |
74 // Create TabContents with devtools. | 108 // Create TabContents with devtools. |
(...skipping 16 matching lines...) Expand all Loading... |
91 registrar_.Add(this, | 125 registrar_.Add(this, |
92 NotificationType::TAB_CLOSING, | 126 NotificationType::TAB_CLOSING, |
93 Source<NavigationController>(&tab_contents_->controller())); | 127 Source<NavigationController>(&tab_contents_->controller())); |
94 registrar_.Add( | 128 registrar_.Add( |
95 this, | 129 this, |
96 NotificationType::BROWSER_THEME_CHANGED, | 130 NotificationType::BROWSER_THEME_CHANGED, |
97 Source<ThemeService>(ThemeServiceFactory::GetForProfile(profile_))); | 131 Source<ThemeService>(ThemeServiceFactory::GetForProfile(profile_))); |
98 TabContents* tab = inspected_rvh->delegate()->GetAsTabContents(); | 132 TabContents* tab = inspected_rvh->delegate()->GetAsTabContents(); |
99 if (tab) | 133 if (tab) |
100 inspected_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); | 134 inspected_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 135 |
| 136 instances_.push_back(this); |
101 } | 137 } |
102 | 138 |
103 DevToolsWindow::~DevToolsWindow() { | 139 DevToolsWindow::~DevToolsWindow() { |
104 } | 140 DevToolsWindowList::iterator it = std::find(instances_.begin(), |
105 | 141 instances_.end(), |
106 DevToolsWindow* DevToolsWindow::AsDevToolsWindow() { | 142 this); |
107 return this; | 143 DCHECK(it != instances_.end()); |
| 144 instances_.erase(it); |
108 } | 145 } |
109 | 146 |
110 void DevToolsWindow::SendMessageToClient(const IPC::Message& message) { | 147 void DevToolsWindow::SendMessageToClient(const IPC::Message& message) { |
111 RenderViewHost* target_host = | 148 RenderViewHost* target_host = |
112 tab_contents_->tab_contents()->render_view_host(); | 149 tab_contents_->tab_contents()->render_view_host(); |
113 IPC::Message* m = new IPC::Message(message); | 150 IPC::Message* m = new IPC::Message(message); |
114 m->set_routing_id(target_host->routing_id()); | 151 m->set_routing_id(target_host->routing_id()); |
115 target_host->Send(m); | 152 target_host->Send(m); |
116 } | 153 } |
117 | 154 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } else { | 226 } else { |
190 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); | 227 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); |
191 if (inspected_window) | 228 if (inspected_window) |
192 tab_contents_->view()->Focus(); | 229 tab_contents_->view()->Focus(); |
193 } | 230 } |
194 } | 231 } |
195 | 232 |
196 void DevToolsWindow::SetDocked(bool docked) { | 233 void DevToolsWindow::SetDocked(bool docked) { |
197 if (docked_ == docked) | 234 if (docked_ == docked) |
198 return; | 235 return; |
| 236 |
| 237 if (!inspected_tab_) |
| 238 return; |
| 239 RenderViewHost* inspected_rvh = inspected_tab_->render_view_host(); |
| 240 DCHECK(inspected_rvh); |
| 241 profile_->GetPrefs()->SetBoolean(prefs::kDevToolsOpenDocked, docked); |
| 242 |
199 if (docked && (!GetInspectedBrowserWindow() || | 243 if (docked && (!GetInspectedBrowserWindow() || |
200 IsInspectedBrowserPopupOrPanel())) { | 244 IsInspectedBrowserPopupOrPanel())) { |
201 // Cannot dock, avoid window flashing due to close-reopen cycle. | 245 // Cannot dock, avoid window flashing due to close-reopen cycle. |
202 return; | 246 return; |
203 } | 247 } |
204 docked_ = docked; | 248 docked_ = docked; |
205 | 249 |
206 if (docked) { | 250 if (docked) { |
207 // Detach window from the external devtools browser. It will lead to | 251 // Detach window from the external devtools browser. It will lead to |
208 // the browser object's close and delete. Remove observer first. | 252 // the browser object's close and delete. Remove observer first. |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 if (docked_) { | 509 if (docked_) { |
466 if (event.windowsKeyCode == 0x08) { | 510 if (event.windowsKeyCode == 0x08) { |
467 // Do not navigate back in history on Windows (http://crbug.com/74156). | 511 // Do not navigate back in history on Windows (http://crbug.com/74156). |
468 return; | 512 return; |
469 } | 513 } |
470 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); | 514 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); |
471 if (inspected_window) | 515 if (inspected_window) |
472 inspected_window->HandleKeyboardEvent(event); | 516 inspected_window->HandleKeyboardEvent(event); |
473 } | 517 } |
474 } | 518 } |
| 519 |
| 520 // static |
| 521 void DevToolsWindow::ToggleDevToolsWindow( |
| 522 RenderViewHost* inspected_rvh, |
| 523 bool force_open, |
| 524 DevToolsToggleAction action) { |
| 525 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 526 DevToolsWindow* window = NULL; |
| 527 |
| 528 DevToolsClientHost* host = manager->GetDevToolsClientHostFor(inspected_rvh); |
| 529 if (host != NULL && AsDevToolsWindow(host) == NULL) { |
| 530 // Break remote debugging / extension debugging session. |
| 531 manager->UnregisterDevToolsClientHostFor(inspected_rvh); |
| 532 } |
| 533 |
| 534 bool do_open = force_open; |
| 535 if (!window) { |
| 536 Profile* profile = inspected_rvh->process()->profile(); |
| 537 bool docked = profile->GetPrefs()->GetBoolean(prefs::kDevToolsOpenDocked); |
| 538 window = new DevToolsWindow(profile, inspected_rvh, docked); |
| 539 manager->RegisterDevToolsClientHostFor(inspected_rvh, window); |
| 540 do_open = true; |
| 541 } |
| 542 |
| 543 // If window is docked and visible, we hide it on toggle. If window is |
| 544 // undocked, we show (activate) it. |
| 545 if (!window->is_docked() || do_open) { |
| 546 window->Show(action); |
| 547 } else { |
| 548 manager->UnregisterDevToolsClientHostFor(inspected_rvh); |
| 549 } |
| 550 } |
| 551 |
| 552 // static |
| 553 DevToolsWindow* DevToolsWindow::AsDevToolsWindow( |
| 554 DevToolsClientHost* client_host) { |
| 555 if (!client_host) |
| 556 return NULL; |
| 557 DevToolsWindowList::iterator it = std::find(instances_.begin(), |
| 558 instances_.end(), |
| 559 client_host); |
| 560 if (it == instances_.end()) { |
| 561 return NULL; |
| 562 } |
| 563 return *it; |
| 564 } |
OLD | NEW |