Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / device_permissions_prompt.h
blob6679a16477856180ec636e64649ad6337f01f4ff
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.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
15 namespace content {
16 class BrowserContext;
17 class WebContents;
20 namespace device {
21 class UsbDevice;
22 class UsbDeviceFilter;
25 namespace extensions {
27 class Extension;
29 // Platform-independent interface for displaing a UI for choosing devices
30 // (similar to choosing files).
31 class DevicePermissionsPrompt {
32 public:
33 // Context information available to the UI implementation.
34 class Prompt : public base::RefCountedThreadSafe<Prompt> {
35 public:
36 // Displayed properties of a device.
37 struct DeviceInfo {
38 DeviceInfo(scoped_refptr<device::UsbDevice> device,
39 const base::string16& name,
40 const base::string16& product_string,
41 const base::string16& manufacturer_string,
42 const base::string16& serial_number);
43 ~DeviceInfo();
45 scoped_refptr<device::UsbDevice> device;
46 base::string16 name;
47 base::string16 product_string;
48 base::string16 manufacturer_string;
49 base::string16 serial_number;
52 // Since the set of devices can change while the UI is visible an
53 // implementation should register an observer.
54 class Observer {
55 public:
56 virtual void OnDevicesChanged() = 0;
59 Prompt();
61 // Only one observer may be registered at a time.
62 void SetObserver(Observer* observer);
64 base::string16 GetHeading() const;
65 base::string16 GetPromptMessage() const;
66 size_t GetDeviceCount() const { return devices_.size(); }
67 scoped_refptr<device::UsbDevice> GetDevice(size_t index) const;
68 base::string16 GetDeviceName(size_t index) const {
69 DCHECK_LT(index, devices_.size());
70 return devices_[index].name;
72 base::string16 GetDeviceSerialNumber(size_t index) const {
73 DCHECK_LT(index, devices_.size());
74 return devices_[index].serial_number;
77 // Notifies the DevicePermissionsManager for the current extension that
78 // access to the device at the given index is now granted.
79 void GrantDevicePermission(size_t index) const;
81 const extensions::Extension* extension() const { return extension_; }
82 void set_extension(const extensions::Extension* extension) {
83 extension_ = extension;
86 void set_browser_context(content::BrowserContext* context) {
87 browser_context_ = context;
90 bool multiple() const { return multiple_; }
91 void set_multiple(bool multiple) { multiple_ = multiple; }
93 const std::vector<device::UsbDeviceFilter>& filters() const {
94 return filters_;
96 void set_filters(const std::vector<device::UsbDeviceFilter>& filters);
98 private:
99 friend class base::RefCountedThreadSafe<Prompt>;
101 virtual ~Prompt();
103 // Querying for devices must be done asynchronously on the FILE thread.
104 void DoDeviceQuery();
105 void SetDevices(const std::vector<DeviceInfo>& devices);
107 const extensions::Extension* extension_;
108 content::BrowserContext* browser_context_;
109 bool multiple_;
110 std::vector<device::UsbDeviceFilter> filters_;
111 std::vector<DeviceInfo> devices_;
112 Observer* observer_;
115 class Delegate {
116 public:
117 // Called with the list of selected USB devices.
118 virtual void OnUsbDevicesChosen(
119 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0;
121 protected:
122 virtual ~Delegate() {}
125 DevicePermissionsPrompt(content::WebContents* web_contents);
126 virtual ~DevicePermissionsPrompt();
128 void AskForUsbDevices(Delegate* delegate,
129 const Extension* extension,
130 content::BrowserContext* context,
131 bool multiple,
132 const std::vector<device::UsbDeviceFilter>& filters);
134 protected:
135 virtual void ShowDialog() = 0;
137 content::WebContents* web_contents() { return web_contents_; }
138 Delegate* delegate() { return delegate_; }
139 scoped_refptr<Prompt> prompt() { return prompt_; }
141 private:
142 // Parent web contents of the device permissions UI dialog.
143 content::WebContents* web_contents_;
145 // The delegate called after the UI has been dismissed.
146 Delegate* delegate_;
148 // Parameters available to the UI implementation.
149 scoped_refptr<Prompt> prompt_;
152 } // namespace extensions
154 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_