Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: chrome/browser/extensions/api/debugger/debugger_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698