blob: 1fc371bfc0b36e0d7490f8b84b181f64d408a453 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2016 The Chromium Authors
juncaibf183dd2016-05-27 16:57:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Michael van Ouwerkerk12553e22021-05-17 14:20:585#ifndef COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_
6#define COMPONENTS_PERMISSIONS_CHOOSER_CONTROLLER_H_
juncaibf183dd2016-05-27 16:57:247
Jan Wilken Dörriead587c32021-03-11 14:09:278#include <string>
juncai0f6d2792016-11-23 18:38:079#include <vector>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
12
Michael van Ouwerkerk12553e22021-05-17 14:20:5813namespace permissions {
14
juncaibf183dd2016-05-27 16:57:2415// 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.
22class ChooserController {
23 public:
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4324 explicit ChooserController(std::u16string title);
Peter Boström09c01822021-09-20 22:43:2725
26 ChooserController(const ChooserController&) = delete;
27 ChooserController& operator=(const ChooserController&) = delete;
28
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
James Hollyer6a6c9e32021-01-25 20:45:3457 // Called when the platform level device permission is changed.
58 // Currently only needed on macOS.
59 virtual void OnAdapterAuthorizationChanged(bool authorized);
60
juncai9c9d7512016-07-20 00:56:2361 // Called when refreshing options is in progress or complete.
62 virtual void OnRefreshStateChanged(bool refreshing) = 0;
63
juncaibf183dd2016-05-27 16:57:2464 protected:
Sorin Jianuc659b552024-10-04 07:56:2865 virtual ~View() = default;
juncaibf183dd2016-05-27 16:57:2466 };
67
juncaia29e116d2016-07-13 02:53:3468 // Returns the text to be displayed in the chooser title.
Elly Fong-Jones1ec06c6e2020-07-16 14:44:2869 // 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örrief27844b2021-03-11 23:18:4871 std::u16string GetTitle() const;
juncaibf183dd2016-05-27 16:57:2472
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3873 // Returns whether the chooser needs to show an icon before the text.
juncai3ecef4e2016-08-23 00:10:1674 // For WebBluetooth, it is a signal strength icon.
75 virtual bool ShouldShowIconBeforeText() const;
76
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3877 // Returns whether the chooser needs to show a help button.
78 virtual bool ShouldShowHelpButton() const;
juncai43003622016-11-29 20:43:1879
Bret Sepulvedaa4975e7a5e2017-12-18 23:16:3880 // 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.
juncai0f6d2792016-11-23 18:38:0784 virtual bool AllowMultipleSelection() const;
85
Olivier Yiptong6824206f2020-12-18 18:40:1386 // Returns whether the chooser needs to show a select-all checkbox.
87 virtual bool ShouldShowSelectAllCheckbox() const;
88
juncai9c9d7512016-07-20 00:56:2389 // Returns the text to be displayed in the chooser when there are no options.
Jan Wilken Dörrief27844b2021-03-11 23:18:4890 virtual std::u16string GetNoOptionsText() const = 0;
juncai9c9d7512016-07-20 00:56:2391
juncai3f5e3252016-07-13 05:21:1992 // Returns the label for OK button.
Jan Wilken Dörrief27844b2021-03-11 23:18:4893 virtual std::u16string GetOkButtonLabel() const = 0;
juncai3f5e3252016-07-13 05:21:1994
Jun Cai149002e2019-05-09 23:13:0795 // Returns the label for Cancel button.
Jan Wilken Dörrief27844b2021-03-11 23:18:4896 virtual std::u16string GetCancelButtonLabel() const;
Jun Cai149002e2019-05-09 23:13:0797
Olivier Yiptong6824206f2020-12-18 18:40:1398 // Returns the label for SelectAll checkbox.
Jan Wilken Dörrief27844b2021-03-11 23:18:48