blob: 99325e36f072531be188251941bc09047eede492 [file] [log] [blame]
juncaibf183dd2016-05-27 16:57:241// 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
juncaibadc1daa2016-07-11 20:36:545#ifndef CHROME_BROWSER_CHOOSER_CONTROLLER_CHOOSER_CONTROLLER_H_
6#define CHROME_BROWSER_CHOOSER_CONTROLLER_CHOOSER_CONTROLLER_H_
juncaibf183dd2016-05-27 16:57:247
juncai0f6d2792016-11-23 18:38:078#include <vector>
9
juncaibf183dd2016-05-27 16:57:2410#include "base/macros.h"
11#include "base/strings/string16.h"
12
13namespace content {
14class RenderFrameHost;
15}
16
juncaibf183dd2016-05-27 16:57:2417// Subclass ChooserController to implement a chooser, which has some
18// introductory text and a list of options that users can pick one of.
19// Your subclass must define the set of options users can pick from;
20// the actions taken after users select an item or press the 'Cancel'
21// button or the chooser is closed.
22// After Select/Cancel/Close is called, this object is destroyed and
23// calls back into it are not allowed.
24class ChooserController {
25 public:
juncai6d9f7732016-07-14 00:46:5426 ChooserController(content::RenderFrameHost* owner,
27 int title_string_id_origin,
28 int title_string_id_extension);
juncaibf183dd2016-05-27 16:57:2429 virtual ~ChooserController();
30
31 // Since the set of options can change while the UI is visible an
juncai796733012016-07-20 22:30:4632 // implementation should register a view to observe changes.
33 class View {
juncaibf183dd2016-05-27 16:57:2434 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
juncaibb5ba2302016-08-16 18:01:1351 // Called when the option at |index| has been updated.
52 virtual void OnOptionUpdated(size_t index) = 0;
53
juncai9c9d7512016-07-20 00:56:2354 // Called when the device adapter is turned on or off.
55 virtual void OnAdapterEnabledChanged(bool enabled) = 0;
56
57 // Called when refreshing options is in progress or complete.
58 virtual void OnRefreshStateChanged(bool refreshing) = 0;
59
juncaibf183dd2016-05-27 16:57:2460 protected:
juncai796733012016-07-20 22:30:4661 virtual ~View() {}
juncaibf183dd2016-05-27 16:57:2462 };
63
juncaia29e116d2016-07-13 02:53:3464 // Returns the text to be displayed in the chooser title.
Elly Fong-Jones1ec06c6e2020-07-16 14:44:2865 // Note that this is only called once, and there is no way to update the title
66 // for a given instance of ChooserController.
juncaia29e116d2016-07-13 02:53:3467 base::string16 GetTitle() const;
juncaibf183dd2016-05-27 16:57:2468
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3869 // Returns whether the chooser needs to show an icon before the text.
juncai3ecef4e2016-08-23 00:10:1670 // For WebBluetooth, it is a signal strength icon.
71 virtual bool ShouldShowIconBeforeText() const;
72
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3873 // Returns whether the chooser needs to show a help button.
74 virtual bool ShouldShowHelpButton() const;
juncai43003622016-11-29 20:43:1875
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3876 // Returns whether the chooser needs to show a button to re-scan for devices.
77 virtual bool ShouldShowReScanButton() const;
78
79 // Returns whether the chooser allows multiple items to be selected.
juncai0f6d2792016-11-23 18:38:0780 virtual bool AllowMultipleSelection() const;
81
Olivier Yiptong6824206f2020-12-18 18:40:1382 // Returns whether the chooser needs to show a select-all checkbox.
83 virtual bool ShouldShowSelectAllCheckbox() const;
84
juncai9c9d7512016-07-20 00:56:2385 // Returns the text to be displayed in the chooser when there are no options.
86 virtual base::string16 GetNoOptionsText() const = 0;
87
juncai3f5e3252016-07-13 05:21:1988 // Returns the label for OK button.
89 virtual base::string16 GetOkButtonLabel() const = 0;
90
Jun Cai149002e2019-05-09 23:13:0791 // Returns the label for Cancel button.
92 virtual base::string16 GetCancelButtonLabel() const;
93
Olivier Yiptong6824206f2020-12-18 18:40:1394 // Returns the label for SelectAll checkbox.
95 virtual base::string16 GetSelectAllCheckboxLabel() const;
96
Reilly Grant06bb2df52021-01-13 22:26:3797 // Returns the label for the throbber shown while options are initializing or
98 // a re-scan is in progress.
99 virtual std::pair<base::string16, base::string16> GetThrobberLabelAndTooltip()
100 const = 0;
101
Jun Cai149002e2019-05-09 23:13:07102 // Returns whether both OK and Cancel buttons are enabled.
103 //
104 // For chooser used in Web APIs such as WebBluetooth, WebUSB,
105 // WebSerial, etc., the OK button is only enabled when there is at least
106 // one device listed in the chooser, because user needs to be able to select
107 // a device to grant access permission in these APIs.
108 //
109 // For permission prompt used in Bluetooth scanning Web API, the two buttons
110 // represent Allow and Block, and should always be enabled so that user can
111 // make their permission decision.
112 virtual bool BothButtonsAlwaysEnabled() const;
113
114 // Returns whether table view should always be disabled.
115 //
116 // For permission prompt used in Bluetooth scanning Web API, the table is
117 // used for displaying device names, and user doesn't need to select a device
118 // from the table, so it should always be disabled.
119 virtual bool TableViewAlwaysDisabled() const;
120
juncaibf183dd2016-05-27 16:57:24121 // The number of options users can pick from. For example, it can be
122 // the number of USB/Bluetooth device names which are listed in the
123 // chooser so that users can grant permission.
124 virtual size_t NumOptions() const = 0;
125
juncai3ecef4e2016-08-23 00:10:16126 // The signal strength level (0-4 inclusive) of the device at |index|, which
127 // is used to retrieve the corresponding icon to be displayed before the
128 // text. Returns -1 if no icon should be shown.
129 virtual int GetSignalStrengthLevel(size_t index) const;
130
juncaibf183dd2016-05-27 16:57:24131 // The |index|th option string which is listed in the chooser.
juncai830ffff72016-07-01 21:27:28132 virtual base::string16 GetOption(size_t index) const = 0;
juncaibf183dd2016-05-27 16:57:24133
juncai87d09292016-09-15 04:02:53134 // Returns if the |index|th option is connected.
135 // This function returns false by default.
136 virtual bool IsConnected(size_t index) const;
137
138 // Returns if the |index|th option is paired.
139 // This function returns false by default.
140 virtual bool IsPaired(size_t index) const;
141
juncai9c9d7512016-07-20 00:56:23142 // Refresh the list of options.
Bret Sepulvedae6e84f72017-10-20 03:37:40143 virtual void RefreshOptions();
juncai9c9d7512016-07-20 00:56:23144
juncaibf183dd2016-05-27 16:57:24145 // These three functions are called just before this object is destroyed:
146
juncai0f6d2792016-11-23 18:38:07147 // Called when the user selects elements from the dialog. |indices| contains
148 // the indices of the selected elements.
149 virtual void Select(const std::vector<size_t>& indices) = 0;
juncaibf183dd2016-05-27 16:57:24150
151 // Called when the user presses the 'Cancel' button in the dialog.
152 virtual void Cancel() = 0;
153
154 // Called when the user clicks outside the dialog or the dialog otherwise
155 // closes without the user taking an explicit action.
156 virtual void Close() = 0;
157
158 // Open help center URL.
159 virtual void OpenHelpCenterUrl() const = 0;
160
juncaic1bb01a2016-09-29 22:20:19161 // Provide help information when the adapter is off.
162 virtual void OpenAdapterOffHelpUrl() const;
163
juncai796733012016-07-20 22:30:46164 // Only one view may be registered at a time.
165 void set_view(View* view) { view_ = view; }
166 View* view() const { return view_; }
juncaibf183dd2016-05-27 16:57:24167
bsep04d6c622017-06-16 22:11:18168 protected:
169 void set_title_for_testing(const base::string16& title) { title_ = title; }
170
juncaibf183dd2016-05-27 16:57:24171 private:
reillygf50d8d32017-03-14 22:55:00172 base::string16 title_;
juncai796733012016-07-20 22:30:46173 View* view_ = nullptr;
juncaibf183dd2016-05-27 16:57:24174
175 DISALLOW_COPY_AND_ASSIGN(ChooserController);
176};
177
juncaibadc1daa2016-07-11 20:36:54178#endif // CHROME_BROWSER_CHOOSER_CONTROLLER_CHOOSER_CONTROLLER_H_