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_DEVICE_PERMISSION_MANAGER_H_
6 #define EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/scoped_observer.h"
17 #include "base/strings/string16.h"
18 #include "base/threading/thread_checker.h"
19 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "device/hid/hid_service.h"
22 #include "device/usb/usb_service.h"
25 struct DefaultSingletonTraits
;
35 namespace extensions
{
37 // Stores information about a device saved with access granted.
38 class DevicePermissionEntry
: public base::RefCounted
<DevicePermissionEntry
> {
45 DevicePermissionEntry(scoped_refptr
<device::UsbDevice
> device
);
46 DevicePermissionEntry(scoped_refptr
<device::HidDeviceInfo
> device
);
47 DevicePermissionEntry(Type type
,
50 const base::string16
& serial_number
,
51 const base::string16
& manufacturer_string
,
52 const base::string16
& product_string
,
53 const base::Time
& last_used
);
55 // A persistent device is one that can be recognized when it is reconnected
56 // and can therefore be remembered persistently by writing information about
57 // it to ExtensionPrefs. Currently this means it has a serial number string.
58 bool IsPersistent() const;
60 // Convert the device to a serializable value, returns a null pointer if the
61 // entry is not persistent.
62 scoped_ptr
<base::Value
> ToValue() const;
64 base::string16
GetPermissionMessageString() const;
66 Type
type() const { return type_
; }
67 uint16_t vendor_id() const { return vendor_id_
; }
68 uint16_t product_id() const { return product_id_
; }
69 const base::string16
& serial_number() const { return serial_number_
; }
70 const base::Time
& last_used() const { return last_used_
; }
72 base::string16
GetManufacturer() const;
73 base::string16
GetProduct() const;
76 friend class base::RefCounted
<DevicePermissionEntry
>;
77 friend class DevicePermissionsManager
;
79 ~DevicePermissionEntry();
81 void set_last_used(const base::Time
& last_used
) { last_used_
= last_used
; }
83 // The USB device tracked by this entry. Will be nullptr if this entry was
84 // restored from ExtensionPrefs or type_ is not Type::USB.
85 scoped_refptr
<device::UsbDevice
> usb_device_
;
86 // The HID device tracked by this entry. Will be nullptr if this entry was
87 // restored from ExtensionPrefs or type_ is not Type::HID.
88 scoped_refptr
<device::HidDeviceInfo
> hid_device_
;
90 // The type of device this entry represents.
92 // The vendor ID of this device.
94 // The product ID of this device.
96 // The serial number (possibly alphanumeric) of this device.
97 base::string16 serial_number_
;
98 // The manufacturer string read from the device (optional).
99 base::string16 manufacturer_string_
;
100 // The product string read from the device (optional).
101 base::string16 product_string_
;
102 // The last time this device was used by the extension.
103 base::Time last_used_
;
106 // Stores device permissions associated with a particular extension.
107 class DevicePermissions
{
109 virtual ~DevicePermissions();
111 // Attempts to find a permission entry matching the given device.
112 scoped_refptr
<DevicePermissionEntry
> FindUsbDeviceEntry(
113 scoped_refptr
<device::UsbDevice
> device
) const;
114 scoped_refptr
<DevicePermissionEntry
> FindHidDeviceEntry(
115 scoped_refptr
<device::HidDeviceInfo
> device
) const;
117 const std::set
<scoped_refptr
<DevicePermissionEntry
>>& entries() const {
122 friend class DevicePermissionsManager
;
124 // Reads permissions out of ExtensionPrefs.
125 DevicePermissions(content::BrowserContext
* context
,
126 const std::string
& extension_id
);
128 std::set
<scoped_refptr
<DevicePermissionEntry
>> entries_
;
129 std::map
<device::UsbDevice
*, scoped_refptr
<DevicePermissionEntry
>>
130 ephemeral_usb_devices_
;
131 std::map
<device::HidDeviceInfo
*, scoped_refptr
<DevicePermissionEntry
>>
132 ephemeral_hid_devices_
;
134 DISALLOW_COPY_AND_ASSIGN(DevicePermissions
);
137 // Manages saved device permissions for all extensions.
138 class DevicePermissionsManager
: public KeyedService
,
139 public device::UsbService::Observer
,
140 public device::HidService::Observer
{
142 static DevicePermissionsManager
* Get(content::BrowserContext
* context
);
144 static base::string16
GetPermissionMessage(
147 const base::string16
& manufacturer_string
,
148 const base::string16
& product_string
,
149 const base::string16
& serial_number
,
150 bool always_include_manufacturer
);
152 // The DevicePermissions object for a given extension.
153 DevicePermissions
* GetForExtension(const std::string
& extension_id
);
155 // Equivalent to calling GetForExtension and extracting the permission string
157 std::vector
<base::string16
> GetPermissionMessageStrings(
158 const std::string
& extension_id
) const;
160 void AllowUsbDevice(const std::string
& extension_id
,
161 scoped_refptr
<device::UsbDevice
> device
);
162 void AllowHidDevice(const std::string
& extension_id
,
163 scoped_refptr
<device::HidDeviceInfo
> device
);
165 // Updates the "last used" timestamp on the given device entry and writes it
166 // out to ExtensionPrefs.
167 void UpdateLastUsed(const std::string
& extension_id
,
168 scoped_refptr
<DevicePermissionEntry
> entry
);
170 // Revokes permission for the extension to access the given device.
171 void RemoveEntry(const std::string
& extension_id
,
172 scoped_refptr
<DevicePermissionEntry
> entry
);
174 // Revokes permission for the extension to access all allowed devices.
175 void Clear(const std::string
& extension_id
);
179 friend class DevicePermissionsManagerFactory
;
180 FRIEND_TEST_ALL_PREFIXES(DevicePermissionsManagerTest
, SuspendExtension
);
182 DevicePermissionsManager(content::BrowserContext
* context
);
183 ~DevicePermissionsManager() override
;
185 DevicePermissions
* GetInternal(const std::string
& extension_id
) const;
187 // UsbService::Observer implementation
188 void OnDeviceRemovedCleanup(scoped_refptr
<device::UsbDevice
> device
) override
;
190 // HidService::Observer implementation
191 void OnDeviceRemovedCleanup(
192 scoped_refptr
<device::HidDeviceInfo
> device
) override
;
194 base::ThreadChecker thread_checker_
;
195 content::BrowserContext
* context_
;
196 std::map
<std::string
, DevicePermissions
*> extension_id_to_device_permissions_
;
197 ScopedObserver
<device::UsbService
, device::UsbService::Observer
>
198 usb_service_observer_
;
199 ScopedObserver
<device::HidService
, device::HidService::Observer
>
200 hid_service_observer_
;
202 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager
);
205 class DevicePermissionsManagerFactory
206 : public BrowserContextKeyedServiceFactory
{
208 static DevicePermissionsManager
* GetForBrowserContext(
209 content::BrowserContext
* context
);
210 static DevicePermissionsManagerFactory
* GetInstance();
213 friend struct DefaultSingletonTraits
<DevicePermissionsManagerFactory
>;
215 DevicePermissionsManagerFactory();
216 ~DevicePermissionsManagerFactory() override
;
218 // BrowserContextKeyedServiceFactory implementation
219 KeyedService
* BuildServiceInstanceFor(
220 content::BrowserContext
* context
) const override
;
221 content::BrowserContext
* GetBrowserContextToUse(
222 content::BrowserContext
* context
) const override
;
224 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory
);
227 } // namespace extensions
229 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_