OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/debugger/debugger_api.h" | 7 #include "chrome/browser/extensions/api/debugger/debugger_api.h" |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 using content::WebContents; | 59 using content::WebContents; |
60 using extensions::ErrorUtils; | 60 using extensions::ErrorUtils; |
61 | 61 |
62 namespace keys = debugger_api_constants; | 62 namespace keys = debugger_api_constants; |
63 namespace Attach = extensions::api::debugger::Attach; | 63 namespace Attach = extensions::api::debugger::Attach; |
64 namespace Detach = extensions::api::debugger::Detach; | 64 namespace Detach = extensions::api::debugger::Detach; |
65 namespace OnDetach = extensions::api::debugger::OnDetach; | 65 namespace OnDetach = extensions::api::debugger::OnDetach; |
66 namespace OnEvent = extensions::api::debugger::OnEvent; | 66 namespace OnEvent = extensions::api::debugger::OnEvent; |
67 namespace SendCommand = extensions::api::debugger::SendCommand; | 67 namespace SendCommand = extensions::api::debugger::SendCommand; |
68 | 68 |
| 69 namespace { |
| 70 class ExtensionDevToolsInfoBarDelegate; |
| 71 } // namespace |
| 72 |
69 | 73 |
70 // ExtensionDevToolsClientHost ------------------------------------------------ | 74 // ExtensionDevToolsClientHost ------------------------------------------------ |
71 | 75 |
72 class ExtensionDevToolsClientHost : public DevToolsClientHost, | 76 class ExtensionDevToolsClientHost : public DevToolsClientHost, |
73 public content::NotificationObserver { | 77 public content::NotificationObserver { |
74 public: | 78 public: |
75 ExtensionDevToolsClientHost( | 79 ExtensionDevToolsClientHost( |
76 Profile* profile, | 80 Profile* profile, |
77 DevToolsAgentHost* agent_host, | 81 DevToolsAgentHost* agent_host, |
78 const std::string& extension_id, | 82 const std::string& extension_id, |
79 const std::string& extension_name, | 83 const std::string& extension_name, |
80 const Debuggee& debuggee, | 84 const Debuggee& debuggee, |
81 InfoBar* infobar); | 85 ExtensionDevToolsInfoBarDelegate* infobar); |
82 | 86 |
83 virtual ~ExtensionDevToolsClientHost(); | 87 virtual ~ExtensionDevToolsClientHost(); |
84 | 88 |
85 const std::string& extension_id() { return extension_id_; } | 89 const std::string& extension_id() { return extension_id_; } |
86 void Close(); | 90 void Close(); |
87 void SendMessageToBackend(DebuggerSendCommandFunction* function, | 91 void SendMessageToBackend(DebuggerSendCommandFunction* function, |
88 const std::string& method, | 92 const std::string& method, |
89 SendCommand::Params::CommandParams* command_params); | 93 SendCommand::Params::CommandParams* command_params); |
90 | 94 |
91 // Marks connection as to-be-terminated by the user. | 95 // Marks connection as to-be-terminated by the user. |
(...skipping 14 matching lines...) Expand all Loading... |
106 | 110 |
107 Profile* profile_; | 111 Profile* profile_; |
108 scoped_refptr<DevToolsAgentHost> agent_host_; | 112 scoped_refptr<DevToolsAgentHost> agent_host_; |
109 std::string extension_id_; | 113 std::string extension_id_; |
110 Debuggee debuggee_; | 114 Debuggee debuggee_; |
111 content::NotificationRegistrar registrar_; | 115 content::NotificationRegistrar registrar_; |
112 int last_request_id_; | 116 int last_request_id_; |
113 typedef std::map<int, scoped_refptr<DebuggerSendCommandFunction> > | 117 typedef std::map<int, scoped_refptr<DebuggerSendCommandFunction> > |
114 PendingRequests; | 118 PendingRequests; |
115 PendingRequests pending_requests_; | 119 PendingRequests pending_requests_; |
116 InfoBar* infobar_; | 120 ExtensionDevToolsInfoBarDelegate* infobar_; |
117 OnDetach::Reason detach_reason_; | 121 OnDetach::Reason detach_reason_; |
118 | 122 |
119 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsClientHost); | 123 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsClientHost); |
120 }; | 124 }; |
121 | 125 |
122 // The member function declarations come after the other class declarations, so | 126 // The member function declarations come after the other class declarations, so |
123 // they can call members on them. | 127 // they can call members on them. |
124 | 128 |
125 | 129 |
126 namespace { | 130 namespace { |
127 | 131 |
128 // Helpers -------------------------------------------------------------------- | 132 // Helpers -------------------------------------------------------------------- |
129 | 133 |
130 void CopyDebuggee(Debuggee* dst, const Debuggee& src) { | 134 void CopyDebuggee(Debuggee* dst, const Debuggee& src) { |
131 if (src.tab_id) | 135 if (src.tab_id) |
132 dst->tab_id.reset(new int(*src.tab_id)); | 136 dst->tab_id.reset(new int(*src.tab_id)); |
133 if (src.extension_id) | 137 if (src.extension_id) |
134 dst->extension_id.reset(new std::string(*src.extension_id)); | 138 dst->extension_id.reset(new std::string(*src.extension_id)); |
135 if (src.target_id) | 139 if (src.target_id) |
136 dst->target_id.reset(new std::string(*src.target_id)); | 140 dst->target_id.reset(new std::string(*src.target_id)); |
137 } | 141 } |
138 | 142 |
139 | 143 |
140 // ExtensionDevToolsInfoBarDelegate ------------------------------------------- | 144 // ExtensionDevToolsInfoBarDelegate ------------------------------------------- |
141 | 145 |
142 class ExtensionDevToolsInfoBarDelegate : public ConfirmInfoBarDelegate { | 146 class ExtensionDevToolsInfoBarDelegate : public ConfirmInfoBarDelegate { |
143 public: | 147 public: |
144 // Creates an extension dev tools infobar and delegate and adds the infobar to | 148 // Creates an extension dev tools infobar delegate and adds it to the |
145 // the InfoBarService associated with |rvh|. Returns the infobar if it was | 149 // InfoBarService associated with |rvh|. Returns the delegate if it was |
146 // successfully added. | 150 // successfully added. |
147 static InfoBar* Create(RenderViewHost* rvh, const std::string& client_name); | 151 static ExtensionDevToolsInfoBarDelegate* Create( |
| 152 RenderViewHost* rvh, |
| 153 const std::string& client_name); |
148 | 154 |
149 void set_client_host(ExtensionDevToolsClientHost* client_host) { | 155 void set_client_host(ExtensionDevToolsClientHost* client_host) { |
150 client_host_ = client_host; | 156 client_host_ = client_host; |
151 } | 157 } |
152 | 158 |
153 private: | 159 private: |
154 explicit ExtensionDevToolsInfoBarDelegate(const std::string& client_name); | 160 ExtensionDevToolsInfoBarDelegate(InfoBarService* infobar_service, |
| 161 const std::string& client_name); |
155 virtual ~ExtensionDevToolsInfoBarDelegate(); | 162 virtual ~ExtensionDevToolsInfoBarDelegate(); |
156 | 163 |
157 // ConfirmInfoBarDelegate: | 164 // ConfirmInfoBarDelegate: |
158 virtual void InfoBarDismissed() OVERRIDE; | 165 virtual void InfoBarDismissed() OVERRIDE; |
159 virtual Type GetInfoBarType() const OVERRIDE; | 166 virtual Type GetInfoBarType() const OVERRIDE; |
160 virtual bool ShouldExpireInternal( | 167 virtual bool ShouldExpireInternal( |
161 const content::LoadCommittedDetails& details) const OVERRIDE; | 168 const content::LoadCommittedDetails& details) const OVERRIDE; |
162 virtual string16 GetMessageText() const OVERRIDE; | 169 virtual string16 GetMessageText() const OVERRIDE; |
163 virtual int GetButtons() const OVERRIDE; | 170 virtual int GetButtons() const OVERRIDE; |
164 virtual bool Cancel() OVERRIDE; | 171 virtual bool Cancel() OVERRIDE; |
165 | 172 |
166 std::string client_name_; | 173 std::string client_name_; |
167 ExtensionDevToolsClientHost* client_host_; | 174 ExtensionDevToolsClientHost* client_host_; |
168 | 175 |
169 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsInfoBarDelegate); | 176 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsInfoBarDelegate); |
170 }; | 177 }; |
171 | 178 |
172 // static | 179 // static |
173 InfoBar* ExtensionDevToolsInfoBarDelegate::Create( | 180 ExtensionDevToolsInfoBarDelegate* ExtensionDevToolsInfoBarDelegate::Create( |
174 RenderViewHost* rvh, | 181 RenderViewHost* rvh, |
175 const std::string& client_name) { | 182 const std::string& client_name) { |
176 if (!rvh) | 183 if (!rvh) |
177 return NULL; | 184 return NULL; |
178 | 185 |
179 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | 186 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); |
180 if (!web_contents) | 187 if (!web_contents) |
181 return NULL; | 188 return NULL; |
182 | 189 |
183 InfoBarService* infobar_service = | 190 InfoBarService* infobar_service = |
184 InfoBarService::FromWebContents(web_contents); | 191 InfoBarService::FromWebContents(web_contents); |
185 if (!infobar_service) | 192 if (!infobar_service) |
186 return NULL; | 193 return NULL; |
187 | 194 |
188 return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( | 195 return static_cast<ExtensionDevToolsInfoBarDelegate*>( |
189 scoped_ptr<ConfirmInfoBarDelegate>( | 196 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( |
190 new ExtensionDevToolsInfoBarDelegate(client_name)))); | 197 new ExtensionDevToolsInfoBarDelegate(infobar_service, client_name)))); |
191 } | 198 } |
192 | 199 |
193 ExtensionDevToolsInfoBarDelegate::ExtensionDevToolsInfoBarDelegate( | 200 ExtensionDevToolsInfoBarDelegate::ExtensionDevToolsInfoBarDelegate( |
| 201 InfoBarService* infobar_service, |
194 const std::string& client_name) | 202 const std::string& client_name) |
195 : ConfirmInfoBarDelegate(), | 203 : ConfirmInfoBarDelegate(infobar_service), |
196 client_name_(client_name), | 204 client_name_(client_name), |
197 client_host_(NULL) { | 205 client_host_(NULL) { |
198 } | 206 } |
199 | 207 |
200 ExtensionDevToolsInfoBarDelegate::~ExtensionDevToolsInfoBarDelegate() { | 208 ExtensionDevToolsInfoBarDelegate::~ExtensionDevToolsInfoBarDelegate() { |
201 } | 209 } |
202 | 210 |
203 void ExtensionDevToolsInfoBarDelegate::InfoBarDismissed() { | 211 void ExtensionDevToolsInfoBarDelegate::InfoBarDismissed() { |
204 if (client_host_) | 212 if (client_host_) |
205 client_host_->MarkAsDismissed(); | 213 client_host_->MarkAsDismissed(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 296 |
289 | 297 |
290 // ExtensionDevToolsClientHost ------------------------------------------------ | 298 // ExtensionDevToolsClientHost ------------------------------------------------ |
291 | 299 |
292 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( | 300 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( |
293 Profile* profile, | 301 Profile* profile, |
294 DevToolsAgentHost* agent_host, | 302 DevToolsAgentHost* agent_host, |
295 const std::string& extension_id, | 303 const std::string& extension_id, |
296 const std::string& extension_name, | 304 const std::string& extension_name, |
297 const Debuggee& debuggee, | 305 const Debuggee& debuggee, |
298 InfoBar* infobar) | 306 ExtensionDevToolsInfoBarDelegate* infobar) |
299 : profile_(profile), | 307 : profile_(profile), |
300 agent_host_(agent_host), | 308 agent_host_(agent_host), |
301 extension_id_(extension_id), | 309 extension_id_(extension_id), |
302 last_request_id_(0), | 310 last_request_id_(0), |
303 infobar_(infobar), | 311 infobar_(infobar), |
304 detach_reason_(OnDetach::REASON_TARGET_CLOSED) { | 312 detach_reason_(OnDetach::REASON_TARGET_CLOSED) { |
305 CopyDebuggee(&debuggee_, debuggee); | 313 CopyDebuggee(&debuggee_, debuggee); |
306 | 314 |
307 AttachedClientHosts::GetInstance()->Add(this); | 315 AttachedClientHosts::GetInstance()->Add(this); |
308 | 316 |
309 // Detach from debugger when extension unloads. | 317 // Detach from debugger when extension unloads. |
310 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 318 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
311 content::Source<Profile>(profile_)); | 319 content::Source<Profile>(profile_)); |
312 | 320 |
313 // RVH-based agents disconnect from their clients when the app is terminating | 321 // RVH-based agents disconnect from their clients when the app is terminating |
314 // but shared worker-based agents do not. | 322 // but shared worker-based agents do not. |
315 // Disconnect explicitly to make sure that |this| observer is not leaked. | 323 // Disconnect explicitly to make sure that |this| observer is not leaked. |
316 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 324 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
317 content::NotificationService::AllSources()); | 325 content::NotificationService::AllSources()); |
318 | 326 |
319 // Attach to debugger and tell it we are ready. | 327 // Attach to debugger and tell it we are ready. |
320 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( | 328 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( |
321 agent_host_.get(), this); | 329 agent_host_.get(), this); |
322 | 330 |
323 if (infobar_) { | 331 if (infobar_) { |
324 static_cast<ExtensionDevToolsInfoBarDelegate*>( | 332 infobar_->set_client_host(this); |
325 infobar_->delegate())->set_client_host(this); | |
326 registrar_.Add( | 333 registrar_.Add( |
327 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 334 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
328 content::Source<InfoBarService>(InfoBarService::FromWebContents( | 335 content::Source<InfoBarService>(InfoBarService::FromWebContents( |
329 WebContents::FromRenderViewHost( | 336 WebContents::FromRenderViewHost( |
330 agent_host_->GetRenderViewHost())))); | 337 agent_host_->GetRenderViewHost())))); |
331 } | 338 } |
332 } | 339 } |
333 | 340 |
334 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { | 341 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { |
335 // Ensure calling RemoveInfoBar() below won't result in Observe() trying to | 342 // Ensure calling RemoveInfoBar() below won't result in Observe() trying to |
336 // Close() us. | 343 // Close() us. |
337 registrar_.RemoveAll(); | 344 registrar_.RemoveAll(); |
338 | 345 |
339 if (infobar_) { | 346 if (infobar_) { |
340 static_cast<ExtensionDevToolsInfoBarDelegate*>( | 347 infobar_->set_client_host(NULL); |
341 infobar_->delegate())->set_client_host(NULL); | |
342 InfoBarService::FromWebContents(WebContents::FromRenderViewHost( | 348 InfoBarService::FromWebContents(WebContents::FromRenderViewHost( |
343 agent_host_->GetRenderViewHost()))->RemoveInfoBar(infobar_); | 349 agent_host_->GetRenderViewHost()))->RemoveInfoBar(infobar_); |
344 } | 350 } |
345 AttachedClientHosts::GetInstance()->Remove(this); | 351 AttachedClientHosts::GetInstance()->Remove(this); |
346 } | 352 } |
347 | 353 |
348 // DevToolsClientHost interface | 354 // DevToolsClientHost interface |
349 void ExtensionDevToolsClientHost::InspectedContentsClosing() { | 355 void ExtensionDevToolsClientHost::InspectedContentsClosing() { |
350 SendDetachedEvent(); | 356 SendDetachedEvent(); |
351 delete this; | 357 delete this; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 keys::kProtocolVersionNotSupportedError, | 562 keys::kProtocolVersionNotSupportedError, |
557 params->required_version); | 563 params->required_version); |
558 return false; | 564 return false; |
559 } | 565 } |
560 | 566 |
561 if (agent_host_->IsAttached()) { | 567 if (agent_host_->IsAttached()) { |
562 FormatErrorMessage(keys::kAlreadyAttachedError); | 568 FormatErrorMessage(keys::kAlreadyAttachedError); |
563 return false; | 569 return false; |
564 } | 570 } |
565 | 571 |
566 InfoBar* infobar = NULL; | 572 ExtensionDevToolsInfoBarDelegate* infobar = NULL; |
567 if (!CommandLine::ForCurrentProcess()-> | 573 if (!CommandLine::ForCurrentProcess()-> |
568 HasSwitch(switches::kSilentDebuggerExtensionAPI)) { | 574 HasSwitch(switches::kSilentDebuggerExtensionAPI)) { |
569 // Do not attach to the target if for any reason the infobar cannot be shown | 575 // Do not attach to the target if for any reason the infobar cannot be shown |
570 // for this WebContents instance. | 576 // for this WebContents instance. |
571 infobar = ExtensionDevToolsInfoBarDelegate::Create( | 577 infobar = ExtensionDevToolsInfoBarDelegate::Create( |
572 agent_host_->GetRenderViewHost(), GetExtension()->name()); | 578 agent_host_->GetRenderViewHost(), GetExtension()->name()); |
573 if (!infobar) { | 579 if (!infobar) { |
574 error_ = ErrorUtils::FormatErrorMessage( | 580 error_ = ErrorUtils::FormatErrorMessage( |
575 keys::kSilentDebuggingRequired, | 581 keys::kSilentDebuggingRequired, |
576 switches::kSilentDebuggerExtensionAPI); | 582 switches::kSilentDebuggerExtensionAPI); |
577 return false; | 583 return false; |
578 } | 584 } |
579 } | 585 } |
580 | 586 |
581 new ExtensionDevToolsClientHost(GetProfile(), | 587 new ExtensionDevToolsClientHost(GetProfile(), |
582 agent_host_.get(), | 588 agent_host_.get(), |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 | 716 |
711 void DebuggerGetTargetsFunction::SendTargetList( | 717 void DebuggerGetTargetsFunction::SendTargetList( |
712 const std::vector<DevToolsTargetImpl*>& target_list) { | 718 const std::vector<DevToolsTargetImpl*>& target_list) { |
713 scoped_ptr<base::ListValue> result(new base::ListValue()); | 719 scoped_ptr<base::ListValue> result(new base::ListValue()); |
714 for (size_t i = 0; i < target_list.size(); ++i) | 720 for (size_t i = 0; i < target_list.size(); ++i) |
715 result->Append(SerializeTarget(*target_list[i])); | 721 result->Append(SerializeTarget(*target_list[i])); |
716 STLDeleteContainerPointers(target_list.begin(), target_list.end()); | 722 STLDeleteContainerPointers(target_list.begin(), target_list.end()); |
717 SetResult(result.release()); | 723 SetResult(result.release()); |
718 SendResponse(true); | 724 SendResponse(true); |
719 } | 725 } |
OLD | NEW |