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 "chrome/browser/extensions/extension_devtools_bridge.h" | 5 #include "chrome/browser/extensions/extension_devtools_bridge.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_devtools_events.h" | 12 #include "chrome/browser/extensions/extension_devtools_events.h" |
13 #include "chrome/browser/extensions/extension_devtools_manager.h" | 13 #include "chrome/browser/extensions/extension_devtools_manager.h" |
14 #include "chrome/browser/extensions/extension_event_router.h" | 14 #include "chrome/browser/extensions/extension_event_router.h" |
15 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
18 #include "content/browser/debugger/devtools_manager.h" | |
19 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "content/common/devtools_messages.h" | 19 #include "content/public/browser/devtools_agent_host_registry.h" |
| 20 #include "content/public/browser/devtools_manager.h" |
| 21 |
| 22 using content::DevToolsAgentHost; |
| 23 using content::DevToolsAgentHostRegistry; |
| 24 using content::DevToolsManager; |
21 | 25 |
22 ExtensionDevToolsBridge::ExtensionDevToolsBridge(int tab_id, | 26 ExtensionDevToolsBridge::ExtensionDevToolsBridge(int tab_id, |
23 Profile* profile) | 27 Profile* profile) |
24 : tab_id_(tab_id), | 28 : tab_id_(tab_id), |
25 profile_(profile), | 29 profile_(profile), |
26 on_page_event_name_( | 30 on_page_event_name_( |
27 ExtensionDevToolsEvents::OnPageEventNameForTab(tab_id)), | 31 ExtensionDevToolsEvents::OnPageEventNameForTab(tab_id)), |
28 on_tab_close_event_name_( | 32 on_tab_close_event_name_( |
29 ExtensionDevToolsEvents::OnTabCloseEventNameForTab(tab_id)) { | 33 ExtensionDevToolsEvents::OnTabCloseEventNameForTab(tab_id)) { |
30 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); | 34 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); |
(...skipping 18 matching lines...) Expand all Loading... |
49 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 53 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
50 | 54 |
51 Browser* browser; | 55 Browser* browser; |
52 TabStripModel* tab_strip; | 56 TabStripModel* tab_strip; |
53 TabContentsWrapper* contents; | 57 TabContentsWrapper* contents; |
54 int tab_index; | 58 int tab_index; |
55 if (ExtensionTabUtil::GetTabById(tab_id_, profile_, true, | 59 if (ExtensionTabUtil::GetTabById(tab_id_, profile_, true, |
56 &browser, &tab_strip, | 60 &browser, &tab_strip, |
57 &contents, &tab_index)) { | 61 &contents, &tab_index)) { |
58 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | 62 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); |
59 if (devtools_manager->GetDevToolsClientHostFor(contents-> | 63 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
60 render_view_host()) != NULL) | 64 contents->render_view_host()); |
| 65 if (devtools_manager->GetDevToolsClientHostFor(agent)) |
61 return false; | 66 return false; |
62 | 67 |
63 devtools_manager->RegisterDevToolsClientHostFor( | 68 devtools_manager->RegisterDevToolsClientHostFor(agent, this); |
64 contents->render_view_host(), this); | |
65 | 69 |
66 // Following messages depend on inspector protocol that is not yet | 70 // Following messages depend on inspector protocol that is not yet |
67 // finalized. | 71 // finalized. |
68 | 72 |
69 // 1. Start timeline profiler. | 73 // 1. Start timeline profiler. |
70 devtools_manager->ForwardToDevToolsAgent( | 74 devtools_manager->DispatchOnInspectorBackend( |
71 this, | 75 this, |
72 DevToolsAgentMsg_DispatchOnInspectorBackend( | 76 FormatDevToolsMessage(2, "Timeline.start")); |
73 MSG_ROUTING_NONE, | |
74 FormatDevToolsMessage(2, "Timeline.start"))); | |
75 | 77 |
76 // 2. Enable network resource tracking. | 78 // 2. Enable network resource tracking. |
77 devtools_manager->ForwardToDevToolsAgent( | 79 devtools_manager->DispatchOnInspectorBackend( |
78 this, | 80 this, |
79 DevToolsAgentMsg_DispatchOnInspectorBackend( | 81 FormatDevToolsMessage(3, "Network.enable")); |
80 MSG_ROUTING_NONE, | |
81 FormatDevToolsMessage(3, "Network.enable"))); | |
82 | 82 |
83 return true; | 83 return true; |
84 } | 84 } |
85 return false; | 85 return false; |
86 } | 86 } |
87 | 87 |
88 void ExtensionDevToolsBridge::UnregisterAsDevToolsClientHost() { | 88 void ExtensionDevToolsBridge::UnregisterAsDevToolsClientHost() { |
89 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 89 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
90 | 90 DevToolsManager::GetInstance()->ClientHostClosing(this); |
91 NotifyCloseListener(); | |
92 } | 91 } |
93 | 92 |
94 // If the tab we are looking at is going away then we fire a closing event at | 93 // If the tab we are looking at is going away then we fire a closing event at |
95 // the extension. | 94 // the extension. |
96 void ExtensionDevToolsBridge::InspectedTabClosing() { | 95 void ExtensionDevToolsBridge::InspectedTabClosing() { |
97 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 96 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
98 | 97 |
99 // TODO(knorton): Remove this event in favor of the standard tabs.onRemoved | 98 // TODO(knorton): Remove this event in favor of the standard tabs.onRemoved |
100 // event in extensions. | 99 // event in extensions. |
101 std::string json("[{}]"); | 100 std::string json("[{}]"); |
102 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 101 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
103 on_tab_close_event_name_, json, profile_, GURL()); | 102 on_tab_close_event_name_, json, profile_, GURL()); |
104 | 103 |
105 // This may result in this object being destroyed. | 104 // This may result in this object being destroyed. |
106 extension_devtools_manager_->BridgeClosingForTab(tab_id_); | 105 extension_devtools_manager_->BridgeClosingForTab(tab_id_); |
107 } | 106 } |
108 | 107 |
109 void ExtensionDevToolsBridge::SendMessageToClient(const IPC::Message& msg) { | 108 void ExtensionDevToolsBridge::DispatchOnInspectorFrontend( |
110 IPC_BEGIN_MESSAGE_MAP(ExtensionDevToolsBridge, msg) | |
111 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | |
112 OnDispatchOnInspectorFrontend); | |
113 IPC_MESSAGE_UNHANDLED_ERROR() | |
114 IPC_END_MESSAGE_MAP() | |
115 } | |
116 | |
117 void ExtensionDevToolsBridge::TabReplaced(TabContents* new_tab) { | |
118 // We don't update the tab id as it needs to remain the same so that we can | |
119 // properly unregister. | |
120 } | |
121 | |
122 void ExtensionDevToolsBridge::OnDispatchOnInspectorFrontend( | |
123 const std::string& data) { | 109 const std::string& data) { |
124 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 110 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
125 | 111 |
126 std::string json = base::StringPrintf("[%s]", data.c_str()); | 112 std::string json = base::StringPrintf("[%s]", data.c_str()); |
127 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 113 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
128 on_page_event_name_, json, profile_, GURL()); | 114 on_page_event_name_, json, profile_, GURL()); |
129 } | 115 } |
| 116 |
| 117 void ExtensionDevToolsBridge::TabReplaced(TabContents* new_tab) { |
| 118 // We don't update the tab id as it needs to remain the same so that we can |
| 119 // properly unregister. |
| 120 } |
OLD | NEW |