blob: 749aee946df3dff3e346ee8acd9c0b8b626551d8 [file] [log] [blame]
[email protected]b4d3771d2012-11-14 14:44:101// 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.
4
zhuoyu.qian6fcce6a2015-02-25 06:02:095#include "chrome/browser/extensions/warning_badge_service.h"
[email protected]b4d3771d2012-11-14 14:44:106
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
8
9#include "base/macros.h"
avi2451b252016-12-13 16:55:1710#include "base/memory/ptr_util.h"
[email protected]b4d3771d2012-11-14 14:44:1011#include "base/stl_util.h"
12#include "chrome/app/chrome_command_ids.h"
zhuoyu.qian6fcce6a2015-02-25 06:02:0913#include "chrome/browser/extensions/warning_badge_service_factory.h"
[email protected]59b0e602014-01-30 00:41:2414#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/ui/browser_commands.h"
[email protected]b4d3771d2012-11-14 14:44:1016#include "chrome/browser/ui/global_error/global_error.h"
17#include "chrome/browser/ui/global_error/global_error_service.h"
18#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]af39f002014-08-22 10:18:1819#include "chrome/grit/generated_resources.h"
gab234cd7822017-05-31 22:26:5120#include "content/public/browser/browser_thread.h"
[email protected]b4d3771d2012-11-14 14:44:1021#include "ui/base/l10n/l10n_util.h"
22
23namespace extensions {
24
25namespace {
26// Non-modal GlobalError implementation that warns the user if extensions
27// created warnings or errors. If the user clicks on the wrench menu, the user
28// is redirected to chrome://extensions to inspect the errors.
29class ErrorBadge : public GlobalError {
30 public:
zhuoyu.qian6fcce6a2015-02-25 06:02:0931 explicit ErrorBadge(WarningBadgeService* badge_service);
dchengae36a4a2014-10-21 12:36:3632 ~ErrorBadge() override;
[email protected]b4d3771d2012-11-14 14:44:1033
34 // Implementation for GlobalError:
dchengae36a4a2014-10-21 12:36:3635 bool HasMenuItem() override;
36 int MenuItemCommandID() override;
37 base::string16 MenuItemLabel() override;
38 void ExecuteMenuItem(Browser* browser) override;
[email protected]b4d3771d2012-11-14 14:44:1039
dchengae36a4a2014-10-21 12:36:3640 bool HasBubbleView() override;
41 bool HasShownBubbleView() override;
42 void ShowBubbleView(Browser* browser) override;
43 GlobalErrorBubbleViewBase* GetBubbleView() override;
[email protected]b4d3771d2012-11-14 14:44:1044
45 static int GetMenuItemCommandID();
46
47 private:
zhuoyu.qian6fcce6a2015-02-25 06:02:0948 WarningBadgeService* badge_service_;
[email protected]b4d3771d2012-11-14 14:44:1049
50 DISALLOW_COPY_AND_ASSIGN(ErrorBadge);
51};
52
zhuoyu.qian6fcce6a2015-02-25 06:02:0953ErrorBadge::ErrorBadge(WarningBadgeService* badge_service)
54 : badge_service_(badge_service) {
55}
[email protected]b4d3771d2012-11-14 14:44:1056
zhuoyu.qian6fcce6a2015-02-25 06:02:0957ErrorBadge::~ErrorBadge() {
58}
[email protected]b4d3771d2012-11-14 14:44:1059
[email protected]b4d3771d2012-11-14 14:44:1060bool ErrorBadge::HasMenuItem() {
61 return true;
62}
63
64int ErrorBadge::MenuItemCommandID() {
65 return GetMenuItemCommandID();
66}
67
[email protected]6a72a632013-12-12 22:22:0068base::string16 ErrorBadge::MenuItemLabel() {
[email protected]b4d3771d2012-11-14 14:44:1069 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM);
70}
71
72void ErrorBadge::ExecuteMenuItem(Browser* browser) {
73 // Suppress all current warnings in the extension service from triggering
74 // a badge on the wrench menu in the future of this session.
75 badge_service_->SuppressCurrentWarnings();
76
77 chrome::ExecuteCommand(browser, IDC_MANAGE_EXTENSIONS);
78}
79
zhuoyu.qian6fcce6a2015-02-25 06:02:0980bool ErrorBadge::HasBubbleView() {
81 return false;
82}
[email protected]b4d3771d2012-11-14 14:44:1083
zhuoyu.qian6fcce6a2015-02-25 06:02:0984bool ErrorBadge::HasShownBubbleView() {
85 return false;
86}
[email protected]b4d3771d2012-11-14 14:44:1087
zhuoyu.qian6fcce6a2015-02-25 06:02:0988void ErrorBadge::ShowBubbleView(Browser* browser) {
89 NOTREACHED();
90}
[email protected]b4d3771d2012-11-14 14:44:1091
[email protected]4deac0de2013-11-01 17:20:0692GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() {
93 return NULL;
[email protected]b4d3771d2012-11-14 14:44:1094}
95
96// static
97int ErrorBadge::GetMenuItemCommandID() {
98 return IDC_EXTENSION_ERRORS;
99}
100
101} // namespace
102
zhuoyu.qian6fcce6a2015-02-25 06:02:09103WarningBadgeService::WarningBadgeService(Profile* profile)
reillyg22114a382014-11-03 16:47:57104 : profile_(profile), warning_service_observer_(this) {
gab234cd7822017-05-31 22:26:51105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
reillyg22114a382014-11-03 16:47:57106 warning_service_observer_.Add(WarningService::Get(profile_));
[email protected]b4d3771d2012-11-14 14:44:10107}
108
zhuoyu.qian6fcce6a2015-02-25 06:02:09109WarningBadgeService::~WarningBadgeService() {
gab234cd7822017-05-31 22:26:51110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
reillyg22114a382014-11-03 16:47:57111}
112
zhuoyu.qian6fcce6a2015-02-25 06:02:09113// static
114WarningBadgeService* WarningBadgeService::Get(
115 content::BrowserContext* context) {
116 return WarningBadgeServiceFactory::GetForBrowserContext(context);
117}
118
119void WarningBadgeService::SuppressCurrentWarnings() {
gab234cd7822017-05-31 22:26:51120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]b4d3771d2012-11-14 14:44:10121 size_t old_size = suppressed_warnings_.size();
122
hanxic7e55202014-08-28 14:13:21123 const WarningSet& warnings = GetCurrentWarnings();
[email protected]b4d3771d2012-11-14 14:44:10124 suppressed_warnings_.insert(warnings.begin(), warnings.end());
125
126 if (old_size != suppressed_warnings_.size())
127 UpdateBadgeStatus();
128}
129
zhuoyu.qian6fcce6a2015-02-25 06:02:09130const WarningSet& WarningBadgeService::GetCurrentWarnings() const {
reillyg22114a382014-11-03 16:47:57131 return WarningService::Get(profile_)->warnings();
[email protected]b4d3771d2012-11-14 14:44:10132}
133
rdevlin.cronin7dd052902015-05-19 22:23:22134void WarningBadgeService::ExtensionWarningsChanged(
135 const ExtensionIdSet& affected_extensions) {
gab234cd7822017-05-31 22:26:51136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]b4d3771d2012-11-14 14:44:10137 UpdateBadgeStatus();
138}
139
zhuoyu.qian6fcce6a2015-02-25 06:02:09140void WarningBadgeService::UpdateBadgeStatus() {
hanxic7e55202014-08-28 14:13:21141 const std::set<Warning>& warnings = GetCurrentWarnings();
[email protected]b4d3771d2012-11-14 14:44:10142 bool non_suppressed_warnings_exist = false;
hanxic7e55202014-08-28 14:13:21143 for (std::set<Warning>::const_iterator i = warnings.begin();
[email protected]b4d3771d2012-11-14 14:44:10144 i != warnings.end(); ++i) {
skyostil32fa6b3f92016-08-12 13:15:43145 if (!base::ContainsKey(suppressed_warnings_, *i)) {
[email protected]b4d3771d2012-11-14 14:44:10146 non_suppressed_warnings_exist = true;
147 break;
148 }
149 }
150 ShowBadge(non_suppressed_warnings_exist);
151}
152
zhuoyu.qian6fcce6a2015-02-25 06:02:09153void WarningBadgeService::ShowBadge(bool show) {
[email protected]b4d3771d2012-11-14 14:44:10154 GlobalErrorService* service =
155 GlobalErrorServiceFactory::GetForProfile(profile_);
156 GlobalError* error = service->GetGlobalErrorByMenuItemCommandID(
157 ErrorBadge::GetMenuItemCommandID());
158
159 // Activate or hide the warning badge in case the current state is incorrect.
avi2451b252016-12-13 16:55:17160 if (error && !show)
[email protected]b4d3771d2012-11-14 14:44:10161 service->RemoveGlobalError(error);
avi2451b252016-12-13 16:55:17162 else if (!error && show)
163 service->AddGlobalError(base::MakeUnique<ErrorBadge>(this));
[email protected]b4d3771d2012-11-14 14:44:10164}
165
[email protected]4deac0de2013-11-01 17:20:06166} // namespace extensions