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/warning_badge_service.h"
7 #include "base/stl_util.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/extensions/warning_badge_service_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser_commands.h"
12 #include "chrome/browser/ui/global_error/global_error.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 namespace extensions
{
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
{
26 explicit ErrorBadge(WarningBadgeService
* badge_service
);
27 ~ErrorBadge() override
;
29 // Implementation for GlobalError:
30 bool HasMenuItem() override
;
31 int MenuItemCommandID() override
;
32 base::string16
MenuItemLabel() override
;
33 void ExecuteMenuItem(Browser
* browser
) override
;
35 bool HasBubbleView() override
;
36 bool HasShownBubbleView() override
;
37 void ShowBubbleView(Browser
* browser
) override
;
38 GlobalErrorBubbleViewBase
* GetBubbleView() override
;
40 static int GetMenuItemCommandID();
43 WarningBadgeService
* badge_service_
;
45 DISALLOW_COPY_AND_ASSIGN(ErrorBadge
);
48 ErrorBadge::ErrorBadge(WarningBadgeService
* badge_service
)
49 : badge_service_(badge_service
) {
52 ErrorBadge::~ErrorBadge() {
55 bool ErrorBadge::HasMenuItem() {
59 int ErrorBadge::MenuItemCommandID() {
60 return GetMenuItemCommandID();
63 base::string16
ErrorBadge::MenuItemLabel() {
64 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM
);
67 void ErrorBadge::ExecuteMenuItem(Browser
* browser
) {
68 // Suppress all current warnings in the extension service from triggering
69 // a badge on the wrench menu in the future of this session.
70 badge_service_
->SuppressCurrentWarnings();
72 chrome::ExecuteCommand(browser
, IDC_MANAGE_EXTENSIONS
);
75 bool ErrorBadge::HasBubbleView() {
79 bool ErrorBadge::HasShownBubbleView() {
83 void ErrorBadge::ShowBubbleView(Browser
* browser
) {
87 GlobalErrorBubbleViewBase
* ErrorBadge::GetBubbleView() {
92 int ErrorBadge::GetMenuItemCommandID() {
93 return IDC_EXTENSION_ERRORS
;
98 WarningBadgeService::WarningBadgeService(Profile
* profile
)
99 : profile_(profile
), warning_service_observer_(this) {
100 DCHECK(CalledOnValidThread());
101 warning_service_observer_
.Add(WarningService::Get(profile_
));
104 WarningBadgeService::~WarningBadgeService() {
108 WarningBadgeService
* WarningBadgeService::Get(
109 content::BrowserContext
* context
) {
110 return WarningBadgeServiceFactory::GetForBrowserContext(context
);
113 void WarningBadgeService::SuppressCurrentWarnings() {
114 DCHECK(CalledOnValidThread());
115 size_t old_size
= suppressed_warnings_
.size();
117 const WarningSet
& warnings
= GetCurrentWarnings();
118 suppressed_warnings_
.insert(warnings
.begin(), warnings
.end());
120 if (old_size
!= suppressed_warnings_
.size())
124 const WarningSet
& WarningBadgeService::GetCurrentWarnings() const {
125 return WarningService::Get(profile_
)->warnings();
128 void WarningBadgeService::ExtensionWarningsChanged(
129 const ExtensionIdSet
& affected_extensions
) {
130 DCHECK(CalledOnValidThread());
134 void WarningBadgeService::UpdateBadgeStatus() {
135 const std::set
<Warning
>& warnings
= GetCurrentWarnings();
136 bool non_suppressed_warnings_exist
= false;
137 for (std::set
<Warning
>::const_iterator i
= warnings
.begin();
138 i
!= warnings
.end(); ++i
) {
139 if (!ContainsKey(suppressed_warnings_
, *i
)) {
140 non_suppressed_warnings_exist
= true;
144 ShowBadge(non_suppressed_warnings_exist
);
147 void WarningBadgeService::ShowBadge(bool show
) {
148 GlobalErrorService
* service
=
149 GlobalErrorServiceFactory::GetForProfile(profile_
);
150 GlobalError
* error
= service
->GetGlobalErrorByMenuItemCommandID(
151 ErrorBadge::GetMenuItemCommandID());
153 // Activate or hide the warning badge in case the current state is incorrect.
154 if (error
&& !show
) {
155 service
->RemoveGlobalError(error
);
157 } else if (!error
&& show
) {
158 service
->AddGlobalError(new ErrorBadge(this));
162 } // namespace extensions