Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / wrench_menu_badge_controller.cc
blobd3838b1584bd8c8570fea23a08b6b04d36bf1c96
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"
13 #if defined(OS_WIN)
14 #include "base/win/windows_version.h"
15 #include "chrome/browser/enumerate_modules_model_win.h"
16 #endif
18 namespace {
20 // Maps an upgrade level to a severity level.
21 WrenchIconPainter::Severity SeverityFromUpgradeLevel(
22 UpgradeDetector::UpgradeNotificationAnnoyanceLevel level) {
23 switch (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;
37 NOTREACHED();
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.
59 return false;
60 #else
61 return UpgradeDetector::GetInstance()->notify_upgrade();
62 #endif
65 // Returns true if we should show the warning for incompatible software.
66 bool ShouldShowIncompatibilityWarning() {
67 #if defined(OS_WIN)
68 EnumerateModulesModel* loaded_modules = EnumerateModulesModel::GetInstance();
69 loaded_modules->MaybePostScanningTask();
70 return loaded_modules->ShouldShowConflictWarning();
71 #else
72 return false;
73 #endif
76 } // namespace
78 WrenchMenuBadgeController::WrenchMenuBadgeController(Profile* profile,
79 Delegate* delegate)
80 : profile_(profile),
81 delegate_(delegate),
82 overflowed_toolbar_action_wants_to_run_(false) {
83 DCHECK(profile_);
84 DCHECK(delegate_);
86 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
87 content::NotificationService::AllSources());
88 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
89 content::Source<Profile>(profile_));
91 #if defined(OS_WIN)
92 if (base::win::GetVersion() == base::win::VERSION_XP) {
93 registrar_.Add(this, chrome::NOTIFICATION_MODULE_LIST_ENUMERATED,
94 content::NotificationService::AllSources());
96 registrar_.Add(this, chrome::NOTIFICATION_MODULE_INCOMPATIBILITY_BADGE_CHANGE,
97 content::NotificationService::AllSources());
98 #endif
101 WrenchMenuBadgeController::~WrenchMenuBadgeController() {
104 void WrenchMenuBadgeController::UpdateDelegate() {
105 if (ShouldShowUpgradeRecommended()) {
106 UpgradeDetector::UpgradeNotificationAnnoyanceLevel level =
107 UpgradeDetector::GetInstance()->upgrade_notification_stage();
108 delegate_->UpdateBadgeSeverity(BADGE_TYPE_UPGRADE_NOTIFICATION,
109 SeverityFromUpgradeLevel(level),
110 ShouldAnimateUpgradeLevel(level));
111 return;
114 if (ShouldShowIncompatibilityWarning()) {
115 delegate_->UpdateBadgeSeverity(BADGE_TYPE_INCOMPATIBILITY_WARNING,
116 WrenchIconPainter::SEVERITY_MEDIUM, true);
117 return;
120 if (GlobalErrorServiceFactory::GetForProfile(profile_)->
121 GetHighestSeverityGlobalErrorWithWrenchMenuItem()) {
122 // If you change the severity here, make sure to also change the menu icon
123 // and the bubble icon.
124 delegate_->UpdateBadgeSeverity(BADGE_TYPE_GLOBAL_ERROR,
125 WrenchIconPainter::SEVERITY_MEDIUM, true);
126 return;
129 if (overflowed_toolbar_action_wants_to_run_) {
130 delegate_->UpdateBadgeSeverity(BADGE_TYPE_NONE,
131 WrenchIconPainter::SEVERITY_INFO, true);
132 return;
135 delegate_->UpdateBadgeSeverity(BADGE_TYPE_NONE,
136 WrenchIconPainter::SEVERITY_NONE, true);
139 void WrenchMenuBadgeController::SetOverflowedToolbarActionWantsToRun(
140 bool wants_to_run) {
141 overflowed_toolbar_action_wants_to_run_ = wants_to_run;
142 UpdateDelegate();
145 void WrenchMenuBadgeController::Observe(
146 int type,
147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) {
149 UpdateDelegate();