File: high_efficiency_chip_tab_helper.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (83 lines) | stat: -rw-r--r-- 3,453 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
// Copyright 2022 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_PERFORMANCE_CONTROLS_HIGH_EFFICIENCY_CHIP_TAB_HELPER_H_
#define CHROME_BROWSER_UI_PERFORMANCE_CONTROLS_HIGH_EFFICIENCY_CHIP_TAB_HELPER_H_

#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom-shared.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace high_efficiency {
enum class ChipState {
  // Chip is not shown for this tab.
  HIDDEN,
  // Chip is rendered in a collapsed state with not text.
  COLLAPSED,
  // Chip expands to educate about the memory saver feature.
  EXPANDED_EDUCATION,
  // Chip expands to highlight large savings from a previously discarded tab.
  EXPANDED_WITH_SAVINGS,
  // Previously expanded chip is collapsed due to tab switching.
  COLLAPSED_FROM_EXPANDED,
};
}  // namespace high_efficiency

// When a page in the background has been discarded due to high efficiency mode,
// and the user returns to that tab, a page action chip should be shown to the
// user which conveys information about the discarded tab to the user.
// The HighEfficiencyChipTabHelper is a per-tab class which manages the state of
// the high efficiency chip.
class HighEfficiencyChipTabHelper
    : public content::WebContentsObserver,
      public content::WebContentsUserData<HighEfficiencyChipTabHelper> {
 public:
  HighEfficiencyChipTabHelper(const HighEfficiencyChipTabHelper&) = delete;
  HighEfficiencyChipTabHelper& operator=(const HighEfficiencyChipTabHelper&) =
      delete;

  ~HighEfficiencyChipTabHelper() override;

  static constexpr int kChipAnimationCount = 3;

  high_efficiency::ChipState chip_state() const { return chip_state_; }

  // content::WebContentsObserver
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void OnVisibilityChanged(content::Visibility visibility) override;

  // Returns whether the tab associated with this helper has been navigated
  // away from and to another tab.
  bool ShouldChipAnimate();

 private:
  friend class content::WebContentsUserData<HighEfficiencyChipTabHelper>;
  explicit HighEfficiencyChipTabHelper(content::WebContents* contents);

  // Checks whether a promotional expanded chip should be shown to highlight
  // memory savings and, if so, update prefs to reflect that it is shown.
  bool ComputeShouldHighlightMemorySavings();

  // Checks whether an educational expanded chip should be shown about the
  // feature and, if so, update prefs to reflect that it is shown.
  bool ComputeShouldEducateAboutMemorySavings();

  // Computes and updates the `chip_state_` based on information about the
  // recent navigation.
  void ComputeChipState(content::NavigationHandle* navigation_handle);

  high_efficiency::ChipState chip_state_ = high_efficiency::ChipState::HIDDEN;
  // Represents whether the current chip state has been properly rendered. This
  // gets reset when a tab gets hidden so the chip can be redrawn.
  bool was_rendered_ = false;
  raw_ptr<PrefService> pref_service_;

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif  // CHROME_BROWSER_UI_PERFORMANCE_CONTROLS_HIGH_EFFICIENCY_CHIP_TAB_HELPER_H_