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/strings/string16.h"
22 class UsbDeviceFilter
;
25 namespace extensions
{
29 // Platform-independent interface for displaing a UI for choosing devices
30 // (similar to choosing files).
31 class DevicePermissionsPrompt
{
33 // Context information available to the UI implementation.
34 class Prompt
: public base::RefCountedThreadSafe
<Prompt
> {
36 // Displayed properties of a device.
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
);
45 scoped_refptr
<device::UsbDevice
> device
;
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.
56 virtual void OnDevicesChanged() = 0;
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 {
96 void set_filters(const std::vector
<device::UsbDeviceFilter
>& filters
);
99 friend class base::RefCountedThreadSafe
<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_
;
110 std::vector
<device::UsbDeviceFilter
> filters_
;
111 std::vector
<DeviceInfo
> devices_
;
117 // Called with the list of selected USB devices.
118 virtual void OnUsbDevicesChosen(
119 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
) = 0;
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
,
132 const std::vector
<device::UsbDeviceFilter
>& filters
);
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_
; }
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.
148 // Parameters available to the UI implementation.
149 scoped_refptr
<Prompt
> prompt_
;
152 } // namespace extensions
154 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_