Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / device_permissions_prompt.h
blob68498510a5293283ae657f0259f37db7cb5e64c1
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_
6 #define EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_
8 #include <vector>
10 #include "base/callback_forward.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/strings/string16.h"
16 namespace content {
17 class BrowserContext;
18 class WebContents;
21 namespace device {
22 class HidDeviceFilter;
23 class HidDeviceInfo;
24 class UsbDevice;
25 class UsbDeviceFilter;
28 namespace extensions {
30 class Extension;
32 // Platform-independent interface for displaing a UI for choosing devices
33 // (similar to choosing files).
34 class DevicePermissionsPrompt {
35 public:
36 using UsbDevicesCallback = base::Callback<void(
37 const std::vector<scoped_refptr<device::UsbDevice>>&)>;
38 using HidDevicesCallback = base::Callback<void(
39 const std::vector<scoped_refptr<device::HidDeviceInfo>>&)>;
41 // Context information available to the UI implementation.
42 class Prompt : public base::RefCounted<Prompt> {
43 public:
44 // This class stores the device information displayed in the UI. It should
45 // be extended to support particular device types.
46 class DeviceInfo {
47 public:
48 DeviceInfo();
49 virtual ~DeviceInfo();
51 const base::string16& name() const { return name_; }
52 const base::string16& serial_number() const { return serial_number_; }
53 bool granted() const { return granted_; }
54 void set_granted() { granted_ = true; }
56 protected:
57 base::string16 name_;
58 base::string16 serial_number_;
60 private:
61 bool granted_ = false;
64 // Since the set of devices can change while the UI is visible an
65 // implementation should register an observer.
66 class Observer {
67 public:
68 virtual void OnDevicesChanged() = 0;
70 protected:
71 virtual ~Observer();
74 Prompt(const Extension* extension,
75 content::BrowserContext* context,
76 bool multiple);
78 // Only one observer may be registered at a time.
79 virtual void SetObserver(Observer* observer);
81 virtual base::string16 GetHeading() const = 0;
82 base::string16 GetPromptMessage() const;
83 size_t GetDeviceCount() const { return devices_.size(); }
84 base::string16 GetDeviceName(size_t index) const;
85 base::string16 GetDeviceSerialNumber(size_t index) const;
87 // Notifies the DevicePermissionsManager for the current extension that
88 // access to the device at the given index is now granted.
89 void GrantDevicePermission(size_t index);
91 virtual void Dismissed() = 0;
93 // Allow the user to select multiple devices.
94 bool multiple() const { return multiple_; }
96 protected:
97 virtual ~Prompt();
99 void AddCheckedDevice(scoped_ptr<DeviceInfo> device, bool allowed);
101 const Extension* extension() const { return extension_; }
102 Observer* observer() const { return observer_; }
103 content::BrowserContext* browser_context() const {
104 return browser_context_;
107 // Subclasses may fill this with a particular subclass of DeviceInfo and may
108 // assume that only that instances of that type are stored here.
109 ScopedVector<DeviceInfo> devices_;
111 private:
112 friend class base::RefCounted<Prompt>;
114 const extensions::Extension* extension_ = nullptr;
115 Observer* observer_ = nullptr;
116 content::BrowserContext* browser_context_ = nullptr;
117 bool multiple_ = false;
119 DISALLOW_COPY_AND_ASSIGN(Prompt);
122 DevicePermissionsPrompt(content::WebContents* web_contents);
123 virtual ~DevicePermissionsPrompt();
125 void AskForUsbDevices(const Extension* extension,
126 content::BrowserContext* context,
127 bool multiple,
128 const std::vector<device::UsbDeviceFilter>& filters,
129 const UsbDevicesCallback& callback);
131 void AskForHidDevices(const Extension* extension,
132 content::BrowserContext* context,
133 bool multiple,
134 const std::vector<device::HidDeviceFilter>& filters,
135 const HidDevicesCallback& callback);
137 protected:
138 virtual void ShowDialog() = 0;
140 content::WebContents* web_contents() { return web_contents_; }
141 scoped_refptr<Prompt> prompt() { return prompt_; }
143 private:
144 // Parent web contents of the device permissions UI dialog.
145 content::WebContents* web_contents_;
147 // Parameters available to the UI implementation.
148 scoped_refptr<Prompt> prompt_;
150 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsPrompt);
153 } // namespace extensions
155 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_