File: avatar_toolbar_button_delegate.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 5,681 bytes parent folder | download
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_TOOLBAR_BUTTON_DELEGATE_H_
#define CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_TOOLBAR_BUTTON_DELEGATE_H_

#include <optional>
#include <string>

#include "base/auto_reset.h"
#include "base/callback_list.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "google_apis/gaia/gaia_id.h"
#include "ui/base/models/image_model.h"

class Browser;
class Profile;
class AvatarToolbarButton;
enum class AvatarDelayType;

namespace ui {
class ColorProvider;
}

// Internal structures.
namespace internal {
class StateManager;
enum class ButtonState;
}  // namespace internal

// Handles the business logic for AvatarToolbarButton.
// Listens to Chrome and Profile changes in order to compute the proper state of
// the button. This state is used to compute the information requested by
// the button to be shown, such as Text and color, Icon, tooltip text etc...
// The different states that can be reached:
// - Regular state: regular browsing session.
// - Private mode: Incognito or Guest browser sessions.
// - Identity name shown: the identity name is shown for a short period of time.
//   This can be triggered by identity changes in Chrome or when an IPH is
//   showing.
// - Explicit modifications override: such as displaying specific text when
//   intercept bubbles are displayed.
// - Sync paused/error state.
class AvatarToolbarButtonDelegate : public signin::IdentityManager::Observer {
 public:
  AvatarToolbarButtonDelegate(AvatarToolbarButton* button, Browser* browser);

  AvatarToolbarButtonDelegate(const AvatarToolbarButtonDelegate&) = delete;
  AvatarToolbarButtonDelegate& operator=(const AvatarToolbarButtonDelegate&) =
      delete;

  ~AvatarToolbarButtonDelegate() override;

  // Expected to be called once the avatar button view is properly added to the
  // widget. Expected to be called once to initialize the StateManager. Using
  // `state_manager_` can only be done after calling this method.
  void InitializeStateManager();
  bool IsStateManagerInitialized() const;

  // These info are based on the `ButtonState`.
  std::pair<std::u16string, std::optional<SkColor>> GetTextAndColor(
      const ui::ColorProvider* color_provider) const;
  SkColor GetHighlightTextColor(const ui::ColorProvider* color_provider) const;
  std::optional<std::u16string> GetAccessibilityLabel() const;
  std::u16string GetAvatarTooltipText() const;
  std::pair<ChromeColorIds, ChromeColorIds> GetInkdropColors() const;
  ui::ImageModel GetAvatarIcon(int icon_size,
                               SkColor icon_color,
                               const ui::ColorProvider* color_provider) const;
  bool ShouldPaintBorder() const;
  bool ShouldBlendHighlightColor() const;
  std::optional<base::RepeatingClosure> GetButtonAction();

  [[nodiscard]] base::ScopedClosureRunner SetExplicitButtonState(
      const std::u16string& text,
      std::optional<std::u16string> accessibility_label,
      std::optional<base::RepeatingClosure> explicit_action);

  // Called by the AvatarToolbarButton to notify the delegate about events.
  void OnThemeChanged(const ui::ColorProvider* color_provider);

  // Testing functions: check `AvatarToolbarButton` equivalent functions.
  [[nodiscard]] static base::AutoReset<std::optional<base::TimeDelta>>
  CreateScopedInfiniteDelayOverrideForTesting(AvatarDelayType delay_type);
  void TriggerTimeoutForTesting(AvatarDelayType delay_type);
  [[nodiscard]] static base::AutoReset<std::optional<base::TimeDelta>>
  CreateScopedZeroDelayOverrideSigninPendingTextForTesting();

 private:
  std::u16string GetProfileName() const;
  std::u16string GetShortProfileName() const;
  // Must only be called in states which have an avatar image (i.e. not
  // kGuestSession and not kIncognitoProfile).
  gfx::Image GetProfileAvatarImage(int preferred_size) const;
  // Returns the count of incognito or guest windows attached to the profile.
  int GetWindowCount() const;
  gfx::Image GetGaiaAccountImage() const;

  // signin::IdentityManager::Observer:
  void OnPrimaryAccountChanged(
      const signin::PrimaryAccountChangeEvent& event_details) override;
  void OnExtendedAccountInfoUpdated(const AccountInfo& info) override;
  void OnErrorStateOfRefreshTokenUpdatedForAccount(
      const CoreAccountInfo& account_info,
      const GoogleServiceAuthError& error,
      signin_metrics::SourceForRefreshTokenOperation token_operation_source)
      override;

  const raw_ptr<AvatarToolbarButton> avatar_toolbar_button_;
  const raw_ptr<Browser> browser_;
  const raw_ptr<Profile> profile_;
  const raw_ptr<signin::IdentityManager> identity_manager_;

  // Gaia Id of the account that was signed in from having it's choice
  // remembered following a web sign-in event but waiting for the available
  // account information to be fetched in order to show the sign in IPH.
  GaiaId gaia_id_for_signin_choice_remembered_;

  // Initialized in `InitializeStates()`.
  std::unique_ptr<internal::StateManager> state_manager_;

  base::ScopedObservation<signin::IdentityManager,
                          signin::IdentityManager::Observer>
      identity_manager_observation_{this};

  base::WeakPtrFactory<AvatarToolbarButtonDelegate> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_TOOLBAR_BUTTON_DELEGATE_H_