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

Side by Side Diff: chrome/browser/extensions/external_install_ui.cc

Issue 11150002: New post-sideload UI: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 2 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/external_install_ui.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/extensions/extension_install_prompt.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/global_error/global_error.h"
23 #include "chrome/browser/ui/global_error/global_error_service.h"
24 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
25 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/extensions/extension.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h"
30 #include "content/public/browser/notification_source.h"
31 #include "grit/chromium_strings.h"
32 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
35
36 namespace extensions {
37
38 static const int kMenuCommandId = IDC_EXTERNAL_EXTENSION_ALERT;
39
40 // ExternalInstallDialogDelegate --------------------------------------------
41
42 class ExternalInstallDialogDelegate
43 : public ExtensionInstallPrompt::Delegate,
44 public base::RefCountedThreadSafe<ExternalInstallDialogDelegate> {
Jeffrey Yasskin 2012/10/15 00:43:59 Please don't use refcounting to manage the lifetim
Matt Perry 2012/10/15 19:13:38 This file is a copy+paste of extension_disabled_ui
45 public:
46 ExternalInstallDialogDelegate(Browser* browser,
47 ExtensionService* service,
48 const Extension* extension);
49
50 private:
51 friend class base::RefCountedThreadSafe<ExternalInstallDialogDelegate>;
52
53 virtual ~ExternalInstallDialogDelegate();
54
55 // ExtensionInstallPrompt::Delegate:
56 virtual void InstallUIProceed() OVERRIDE;
57 virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
58
59 // The UI for showing the install dialog when enabling.
60 scoped_ptr<ExtensionInstallPrompt> install_ui_;
61
62 ExtensionService* service_;
63 const Extension* extension_;
64 };
65
66 ExternalInstallDialogDelegate::ExternalInstallDialogDelegate(
67 Browser* browser,
68 ExtensionService* service,
69 const Extension* extension)
70 : service_(service), extension_(extension) {
71 AddRef(); // Balanced in Proceed or Abort.
72
73 install_ui_.reset(chrome::CreateExtensionInstallPromptWithBrowser(browser));
74 install_ui_->ConfirmExternalInstall(this, extension_);
75 }
76
77 ExternalInstallDialogDelegate::~ExternalInstallDialogDelegate() {
78 }
79
80 void ExternalInstallDialogDelegate::InstallUIProceed() {
81 service_->GrantPermissionsAndEnableExtension(
82 extension_, install_ui_->record_oauth2_grant());
83 Release();
84 }
85
86 void ExternalInstallDialogDelegate::InstallUIAbort(bool user_initiated) {
87 service_->UninstallExtension(extension_->id(), false, NULL);
88 Release();
89 }
90
91 // ExternalInstallGlobalError -----------------------------------------------
92
93 class ExternalInstallGlobalError : public GlobalError,
94 public content::NotificationObserver {
95 public:
96 ExternalInstallGlobalError(ExtensionService* service,
97 const Extension* extension);
98 virtual ~ExternalInstallGlobalError();
99
100 const Extension* extension() const { return extension_; }
101
102 // GlobalError implementation.
103 virtual bool HasBadge() OVERRIDE;
104 virtual int GetBadgeResourceID() OVERRIDE;
105 virtual bool HasMenuItem() OVERRIDE;
106 virtual int MenuItemCommandID() OVERRIDE;
107 virtual string16 MenuItemLabel() OVERRIDE;
108 virtual int MenuItemIconResourceID() OVERRIDE;
109 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
110 virtual bool HasBubbleView() OVERRIDE;
111 virtual string16 GetBubbleViewTitle() OVERRIDE;
112 virtual string16 GetBubbleViewMessage() OVERRIDE;
113 virtual string16 GetBubbleViewAcceptButtonLabel() OVERRIDE;
114 virtual string16 GetBubbleViewCancelButtonLabel() OVERRIDE;
115 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE;
116 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE;
117 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE;
118
119 // content::NotificationObserver implementation.
120 virtual void Observe(int type,
121 const content::NotificationSource& source,
122 const content::NotificationDetails& details) OVERRIDE;
123
124 private:
125 ExtensionService* service_;
126 const Extension* extension_;
127
128 // How the user responded to the error; used for metrics.
129 enum UserResponse {
130 IGNORED,
131 REENABLE,
132 UNINSTALL,
133 EXTENSION_DISABLED_UI_BUCKET_BOUNDARY
134 };
135 UserResponse user_response_;
136
137 content::NotificationRegistrar registrar_;
138 };
139
140 // TODO(yoz): create error at startup for disabled extensions.
Jeffrey Yasskin 2012/10/15 00:43:59 Is this comment correct for this file?
Matt Perry 2012/10/15 19:13:38 Oops, removed.
141 ExternalInstallGlobalError::ExternalInstallGlobalError(
142 ExtensionService* service,
143 const Extension* extension)
144 : service_(service),
145 extension_(extension),
146 user_response_(IGNORED) {
147 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
148 content::Source<Profile>(service->profile()));
149 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
150 content::Source<Profile>(service->profile()));
151 }
152
153 ExternalInstallGlobalError::~ExternalInstallGlobalError() {
154 HISTOGRAM_ENUMERATION("Extensions.ExternalInstallUIUserResponse",
155 user_response_, EXTENSION_DISABLED_UI_BUCKET_BOUNDARY);
156 }
157
158 bool ExternalInstallGlobalError::HasBadge() {
159 return true;
Jeffrey Yasskin 2012/10/15 00:43:59 Since this is a .cc file, I'd probably inline all
160 }
161
162 int ExternalInstallGlobalError::GetBadgeResourceID() {
163 return IDR_UPDATE_BADGE_EXTENSION;
164 }
165
166 bool ExternalInstallGlobalError::HasMenuItem() {
167 return true;
168 }
169
170 int ExternalInstallGlobalError::MenuItemCommandID() {
171 return kMenuCommandId;
172 }
173
174 int ExternalInstallGlobalError::MenuItemIconResourceID() {
175 return IDR_UPDATE_MENU_EXTENSION;
176 }
177
178 string16 ExternalInstallGlobalError::MenuItemLabel() {
179 return l10n_util::GetStringFUTF16(IDS_EXTENSION_EXTERNAL_INSTALL_ALERT,
180 UTF8ToUTF16(extension_->name()));
181 }
182
183 void ExternalInstallGlobalError::ExecuteMenuItem(Browser* browser) {
184 ShowExternalInstallDialog(service_, browser, extension_);
185 }
186
187 bool ExternalInstallGlobalError::HasBubbleView() {
188 return false;
189 }
190
191 string16 ExternalInstallGlobalError::GetBubbleViewTitle() {
192 return string16();
193 }
194
195 string16 ExternalInstallGlobalError::GetBubbleViewMessage() {
196 return string16();
197 }
198
199 string16 ExternalInstallGlobalError::GetBubbleViewAcceptButtonLabel() {
200 return string16();
201 }
202
203 string16 ExternalInstallGlobalError::GetBubbleViewCancelButtonLabel() {
204 return string16();
205 }
206
207 void ExternalInstallGlobalError::OnBubbleViewDidClose(Browser* browser) {
208 NOTREACHED();
209 }
210
211 void ExternalInstallGlobalError::BubbleViewAcceptButtonPressed(
212 Browser* browser) {
213 NOTREACHED();
214 }
215
216 void ExternalInstallGlobalError::BubbleViewCancelButtonPressed(
217 Browser* browser) {
218 NOTREACHED();
219 }
220
221 void ExternalInstallGlobalError::Observe(
222 int type,
223 const content::NotificationSource& source,
224 const content::NotificationDetails& details) {
225 const Extension* extension = NULL;
226 // The error is invalidated if the extension has been reloaded
227 // or unloaded.
228 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
229 extension = content::Details<const Extension>(details).ptr();
230 } else {
231 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type);
232 extensions::UnloadedExtensionInfo* info =
233 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
234 extension = info->extension;
235 }
236 if (extension == extension_) {
237 if (type == chrome::NOTIFICATION_EXTENSION_LOADED)
238 user_response_ = REENABLE;
Jeffrey Yasskin 2012/10/15 00:43:59 Naively, this makes it look like if the external s
Matt Perry 2012/10/15 19:13:38 Done.
239 else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED)
240 user_response_ = UNINSTALL;
241
242 // This call deletes us (via Add/RemoveExternalInstallError).
243 service_->AcknowledgeExternalExtension(extension_->id());
244 }
245 }
246
247 // Globals --------------------------------------------------------------------
Jeffrey Yasskin 2012/10/15 00:43:59 Do you mean "public interface"?
Matt Perry 2012/10/15 19:13:38 Done.
248
249 bool AddExternalInstallError(ExtensionService* service,
250 const Extension* extension) {
251 GlobalErrorService* error_service =
252 GlobalErrorServiceFactory::GetForProfile(service->profile());
253 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID(
254 kMenuCommandId);
255 if (error) {
256 if (static_cast<ExternalInstallGlobalError*>(error)->extension() ==
257 extension) {
258 return false; // already showing
259 }
260 error_service->RemoveGlobalError(error);
261 delete error;
262 }
263
264 error_service->AddGlobalError(
265 new ExternalInstallGlobalError(service, extension));
266 return true;
267 }
268
269 void RemoveExternalInstallError(ExtensionService* service) {
270 GlobalErrorService* error_service =
271 GlobalErrorServiceFactory::GetForProfile(service->profile());
272 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID(
273 kMenuCommandId);
274 if (error) {
275 error_service->RemoveGlobalError(error);
276 delete error;
277 }
278 }
279
280 void ShowExternalInstallDialog(ExtensionService* service, Browser* browser,
281 const Extension* extension) {
282 // This object manages its own lifetime.
283 new ExternalInstallDialogDelegate(browser, service, extension);
284 }
285
286 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698