Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / extension_disabled_ui.cc
blob3d6ce080640d2d3038b72fa0b69e4080596a0b7d
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.
5 #include "chrome/browser/extensions/extension_disabled_ui.h"
7 #include <bitset>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/app/chrome_command_ids.h"
19 #include "chrome/browser/extensions/extension_install_prompt.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
22 #include "chrome/browser/extensions/extension_util.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/global_error/global_error.h"
27 #include "chrome/browser/ui/global_error/global_error_service.h"
28 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/grit/chromium_strings.h"
31 #include "chrome/grit/generated_resources.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_observer.h"
34 #include "content/public/browser/notification_registrar.h"
35 #include "content/public/browser/notification_source.h"
36 #include "extensions/browser/extension_util.h"
37 #include "extensions/browser/image_loader.h"
38 #include "extensions/browser/notification_types.h"
39 #include "extensions/browser/uninstall_reason.h"
40 #include "extensions/common/constants.h"
41 #include "extensions/common/extension.h"
42 #include "extensions/common/extension_icon_set.h"
43 #include "extensions/common/manifest_handlers/icons_handler.h"
44 #include "extensions/common/permissions/permission_message_provider.h"
45 #include "extensions/common/permissions/permission_set.h"
46 #include "extensions/common/permissions/permissions_data.h"
47 #include "ui/base/l10n/l10n_util.h"
48 #include "ui/gfx/geometry/size.h"
49 #include "ui/gfx/image/image.h"
50 #include "ui/gfx/image/image_skia_operations.h"
52 using extensions::Extension;
54 namespace {
56 static const int kIconSize = extension_misc::EXTENSION_ICON_SMALL;
58 static base::LazyInstance<
59 std::bitset<IDC_EXTENSION_DISABLED_LAST -
60 IDC_EXTENSION_DISABLED_FIRST + 1> >
61 menu_command_ids = LAZY_INSTANCE_INITIALIZER;
63 // Get an available menu ID.
64 int GetMenuCommandID() {
65 int id;
66 for (id = IDC_EXTENSION_DISABLED_FIRST;
67 id <= IDC_EXTENSION_DISABLED_LAST; ++id) {
68 if (!menu_command_ids.Get()[id - IDC_EXTENSION_DISABLED_FIRST]) {
69 menu_command_ids.Get().set(id - IDC_EXTENSION_DISABLED_FIRST);
70 return id;
73 // This should not happen.
74 DCHECK(id <= IDC_EXTENSION_DISABLED_LAST) <<
75 "No available menu command IDs for ExtensionDisabledGlobalError";
76 return IDC_EXTENSION_DISABLED_LAST;
79 // Make a menu ID available when it is no longer used.
80 void ReleaseMenuCommandID(int id) {
81 menu_command_ids.Get().reset(id - IDC_EXTENSION_DISABLED_FIRST);
84 } // namespace
86 // ExtensionDisabledDialogDelegate --------------------------------------------
88 class ExtensionDisabledDialogDelegate
89 : public ExtensionInstallPrompt::Delegate,
90 public base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate> {
91 public:
92 ExtensionDisabledDialogDelegate(ExtensionService* service,
93 scoped_ptr<ExtensionInstallPrompt> install_ui,
94 const Extension* extension);
96 private:
97 friend class base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate>;
99 ~ExtensionDisabledDialogDelegate() override;
101 // ExtensionInstallPrompt::Delegate:
102 void InstallUIProceed() override;
103 void InstallUIAbort(bool user_initiated) override;
105 // The UI for showing the install dialog when enabling.
106 scoped_ptr<ExtensionInstallPrompt> install_ui_;
108 ExtensionService* service_;
109 const Extension* extension_;
112 ExtensionDisabledDialogDelegate::ExtensionDisabledDialogDelegate(
113 ExtensionService* service,
114 scoped_ptr<ExtensionInstallPrompt> install_ui,
115 const Extension* extension)
116 : install_ui_(install_ui.Pass()),
117 service_(service),
118 extension_(extension) {
119 AddRef(); // Balanced in Proceed or Abort.
120 install_ui_->ConfirmReEnable(this, extension_);
123 ExtensionDisabledDialogDelegate::~ExtensionDisabledDialogDelegate() {
126 void ExtensionDisabledDialogDelegate::InstallUIProceed() {
127 service_->GrantPermissionsAndEnableExtension(extension_);
128 Release();
131 void ExtensionDisabledDialogDelegate::InstallUIAbort(bool user_initiated) {
132 std::string histogram_name = user_initiated
133 ? "Extensions.Permissions_ReEnableCancel2"
134 : "Extensions.Permissions_ReEnableAbort2";
135 ExtensionService::RecordPermissionMessagesHistogram(
136 extension_, histogram_name.c_str());
138 // Do nothing. The extension will remain disabled.
139 Release();
142 // ExtensionDisabledGlobalError -----------------------------------------------
144 class ExtensionDisabledGlobalError
145 : public GlobalErrorWithStandardBubble,
146 public content::NotificationObserver,
147 public extensions::ExtensionUninstallDialog::Delegate {
148 public:
149 ExtensionDisabledGlobalError(ExtensionService* service,
150 const Extension* extension,
151 bool is_remote_install,
152 const gfx::Image& icon);
153 ~ExtensionDisabledGlobalError() override;
155 // GlobalError implementation.
156 Severity GetSeverity() override;
157 bool HasMenuItem() override;
158 int MenuItemCommandID() override;
159 base::string16 MenuItemLabel() override;
160 void ExecuteMenuItem(Browser* browser) override;
161 gfx::Image GetBubbleViewIcon() override;
162 base::string16 GetBubbleViewTitle() override;
163 std::vector<base::string16> GetBubbleViewMessages() override;
164 base::string16 GetBubbleViewAcceptButtonLabel() override;
165 base::string16 GetBubbleViewCancelButtonLabel() override;
166 void OnBubbleViewDidClose(Browser* browser) override;
167 void BubbleViewAcceptButtonPressed(Browser* browser) override;
168 void BubbleViewCancelButtonPressed(Browser* browser) override;
169 bool ShouldCloseOnDeactivate() const override;
171 // ExtensionUninstallDialog::Delegate implementation.
172 void ExtensionUninstallAccepted() override;
173 void ExtensionUninstallCanceled() override;
175 // content::NotificationObserver implementation.
176 void Observe(int type,
177 const content::NotificationSource& source,
178 const content::NotificationDetails& details) override;
180 private:
181 ExtensionService* service_;
182 const Extension* extension_;
183 bool is_remote_install_;
184 gfx::Image icon_;
186 // How the user responded to the error; used for metrics.
187 enum UserResponse {
188 IGNORED,
189 REENABLE,
190 UNINSTALL,
191 EXTENSION_DISABLED_UI_BUCKET_BOUNDARY
193 UserResponse user_response_;
195 scoped_ptr<extensions::ExtensionUninstallDialog> uninstall_dialog_;
197 // Menu command ID assigned for this extension's error.
198 int menu_command_id_;
200 content::NotificationRegistrar registrar_;
203 // TODO(yoz): create error at startup for disabled extensions.
204 ExtensionDisabledGlobalError::ExtensionDisabledGlobalError(
205 ExtensionService* service,
206 const Extension* extension,
207 bool is_remote_install,
208 const gfx::Image& icon)
209 : service_(service),
210 extension_(extension),
211 is_remote_install_(is_remote_install),
212 icon_(icon),
213 user_response_(IGNORED),
214 menu_command_id_(GetMenuCommandID()) {
215 if (icon_.IsEmpty()) {
216 icon_ = gfx::Image(
217 gfx::ImageSkiaOperations::CreateResizedImage(
218 extension_->is_app() ?
219 extensions::util::GetDefaultAppIcon() :
220 extensions::util::GetDefaultExtensionIcon(),
221 skia::ImageOperations::RESIZE_BEST,
222 gfx::Size(kIconSize, kIconSize)));
224 registrar_.Add(this,
225 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
226 content::Source<Profile>(service->profile()));
227 registrar_.Add(this,
228 extensions::NOTIFICATION_EXTENSION_REMOVED,
229 content::Source<Profile>(service->profile()));
232 ExtensionDisabledGlobalError::~ExtensionDisabledGlobalError() {
233 ReleaseMenuCommandID(menu_command_id_);
234 if (is_remote_install_) {
235 UMA_HISTOGRAM_ENUMERATION("Extensions.DisabledUIUserResponseRemoteInstall",
236 user_response_,
237 EXTENSION_DISABLED_UI_BUCKET_BOUNDARY);
238 } else {
239 UMA_HISTOGRAM_ENUMERATION("Extensions.DisabledUIUserResponse",
240 user_response_,
241 EXTENSION_DISABLED_UI_BUCKET_BOUNDARY);
245 GlobalError::Severity ExtensionDisabledGlobalError::GetSeverity() {
246 return SEVERITY_LOW;
249 bool ExtensionDisabledGlobalError::HasMenuItem() {
250 return true;
253 int ExtensionDisabledGlobalError::MenuItemCommandID() {
254 return menu_command_id_;
257 base::string16 ExtensionDisabledGlobalError::MenuItemLabel() {
258 std::string extension_name = extension_->name();
259 // Ampersands need to be escaped to avoid being treated like
260 // mnemonics in the menu.
261 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
263 if (is_remote_install_) {
264 return l10n_util::GetStringFUTF16(
265 IDS_EXTENSION_DISABLED_REMOTE_INSTALL_ERROR_TITLE,
266 base::UTF8ToUTF16(extension_name));
267 } else {
268 return l10n_util::GetStringFUTF16(IDS_EXTENSION_DISABLED_ERROR_TITLE,
269 base::UTF8ToUTF16(extension_name));
273 void ExtensionDisabledGlobalError::ExecuteMenuItem(Browser* browser) {
274 ShowBubbleView(browser);
277 gfx::Image ExtensionDisabledGlobalError::GetBubbleViewIcon() {
278 return icon_;
281 base::string16 ExtensionDisabledGlobalError::GetBubbleViewTitle() {
282 if (is_remote_install_) {
283 return l10n_util::GetStringFUTF16(
284 IDS_EXTENSION_DISABLED_REMOTE_INSTALL_ERROR_TITLE,
285 base::UTF8ToUTF16(extension_->name()));
286 } else {
287 return l10n_util::GetStringFUTF16(IDS_EXTENSION_DISABLED_ERROR_TITLE,
288 base::UTF8ToUTF16(extension_->name()));
292 std::vector<base::string16>
293 ExtensionDisabledGlobalError::GetBubbleViewMessages() {
294 std::vector<base::string16> messages;
295 std::vector<base::string16> permission_warnings =
296 extensions::PermissionMessageProvider::Get()->GetWarningMessages(
297 extension_->permissions_data()->active_permissions().get(),
298 extension_->GetType());
299 if (is_remote_install_) {
300 messages.push_back(l10n_util::GetStringFUTF16(
301 extension_->is_app()
302 ? IDS_APP_DISABLED_REMOTE_INSTALL_ERROR_LABEL
303 : IDS_EXTENSION_DISABLED_REMOTE_INSTALL_ERROR_LABEL,
304 base::UTF8ToUTF16(extension_->name())));
305 if (!permission_warnings.empty())
306 messages.push_back(
307 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WILL_HAVE_ACCESS_TO));
308 } else {
309 // TODO(treib): Add an extra message for supervised users. crbug.com/461261
310 messages.push_back(l10n_util::GetStringFUTF16(
311 extension_->is_app() ? IDS_APP_DISABLED_ERROR_LABEL
312 : IDS_EXTENSION_DISABLED_ERROR_LABEL,
313 base::UTF8ToUTF16(extension_->name())));
314 messages.push_back(l10n_util::GetStringUTF16(
315 IDS_EXTENSION_PROMPT_WILL_NOW_HAVE_ACCESS_TO));
317 for (size_t i = 0; i < permission_warnings.size(); ++i) {
318 messages.push_back(l10n_util::GetStringFUTF16(
319 IDS_EXTENSION_PERMISSION_LINE, permission_warnings[i]));
321 return messages;
324 base::string16 ExtensionDisabledGlobalError::GetBubbleViewAcceptButtonLabel() {
325 if (extensions::util::IsExtensionSupervised(extension_,
326 service_->profile())) {
327 // TODO(treib): Probably use a new string here once we get UX design.
328 // For now, just re-use an existing string that says "OK". crbug.com/461261
329 return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_ITEM_OK);
331 if (is_remote_install_) {
332 return l10n_util::GetStringUTF16(
333 IDS_EXTENSION_PROMPT_REMOTE_INSTALL_BUTTON);
335 return l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_RE_ENABLE_BUTTON);
338 base::string16 ExtensionDisabledGlobalError::GetBubbleViewCancelButtonLabel() {
339 // For custodian-installed extensions, supervised users only get a single
340 // "acknowledge" button.
341 if (extensions::util::IsExtensionSupervised(extension_, service_->profile()))
342 return base::string16();
343 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL);
346 void ExtensionDisabledGlobalError::OnBubbleViewDidClose(Browser* browser) {
349 void ExtensionDisabledGlobalError::BubbleViewAcceptButtonPressed(
350 Browser* browser) {
351 // Supervised users can't re-enable custodian-installed extensions, just
352 // acknowledge that they've been disabled.
353 if (extensions::util::IsExtensionSupervised(extension_, service_->profile()))
354 return;
355 // Delay extension reenabling so this bubble closes properly.
356 base::MessageLoop::current()->PostTask(FROM_HERE,
357 base::Bind(&ExtensionService::GrantPermissionsAndEnableExtension,
358 service_->AsWeakPtr(), extension_));
361 void ExtensionDisabledGlobalError::BubbleViewCancelButtonPressed(
362 Browser* browser) {
363 // This button shouldn't exist for custodian-installed extensions in a
364 // supervised profile.
365 DCHECK(!extensions::util::IsExtensionSupervised(extension_,
366 service_->profile()));
368 uninstall_dialog_.reset(extensions::ExtensionUninstallDialog::Create(
369 service_->profile(), browser->window()->GetNativeWindow(), this));
370 // Delay showing the uninstall dialog, so that this function returns
371 // immediately, to close the bubble properly. See crbug.com/121544.
372 base::MessageLoop::current()->PostTask(
373 FROM_HERE,
374 base::Bind(&extensions::ExtensionUninstallDialog::ConfirmUninstall,
375 uninstall_dialog_->AsWeakPtr(),
376 extension_));
379 bool ExtensionDisabledGlobalError::ShouldCloseOnDeactivate() const {
380 // Since this indicates that an extension was disabled, we should definitely
381 // have the user acknowledge it, rather than having the bubble disappear when
382 // a new window pops up.
383 return false;
386 void ExtensionDisabledGlobalError::ExtensionUninstallAccepted() {
387 service_->UninstallExtension(extension_->id(),
388 extensions::UNINSTALL_REASON_EXTENSION_DISABLED,
389 base::Bind(&base::DoNothing),
390 NULL);
393 void ExtensionDisabledGlobalError::ExtensionUninstallCanceled() {
394 // Nothing happens, and the error is still there.
397 void ExtensionDisabledGlobalError::Observe(
398 int type,
399 const content::NotificationSource& source,
400 const content::NotificationDetails& details) {
401 // The error is invalidated if the extension has been loaded or removed.
402 DCHECK(type == extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED ||
403 type == extensions::NOTIFICATION_EXTENSION_REMOVED);
404 const Extension* extension = content::Details<const Extension>(details).ptr();
405 if (extension != extension_)
406 return;
407 GlobalErrorServiceFactory::GetForProfile(service_->profile())->
408 RemoveGlobalError(this);
410 if (type == extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED)
411 user_response_ = REENABLE;
412 else if (type == extensions::NOTIFICATION_EXTENSION_REMOVED)
413 user_response_ = UNINSTALL;
414 delete this;
417 // Globals --------------------------------------------------------------------
419 namespace extensions {
421 void AddExtensionDisabledErrorWithIcon(base::WeakPtr<ExtensionService> service,
422 const std::string& extension_id,
423 bool is_remote_install,
424 const gfx::Image& icon) {
425 if (!service.get())
426 return;
427 const Extension* extension = service->GetInstalledExtension(extension_id);
428 if (extension) {
429 GlobalErrorServiceFactory::GetForProfile(service->profile())
430 ->AddGlobalError(new ExtensionDisabledGlobalError(
431 service.get(), extension, is_remote_install, icon));
435 void AddExtensionDisabledError(ExtensionService* service,
436 const Extension* extension,
437 bool is_remote_install) {
438 // Do not display notifications for ephemeral apps that have been disabled.
439 // Instead, a prompt will be shown the next time the app is launched.
440 if (util::IsEphemeralApp(extension->id(), service->profile()))
441 return;
443 extensions::ExtensionResource image = extensions::IconsInfo::GetIconResource(
444 extension, kIconSize, ExtensionIconSet::MATCH_BIGGER);
445 gfx::Size size(kIconSize, kIconSize);
446 ImageLoader::Get(service->profile())
447 ->LoadImageAsync(extension,
448 image,
449 size,
450 base::Bind(&AddExtensionDisabledErrorWithIcon,
451 service->AsWeakPtr(),
452 extension->id(),
453 is_remote_install));
456 void ShowExtensionDisabledDialog(ExtensionService* service,
457 content::WebContents* web_contents,
458 const Extension* extension) {
459 scoped_ptr<ExtensionInstallPrompt> install_ui(
460 new ExtensionInstallPrompt(web_contents));
461 // This object manages its own lifetime.
462 new ExtensionDisabledDialogDelegate(service, install_ui.Pass(), extension);
465 } // namespace extensions