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 string16
MenuItemLabel() OVERRIDE
;
32 virtual void ExecuteMenuItem(Browser
* browser
) OVERRIDE
;
34 virtual bool HasBubbleView() OVERRIDE
;
35 virtual string16
GetBubbleViewTitle() OVERRIDE
;
36 virtual std::vector
<string16
> GetBubbleViewMessages() OVERRIDE
;
37 virtual string16
GetBubbleViewAcceptButtonLabel() OVERRIDE
;
38 virtual string16
GetBubbleViewCancelButtonLabel() OVERRIDE
;
39 virtual void OnBubbleViewDidClose(Browser
* browser
) OVERRIDE
;
40 virtual void BubbleViewAcceptButtonPressed(Browser
* browser
) OVERRIDE
;
41 virtual void BubbleViewCancelButtonPressed(Browser
* browser
) OVERRIDE
;
43 static int GetMenuItemCommandID();
46 ExtensionWarningBadgeService
* badge_service_
;
48 DISALLOW_COPY_AND_ASSIGN(ErrorBadge
);
51 ErrorBadge::ErrorBadge(ExtensionWarningBadgeService
* badge_service
)
52 : badge_service_(badge_service
) {}
54 ErrorBadge::~ErrorBadge() {}
56 bool ErrorBadge::HasMenuItem() {
60 int ErrorBadge::MenuItemCommandID() {
61 return GetMenuItemCommandID();
64 string16
ErrorBadge::MenuItemLabel() {
65 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM
);
68 void ErrorBadge::ExecuteMenuItem(Browser
* browser
) {
69 // Suppress all current warnings in the extension service from triggering
70 // a badge on the wrench menu in the future of this session.
71 badge_service_
->SuppressCurrentWarnings();
73 chrome::ExecuteCommand(browser
, IDC_MANAGE_EXTENSIONS
);
76 bool ErrorBadge::HasBubbleView() {
80 string16
ErrorBadge::GetBubbleViewTitle() {
84 std::vector
<string16
> ErrorBadge::GetBubbleViewMessages() {
85 return std::vector
<string16
>();
88 string16
ErrorBadge::GetBubbleViewAcceptButtonLabel() {
92 string16
ErrorBadge::GetBubbleViewCancelButtonLabel() {
96 void ErrorBadge::OnBubbleViewDidClose(Browser
* browser
) {
99 void ErrorBadge::BubbleViewAcceptButtonPressed(Browser
* browser
) {
103 void ErrorBadge::BubbleViewCancelButtonPressed(Browser
* browser
) {
108 int ErrorBadge::GetMenuItemCommandID() {
109 return IDC_EXTENSION_ERRORS
;
115 ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile
* profile
)
116 : profile_(profile
) {
117 DCHECK(CalledOnValidThread());
120 ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {}
122 void ExtensionWarningBadgeService::SuppressCurrentWarnings() {
123 DCHECK(CalledOnValidThread());
124 size_t old_size
= suppressed_warnings_
.size();
126 const ExtensionWarningSet
& warnings
= GetCurrentWarnings();
127 suppressed_warnings_
.insert(warnings
.begin(), warnings
.end());
129 if (old_size
!= suppressed_warnings_
.size())
133 const ExtensionWarningSet
&
134 ExtensionWarningBadgeService::GetCurrentWarnings() const {
135 return ExtensionSystem::Get(profile_
)->warning_service()->warnings();
138 void ExtensionWarningBadgeService::ExtensionWarningsChanged() {
139 DCHECK(CalledOnValidThread());
143 void ExtensionWarningBadgeService::UpdateBadgeStatus() {
144 const std::set
<ExtensionWarning
>& warnings
= GetCurrentWarnings();
145 bool non_suppressed_warnings_exist
= false;
146 for (std::set
<ExtensionWarning
>::const_iterator i
= warnings
.begin();
147 i
!= warnings
.end(); ++i
) {
148 if (!ContainsKey(suppressed_warnings_
, *i
)) {
149 non_suppressed_warnings_exist
= true;
153 ShowBadge(non_suppressed_warnings_exist
);
156 void ExtensionWarningBadgeService::ShowBadge(bool show
) {
157 GlobalErrorService
* service
=
158 GlobalErrorServiceFactory::GetForProfile(profile_
);
159 GlobalError
* error
= service
->GetGlobalErrorByMenuItemCommandID(
160 ErrorBadge::GetMenuItemCommandID());
162 // Activate or hide the warning badge in case the current state is incorrect.
163 if (error
&& !show
) {
164 service
->RemoveGlobalError(error
);
166 } else if (!error
&& show
) {
167 service
->AddGlobalError(new ErrorBadge(this));