OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 8 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" |
10 #include "chrome/browser/debugger/devtools_manager.h" | 12 #include "chrome/browser/debugger/devtools_manager.h" |
11 #include "chrome/browser/extensions/extension_devtools_events.h" | 13 #include "chrome/browser/extensions/extension_devtools_events.h" |
12 #include "chrome/browser/extensions/extension_devtools_manager.h" | 14 #include "chrome/browser/extensions/extension_devtools_manager.h" |
13 #include "chrome/browser/extensions/extension_event_router.h" | 15 #include "chrome/browser/extensions/extension_event_router.h" |
14 #include "chrome/browser/extensions/extension_tabs_module.h" | 16 #include "chrome/browser/extensions/extension_tabs_module.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 18 #include "chrome/browser/tab_contents/tab_contents.h" |
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
18 #include "chrome/common/devtools_messages.h" | 20 #include "chrome/common/devtools_messages.h" |
19 | 21 |
20 ExtensionDevToolsBridge::ExtensionDevToolsBridge(int tab_id, | 22 ExtensionDevToolsBridge::ExtensionDevToolsBridge(int tab_id, |
21 Profile* profile) | 23 Profile* profile) |
22 : tab_id_(tab_id), | 24 : tab_id_(tab_id), |
23 profile_(profile), | 25 profile_(profile), |
24 on_page_event_name_( | 26 on_page_event_name_( |
25 ExtensionDevToolsEvents::OnPageEventNameForTab(tab_id)), | 27 ExtensionDevToolsEvents::OnPageEventNameForTab(tab_id)), |
26 on_tab_close_event_name_( | 28 on_tab_close_event_name_( |
27 ExtensionDevToolsEvents::OnTabCloseEventNameForTab(tab_id)) { | 29 ExtensionDevToolsEvents::OnTabCloseEventNameForTab(tab_id)) { |
28 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); | 30 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); |
29 DCHECK(extension_devtools_manager_.get()); | 31 DCHECK(extension_devtools_manager_.get()); |
30 } | 32 } |
31 | 33 |
32 ExtensionDevToolsBridge::~ExtensionDevToolsBridge() { | 34 ExtensionDevToolsBridge::~ExtensionDevToolsBridge() { |
33 } | 35 } |
34 | 36 |
| 37 static std::string FormatDevToolsMessage(int seq, |
| 38 const std::string& domain, |
| 39 const std::string& command, |
| 40 DictionaryValue* arguments) { |
| 41 |
| 42 DictionaryValue message; |
| 43 message.SetInteger("seq", seq); |
| 44 message.SetString("domain", domain); |
| 45 message.SetString("command", command); |
| 46 message.Set("arguments", arguments); |
| 47 |
| 48 std::string json; |
| 49 base::JSONWriter::Write(&message, false, &json); |
| 50 return json; |
| 51 } |
| 52 |
35 bool ExtensionDevToolsBridge::RegisterAsDevToolsClientHost() { | 53 bool ExtensionDevToolsBridge::RegisterAsDevToolsClientHost() { |
36 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 54 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
37 | 55 |
38 Browser* browser; | 56 Browser* browser; |
39 TabStripModel* tab_strip; | 57 TabStripModel* tab_strip; |
40 TabContentsWrapper* contents; | 58 TabContentsWrapper* contents; |
41 int tab_index; | 59 int tab_index; |
42 if (ExtensionTabUtil::GetTabById(tab_id_, profile_, true, | 60 if (ExtensionTabUtil::GetTabById(tab_id_, profile_, true, |
43 &browser, &tab_strip, | 61 &browser, &tab_strip, |
44 &contents, &tab_index)) { | 62 &contents, &tab_index)) { |
45 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | 63 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); |
| 64 if (devtools_manager->GetDevToolsClientHostFor(contents-> |
| 65 render_view_host()) != NULL) |
| 66 return false; |
| 67 |
46 devtools_manager->RegisterDevToolsClientHostFor( | 68 devtools_manager->RegisterDevToolsClientHostFor( |
47 contents->render_view_host(), this); | 69 contents->render_view_host(), this); |
| 70 |
| 71 // Following messages depend on inspector protocol that is not yet |
| 72 // finalized. |
| 73 |
| 74 // 1. Set injected script content. |
| 75 DictionaryValue* arguments = new DictionaryValue(); |
| 76 arguments->SetString("scriptSource", "'{}'"); |
48 devtools_manager->ForwardToDevToolsAgent( | 77 devtools_manager->ForwardToDevToolsAgent( |
49 this, | 78 this, |
50 DevToolsAgentMsg_SetApuAgentEnabled(true)); | 79 DevToolsAgentMsg_DispatchOnInspectorBackend( |
| 80 FormatDevToolsMessage(0, |
| 81 "Inspector", |
| 82 "setInjectedScriptSource", |
| 83 arguments))); |
| 84 |
| 85 // 2. Report front-end is loaded. |
| 86 devtools_manager->ForwardToDevToolsAgent( |
| 87 this, |
| 88 DevToolsAgentMsg_FrontendLoaded()); |
| 89 |
| 90 // 3. Do not break on exceptions. |
| 91 arguments = new DictionaryValue(); |
| 92 arguments->SetInteger("pauseOnExceptionsState", 0); |
| 93 devtools_manager->ForwardToDevToolsAgent( |
| 94 this, |
| 95 DevToolsAgentMsg_DispatchOnInspectorBackend( |
| 96 FormatDevToolsMessage(1, |
| 97 "Debugger", |
| 98 "setPauseOnExceptionsState", |
| 99 arguments))); |
| 100 |
| 101 // 4. Start timeline profiler. |
| 102 devtools_manager->ForwardToDevToolsAgent( |
| 103 this, |
| 104 DevToolsAgentMsg_DispatchOnInspectorBackend( |
| 105 FormatDevToolsMessage(2, |
| 106 "Inspector", |
| 107 "startTimelineProfiler", |
| 108 new DictionaryValue()))); |
51 return true; | 109 return true; |
52 } | 110 } |
53 return false; | 111 return false; |
54 } | 112 } |
55 | 113 |
56 void ExtensionDevToolsBridge::UnregisterAsDevToolsClientHost() { | 114 void ExtensionDevToolsBridge::UnregisterAsDevToolsClientHost() { |
57 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 115 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
58 | 116 |
59 NotifyCloseListener(); | 117 NotifyCloseListener(); |
60 } | 118 } |
61 | 119 |
62 // If the tab we are looking at is going away then we fire a closing event at | 120 // If the tab we are looking at is going away then we fire a closing event at |
63 // the extension. | 121 // the extension. |
64 void ExtensionDevToolsBridge::InspectedTabClosing() { | 122 void ExtensionDevToolsBridge::InspectedTabClosing() { |
65 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 123 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
66 | 124 |
67 // TODO(knorton): Remove this event in favor of the standard tabs.onRemoved | 125 // TODO(knorton): Remove this event in favor of the standard tabs.onRemoved |
68 // event in extensions. | 126 // event in extensions. |
69 std::string json("[{}]"); | 127 std::string json("[{}]"); |
70 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 128 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
71 on_tab_close_event_name_, json, profile_, GURL()); | 129 on_tab_close_event_name_, json, profile_, GURL()); |
72 | 130 |
73 // This may result in this object being destroyed. | 131 // This may result in this object being destroyed. |
74 extension_devtools_manager_->BridgeClosingForTab(tab_id_); | 132 extension_devtools_manager_->BridgeClosingForTab(tab_id_); |
75 } | 133 } |
76 | 134 |
77 void ExtensionDevToolsBridge::SendMessageToClient(const IPC::Message& msg) { | 135 void ExtensionDevToolsBridge::SendMessageToClient(const IPC::Message& msg) { |
78 IPC_BEGIN_MESSAGE_MAP(ExtensionDevToolsBridge, msg) | 136 IPC_BEGIN_MESSAGE_MAP(ExtensionDevToolsBridge, msg) |
79 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchToAPU, OnDispatchToAPU); | 137 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 138 OnDispatchOnInspectorFrontend); |
80 IPC_MESSAGE_UNHANDLED_ERROR() | 139 IPC_MESSAGE_UNHANDLED_ERROR() |
81 IPC_END_MESSAGE_MAP() | 140 IPC_END_MESSAGE_MAP() |
82 } | 141 } |
83 | 142 |
84 void ExtensionDevToolsBridge::OnDispatchToAPU(const std::string& data) { | 143 void ExtensionDevToolsBridge::OnDispatchOnInspectorFrontend( |
| 144 const std::string& data) { |
85 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 145 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
86 | 146 |
87 std::string json = base::StringPrintf("[%s]", data.c_str()); | 147 std::string json = base::StringPrintf("[%s]", data.c_str()); |
88 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 148 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
89 on_page_event_name_, json, profile_, GURL()); | 149 on_page_event_name_, json, profile_, GURL()); |
90 } | 150 } |
91 | |
OLD | NEW |