Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / wrench_menu_badge_controller.cc
blobda80866318acd6315fc3a9e973b4fac1a6f5bc53
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 DCHECK(profile_);
83 DCHECK(delegate_);
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_));
90 #if defined(OS_WIN)
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());
97 #endif
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));
110 return;
113 if (ShouldShowIncompatibilityWarning()) {
114 delegate_->UpdateBadgeSeverity(BADGE_TYPE_INCOMPATIBILITY_WARNING,
115 WrenchIconPainter::SEVERITY_MEDIUM, true);
116 return;
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);
125 return;
128 delegate_->UpdateBadgeSeverity(BADGE_TYPE_NONE,
129 WrenchIconPainter::SEVERITY_NONE, true);
132 void WrenchMenuBadgeController::Observe(
133 int type,
134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) {
136 UpdateDelegate();