File: page_action_model.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (205 lines) | stat: -rw-r--r-- 7,861 bytes parent folder | download | duplicates (5)
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
198
199
200
201
202
203
204
205
// 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_UI_VIEWS_PAGE_ACTION_PAGE_ACTION_MODEL_H_
#define CHROME_BROWSER_UI_VIEWS_PAGE_ACTION_PAGE_ACTION_MODEL_H_

#include <iterator>
#include <memory>
#include <optional>
#include <string>

#include "base/observer_list.h"
#include "base/types/pass_key.h"
#include "ui/base/models/image_model.h"

namespace actions {
class ActionItem;
}  // namespace actions

namespace page_actions {

struct SuggestionChipConfig;
class PageActionController;
class PageActionModelObserver;

// Interface to PageActionModel, used for either the concrete implementation
// or a mock for testing.
class PageActionModelInterface {
 public:
  PageActionModelInterface() = default;
  virtual ~PageActionModelInterface() = default;

  virtual void AddObserver(PageActionModelObserver* observer) = 0;
  virtual void RemoveObserver(PageActionModelObserver* observer) = 0;

  virtual void SetActionItemProperties(
      base::PassKey<PageActionController>,
      const actions::ActionItem* action_item) = 0;
  virtual void SetShowRequested(base::PassKey<PageActionController>,
                                bool requested) = 0;
  virtual void SetShowSuggestionChip(base::PassKey<PageActionController>,
                                     bool show) = 0;
  virtual void SetSuggestionChipConfig(base::PassKey<PageActionController>,
                                       const SuggestionChipConfig& config) = 0;
  virtual void SetTabActive(base::PassKey<PageActionController>,
                            bool is_active) = 0;
  virtual void SetHasPinnedIcon(base::PassKey<PageActionController>,
                                bool has_pinned_icon) = 0;
  virtual void SetOverrideText(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_text) = 0;
  virtual void SetOverrideAccessibleName(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_accessible_name) = 0;
  virtual void SetOverrideImage(
      base::PassKey<PageActionController>,
      const std::optional<ui::ImageModel>& override_image) = 0;
  virtual void SetOverrideTooltip(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_tooltip) = 0;
  virtual void SetShouldHidePageAction(base::PassKey<PageActionController>,
                                       bool should_hide) = 0;

  virtual bool GetVisible() const = 0;
  virtual bool GetShowSuggestionChip() const = 0;
  virtual bool GetShouldAnimateChip() const = 0;
  virtual bool GetShouldAnnounceChip() const = 0;
  virtual const ui::ImageModel& GetImage() const = 0;
  virtual const std::u16string& GetText() const = 0;
  virtual const std::u16string& GetTooltipText() const = 0;
  virtual const std::u16string& GetAccessibleName() const = 0;
  virtual bool GetActionItemIsShowingBubble() const = 0;

  virtual bool IsEphemeral() const = 0;
};

// PageActionModel represents the page action's state, scoped to a single tab.
class PageActionModel : public PageActionModelInterface {
 public:
  explicit PageActionModel(bool is_ephemeral = false);
  PageActionModel(const PageActionModel&) = delete;
  PageActionModel& operator=(const PageActionModel&) = delete;
  ~PageActionModel() override;

  void AddObserver(PageActionModelObserver* observer) override;
  void RemoveObserver(PageActionModelObserver* observer) override;

  // Applies any relevant ActionItem properties to the model, including
  // visibility, text and icon properties.
  void SetActionItemProperties(base::PassKey<PageActionController>,
                               const actions::ActionItem* action_item) override;
  void SetShowRequested(base::PassKey<PageActionController>,
                        bool requested) override;
  void SetShowSuggestionChip(base::PassKey<PageActionController>,
                             bool show) override;
  void SetSuggestionChipConfig(base::PassKey<PageActionController>,
                               const SuggestionChipConfig& config) override;
  void SetTabActive(base::PassKey<PageActionController>,
                    bool is_active) override;
  void SetHasPinnedIcon(base::PassKey<PageActionController>,
                        bool has_pinned_icon) override;

  void SetOverrideText(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_text) override;

  void SetOverrideAccessibleName(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_accessible_name) override;

  void SetOverrideImage(
      base::PassKey<PageActionController>,
      const std::optional<ui::ImageModel>& override_image) override;

  void SetOverrideTooltip(
      base::PassKey<PageActionController>,
      const std::optional<std::u16string>& override_tooltip) override;

  void SetShouldHidePageAction(base::PassKey<PageActionController>,
                               bool should_hide) override;

  // The model distills all visibility properties into a single result.
  bool GetVisible() const override;
  bool GetShowSuggestionChip() const override;
  bool GetShouldAnimateChip() const override;
  bool GetShouldAnnounceChip() const override;

  const ui::ImageModel& GetImage() const override;
  const std::u16string& GetText() const override;
  const std::u16string& GetAccessibleName() const override;
  const std::u16string& GetTooltipText() const override;
  bool GetActionItemIsShowingBubble() const override;

  bool IsEphemeral() const override;

 private:
  // Notifies observers of a model change.
  void NotifyChange();

  // Represents whether this page action will be always visible or not.
  const bool is_ephemeral_ = false;

  // Represents whether the tab this model belongs to is active.
  bool is_tab_active_ = false;

  // Represents whether this page action has a corresponding pinned icon sharing
  // the same action.
  bool has_pinned_icon_ = false;

  // Represents whether a feature requested to show this page action.
  bool show_requested_ = false;

  // Represents whether the page action associated with this model should show
  // as suggestion chip.
  bool show_suggestion_chip_ = false;

  // Represents whether suggestion chips should animate in/out.
  bool should_animate_ = true;

  // Represents whether suggestion chips should be announced by a screen
  // reader.
  bool should_announce_chip_ = false;

  // Properties taken from ActionItem.
  bool action_item_enabled_ = false;
  bool action_item_visible_ = false;
  bool action_item_is_showing_bubble_ = false;
  std::u16string text_;
  std::u16string tooltip_;
  // When set, it will always take precedence over `tooltip_`.
  std::optional<std::u16string> override_tooltip_;
  ui::ImageModel action_item_image_;
  // When set, it will always take precedence over `action_item_image_`.
  std::optional<ui::ImageModel> override_image_;

  // When set, it will always take precedence over `text_`.
  std::optional<std::u16string> override_text_;

  // When set, it will always take precedence over `text_` because by default
  // `text_` will be used.
  std::optional<std::u16string> override_accessible_name_;

  // Tracks whether we should forcibly hide the page action (e.g., Omnibox is
  // getting updated).
  bool should_hide_ = false;

  // Flag used to disallow reentrant behaviour.
  bool is_notifying_observers_ = false;

  base::ObserverList<PageActionModelObserver> observer_list_;
};

class PageActionModelFactory {
 public:
  virtual ~PageActionModelFactory() = default;

  virtual std::unique_ptr<PageActionModelInterface> Create(
      int action_id,
      bool is_ephemeral) = 0;
};

}  // namespace page_actions

#endif  // CHROME_BROWSER_UI_VIEWS_PAGE_ACTION_PAGE_ACTION_MODEL_H_