1 // Copyright 2014 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/ui/toolbar/wrench_menu_badge_controller.h"
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/ui/global_error/global_error_service.h"
10 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
11 #include "chrome/browser/upgrade_detector.h"
14 #include "base/win/windows_version.h"
15 #include "chrome/browser/enumerate_modules_model_win.h"
20 // Maps an upgrade level to a severity level.
21 WrenchIconPainter::Severity
SeverityFromUpgradeLevel(
22 UpgradeDetector::UpgradeNotificationAnnoyanceLevel level
) {
24 case UpgradeDetector::UPGRADE_ANNOYANCE_NONE
:
25 return WrenchIconPainter::SEVERITY_NONE
;
26 case UpgradeDetector::UPGRADE_ANNOYANCE_LOW
:
27 return WrenchIconPainter::SEVERITY_LOW
;
28 case UpgradeDetector::UPGRADE_ANNOYANCE_ELEVATED
:
29 return WrenchIconPainter::SEVERITY_MEDIUM
;
30 case UpgradeDetector::UPGRADE_ANNOYANCE_HIGH
:
31 return WrenchIconPainter::SEVERITY_HIGH
;
32 case UpgradeDetector::UPGRADE_ANNOYANCE_SEVERE
:
33 return WrenchIconPainter::SEVERITY_HIGH
;
34 case UpgradeDetector::UPGRADE_ANNOYANCE_CRITICAL
:
35 return WrenchIconPainter::SEVERITY_HIGH
;
38 return WrenchIconPainter::SEVERITY_NONE
;
41 // Checks if the wrench icon should be animated for the given upgrade level.
42 bool ShouldAnimateUpgradeLevel(
43 UpgradeDetector::UpgradeNotificationAnnoyanceLevel level
) {
44 bool should_animate
= true;
45 if (level
== UpgradeDetector::UPGRADE_ANNOYANCE_LOW
) {
46 // Only animate low severity upgrades once.
47 static bool should_animate_low_severity
= true;
48 should_animate
= should_animate_low_severity
;
49 should_animate_low_severity
= false;
51 return should_animate
;
54 // Returns true if we should show the upgrade recommended badge.
55 bool ShouldShowUpgradeRecommended() {
56 #if defined(OS_CHROMEOS)
57 // In chromeos, the update recommendation is shown in the system tray. So it
58 // should not be displayed in the wrench menu.
61 return UpgradeDetector::GetInstance()->notify_upgrade();
65 // Returns true if we should show the warning for incompatible software.
66 bool ShouldShowIncompatibilityWarning() {
68 EnumerateModulesModel
* loaded_modules
= EnumerateModulesModel::GetInstance();
69 loaded_modules
->MaybePostScanningTask();
70 return loaded_modules
->ShouldShowConflictWarning();
78 WrenchMenuBadgeController::WrenchMenuBadgeController(Profile
* profile
,
85 registrar_
.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
86 content::NotificationService::AllSources());
87 registrar_
.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED
,
88 content::Source
<Profile
>(profile_
));
91 if (base::win::GetVersion() == base::win::VERSION_XP
) {
92 registrar_
.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED
,
93 content::NotificationService::AllSources());
95 registrar_
.Add(this, chrome::NOTIFICATION_MODULE_INCOMPATIBILITY_BADGE_CHANGE
,
96 content::NotificationService::AllSources());
100 WrenchMenuBadgeController::~WrenchMenuBadgeController() {
103 void WrenchMenuBadgeController::UpdateDelegate() {
104 if (ShouldShowUpgradeRecommended()) {
105 UpgradeDetector::UpgradeNotificationAnnoyanceLevel level
=
106 UpgradeDetector::GetInstance()->upgrade_notification_stage();
107 delegate_
->UpdateBadgeSeverity(BADGE_TYPE_UPGRADE_NOTIFICATION
,
108 SeverityFromUpgradeLevel(level
),
109 ShouldAnimateUpgradeLevel(level
));
113 if (ShouldShowIncompatibilityWarning()) {
114 delegate_
->UpdateBadgeSeverity(BADGE_TYPE_INCOMPATIBILITY_WARNING
,
115 WrenchIconPainter::SEVERITY_MEDIUM
, true);
119 if (GlobalErrorServiceFactory::GetForProfile(profile_
)->
120 GetHighestSeverityGlobalErrorWithWrenchMenuItem()) {
121 // If you change the severity here, make sure to also change the menu icon
122 // and the bubble icon.
123 delegate_
->UpdateBadgeSeverity(BADGE_TYPE_GLOBAL_ERROR
,
124 WrenchIconPainter::SEVERITY_MEDIUM
, true);
128 delegate_
->UpdateBadgeSeverity(BADGE_TYPE_NONE
,
129 WrenchIconPainter::SEVERITY_NONE
, true);
132 void WrenchMenuBadgeController::Observe(
134 const content::NotificationSource
& source
,
135 const content::NotificationDetails
& details
) {