File: chooser_controller.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 (185 lines) | stat: -rw-r--r-- 7,226 bytes parent folder | download | duplicates (2)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_
#define COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_

#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"

namespace permissions {

// Subclass ChooserController to implement a chooser, which has some
// introductory text and a list of options that users can pick one of.
// Your subclass must define the set of options users can pick from;
// the actions taken after users select an item or press the 'Cancel'
// button or the chooser is closed.
// After Select/Cancel/Close is called, this object is destroyed and
// calls back into it are not allowed.
class ChooserController {
 public:
  explicit ChooserController(std::u16string title);

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

  virtual ~ChooserController();

  // Since the set of options can change while the UI is visible an
  // implementation should register a view to observe changes.
  class View {
   public:
    // Called after the options list is initialized for the first time.
    // OnOptionsInitialized should only be called once.
    virtual void OnOptionsInitialized() = 0;

    // Called after GetOption(index) has been added to the options and the
    // newly added option is the last element in the options list. Calling
    // GetOption(index) from inside a call to OnOptionAdded will see the
    // added string since the options have already been updated.
    virtual void OnOptionAdded(size_t index) = 0;

    // Called when GetOption(index) is no longer present, and all later
    // options have been moved earlier by 1 slot. Calling GetOption(index)
    // from inside a call to OnOptionRemoved will NOT see the removed string
    // since the options have already been updated.
    virtual void OnOptionRemoved(size_t index) = 0;

    // Called when the option at |index| has been updated.
    virtual void OnOptionUpdated(size_t index) = 0;

    // Called when the device adapter is turned on or off.
    virtual void OnAdapterEnabledChanged(bool enabled) = 0;

    // Called when the platform level device permission is changed.
    // Currently only needed on macOS.
    virtual void OnAdapterAuthorizationChanged(bool authorized);

    // Called when refreshing options is in progress or complete.
    virtual void OnRefreshStateChanged(bool refreshing) = 0;

   protected:
    virtual ~View() {}
  };

  // Returns the text to be displayed in the chooser title.
  // Note that this is only called once, and there is no way to update the title
  // for a given instance of ChooserController.
  std::u16string GetTitle() const;

  // Returns whether the chooser needs to show an icon before the text.
  // For WebBluetooth, it is a signal strength icon.
  virtual bool ShouldShowIconBeforeText() const;

  // Returns whether the chooser needs to show a help button.
  virtual bool ShouldShowHelpButton() const;

  // Returns whether the chooser needs to show a button to re-scan for devices.
  virtual bool ShouldShowReScanButton() const;

  // Returns whether the chooser allows multiple items to be selected.
  virtual bool AllowMultipleSelection() const;

  // Returns whether the chooser needs to show a select-all checkbox.
  virtual bool ShouldShowSelectAllCheckbox() const;

  // Returns the text to be displayed in the chooser when there are no options.
  virtual std::u16string GetNoOptionsText() const = 0;

  // Returns the label for OK button.
  virtual std::u16string GetOkButtonLabel() const = 0;

  // Returns the label for Cancel button.
  virtual std::u16string GetCancelButtonLabel() const;

  // Returns the label for SelectAll checkbox.
  virtual std::u16string GetSelectAllCheckboxLabel() const;

  // Returns the label for the throbber shown while options are initializing or
  // a re-scan is in progress.
  virtual std::pair<std::u16string, std::u16string> GetThrobberLabelAndTooltip()
      const = 0;

  // Returns whether both OK and Cancel buttons are enabled.
  //
  // For chooser used in Web APIs such as WebBluetooth, WebUSB,
  // WebSerial, etc., the OK button is only enabled when there is at least
  // one device listed in the chooser, because user needs to be able to select
  // a device to grant access permission in these APIs.
  //
  // For permission prompt used in Bluetooth scanning Web API, the two buttons
  // represent Allow and Block, and should always be enabled so that user can
  // make their permission decision.
  virtual bool BothButtonsAlwaysEnabled() const;

  // Returns whether table view should always be disabled.
  //
  // For permission prompt used in Bluetooth scanning Web API, the table is
  // used for displaying device names, and user doesn't need to select a device
  // from the table, so it should always be disabled.
  virtual bool TableViewAlwaysDisabled() const;

  // The number of options users can pick from. For example, it can be
  // the number of USB/Bluetooth device names which are listed in the
  // chooser so that users can grant permission.
  virtual size_t NumOptions() const = 0;

  // The signal strength level (0-4 inclusive) of the device at |index|, which
  // is used to retrieve the corresponding icon to be displayed before the
  // text. Returns -1 if no icon should be shown.
  virtual int GetSignalStrengthLevel(size_t index) const;

  // The |index|th option string which is listed in the chooser.
  virtual std::u16string GetOption(size_t index) const = 0;

  // Returns if the |index|th option is connected.
  // This function returns false by default.
  virtual bool IsConnected(size_t index) const;

  // Returns if the |index|th option is paired.
  // This function returns false by default.
  virtual bool IsPaired(size_t index) const;

  // Refresh the list of options.
  virtual void RefreshOptions();

  // These three functions are called just before this object is destroyed:

  // Called when the user selects elements from the dialog. |indices| contains
  // the indices of the selected elements.
  virtual void Select(const std::vector<size_t>& indices) = 0;

  // Called when the user presses the 'Cancel' button in the dialog.
  virtual void Cancel() = 0;

  // Called when the user clicks outside the dialog or the dialog otherwise
  // closes without the user taking an explicit action.
  virtual void Close() = 0;

  // Open help center URL.
  virtual void OpenHelpCenterUrl() const = 0;

  // Provide help information when the adapter is off.
  virtual void OpenAdapterOffHelpUrl() const;

  // Navigate user to preferences in order to acquire Bluetooth permission.
  virtual void OpenPermissionPreferences() const;

  // Only one view may be registered at a time.
  void set_view(View* view) { view_ = view; }
  View* view() const { return view_; }

 protected:
  void set_title_for_testing(const std::u16string& title) { title_ = title; }

 private:
  std::u16string title_;
  raw_ptr<View, DanglingUntriaged> view_ = nullptr;
};

}  // namespace permissions

#endif  // COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_