Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Michael van Ouwerkerk | 12553e2 | 2021-05-17 14:20:58 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_ |
| 6 | #define COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_ |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 7 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 8 | #include <string> |
juncai | 0f6d279 | 2016-11-23 18:38:07 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
| 12 | |
Michael van Ouwerkerk | 12553e2 | 2021-05-17 14:20:58 | [diff] [blame] | 13 | namespace permissions { |
| 14 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 15 | // Subclass ChooserController to implement a chooser, which has some |
| 16 | // introductory text and a list of options that users can pick one of. |
| 17 | // Your subclass must define the set of options users can pick from; |
| 18 | // the actions taken after users select an item or press the 'Cancel' |
| 19 | // button or the chooser is closed. |
| 20 | // After Select/Cancel/Close is called, this object is destroyed and |
| 21 | // calls back into it are not allowed. |
| 22 | class ChooserController { |
| 23 | public: |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 24 | explicit ChooserController(std::u16string title); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 25 | |
| 26 | ChooserController(const ChooserController&) = delete; |
| 27 | ChooserController& operator=(const ChooserController&) = delete; |
| 28 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 29 | virtual ~ChooserController(); |
| 30 | |
| 31 | // Since the set of options can change while the UI is visible an |
juncai | 79673301 | 2016-07-20 22:30:46 | [diff] [blame] | 32 | // implementation should register a view to observe changes. |
| 33 | class View { |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 34 | public: |
| 35 | // Called after the options list is initialized for the first time. |
| 36 | // OnOptionsInitialized should only be called once. |
| 37 | virtual void OnOptionsInitialized() = 0; |
| 38 | |
| 39 | // Called after GetOption(index) has been added to the options and the |
| 40 | // newly added option is the last element in the options list. Calling |
| 41 | // GetOption(index) from inside a call to OnOptionAdded will see the |
| 42 | // added string since the options have already been updated. |
| 43 | virtual void OnOptionAdded(size_t index) = 0; |
| 44 | |
| 45 | // Called when GetOption(index) is no longer present, and all later |
| 46 | // options have been moved earlier by 1 slot. Calling GetOption(index) |
| 47 | // from inside a call to OnOptionRemoved will NOT see the removed string |
| 48 | // since the options have already been updated. |
| 49 | virtual void OnOptionRemoved(size_t index) = 0; |
| 50 | |
juncai | bb5ba230 | 2016-08-16 18:01:13 | [diff] [blame] | 51 | // Called when the option at |index| has been updated. |
| 52 | virtual void OnOptionUpdated(size_t index) = 0; |
| 53 | |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 54 | // Called when the device adapter is turned on or off. |
| 55 | virtual void OnAdapterEnabledChanged(bool enabled) = 0; |
| 56 | |
James Hollyer | 6a6c9e3 | 2021-01-25 20:45:34 | [diff] [blame] | 57 | // Called when the platform level device permission is changed. |
| 58 | // Currently only needed on macOS. |
| 59 | virtual void OnAdapterAuthorizationChanged(bool authorized); |
| 60 | |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 61 | // Called when refreshing options is in progress or complete. |
| 62 | virtual void OnRefreshStateChanged(bool refreshing) = 0; |
| 63 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 64 | protected: |
Sorin Jianu | c659b55 | 2024-10-04 07:56:28 | [diff] [blame] | 65 | virtual ~View() = default; |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 66 | }; |
| 67 | |
juncai | a29e116d | 2016-07-13 02:53:34 | [diff] [blame] | 68 | // Returns the text to be displayed in the chooser title. |
Elly Fong-Jones | 1ec06c6e | 2020-07-16 14:44:28 | [diff] [blame] | 69 | // Note that this is only called once, and there is no way to update the title |
| 70 | // for a given instance of ChooserController. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 71 | std::u16string GetTitle() const; |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 72 | |
Bret Sepulveda | a4975e7a5e | 2017-12-18 23:16:38 | [diff] [blame] | 73 | // Returns whether the chooser needs to show an icon before the text. |
juncai | 3ecef4e | 2016-08-23 00:10:16 | [diff] [blame] | 74 | // For WebBluetooth, it is a signal strength icon. |
| 75 | virtual bool ShouldShowIconBeforeText() const; |
| 76 | |
Bret Sepulveda | a4975e7a5e | 2017-12-18 23:16:38 | [diff] [blame] | 77 | // Returns whether the chooser needs to show a help button. |
| 78 | virtual bool ShouldShowHelpButton() const; |
juncai | 4300362 | 2016-11-29 20:43:18 | [diff] [blame] | 79 | |
Bret Sepulveda | a4975e7a5e | 2017-12-18 23:16:38 | [diff] [blame] | 80 | // Returns whether the chooser needs to show a button to re-scan for devices. |
| 81 | virtual bool ShouldShowReScanButton() const; |
| 82 | |
| 83 | // Returns whether the chooser allows multiple items to be selected. |
juncai | 0f6d279 | 2016-11-23 18:38:07 | [diff] [blame] | 84 | virtual bool AllowMultipleSelection() const; |
| 85 | |
Olivier Yiptong | 6824206f | 2020-12-18 18:40:13 | [diff] [blame] | 86 | // Returns whether the chooser needs to show a select-all checkbox. |
| 87 | virtual bool ShouldShowSelectAllCheckbox() const; |
| 88 | |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 89 | // Returns the text to be displayed in the chooser when there are no options. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 90 | virtual std::u16string GetNoOptionsText() const = 0; |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 91 | |
juncai | 3f5e325 | 2016-07-13 05:21:19 | [diff] [blame] | 92 | // Returns the label for OK button. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 93 | virtual std::u16string GetOkButtonLabel() const = 0; |
juncai | 3f5e325 | 2016-07-13 05:21:19 | [diff] [blame] | 94 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 95 | // Returns the label for Cancel button. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 96 | virtual std::u16string GetCancelButtonLabel() const; |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 97 | |
Olivier Yiptong | 6824206f | 2020-12-18 18:40:13 | [diff] [blame] | 98 | // Returns the label for SelectAll checkbox. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 99 | virtual std::u16string GetSelectAllCheckboxLabel() const; |
Olivier Yiptong | 6824206f | 2020-12-18 18:40:13 | [diff] [blame] | 100 | |
Reilly Grant | 06bb2df5 | 2021-01-13 22:26:37 | [diff] [blame] | 101 | // Returns the label for the throbber shown while options are initializing or |
| 102 | // a re-scan is in progress. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 103 | virtual std::pair<std::u16string, std::u16string> GetThrobberLabelAndTooltip() |
Reilly Grant | 06bb2df5 | 2021-01-13 22:26:37 | [diff] [blame] | 104 | const = 0; |
| 105 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 106 | // Returns whether both OK and Cancel buttons are enabled. |
| 107 | // |
| 108 | // For chooser used in Web APIs such as WebBluetooth, WebUSB, |
| 109 | // WebSerial, etc., the OK button is only enabled when there is at least |
| 110 | // one device listed in the chooser, because user needs to be able to select |
| 111 | // a device to grant access permission in these APIs. |
| 112 | // |
| 113 | // For permission prompt used in Bluetooth scanning Web API, the two buttons |
| 114 | // represent Allow and Block, and should always be enabled so that user can |
| 115 | // make their permission decision. |
| 116 | virtual bool BothButtonsAlwaysEnabled() const; |
| 117 | |
| 118 | // Returns whether table view should always be disabled. |
| 119 | // |
| 120 | // For permission prompt used in Bluetooth scanning Web API, the table is |
| 121 | // used for displaying device names, and user doesn't need to select a device |
| 122 | // from the table, so it should always be disabled. |
| 123 | virtual bool TableViewAlwaysDisabled() const; |
| 124 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 125 | // The number of options users can pick from. For example, it can be |
| 126 | // the number of USB/Bluetooth device names which are listed in the |
| 127 | // chooser so that users can grant permission. |
| 128 | virtual size_t NumOptions() const = 0; |
| 129 | |
juncai | 3ecef4e | 2016-08-23 00:10:16 | [diff] [blame] | 130 | // The signal strength level (0-4 inclusive) of the device at |index|, which |
| 131 | // is used to retrieve the corresponding icon to be displayed before the |
| 132 | // text. Returns -1 if no icon should be shown. |
| 133 | virtual int GetSignalStrengthLevel(size_t index) const; |
| 134 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 135 | // The |index|th option string which is listed in the chooser. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 136 | virtual std::u16string GetOption(size_t index) const = 0; |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 137 | |
juncai | 87d0929 | 2016-09-15 04:02:53 | [diff] [blame] | 138 | // Returns if the |index|th option is connected. |
| 139 | // This function returns false by default. |
| 140 | virtual bool IsConnected(size_t index) const; |
| 141 | |
| 142 | // Returns if the |index|th option is paired. |
| 143 | // This function returns false by default. |
| 144 | virtual bool IsPaired(size_t index) const; |
| 145 | |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 146 | // Refresh the list of options. |
Bret Sepulveda | e6e84f7 | 2017-10-20 03:37:40 | [diff] [blame] | 147 | virtual void RefreshOptions(); |
juncai | 9c9d751 | 2016-07-20 00:56:23 | [diff] [blame] | 148 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 149 | // These three functions are called just before this object is destroyed: |
| 150 | |
juncai | 0f6d279 | 2016-11-23 18:38:07 | [diff] [blame] | 151 | // Called when the user selects elements from the dialog. |indices| contains |
| 152 | // the indices of the selected elements. |
| 153 | virtual void Select(const std::vector<size_t>& indices) = 0; |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 154 | |
| 155 | // Called when the user presses the 'Cancel' button in the dialog. |
| 156 | virtual void Cancel() = 0; |
| 157 | |
| 158 | // Called when the user clicks outside the dialog or the dialog otherwise |
| 159 | // closes without the user taking an explicit action. |
| 160 | virtual void Close() = 0; |
| 161 | |
| 162 | // Open help center URL. |
| 163 | virtual void OpenHelpCenterUrl() const = 0; |
| 164 | |
juncai | c1bb01a | 2016-09-29 22:20:19 | [diff] [blame] | 165 | // Provide help information when the adapter is off. |
| 166 | virtual void OpenAdapterOffHelpUrl() const; |
| 167 | |
James Hollyer | 6a6c9e3 | 2021-01-25 20:45:34 | [diff] [blame] | 168 | // Navigate user to preferences in order to acquire Bluetooth permission. |
| 169 | virtual void OpenPermissionPreferences() const; |
| 170 | |
Garfield Tan | 8e410aa | 2025-03-06 00:13:15 | [diff] [blame] | 171 | // Provide help information about system Bluetooth permissions. |
| 172 | virtual void OpenBluetoothPermissionHelpUrl() const; |
| 173 | |
Jack Hsieh | 390ce97 | 2024-07-26 19:53:37 | [diff] [blame] | 174 | // Return whether the chooser needs to show Bluetooth adapter view. |
| 175 | virtual bool ShouldShowAdapterOffView() const; |
| 176 | |
| 177 | // Return the message id for Bluetooth adapter being off. |
| 178 | virtual int GetAdapterOffMessageId() const; |
| 179 | |
| 180 | // Return the message id of the link text for turning Bluetooth adapter on. |
| 181 | virtual int GetTurnAdapterOnLinkTextMessageId() const; |
| 182 | |
| 183 | // Return whether the chooser needs to show Bluetooth unauthorized view. |
| 184 | virtual bool ShouldShowAdapterUnauthorizedView() const; |
| 185 | |
| 186 | // Return the message id for Bluetooth access unauthorized. |
| 187 | virtual int GetBluetoothUnauthorizedMessageId() const; |
| 188 | |
| 189 | // Return the label of the link text for authotizing Bluetooth access. |
| 190 | virtual int GetAuthorizeBluetoothLinkTextMessageId() const; |
| 191 | |
juncai | 79673301 | 2016-07-20 22:30:46 | [diff] [blame] | 192 | // Only one view may be registered at a time. |
| 193 | void set_view(View* view) { view_ = view; } |
| 194 | View* view() const { return view_; } |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 195 | |
bsep | 04d6c62 | 2017-06-16 22:11:18 | [diff] [blame] | 196 | protected: |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 197 | void set_title_for_testing(const std::u16string& title) { title_ = title; } |
bsep | 04d6c62 | 2017-06-16 22:11:18 | [diff] [blame] | 198 | |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 199 | private: |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 200 | std::u16string title_; |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 201 | raw_ptr<View, DanglingUntriaged> view_ = nullptr; |
juncai | bf183dd | 2016-05-27 16:57:24 | [diff] [blame] | 202 | }; |
| 203 | |
Michael van Ouwerkerk | 12553e2 | 2021-05-17 14:20:58 | [diff] [blame] | 204 | } // namespace permissions |
| 205 | |
| 206 | #endif // COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_ |