NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_warning_badge_service.cc
blob557d23ee5ab69df6a19640ace9ec68a8a004d2e7
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_warning_badge_service.h"
7 #include "base/stl_util.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/global_error/global_error.h"
12 #include "chrome/browser/ui/global_error/global_error_service.h"
13 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
14 #include "extensions/browser/extension_system.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 namespace extensions {
20 namespace {
21 // Non-modal GlobalError implementation that warns the user if extensions
22 // created warnings or errors. If the user clicks on the wrench menu, the user
23 // is redirected to chrome://extensions to inspect the errors.
24 class ErrorBadge : public GlobalError {
25 public:
26 explicit ErrorBadge(ExtensionWarningBadgeService* badge_service);
27 virtual ~ErrorBadge();
29 // Implementation for GlobalError:
30 virtual bool HasMenuItem() OVERRIDE;
31 virtual int MenuItemCommandID() OVERRIDE;
32 virtual base::string16 MenuItemLabel() OVERRIDE;
33 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
35 virtual bool HasBubbleView() OVERRIDE;
36 virtual bool HasShownBubbleView() OVERRIDE;
37 virtual void ShowBubbleView(Browser* browser) OVERRIDE;
38 virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE;
40 static int GetMenuItemCommandID();
42 private:
43 ExtensionWarningBadgeService* badge_service_;
45 DISALLOW_COPY_AND_ASSIGN(ErrorBadge);
48 ErrorBadge::ErrorBadge(ExtensionWarningBadgeService* badge_service)
49 : badge_service_(badge_service) {}
51 ErrorBadge::~ErrorBadge() {}
53 bool ErrorBadge::HasMenuItem() {
54 return true;
57 int ErrorBadge::MenuItemCommandID() {
58 return GetMenuItemCommandID();
61 base::string16 ErrorBadge::MenuItemLabel() {
62 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM);
65 void ErrorBadge::ExecuteMenuItem(Browser* browser) {
66 // Suppress all current warnings in the extension service from triggering
67 // a badge on the wrench menu in the future of this session.
68 badge_service_->SuppressCurrentWarnings();
70 chrome::ExecuteCommand(browser, IDC_MANAGE_EXTENSIONS);
73 bool ErrorBadge::HasBubbleView() { return false; }
75 bool ErrorBadge::HasShownBubbleView() { return false; }
77 void ErrorBadge::ShowBubbleView(Browser* browser) { NOTREACHED(); }
79 GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() {
80 return NULL;
83 // static
84 int ErrorBadge::GetMenuItemCommandID() {
85 return IDC_EXTENSION_ERRORS;
88 } // namespace
91 ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile* profile)
92 : profile_(profile) {
93 DCHECK(CalledOnValidThread());
96 ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {}
98 void ExtensionWarningBadgeService::SuppressCurrentWarnings() {
99 DCHECK(CalledOnValidThread());
100 size_t old_size = suppressed_warnings_.size();
102 const ExtensionWarningSet& warnings = GetCurrentWarnings();
103 suppressed_warnings_.insert(warnings.begin(), warnings.end());
105 if (old_size != suppressed_warnings_.size())
106 UpdateBadgeStatus();
109 const ExtensionWarningSet&
110 ExtensionWarningBadgeService::GetCurrentWarnings() const {
111 return ExtensionSystem::Get(profile_)->warning_service()->warnings();
114 void ExtensionWarningBadgeService::ExtensionWarningsChanged() {
115 DCHECK(CalledOnValidThread());
116 UpdateBadgeStatus();
119 void ExtensionWarningBadgeService::UpdateBadgeStatus() {
120 const std::set<ExtensionWarning>& warnings = GetCurrentWarnings();
121 bool non_suppressed_warnings_exist = false;
122 for (std::set<ExtensionWarning>::const_iterator i = warnings.begin();
123 i != warnings.end(); ++i) {
124 if (!ContainsKey(suppressed_warnings_, *i)) {
125 non_suppressed_warnings_exist = true;
126 break;
129 ShowBadge(non_suppressed_warnings_exist);
132 void ExtensionWarningBadgeService::ShowBadge(bool show) {
133 GlobalErrorService* service =
134 GlobalErrorServiceFactory::GetForProfile(profile_);
135 GlobalError* error = service->GetGlobalErrorByMenuItemCommandID(
136 ErrorBadge::GetMenuItemCommandID());
138 // Activate or hide the warning badge in case the current state is incorrect.
139 if (error && !show) {
140 service->RemoveGlobalError(error);
141 delete error;
142 } else if (!error && show) {
143 service->AddGlobalError(new ErrorBadge(this));
147 } // namespace extensions