File: app_menu_icon_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (139 lines) | stat: -rw-r--r-- 5,272 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/toolbar/app_menu_icon_controller.h"

#include "base/check_op.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/upgrade_detector/upgrade_detector.h"
#include "chrome/common/channel_info.h"
#include "components/version_info/channel.h"
#include "ui/gfx/paint_vector_icon.h"

namespace {

#if !BUILDFLAG(IS_CHROMEOS)
// Maps an upgrade level to a severity level. When |show_very_low_upgrade_level|
// is true, VERY_LOW through HIGH all return Severity::LOW. Otherwise, VERY_LOW
// is ignored and LOW through HIGH return their respective Severity level, with
// GRACE treated the same as HIGH.
AppMenuIconController::Severity SeverityFromUpgradeLevel(
    bool show_very_low_upgrade_level,
    UpgradeDetector::UpgradeNotificationAnnoyanceLevel level) {
  if (show_very_low_upgrade_level) {
    // Anything between kNone and kCritical is LOW for unstable desktop Chrome.
    switch (level) {
      case UpgradeDetector::UPGRADE_ANNOYANCE_NONE:
        break;
      case UpgradeDetector::UPGRADE_ANNOYANCE_VERY_LOW:
      case UpgradeDetector::UPGRADE_ANNOYANCE_LOW:
      case UpgradeDetector::UPGRADE_ANNOYANCE_ELEVATED:
      case UpgradeDetector::UPGRADE_ANNOYANCE_GRACE:
      case UpgradeDetector::UPGRADE_ANNOYANCE_HIGH:
        return AppMenuIconController::Severity::LOW;
      case UpgradeDetector::UPGRADE_ANNOYANCE_CRITICAL:
        return AppMenuIconController::Severity::HIGH;
    }
  } else {
    switch (level) {
      case UpgradeDetector::UPGRADE_ANNOYANCE_NONE:
        break;
      case UpgradeDetector::UPGRADE_ANNOYANCE_VERY_LOW:
        // kVeryLow is meaningless for stable channels.
        return AppMenuIconController::Severity::NONE;
      case UpgradeDetector::UPGRADE_ANNOYANCE_LOW:
        return AppMenuIconController::Severity::LOW;
      case UpgradeDetector::UPGRADE_ANNOYANCE_ELEVATED:
        return AppMenuIconController::Severity::MEDIUM;
      case UpgradeDetector::UPGRADE_ANNOYANCE_GRACE:
      case UpgradeDetector::UPGRADE_ANNOYANCE_HIGH:
      case UpgradeDetector::UPGRADE_ANNOYANCE_CRITICAL:
        return AppMenuIconController::Severity::HIGH;
    }
  }
  DCHECK_EQ(level, UpgradeDetector::UPGRADE_ANNOYANCE_NONE);

  return AppMenuIconController::Severity::NONE;
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// Return true if the browser is updating on the dev or canary channels.
bool IsUnstableChannel() {
  // Unbranded (Chromium) builds are on the UNKNOWN channel, so check explicitly
  // for the Google Chrome channels that are considered "unstable". This ensures
  // that Chromium builds get the default behavior.
  const version_info::Channel channel = chrome::GetChannel();
  return channel == version_info::Channel::DEV ||
         channel == version_info::Channel::CANARY;
}

}  // namespace

AppMenuIconController::AppMenuIconController(Profile* profile,
                                             Delegate* delegate)
    : AppMenuIconController(UpgradeDetector::GetInstance(), profile, delegate) {
}

AppMenuIconController::AppMenuIconController(UpgradeDetector* upgrade_detector,
                                             Profile* profile,
                                             Delegate* delegate)
    : is_unstable_channel_(IsUnstableChannel()),
      upgrade_detector_(upgrade_detector),
      profile_(profile),
      delegate_(delegate) {
  DCHECK(profile_);
  DCHECK(delegate_);

  global_error_observation_.Observe(
      GlobalErrorServiceFactory::GetForProfile(profile_));

  upgrade_detector_->AddObserver(this);
}

AppMenuIconController::~AppMenuIconController() {
  upgrade_detector_->RemoveObserver(this);
}

void AppMenuIconController::UpdateDelegate() {
  delegate_->UpdateTypeAndSeverity(GetTypeAndSeverity());
}

AppMenuIconController::TypeAndSeverity
AppMenuIconController::GetTypeAndSeverity() const {
#if !BUILDFLAG(IS_CHROMEOS)
  if (browser_defaults::kShowUpgradeMenuItem &&
      upgrade_detector_->notify_upgrade()) {
    UpgradeDetector::UpgradeNotificationAnnoyanceLevel level =
        upgrade_detector_->upgrade_notification_stage();
    // The severity may be NONE even if the detector has been notified of an
    // update. This can happen for beta and stable channels once the VERY_LOW
    // annoyance level is reached.
    auto severity = SeverityFromUpgradeLevel(is_unstable_channel_, level);
    if (severity != Severity::NONE) {
      return {IconType::UPGRADE_NOTIFICATION, severity};
    }
  }

  if (GlobalErrorServiceFactory::GetForProfile(profile_)
          ->GetHighestSeverityGlobalErrorWithAppMenuItem()) {
    // If you change the severity here, make sure to also change the menu icon
    // and the bubble icon.
    return {IconType::GLOBAL_ERROR, Severity::MEDIUM};
  }
#endif

  return {IconType::NONE, Severity::NONE};
}

void AppMenuIconController::OnGlobalErrorsChanged() {
  UpdateDelegate();
}

void AppMenuIconController::OnUpgradeRecommended() {
  UpdateDelegate();
}