Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / warning_badge_service.cc
blob78269b6ef1ee8e7189e68ed56db28605b44397fb
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 {
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(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();
42 private:
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() {
56 return true;
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() {
76 return false;
79 bool ErrorBadge::HasShownBubbleView() {
80 return false;
83 void ErrorBadge::ShowBubbleView(Browser* browser) {
84 NOTREACHED();
87 GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() {
88 return NULL;
91 // static
92 int ErrorBadge::GetMenuItemCommandID() {
93 return IDC_EXTENSION_ERRORS;
96 } // namespace
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() {
107 // static
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())
121 UpdateBadgeStatus();
124 const WarningSet& WarningBadgeService::GetCurrentWarnings() const {
125 return WarningService::Get(profile_)->warnings();
128 void WarningBadgeService::ExtensionWarningsChanged(
129 const ExtensionIdSet& affected_extensions) {
130 DCHECK(CalledOnValidThread());
131 UpdateBadgeStatus();
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;
141 break;
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);
156 delete error;
157 } else if (!error && show) {
158 service->AddGlobalError(new ErrorBadge(this));
162 } // namespace extensions