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 // Implements the Chrome Extensions Debugger API. | 5 // Implements the Chrome Extensions Debugger API. |
6 | 6 |
7 #include "chrome/browser/extensions/extension_debugger_api.h" | 7 #include "chrome/browser/extensions/extension_debugger_api.h" |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/extensions/extension_debugger_api_constants.h" | 17 #include "chrome/browser/extensions/extension_debugger_api_constants.h" |
18 #include "chrome/browser/extensions/extension_event_router.h" | 18 #include "chrome/browser/extensions/extension_event_router.h" |
19 #include "chrome/browser/extensions/extension_tab_util.h" | 19 #include "chrome/browser/extensions/extension_tab_util.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
22 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
24 #include "chrome/common/extensions/extension_error_utils.h" | 24 #include "chrome/common/extensions/extension_error_utils.h" |
25 #include "content/browser/debugger/devtools_client_host.h" | 25 #include "content/public/browser/devtools_agent_host_registry.h" |
26 #include "content/browser/debugger/devtools_manager.h" | 26 #include "content/public/browser/devtools_client_host.h" |
| 27 #include "content/public/browser/devtools_manager.h" |
27 #include "content/browser/tab_contents/tab_contents.h" | 28 #include "content/browser/tab_contents/tab_contents.h" |
28 #include "content/common/devtools_messages.h" | |
29 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
30 #include "webkit/glue/webkit_glue.h" | 30 #include "webkit/glue/webkit_glue.h" |
31 | 31 |
| 32 using content::DevToolsAgentHost; |
| 33 using content::DevToolsAgentHostRegistry; |
| 34 using content::DevToolsClientHost; |
| 35 using content::DevToolsManager; |
| 36 |
32 namespace keys = extension_debugger_api_constants; | 37 namespace keys = extension_debugger_api_constants; |
33 | 38 |
34 class ExtensionDevToolsClientHost : public DevToolsClientHost, | 39 class ExtensionDevToolsClientHost : public DevToolsClientHost, |
35 public content::NotificationObserver { | 40 public content::NotificationObserver { |
36 public: | 41 public: |
37 ExtensionDevToolsClientHost(TabContents* tab_contents, | 42 ExtensionDevToolsClientHost(TabContents* tab_contents, |
38 const std::string& extension_id, | 43 const std::string& extension_id, |
39 int tab_id); | 44 int tab_id); |
40 | 45 |
41 ~ExtensionDevToolsClientHost(); | 46 ~ExtensionDevToolsClientHost(); |
42 | 47 |
43 bool MatchesContentsAndExtensionId(TabContents* tab_contents, | 48 bool MatchesContentsAndExtensionId(TabContents* tab_contents, |
44 const std::string& extension_id); | 49 const std::string& extension_id); |
45 void Close(); | 50 void Close(); |
46 void SendMessageToBackend(SendCommandDebuggerFunction* function, | 51 void SendMessageToBackend(SendCommandDebuggerFunction* function, |
47 const std::string& method, | 52 const std::string& method, |
48 Value* params); | 53 Value* params); |
49 | 54 |
50 // DevToolsClientHost interface | 55 // DevToolsClientHost interface |
51 virtual void InspectedTabClosing(); | 56 virtual void InspectedTabClosing(); |
52 virtual void SendMessageToClient(const IPC::Message& msg); | 57 virtual void DispatchOnInspectorFrontend(const std::string& message); |
53 virtual void TabReplaced(TabContents* tab_contents); | 58 virtual void TabReplaced(TabContents* tab_contents); |
54 virtual void FrameNavigating(const std::string& url) {} | 59 virtual void FrameNavigating(const std::string& url) {} |
55 | 60 |
56 private: | 61 private: |
57 // content::NotificationObserver implementation. | 62 // content::NotificationObserver implementation. |
58 virtual void Observe(int type, | 63 virtual void Observe(int type, |
59 const content::NotificationSource& source, | 64 const content::NotificationSource& source, |
60 const content::NotificationDetails& details); | 65 const content::NotificationDetails& details); |
61 void OnDispatchOnInspectorFrontend(const std::string& data); | |
62 | 66 |
63 TabContents* tab_contents_; | 67 TabContents* tab_contents_; |
64 std::string extension_id_; | 68 std::string extension_id_; |
65 int tab_id_; | 69 int tab_id_; |
66 content::NotificationRegistrar registrar_; | 70 content::NotificationRegistrar registrar_; |
67 int last_request_id_; | 71 int last_request_id_; |
68 typedef std::map<int, scoped_refptr<SendCommandDebuggerFunction> > | 72 typedef std::map<int, scoped_refptr<SendCommandDebuggerFunction> > |
69 PendingRequests; | 73 PendingRequests; |
70 PendingRequests pending_requests_; | 74 PendingRequests pending_requests_; |
71 | 75 |
(...skipping 19 matching lines...) Expand all Loading... |
91 | 95 |
92 void Add(ExtensionDevToolsClientHost* client_host) { | 96 void Add(ExtensionDevToolsClientHost* client_host) { |
93 client_hosts_.insert(client_host); | 97 client_hosts_.insert(client_host); |
94 } | 98 } |
95 | 99 |
96 void Remove(ExtensionDevToolsClientHost* client_host) { | 100 void Remove(ExtensionDevToolsClientHost* client_host) { |
97 client_hosts_.erase(client_host); | 101 client_hosts_.erase(client_host); |
98 } | 102 } |
99 | 103 |
100 ExtensionDevToolsClientHost* Lookup(RenderViewHost* rvh) { | 104 ExtensionDevToolsClientHost* Lookup(RenderViewHost* rvh) { |
| 105 if (!DevToolsAgentHostRegistry::HasDevToolsAgentHost(rvh)) |
| 106 return NULL; |
| 107 DevToolsAgentHost* agent = |
| 108 DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh); |
101 DevToolsClientHost* client_host = | 109 DevToolsClientHost* client_host = |
102 DevToolsManager::GetInstance()->GetDevToolsClientHostFor(rvh); | 110 DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent); |
103 std::set<DevToolsClientHost*>::iterator it = | 111 std::set<DevToolsClientHost*>::iterator it = |
104 client_hosts_.find(client_host); | 112 client_hosts_.find(client_host); |
105 if (it == client_hosts_.end()) | 113 if (it == client_hosts_.end()) |
106 return NULL; | 114 return NULL; |
107 return static_cast<ExtensionDevToolsClientHost*>(client_host); | 115 return static_cast<ExtensionDevToolsClientHost*>(client_host); |
108 } | 116 } |
109 | 117 |
110 private: | 118 private: |
111 std::set<DevToolsClientHost*> client_hosts_; | 119 std::set<DevToolsClientHost*> client_hosts_; |
112 }; | 120 }; |
(...skipping 10 matching lines...) Expand all Loading... |
123 last_request_id_(0) { | 131 last_request_id_(0) { |
124 AttachedClientHosts::GetInstance()->Add(this); | 132 AttachedClientHosts::GetInstance()->Add(this); |
125 | 133 |
126 // Detach from debugger when extension unloads. | 134 // Detach from debugger when extension unloads. |
127 Profile* profile = | 135 Profile* profile = |
128 Profile::FromBrowserContext(tab_contents_->browser_context()); | 136 Profile::FromBrowserContext(tab_contents_->browser_context()); |
129 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 137 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
130 content::Source<Profile>(profile)); | 138 content::Source<Profile>(profile)); |
131 | 139 |
132 // Attach to debugger and tell it we are ready. | 140 // Attach to debugger and tell it we are ready. |
133 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( | 141 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
134 tab_contents_->render_view_host(), | 142 tab_contents_->render_view_host()); |
135 this); | 143 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(agent, this); |
136 } | 144 } |
137 | 145 |
138 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { | 146 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { |
139 AttachedClientHosts::GetInstance()->Remove(this); | 147 AttachedClientHosts::GetInstance()->Remove(this); |
140 } | 148 } |
141 | 149 |
142 bool ExtensionDevToolsClientHost::MatchesContentsAndExtensionId( | 150 bool ExtensionDevToolsClientHost::MatchesContentsAndExtensionId( |
143 TabContents* tab_contents, | 151 TabContents* tab_contents, |
144 const std::string& extension_id) { | 152 const std::string& extension_id) { |
145 return tab_contents == tab_contents_ && extension_id_ == extension_id; | 153 return tab_contents == tab_contents_ && extension_id_ == extension_id; |
(...skipping 10 matching lines...) Expand all Loading... |
156 | 164 |
157 std::string json_args; | 165 std::string json_args; |
158 base::JSONWriter::Write(&args, false, &json_args); | 166 base::JSONWriter::Write(&args, false, &json_args); |
159 | 167 |
160 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 168 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
161 extension_id_, keys::kOnDetach, json_args, profile, GURL()); | 169 extension_id_, keys::kOnDetach, json_args, profile, GURL()); |
162 } | 170 } |
163 delete this; | 171 delete this; |
164 } | 172 } |
165 | 173 |
166 void ExtensionDevToolsClientHost::SendMessageToClient( | |
167 const IPC::Message& msg) { | |
168 IPC_BEGIN_MESSAGE_MAP(ExtensionDevToolsClientHost, msg) | |
169 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | |
170 OnDispatchOnInspectorFrontend); | |
171 IPC_MESSAGE_UNHANDLED_ERROR() | |
172 IPC_END_MESSAGE_MAP() | |
173 } | |
174 | |
175 void ExtensionDevToolsClientHost::TabReplaced( | 174 void ExtensionDevToolsClientHost::TabReplaced( |
176 TabContents* tab_contents) { | 175 TabContents* tab_contents) { |
177 tab_contents_ = tab_contents; | 176 tab_contents_ = tab_contents; |
178 } | 177 } |
179 | 178 |
180 void ExtensionDevToolsClientHost::Close() { | 179 void ExtensionDevToolsClientHost::Close() { |
181 DevToolsClientHost::NotifyCloseListener(); | 180 DevToolsManager::GetInstance()->ClientHostClosing(this); |
182 delete this; | 181 delete this; |
183 } | 182 } |
184 | 183 |
185 void ExtensionDevToolsClientHost::SendMessageToBackend( | 184 void ExtensionDevToolsClientHost::SendMessageToBackend( |
186 SendCommandDebuggerFunction* function, | 185 SendCommandDebuggerFunction* function, |
187 const std::string& method, | 186 const std::string& method, |
188 Value* params) { | 187 Value* params) { |
189 DictionaryValue protocol_request; | 188 DictionaryValue protocol_request; |
190 int request_id = ++last_request_id_; | 189 int request_id = ++last_request_id_; |
191 pending_requests_[request_id] = function; | 190 pending_requests_[request_id] = function; |
192 protocol_request.SetInteger("id", request_id); | 191 protocol_request.SetInteger("id", request_id); |
193 protocol_request.SetString("method", method); | 192 protocol_request.SetString("method", method); |
194 if (params) | 193 if (params) |
195 protocol_request.Set("params", params->DeepCopy()); | 194 protocol_request.Set("params", params->DeepCopy()); |
196 | 195 |
197 std::string json_args; | 196 std::string json_args; |
198 base::JSONWriter::Write(&protocol_request, false, &json_args); | 197 base::JSONWriter::Write(&protocol_request, false, &json_args); |
199 DevToolsManager::GetInstance()->ForwardToDevToolsAgent( | 198 DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this, json_args); |
200 this, | |
201 DevToolsAgentMsg_DispatchOnInspectorBackend(MSG_ROUTING_NONE, | |
202 json_args)); | |
203 } | 199 } |
204 | 200 |
205 void ExtensionDevToolsClientHost::Observe( | 201 void ExtensionDevToolsClientHost::Observe( |
206 int type, | 202 int type, |
207 const content::NotificationSource& source, | 203 const content::NotificationSource& source, |
208 const content::NotificationDetails& details) { | 204 const content::NotificationDetails& details) { |
209 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 205 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
210 Close(); | 206 Close(); |
211 } | 207 } |
212 | 208 |
213 void ExtensionDevToolsClientHost::OnDispatchOnInspectorFrontend( | 209 void ExtensionDevToolsClientHost::DispatchOnInspectorFrontend( |
214 const std::string& data) { | 210 const std::string& message) { |
215 Profile* profile = | 211 Profile* profile = |
216 Profile::FromBrowserContext(tab_contents_->browser_context()); | 212 Profile::FromBrowserContext(tab_contents_->browser_context()); |
217 if (profile == NULL || !profile->GetExtensionEventRouter()) | 213 if (profile == NULL || !profile->GetExtensionEventRouter()) |
218 return; | 214 return; |
219 | 215 |
220 scoped_ptr<Value> result(base::JSONReader::Read(data, false)); | 216 scoped_ptr<Value> result(base::JSONReader::Read(message, false)); |
221 if (!result->IsType(Value::TYPE_DICTIONARY)) | 217 if (!result->IsType(Value::TYPE_DICTIONARY)) |
222 return; | 218 return; |
223 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get()); | 219 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get()); |
224 | 220 |
225 int id; | 221 int id; |
226 if (!dictionary->GetInteger("id", &id)) { | 222 if (!dictionary->GetInteger("id", &id)) { |
227 std::string method_name; | 223 std::string method_name; |
228 if (!dictionary->GetString("method", &method_name)) | 224 if (!dictionary->GetString("method", &method_name)) |
229 return; | 225 return; |
230 | 226 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 std::string version; | 303 std::string version; |
308 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &version)); | 304 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &version)); |
309 | 305 |
310 if (!webkit_glue::IsInspectorProtocolVersionSupported(version)) { | 306 if (!webkit_glue::IsInspectorProtocolVersionSupported(version)) { |
311 error_ = ExtensionErrorUtils::FormatErrorMessage( | 307 error_ = ExtensionErrorUtils::FormatErrorMessage( |
312 keys::kProtocolVersionNotSupportedError, | 308 keys::kProtocolVersionNotSupportedError, |
313 version); | 309 version); |
314 return false; | 310 return false; |
315 } | 311 } |
316 | 312 |
| 313 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 314 contents_->render_view_host()); |
317 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> | 315 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> |
318 GetDevToolsClientHostFor(contents_->render_view_host()); | 316 GetDevToolsClientHostFor(agent); |
319 | 317 |
320 if (client_host != NULL) { | 318 if (client_host != NULL) { |
321 error_ = ExtensionErrorUtils::FormatErrorMessage( | 319 error_ = ExtensionErrorUtils::FormatErrorMessage( |
322 keys::kAlreadyAttachedError, | 320 keys::kAlreadyAttachedError, |
323 base::IntToString(tab_id_)); | 321 base::IntToString(tab_id_)); |
324 return false; | 322 return false; |
325 } | 323 } |
326 | 324 |
327 new ExtensionDevToolsClientHost(contents_, GetExtension()->id(), tab_id_); | 325 new ExtensionDevToolsClientHost(contents_, GetExtension()->id(), tab_id_); |
328 SendResponse(true); | 326 SendResponse(true); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 return; | 369 return; |
372 } | 370 } |
373 | 371 |
374 Value* result_body; | 372 Value* result_body; |
375 if (dictionary->Get("result", &result_body)) | 373 if (dictionary->Get("result", &result_body)) |
376 result_.reset(result_body->DeepCopy()); | 374 result_.reset(result_body->DeepCopy()); |
377 else | 375 else |
378 result_.reset(new DictionaryValue()); | 376 result_.reset(new DictionaryValue()); |
379 SendResponse(true); | 377 SendResponse(true); |
380 } | 378 } |
OLD | NEW |