| 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> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
| 9 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/debugger/devtools_file_util.h" | 15 #include "chrome/browser/debugger/devtools_file_util.h" |
| 15 #include "chrome/browser/debugger/devtools_window.h" | 16 #include "chrome/browser/debugger/devtools_window.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 37 #include "content/browser/tab_contents/navigation_controller.h" | 38 #include "content/browser/tab_contents/navigation_controller.h" |
| 38 #include "content/browser/tab_contents/navigation_entry.h" | 39 #include "content/browser/tab_contents/navigation_entry.h" |
| 39 #include "content/browser/tab_contents/tab_contents.h" | 40 #include "content/browser/tab_contents/tab_contents.h" |
| 40 #include "content/browser/tab_contents/tab_contents_view.h" | 41 #include "content/browser/tab_contents/tab_contents_view.h" |
| 41 #include "content/common/devtools_messages.h" | 42 #include "content/common/devtools_messages.h" |
| 42 #include "content/public/browser/content_browser_client.h" | 43 #include "content/public/browser/content_browser_client.h" |
| 43 #include "content/public/browser/notification_source.h" | 44 #include "content/public/browser/notification_source.h" |
| 44 #include "content/public/common/bindings_policy.h" | 45 #include "content/public/common/bindings_policy.h" |
| 45 #include "grit/generated_resources.h" | 46 #include "grit/generated_resources.h" |
| 46 | 47 |
| 48 typedef std::vector<DevToolsWindow*> DevToolsWindowList; |
| 49 namespace { |
| 50 base::LazyInstance<DevToolsWindowList, |
| 51 base::LeakyLazyInstanceTraits<DevToolsWindowList> > |
| 52 g_instances = LAZY_INSTANCE_INITIALIZER; |
| 53 } // namespace |
| 54 |
| 47 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp"; | 55 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp"; |
| 48 | 56 |
| 49 // static | 57 // static |
| 50 void DevToolsWindow::RegisterUserPrefs(PrefService* prefs) { | 58 void DevToolsWindow::RegisterUserPrefs(PrefService* prefs) { |
| 51 prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked, | 59 prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked, |
| 52 true, | 60 true, |
| 53 PrefService::UNSYNCABLE_PREF); | 61 PrefService::UNSYNCABLE_PREF); |
| 54 } | 62 } |
| 55 | 63 |
| 56 // static | 64 // static |
| 57 TabContentsWrapper* DevToolsWindow::GetDevToolsContents( | 65 TabContentsWrapper* DevToolsWindow::GetDevToolsContents( |
| 58 TabContents* inspected_tab) { | 66 TabContents* inspected_tab) { |
| 59 if (!inspected_tab) | 67 if (!inspected_tab) |
| 60 return NULL; | 68 return NULL; |
| 61 | 69 |
| 62 DevToolsManager* manager = DevToolsManager::GetInstance(); | 70 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 63 if (!manager) | |
| 64 return NULL; // Happens only in tests. | |
| 65 | |
| 66 DevToolsClientHost* client_host = manager-> | 71 DevToolsClientHost* client_host = manager-> |
| 67 GetDevToolsClientHostFor(inspected_tab->render_view_host()); | 72 GetDevToolsClientHostFor(inspected_tab->render_view_host()); |
| 68 DevToolsWindow* window = AsDevToolsWindow(client_host); | 73 DevToolsWindow* window = AsDevToolsWindow(client_host); |
| 69 if (!window || !window->is_docked()) | 74 if (!window || !window->is_docked()) |
| 70 return NULL; | 75 return NULL; |
| 71 return window->tab_contents(); | 76 return window->tab_contents(); |
| 72 } | 77 } |
| 73 | 78 |
| 74 // static | 79 // static |
| 75 DevToolsWindow* DevToolsWindow::FindDevToolsWindow( | 80 bool DevToolsWindow::IsDevToolsWindow(RenderViewHost* window_rvh) { |
| 76 RenderViewHost* window_rvh) { | 81 if (g_instances == NULL) |
| 77 DevToolsClientHost* client_host = | 82 return NULL; |
| 78 DevToolsClientHost::FindOwnerClientHost(window_rvh); | 83 DevToolsWindowList& instances = g_instances.Get(); |
| 79 return client_host != NULL ? DevToolsWindow::AsDevToolsWindow(client_host) | 84 for (DevToolsWindowList::iterator it = instances.begin(); |
| 80 : NULL; | 85 it != instances.end(); ++it) { |
| 86 if ((*it)->tab_contents_->render_view_host() == window_rvh) |
| 87 return true; |
| 88 } |
| 89 return false; |
| 81 } | 90 } |
| 82 | 91 |
| 83 // static | 92 // static |
| 84 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker( | 93 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker( |
| 85 Profile* profile, | 94 Profile* profile, |
| 86 DevToolsAgentHost* worker_agent) { | 95 DevToolsAgentHost* worker_agent) { |
| 87 DevToolsWindow* window; | 96 DevToolsWindow* window; |
| 88 DevToolsClientHost* client = | 97 DevToolsClientHost* client = |
| 89 DevToolsManager::GetInstance()->GetDevToolsClientHostFor(worker_agent); | 98 DevToolsManager::GetInstance()->GetDevToolsClientHostFor(worker_agent); |
| 90 if (client) { | 99 if (client) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 RenderViewHost* inspected_rvh, | 165 RenderViewHost* inspected_rvh, |
| 157 bool docked) | 166 bool docked) |
| 158 : RenderViewHostObserver(tab_contents->render_view_host()), | 167 : RenderViewHostObserver(tab_contents->render_view_host()), |
| 159 profile_(profile), | 168 profile_(profile), |
| 160 inspected_tab_(NULL), | 169 inspected_tab_(NULL), |
| 161 tab_contents_(tab_contents), | 170 tab_contents_(tab_contents), |
| 162 browser_(NULL), | 171 browser_(NULL), |
| 163 docked_(docked), | 172 docked_(docked), |
| 164 is_loaded_(false), | 173 is_loaded_(false), |
| 165 action_on_load_(DEVTOOLS_TOGGLE_ACTION_NONE) { | 174 action_on_load_(DEVTOOLS_TOGGLE_ACTION_NONE) { |
| 175 g_instances.Get().push_back(this); |
| 166 // Wipe out page icon so that the default application icon is used. | 176 // Wipe out page icon so that the default application icon is used. |
| 167 NavigationEntry* entry = tab_contents_->controller().GetActiveEntry(); | 177 NavigationEntry* entry = tab_contents_->controller().GetActiveEntry(); |
| 168 entry->favicon().set_bitmap(SkBitmap()); | 178 entry->favicon().set_bitmap(SkBitmap()); |
| 169 entry->favicon().set_is_valid(true); | 179 entry->favicon().set_is_valid(true); |
| 170 | 180 |
| 171 // Register on-load actions. | 181 // Register on-load actions. |
| 172 registrar_.Add( | 182 registrar_.Add( |
| 173 this, | 183 this, |
| 174 content::NOTIFICATION_LOAD_STOP, | 184 content::NOTIFICATION_LOAD_STOP, |
| 175 content::Source<NavigationController>(&tab_contents_->controller())); | 185 content::Source<NavigationController>(&tab_contents_->controller())); |
| 176 registrar_.Add( | 186 registrar_.Add( |
| 177 this, | 187 this, |
| 178 content::NOTIFICATION_TAB_CLOSING, | 188 content::NOTIFICATION_TAB_CLOSING, |
| 179 content::Source<NavigationController>(&tab_contents_->controller())); | 189 content::Source<NavigationController>(&tab_contents_->controller())); |
| 180 registrar_.Add( | 190 registrar_.Add( |
| 181 this, | 191 this, |
| 182 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 192 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 183 content::Source<ThemeService>( | 193 content::Source<ThemeService>( |
| 184 ThemeServiceFactory::GetForProfile(profile_))); | 194 ThemeServiceFactory::GetForProfile(profile_))); |
| 185 // There is no inspected_rvh in case of shared workers. | 195 // There is no inspected_rvh in case of shared workers. |
| 186 if (inspected_rvh) { | 196 if (inspected_rvh) { |
| 187 TabContents* tab = inspected_rvh->delegate()->GetAsTabContents(); | 197 TabContents* tab = inspected_rvh->delegate()->GetAsTabContents(); |
| 188 if (tab) | 198 if (tab) |
| 189 inspected_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); | 199 inspected_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 190 } | 200 } |
| 191 } | 201 } |
| 192 | 202 |
| 193 DevToolsWindow::~DevToolsWindow() { | 203 DevToolsWindow::~DevToolsWindow() { |
| 204 DevToolsWindowList& instances = g_instances.Get(); |
| 205 DevToolsWindowList::iterator it = std::find(instances.begin(), |
| 206 instances.end(), |
| 207 this); |
| 208 DCHECK(it != instances.end()); |
| 209 instances.erase(it); |
| 194 } | 210 } |
| 195 | 211 |
| 196 void DevToolsWindow::SendMessageToClient(const IPC::Message& message) { | 212 void DevToolsWindow::SendMessageToClient(const IPC::Message& message) { |
| 197 RenderViewHost* target_host = tab_contents_->render_view_host(); | 213 RenderViewHost* target_host = tab_contents_->render_view_host(); |
| 198 IPC::Message* m = new IPC::Message(message); | 214 IPC::Message* m = new IPC::Message(message); |
| 199 m->set_routing_id(target_host->routing_id()); | 215 m->set_routing_id(target_host->routing_id()); |
| 200 target_host->Send(m); | 216 target_host->Send(m); |
| 201 } | 217 } |
| 202 | 218 |
| 203 void DevToolsWindow::InspectedTabClosing() { | 219 void DevToolsWindow::InspectedTabClosing() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 224 void DevToolsWindow::TabReplaced(TabContents* new_tab) { | 240 void DevToolsWindow::TabReplaced(TabContents* new_tab) { |
| 225 TabContentsWrapper* new_tab_wrapper = | 241 TabContentsWrapper* new_tab_wrapper = |
| 226 TabContentsWrapper::GetCurrentWrapperForContents(new_tab); | 242 TabContentsWrapper::GetCurrentWrapperForContents(new_tab); |
| 227 DCHECK(new_tab_wrapper); | 243 DCHECK(new_tab_wrapper); |
| 228 if (!new_tab_wrapper) | 244 if (!new_tab_wrapper) |
| 229 return; | 245 return; |
| 230 DCHECK_EQ(profile_, new_tab_wrapper->profile()); | 246 DCHECK_EQ(profile_, new_tab_wrapper->profile()); |
| 231 inspected_tab_ = new_tab_wrapper; | 247 inspected_tab_ = new_tab_wrapper; |
| 232 } | 248 } |
| 233 | 249 |
| 234 RenderViewHost* DevToolsWindow::GetClientRenderViewHost() { | |
| 235 return tab_contents_->render_view_host(); | |
| 236 } | |
| 237 | |
| 238 void DevToolsWindow::Show(DevToolsToggleAction action) { | 250 void DevToolsWindow::Show(DevToolsToggleAction action) { |
| 239 if (docked_) { | 251 if (docked_) { |
| 240 Browser* inspected_browser; | 252 Browser* inspected_browser; |
| 241 int inspected_tab_index; | 253 int inspected_tab_index; |
| 242 // Tell inspected browser to update splitter and switch to inspected panel. | 254 // Tell inspected browser to update splitter and switch to inspected panel. |
| 243 if (!IsInspectedBrowserPopupOrPanel() && | 255 if (!IsInspectedBrowserPopupOrPanel() && |
| 244 FindInspectedBrowserAndTabIndex(&inspected_browser, | 256 FindInspectedBrowserAndTabIndex(&inspected_browser, |
| 245 &inspected_tab_index)) { | 257 &inspected_tab_index)) { |
| 246 BrowserWindow* inspected_window = inspected_browser->window(); | 258 BrowserWindow* inspected_window = inspected_browser->window(); |
| 247 tab_contents_->tab_contents()->set_delegate(this); | 259 tab_contents_->tab_contents()->set_delegate(this); |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 window->Show(action); | 651 window->Show(action); |
| 640 else | 652 else |
| 641 manager->UnregisterDevToolsClientHostFor(inspected_rvh); | 653 manager->UnregisterDevToolsClientHostFor(inspected_rvh); |
| 642 | 654 |
| 643 return window; | 655 return window; |
| 644 } | 656 } |
| 645 | 657 |
| 646 // static | 658 // static |
| 647 DevToolsWindow* DevToolsWindow::AsDevToolsWindow( | 659 DevToolsWindow* DevToolsWindow::AsDevToolsWindow( |
| 648 DevToolsClientHost* client_host) { | 660 DevToolsClientHost* client_host) { |
| 649 if (!client_host) | 661 if (!client_host || g_instances == NULL) |
| 650 return NULL; | 662 return NULL; |
| 651 if (client_host->GetClientRenderViewHost() != NULL) | 663 DevToolsWindowList& instances = g_instances.Get(); |
| 652 return static_cast<DevToolsWindow*>(client_host); | 664 for (DevToolsWindowList::iterator it = instances.begin(); |
| 665 it != instances.end(); ++it) { |
| 666 DevToolsClientHost* client = *it; |
| 667 if (client == client_host) |
| 668 return *it; |
| 669 } |
| 653 return NULL; | 670 return NULL; |
| 654 } | 671 } |
| 655 | 672 |
| 656 void DevToolsWindow::RenderViewHostDestroyed(RenderViewHost* rvh) { | 673 void DevToolsWindow::RenderViewHostDestroyed(RenderViewHost* rvh) { |
| 657 // Don't delete |this| here, do it on NOTIFICATION_TAB_CLOSING event. | 674 // Don't delete |this| here, do it on NOTIFICATION_TAB_CLOSING event. |
| 658 } | 675 } |
| 659 | 676 |
| 660 bool DevToolsWindow::OnMessageReceived(const IPC::Message& message) { | 677 bool DevToolsWindow::OnMessageReceived(const IPC::Message& message) { |
| 661 bool handled = true; | 678 bool handled = true; |
| 662 IPC_BEGIN_MESSAGE_MAP(DevToolsWindow, message) | 679 IPC_BEGIN_MESSAGE_MAP(DevToolsWindow, message) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 682 RequestSetDocked(false); | 699 RequestSetDocked(false); |
| 683 } | 700 } |
| 684 | 701 |
| 685 content::JavaScriptDialogCreator* DevToolsWindow::GetJavaScriptDialogCreator() { | 702 content::JavaScriptDialogCreator* DevToolsWindow::GetJavaScriptDialogCreator() { |
| 686 if (inspected_tab_ && inspected_tab_->tab_contents()->delegate()) { | 703 if (inspected_tab_ && inspected_tab_->tab_contents()->delegate()) { |
| 687 return inspected_tab_->tab_contents()->delegate()-> | 704 return inspected_tab_->tab_contents()->delegate()-> |
| 688 GetJavaScriptDialogCreator(); | 705 GetJavaScriptDialogCreator(); |
| 689 } | 706 } |
| 690 return TabContentsDelegate::GetJavaScriptDialogCreator(); | 707 return TabContentsDelegate::GetJavaScriptDialogCreator(); |
| 691 } | 708 } |
| OLD | NEW |