File: glic_border_view.h

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 (197 lines) | stat: -rw-r--r-- 6,332 bytes parent folder | download | duplicates (3)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2024 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_GLIC_BROWSER_UI_GLIC_BORDER_VIEW_H_
#define CHROME_BROWSER_GLIC_BROWSER_UI_GLIC_BORDER_VIEW_H_

#include "base/scoped_observation.h"
#include "cc/paint/paint_shader.h"
#include "content/public/browser/gpu_data_manager_observer.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/compositor/compositor_animation_observer.h"
#include "ui/compositor/compositor_observer.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"

class Browser;
class ThemeService;

namespace gfx {
class Canvas;
}  // namespace gfx

namespace content {
class GpuDataManager;
}  // namespace content

namespace glic {

class GlicKeyedService;

class GlicBorderView : public views::View,
                       public ui::CompositorAnimationObserver,
                       public ui::CompositorObserver,
                       public content::GpuDataManagerObserver {
  METADATA_HEADER(GlicBorderView, views::View)

 public:
  // Note: We should avoid add test-only code in production as it is an
  // anti-pattern. There is a planned effort to remove these codes and migrate
  // to unittests + pixel tests. See https://crbug.com/412335211
  class Tester;
  // Allows the test to inject the tester at the border's creation.
  class Factory {
   public:
    static std::unique_ptr<GlicBorderView> Create(Browser* browser);
    static void set_factory(Factory* factory) { factory_ = factory; }

   protected:
    Factory() = default;
    virtual ~Factory() = default;

    // For tests to override.
    virtual std::unique_ptr<GlicBorderView> CreateBorderView(
        Browser* browser) = 0;

   private:
    static Factory* factory_;
  };

  GlicBorderView(const GlicBorderView&) = delete;
  GlicBorderView& operator=(const GlicBorderView&) = delete;
  ~GlicBorderView() override;

  // `views::View`:
  void OnPaint(gfx::Canvas* canvas) override;

  // `ui::CompositorAnimationObserver`:
  void OnAnimationStep(base::TimeTicks timestamp) override;

  // `ui::CompositorObserver`:
  void OnCompositingShuttingDown(ui::Compositor* compositor) override;

  // `content::GpuDataManagerObserver`:
  void OnGpuInfoUpdate() override;

  bool IsShowing() const;

  // TODO(crbug.com/384712084): Ideally we shouldn't expose these internals for
  // testing. The pixel comparison tests were flaky thus reverted. Remove these
  // once we set up the Skia Gold tests.
  float opacity_for_testing() const { return opacity_; }
  float emphasis_for_testing() const { return emphasis_; }
  float progress_for_testing() const { return progress_; }
  float GetEffectTimeForTesting() const;

  // Allows tests to alternate some animation APIs, for the deterministic
  // testing.
  class Tester {
   public:
    virtual ~Tester() = default;
    virtual base::TimeTicks GetTestTimestamp() = 0;
    virtual base::TimeTicks GetTestCreationTime() = 0;
    virtual void AnimationStarted() = 0;
    virtual void EmphasisRestarted() = 0;
    virtual void RampDownStarted() = 0;
  };
  Tester* tester() const { return tester_.get(); }

 protected:
  friend class Factory;
  explicit GlicBorderView(Browser* browser, std::unique_ptr<Tester> tester);

 private:
  void Show();
  void StopShowing();

  // A value from 0 to 1 indicating how much the border is to be emphasized.
  float GetEmphasis(base::TimeDelta delta) const;

  // Only valid to call after the animation has started.
  void ResetEmphasisAndReplay();

  // A value from 0 to 1 indicating the opacity of the border.
  float GetOpacity(base::TimeTicks timestamp);

  // Sets the necessary bits to start ramping down the opacity once it's called.
  void StartRampingDown();

  // Returns the effect evolution time; wraps after an hour.
  float GetEffectTime() const;

  // Returns a value from 0 to 1 indicating progress through the effect.
  float GetEffectProgress(base::TimeTicks timestamp) const;

  // Returns the timestamp when the instance was created (but permits being
  // adjusted by the Tester).
  base::TimeTicks GetCreationTime() const;

  bool ForceSimplifiedShader() const;

  GlicKeyedService* GetGlicService() const;

  void UpdateShader();

  // A utility class that subscribe to `GlicKeyedService` for various browser UI
  // status change.
  class BorderViewUpdater;
  const std::unique_ptr<BorderViewUpdater> updater_;

  std::string shader_;

  // When it is true, the class directly presents a static border and when it is
  // false, it animates the border first.
  bool skip_emphasis_animation_ = false;

  float opacity_ = 0.f;
  float emphasis_ = 0.f;
  float progress_ = 0.f;

  const base::TimeTicks creation_time_;
  base::TimeTicks first_frame_time_;
  base::TimeTicks first_emphasis_frame_;
  base::TimeTicks last_emphasis_frame_;
  base::TimeTicks last_animation_step_time_;
  base::TimeDelta total_steady_time_;

  bool record_first_ramp_down_frame_ = false;
  base::TimeTicks first_ramp_down_frame_;
  // See crbug.com/407106595: Allows the border animation to play seamlessly
  // when the browser UI has lost focus temporarily.
  // TODO(crbug.com/408210785): Add a test for this case.
  float ramp_down_opacity_ = 0.f;

  bool has_hardware_acceleration_ = false;
  base::ScopedObservation<content::GpuDataManager,
                          content::GpuDataManagerObserver>
      gpu_data_manager_observer_{this};

  base::ScopedObservation<ui::Compositor, ui::CompositorObserver>
      compositor_observation_{this};
  base::ScopedObservation<ui::Compositor, ui::CompositorAnimationObserver>
      compositor_animation_observation_{this};

  // Empty in production environment.
  const std::unique_ptr<Tester> tester_;

  sk_sp<cc::PaintShader> cached_paint_shader_;

  const std::vector<SkColor> colors_;
  const std::vector<float> floats_;

  raw_ptr<ui::Compositor> compositor_ = nullptr;
  raw_ptr<ThemeService> theme_service_ = nullptr;
  raw_ptr<Browser> browser_ = nullptr;
};

BEGIN_VIEW_BUILDER(, GlicBorderView, views::View)
VIEW_BUILDER_PROPERTY(bool, Visible)
VIEW_BUILDER_PROPERTY(bool, CanProcessEventsWithinSubtree)
END_VIEW_BUILDER

}  // namespace glic

DEFINE_VIEW_BUILDER(, glic::GlicBorderView)

#endif  // CHROME_BROWSER_GLIC_BROWSER_UI_GLIC_BORDER_VIEW_H_