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_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"
22 class HidDeviceFilter
;
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 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
> {
44 // This class stores the device information displayed in the UI. It should
45 // be extended to support particular device types.
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; }
58 base::string16 serial_number_
;
61 bool granted_
= false;
64 // Since the set of devices can change while the UI is visible an
65 // implementation should register an observer.
68 virtual void OnDevicesChanged() = 0;
74 Prompt(const Extension
* extension
,
75 content::BrowserContext
* context
,
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_
; }
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_
;
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
,
128 const std::vector
<device::UsbDeviceFilter
>& filters
,
129 const UsbDevicesCallback
& callback
);
131 void AskForHidDevices(const Extension
* extension
,
132 content::BrowserContext
* context
,
134 const std::vector
<device::HidDeviceFilter
>& filters
,
135 const HidDevicesCallback
& callback
);
137 static scoped_refptr
<Prompt
> CreateHidPromptForTest(
138 const Extension
* extension
,
140 static scoped_refptr
<Prompt
> CreateUsbPromptForTest(
141 const Extension
* extension
,
145 virtual void ShowDialog() = 0;
147 content::WebContents
* web_contents() { return web_contents_
; }
148 scoped_refptr
<Prompt
> prompt() { return prompt_
; }
151 // Parent web contents of the device permissions UI dialog.
152 content::WebContents
* web_contents_
;
154 // Parameters available to the UI implementation.
155 scoped_refptr
<Prompt
> prompt_
;
157 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsPrompt
);
160 } // namespace extensions
162 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_