blob: 571ed8cdc2240b3857a52edafb55b43e2004774e [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
5#ifndef COMPONENTS_CHOOSER_CONTROLLER_CHOOSER_CONTROLLER_H_
6#define COMPONENTS_CHOOSER_CONTROLLER_CHOOSER_CONTROLLER_H_
7
8#include "base/macros.h"
9#include "base/strings/string16.h"
10
11namespace content {
12class RenderFrameHost;
13}
14
15namespace url {
16class Origin;
17}
18
19// Subclass ChooserController to implement a chooser, which has some
20// introductory text and a list of options that users can pick one of.
21// Your subclass must define the set of options users can pick from;
22// the actions taken after users select an item or press the 'Cancel'
23// button or the chooser is closed.
24// After Select/Cancel/Close is called, this object is destroyed and
25// calls back into it are not allowed.
26class ChooserController {
27 public:
28 explicit ChooserController(content::RenderFrameHost* owner);
29 virtual ~ChooserController();
30
31 // Since the set of options can change while the UI is visible an
32 // implementation should register an observer.
33 class Observer {
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
51 protected:
52 virtual ~Observer() {}
53 };
54
55 // Return the origin URL to be displayed on the chooser title.
56 url::Origin GetOrigin() const;
57
58 // The number of options users can pick from. For example, it can be
59 // the number of USB/Bluetooth device names which are listed in the
60 // chooser so that users can grant permission.
61 virtual size_t NumOptions() const = 0;
62
63 // The |index|th option string which is listed in the chooser.
juncai830ffff72016-07-01 21:27:2864 virtual base::string16 GetOption(size_t index) const = 0;
juncaibf183dd2016-05-27 16:57:2465
66 // These three functions are called just before this object is destroyed:
67
68 // Called when the user selects the |index|th element from the dialog.
69 virtual void Select(size_t index) = 0;
70
71 // Called when the user presses the 'Cancel' button in the dialog.
72 virtual void Cancel() = 0;
73
74 // Called when the user clicks outside the dialog or the dialog otherwise
75 // closes without the user taking an explicit action.