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/extensions/extension_system.h"
10 #include "chrome/browser/ui/global_error/global_error.h"
11 #include "chrome/browser/ui/global_error/global_error_service.h"
12 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
13 #include "chrome/browser/ui/browser_commands.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
17 namespace extensions
{
20 // Non-modal GlobalError implementation that warns the user if extensions
21 // created warnings or errors. If the user clicks on the wrench menu, the user
22 // is redirected to chrome://extensions to inspect the errors.
23 class ErrorBadge
: public GlobalError
{
25 explicit ErrorBadge(ExtensionWarningBadgeService
* badge_service
);
26 virtual ~ErrorBadge();
28 // Implementation for GlobalError:
29 virtual bool HasMenuItem() OVERRIDE
;
30 virtual int MenuItemCommandID() OVERRIDE
;
31 virtual base::string16
MenuItemLabel() OVERRIDE
;
32 virtual void ExecuteMenuItem(Browser
* browser
) OVERRIDE
;
34 virtual bool HasBubbleView() OVERRIDE
;
35 virtual bool HasShownBubbleView() OVERRIDE
;
36 virtual void ShowBubbleView(Browser
* browser
) OVERRIDE
;
37 virtual GlobalErrorBubbleViewBase
* GetBubbleView() OVERRIDE
;
39 static int GetMenuItemCommandID();
42 ExtensionWarningBadgeService
* badge_service_
;
44 DISALLOW_COPY_AND_ASSIGN(ErrorBadge
);
47 ErrorBadge::ErrorBadge(ExtensionWarningBadgeService
* badge_service
)
48 : badge_service_(badge_service
) {}
50 ErrorBadge::~ErrorBadge() {}
52 bool ErrorBadge::HasMenuItem() {
56 int ErrorBadge::MenuItemCommandID() {
57 return GetMenuItemCommandID();
60 base::string16
ErrorBadge::MenuItemLabel() {
61 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM
);
64 void ErrorBadge::ExecuteMenuItem(Browser
* browser
) {
65 // Suppress all current warnings in the extension service from triggering
66 // a badge on the wrench menu in the future of this session.
67 badge_service_
->SuppressCurrentWarnings();
69 chrome::ExecuteCommand(browser
, IDC_MANAGE_EXTENSIONS
);
72 bool ErrorBadge::HasBubbleView() { return false; }
74 bool ErrorBadge::HasShownBubbleView() { return false; }
76 void ErrorBadge::ShowBubbleView(Browser
* browser
) { NOTREACHED(); }
78 GlobalErrorBubbleViewBase
* ErrorBadge::GetBubbleView() {
83 int ErrorBadge::GetMenuItemCommandID() {
84 return IDC_EXTENSION_ERRORS
;
90 ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile
* profile
)
92 DCHECK(CalledOnValidThread());
95 ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {}
97 void ExtensionWarningBadgeService::SuppressCurrentWarnings() {
98 DCHECK(CalledOnValidThread());
99 size_t old_size
= suppressed_warnings_
.size();
101 const ExtensionWarningSet
& warnings
= GetCurrentWarnings();
102 suppressed_warnings_
.insert(warnings
.begin(), warnings
.end());
104 if (old_size
!= suppressed_warnings_
.size())
108 const ExtensionWarningSet
&
109 ExtensionWarningBadgeService::GetCurrentWarnings() const {
110 return ExtensionSystem::Get(profile_
)->warning_service()->warnings();
113 void ExtensionWarningBadgeService::ExtensionWarningsChanged() {
114 DCHECK(CalledOnValidThread());
118 void ExtensionWarningBadgeService::UpdateBadgeStatus() {
119 const std::set
<ExtensionWarning
>& warnings
= GetCurrentWarnings();
120 bool non_suppressed_warnings_exist
= false;
121 for (std::set
<ExtensionWarning
>::const_iterator i
= warnings
.begin();
122 i
!= warnings
.end(); ++i
) {
123 if (!ContainsKey(suppressed_warnings_
, *i
)) {
124 non_suppressed_warnings_exist
= true;
128 ShowBadge(non_suppressed_warnings_exist
);
131 void ExtensionWarningBadgeService::ShowBadge(bool show
) {
132 GlobalErrorService
* service
=
133 GlobalErrorServiceFactory::GetForProfile(profile_
);
134 GlobalError
* error
= service
->GetGlobalErrorByMenuItemCommandID(
135 ErrorBadge::GetMenuItemCommandID());
137 // Activate or hide the warning badge in case the current state is incorrect.
138 if (error
&& !show
) {
139 service
->RemoveGlobalError(error
);
141 } else if (!error
&& show
) {
142 service
->AddGlobalError(new ErrorBadge(this));
146 } // namespace extensions