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_
10 #include "base/callback.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/scoped_observer.h"
14 #include "base/strings/string16.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "device/usb/usb_service.h"
25 class UsbDeviceFilter
;
28 namespace extensions
{
32 // Platform-independent interface for displaing a UI for choosing devices
33 // (similar to choosing files).
34 class DevicePermissionsPrompt
{
36 // Context information available to the UI implementation.
37 class Prompt
: public base::RefCountedThreadSafe
<
39 content::BrowserThread::DeleteOnFileThread
>,
40 public device::UsbService::Observer
{
42 // Displayed properties of a device.
44 DeviceInfo(scoped_refptr
<device::UsbDevice
> device
);
47 scoped_refptr
<device::UsbDevice
> device
;
49 base::string16 original_manufacturer_string
;
50 base::string16 original_product_string
;
51 base::string16 serial_number
;
54 // Since the set of devices can change while the UI is visible an
55 // implementation should register an observer.
58 virtual void OnDevicesChanged() = 0;
63 // Only one observer may be registered at a time.
64 void SetObserver(Observer
* observer
);
66 base::string16
GetHeading() const;
67 base::string16
GetPromptMessage() const;
68 size_t GetDeviceCount() const { return devices_
.size(); }
69 scoped_refptr
<device::UsbDevice
> GetDevice(size_t index
) const;
70 base::string16
GetDeviceName(size_t index
) const {
71 DCHECK_LT(index
, devices_
.size());
72 return devices_
[index
].name
;
74 base::string16
GetDeviceSerialNumber(size_t index
) const {
75 DCHECK_LT(index
, devices_
.size());
76 return devices_
[index
].serial_number
;
79 // Notifies the DevicePermissionsManager for the current extension that
80 // access to the device at the given index is now granted.
81 void GrantDevicePermission(size_t index
) const;
83 const extensions::Extension
* extension() const { return extension_
; }
84 void set_extension(const extensions::Extension
* extension
) {
85 extension_
= extension
;
88 void set_browser_context(content::BrowserContext
* context
) {
89 browser_context_
= context
;
92 bool multiple() const { return multiple_
; }
93 void set_multiple(bool multiple
) { multiple_
= multiple
; }
95 const std::vector
<device::UsbDeviceFilter
>& filters() const {
98 void set_filters(const std::vector
<device::UsbDeviceFilter
>& filters
);
101 friend struct content::BrowserThread::DeleteOnThread
<
102 content::BrowserThread::FILE>;
103 friend class base::DeleteHelper
<Prompt
>;
107 // Querying for devices must be done asynchronously on the FILE thread.
108 void DoDeviceQuery();
109 void SetDevices(const std::vector
<DeviceInfo
>& devices
);
110 void AddDevice(const DeviceInfo
& device
);
111 void RemoveDevice(scoped_refptr
<device::UsbDevice
> device
);
113 // device::UsbService::Observer implementation:
114 void OnDeviceAdded(scoped_refptr
<device::UsbDevice
> device
) override
;
115 void OnDeviceRemoved(scoped_refptr
<device::UsbDevice
> device
) override
;
117 const extensions::Extension
* extension_
;
118 content::BrowserContext
* browser_context_
;
120 std::vector
<device::UsbDeviceFilter
> filters_
;
121 std::vector
<DeviceInfo
> devices_
;
123 ScopedObserver
<device::UsbService
, device::UsbService::Observer
>
124 usb_service_observer_
;
129 // Called with the list of selected USB devices.
130 virtual void OnUsbDevicesChosen(
131 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
) = 0;
134 virtual ~Delegate() {}
137 DevicePermissionsPrompt(content::WebContents
* web_contents
);
138 virtual ~DevicePermissionsPrompt();
140 void AskForUsbDevices(Delegate
* delegate
,
141 const Extension
* extension
,
142 content::BrowserContext
* context
,
144 const std::vector
<device::UsbDeviceFilter
>& filters
);
147 virtual void ShowDialog() = 0;
149 content::WebContents
* web_contents() { return web_contents_
; }
150 Delegate
* delegate() { return delegate_
; }
151 scoped_refptr
<Prompt
> prompt() { return prompt_
; }
154 // Parent web contents of the device permissions UI dialog.
155 content::WebContents
* web_contents_
;
157 // The delegate called after the UI has been dismissed.
160 // Parameters available to the UI implementation.
161 scoped_refptr
<Prompt
> prompt_
;
164 } // namespace extensions
166 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_