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