[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 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. | ||||
4 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 5 | #include "chrome/browser/extensions/warning_badge_service.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 6 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 7 | #include <stddef.h> |
8 | |||||
9 | #include "base/macros.h" | ||||
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 11 | #include "base/stl_util.h" |
12 | #include "chrome/app/chrome_command_ids.h" | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 13 | #include "chrome/browser/extensions/warning_badge_service_factory.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
15 | #include "chrome/browser/ui/browser_commands.h" | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 16 | #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] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 19 | #include "chrome/grit/generated_resources.h" |
gab | 234cd782 | 2017-05-31 22:26:51 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 21 | #include "ui/base/l10n/l10n_util.h" |
22 | |||||
23 | namespace extensions { | ||||
24 | |||||
25 | namespace { | ||||
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. | ||||
29 | class ErrorBadge : public GlobalError { | ||||
30 | public: | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 31 | explicit ErrorBadge(WarningBadgeService* badge_service); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 32 | ~ErrorBadge() override; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 33 | |
34 | // Implementation for GlobalError: | ||||
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 35 | bool HasMenuItem() override; |
36 | int MenuItemCommandID() override; | ||||
37 | base::string16 MenuItemLabel() override; | ||||
38 | void ExecuteMenuItem(Browser* browser) override; | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 39 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 40 | bool HasBubbleView() override; |
41 | bool HasShownBubbleView() override; | ||||
42 | void ShowBubbleView(Browser* browser) override; | ||||
43 | GlobalErrorBubbleViewBase* GetBubbleView() override; | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 44 | |
45 | static int GetMenuItemCommandID(); | ||||
46 | |||||
47 | private: | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 48 | WarningBadgeService* badge_service_; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 49 | |
50 | DISALLOW_COPY_AND_ASSIGN(ErrorBadge); | ||||
51 | }; | ||||
52 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 53 | ErrorBadge::ErrorBadge(WarningBadgeService* badge_service) |
54 | : badge_service_(badge_service) { | ||||
55 | } | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 56 | |
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 57 | ErrorBadge::~ErrorBadge() { |
58 | } | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 59 | |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 60 | bool ErrorBadge::HasMenuItem() { |
61 | return true; | ||||
62 | } | ||||
63 | |||||
64 | int ErrorBadge::MenuItemCommandID() { | ||||
65 | return GetMenuItemCommandID(); | ||||
66 | } | ||||
67 | |||||
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 68 | base::string16 ErrorBadge::MenuItemLabel() { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 69 | return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM); |
70 | } | ||||
71 | |||||
72 | void 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.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 80 | bool ErrorBadge::HasBubbleView() { |
81 | return false; | ||||
82 | } | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 83 | |
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 84 | bool ErrorBadge::HasShownBubbleView() { |
85 | return false; | ||||
86 | } | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 87 | |
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 88 | void ErrorBadge::ShowBubbleView(Browser* browser) { |
89 | NOTREACHED(); | ||||
90 | } | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 91 | |
[email protected] | 4deac0de | 2013-11-01 17:20:06 | [diff] [blame] | 92 | GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() { |
93 | return NULL; | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 94 | } |
95 | |||||
96 | // static | ||||
97 | int ErrorBadge::GetMenuItemCommandID() { | ||||
98 | return IDC_EXTENSION_ERRORS; | ||||
99 | } | ||||
100 | |||||
101 | } // namespace | ||||
102 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 103 | WarningBadgeService::WarningBadgeService(Profile* profile) |
reillyg | 22114a38 | 2014-11-03 16:47:57 | [diff] [blame] | 104 | : profile_(profile), warning_service_observer_(this) { |
gab | 234cd782 | 2017-05-31 22:26:51 | [diff] [blame] | 105 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
reillyg | 22114a38 | 2014-11-03 16:47:57 | [diff] [blame] | 106 | warning_service_observer_.Add(WarningService::Get(profile_)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 107 | } |
108 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 109 | WarningBadgeService::~WarningBadgeService() { |
gab | 234cd782 | 2017-05-31 22:26:51 | [diff] [blame] | 110 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
reillyg | 22114a38 | 2014-11-03 16:47:57 | [diff] [blame] | 111 | } |
112 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 113 | // static |
114 | WarningBadgeService* WarningBadgeService::Get( | ||||
115 | content::BrowserContext* context) { | ||||
116 | return WarningBadgeServiceFactory::GetForBrowserContext(context); | ||||
117 | } | ||||
118 | |||||
119 | void WarningBadgeService::SuppressCurrentWarnings() { | ||||
gab | 234cd782 | 2017-05-31 22:26:51 | [diff] [blame] | 120 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 121 | size_t old_size = suppressed_warnings_.size(); |
122 | |||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 123 | const WarningSet& warnings = GetCurrentWarnings(); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 124 | suppressed_warnings_.insert(warnings.begin(), warnings.end()); |
125 | |||||
126 | if (old_size != suppressed_warnings_.size()) | ||||
127 | UpdateBadgeStatus(); | ||||
128 | } | ||||
129 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 130 | const WarningSet& WarningBadgeService::GetCurrentWarnings() const { |
reillyg | 22114a38 | 2014-11-03 16:47:57 | [diff] [blame] | 131 | return WarningService::Get(profile_)->warnings(); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 132 | } |
133 | |||||
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 134 | void WarningBadgeService::ExtensionWarningsChanged( |
135 | const ExtensionIdSet& affected_extensions) { | ||||
gab | 234cd782 | 2017-05-31 22:26:51 | [diff] [blame] | 136 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 137 | UpdateBadgeStatus(); |
138 | } | ||||
139 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 140 | void WarningBadgeService::UpdateBadgeStatus() { |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 141 | const std::set<Warning>& warnings = GetCurrentWarnings(); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 142 | bool non_suppressed_warnings_exist = false; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 143 | for (std::set<Warning>::const_iterator i = warnings.begin(); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 144 | i != warnings.end(); ++i) { |
skyostil | 32fa6b3f9 | 2016-08-12 13:15:43 | [diff] [blame] | 145 | if (!base::ContainsKey(suppressed_warnings_, *i)) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 146 | non_suppressed_warnings_exist = true; |
147 | break; | ||||
148 | } | ||||
149 | } | ||||
150 | ShowBadge(non_suppressed_warnings_exist); | ||||
151 | } | ||||
152 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 153 | void WarningBadgeService::ShowBadge(bool show) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 154 | 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. | ||||
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 160 | if (error && !show) |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 161 | service->RemoveGlobalError(error); |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 162 | else if (!error && show) |
163 | service->AddGlobalError(base::MakeUnique<ErrorBadge>(this)); | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 164 | } |
165 | |||||
[email protected] | 4deac0de | 2013-11-01 17:20:06 | [diff] [blame] | 166 | } // namespace extensions |