File: payment_request_item_list.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (168 lines) | stat: -rw-r--r-- 6,506 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
// Copyright 2017 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_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_

#include <memory>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/views/payments/payment_request_row_view.h"
#include "ui/base/metadata/metadata_header_macros.h"

namespace views {
class ImageView;
class View;
}  // namespace views

namespace payments {

class PaymentRequestDialogView;
class PaymentRequestSpec;
class PaymentRequestState;

// A control representing a list of selectable items in the PaymentRequest
// dialog. These lists enforce that only one of their elements be selectable at
// a time and that "incomplete" items (for example, a credit card with no known
// expiration date) behave differently when selected. Most of the time, this
// behavior is to show an editor screen.
class PaymentRequestItemList {
 public:
  // Represents an item in the item list.
  class Item : public PaymentRequestRowView {
    METADATA_HEADER(Item, PaymentRequestRowView)

   public:
    // Creates an item that will be owned by `list` with the initial state set
    // to `selected`. `clickable` indicates whether or not the user can interact
    // with this row. The `spec` parameter should not be null.
    Item(base::WeakPtr<PaymentRequestSpec> spec,
         base::WeakPtr<PaymentRequestState> state,
         PaymentRequestItemList* list,
         bool selected,
         bool clickable,
         bool show_edit_button);

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

    ~Item() override;

    bool selected() const { return selected_; }
    // Changes the selected state of this item to |selected|.
    // SelectedStateChanged is called if |notify| is true.
    void SetSelected(bool selected, bool notify);

    // Returns a pointer to the PaymentRequestItemList that owns this object.
    PaymentRequestItemList* list() { return list_; }

    // Returns a pointer to the PaymentRequestSpec/State objects associated with
    // this instance of the UI.
    base::WeakPtr<PaymentRequestSpec> spec() { return spec_; }
    base::WeakPtr<PaymentRequestState> state() { return state_; }

    // PaymentRequestRowView overrides
    // Leaf classes must override this and provide their own factory.
    base::WeakPtr<PaymentRequestRowView> AsWeakPtr() override = 0;

   protected:
    // Initializes the layout and content of the row. Must be called by subclass
    // constructors, so that virtual methods providing row contents are
    // accessible.
    void Init();

    // Called when the selected state of this item changes. Subclasses may
    // assume that they are the only selected item in |list| when this is
    // called. This could be called before CreateItemView so subclasses should
    // be aware that their views might not exist yet.
    virtual void SelectedStateChanged() = 0;

    // Creates an image of a large checkmark, used to indicate that an option is
    // selected.
    std::unique_ptr<views::ImageView> CreateCheckmark(bool selected);

    // Creates the view that represents this item's content. Typically this will
    // be a label describing the payment method, shipping adress, etc. Populates
    // |accessible_content| with the screen reader string for the returned
    // content. |accessible_content| shouldn't be null.
    virtual std::unique_ptr<views::View> CreateContentView(
        std::u16string* accessible_content) = 0;

    // Creates the view that should be displayed after the checkmark in the
    // item's view, such as the credit card icon.
    virtual std::unique_ptr<views::View> CreateExtraView();

    // Returns a string describing the type of data for which this row
    // represents an instance. e.g., "credit card" or "billing address". Used
    // when describing the row for accessibility.
    virtual std::u16string GetNameForDataType() = 0;

    // Returns whether this item is complete/valid and can be selected by the
    // user. If this returns false when the user attempts to select this item,
    // PerformSelectionFallback will be called instead.
    virtual bool CanBeSelected() = 0;

    // Performs the action that replaces selection when CanBeSelected returns
    // false. This will usually be to display an editor.
    virtual void PerformSelectionFallback() = 0;

    // Called when the edit button is pressed. Subclasses should open the editor
    // appropriate for the item they represent.
    virtual void EditButtonPressed() = 0;

   private:
    // Updates the accessible description of this item to reflect its current
    // status (selected/not).
    void UpdateAccessibleName();

    void ButtonPressed();

    base::WeakPtr<PaymentRequestSpec> spec_;
    base::WeakPtr<PaymentRequestState> state_;
    raw_ptr<PaymentRequestItemList, DanglingUntriaged> list_;
    std::u16string accessible_item_description_;
    bool selected_;
    bool show_edit_button_;
  };

  explicit PaymentRequestItemList(
      base::WeakPtr<PaymentRequestDialogView> dialog);

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

  virtual ~PaymentRequestItemList();

  // Adds an item to this list. |item->list()| should return this object.
  void AddItem(std::unique_ptr<Item> item);

  // Removes all items which have been added.
  void Clear();

  // Creates and returns the UI representation of this list. It iterates over
  // the items it contains, creates their associated views, and adds them to the
  // hierarchy.
  std::unique_ptr<views::View> CreateListView();

  // Deselects the currently selected item and selects |item| instead.
  void SelectItem(Item* item);

  base::WeakPtr<PaymentRequestDialogView> dialog() { return dialog_; }

 private:
  // Unselects the currently selected item. This is private so that the list can
  // use it when selecting a new item while avoiding consumers of this class
  // putting the list in a state where no item is selected.
  void UnselectSelectedItem();

  std::vector<std::unique_ptr<Item>> items_;
  raw_ptr<Item, DanglingUntriaged> selected_item_;
  base::WeakPtr<PaymentRequestDialogView> dialog_;
};

}  // namespace payments

#endif  // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_