Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / device_permissions_manager.h
blob12d7b039fe8eb4ec884973bc56b402f43e5019aa
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_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/scoped_observer.h"
16 #include "base/strings/string16.h"
17 #include "base/threading/thread_checker.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "device/usb/usb_device.h"
21 #include "extensions/browser/process_manager.h"
22 #include "extensions/browser/process_manager_observer.h"
24 template <typename T>
25 struct DefaultSingletonTraits;
27 namespace base {
28 class Value;
31 namespace content {
32 class BrowserContext;
35 namespace device {
36 class UsbDevice;
39 namespace extensions {
41 // Stores information about a device saved with access granted.
42 struct DevicePermissionEntry {
43 DevicePermissionEntry(uint16_t vendor_id,
44 uint16_t product_id,
45 const base::string16& serial_number,
46 const base::string16& manufacturer_string,
47 const base::string16& product_string);
49 base::Value* ToValue() const;
51 // The vendor ID of this device.
52 uint16_t vendor_id;
54 // The product ID of this device.
55 uint16_t product_id;
57 // The serial number (possibly alphanumeric) of this device.
58 base::string16 serial_number;
60 // The manufacturer string read from the device (optional).
61 base::string16 manufacturer_string;
63 // The product string read from the device (optional).
64 base::string16 product_string;
67 // Stores a copy of device permissions associated with a particular extension.
68 class DevicePermissions {
69 public:
70 virtual ~DevicePermissions();
72 bool CheckUsbDevice(scoped_refptr<device::UsbDevice> device) const;
74 private:
75 friend class DevicePermissionsManager;
77 DevicePermissions(content::BrowserContext* context,
78 const std::string& extension_id);
79 DevicePermissions(
80 const std::vector<DevicePermissionEntry>& permission_entries,
81 const std::set<scoped_refptr<device::UsbDevice>>& ephemeral_devices);
83 std::vector<DevicePermissionEntry>& permission_entries();
84 std::set<scoped_refptr<device::UsbDevice>>& ephemeral_devices();
86 std::vector<DevicePermissionEntry> permission_entries_;
87 std::set<scoped_refptr<device::UsbDevice>> ephemeral_devices_;
89 DISALLOW_COPY_AND_ASSIGN(DevicePermissions);
92 // Manages saved device permissions for all extensions.
93 class DevicePermissionsManager : public KeyedService,
94 public base::NonThreadSafe,
95 public ProcessManagerObserver,
96 public device::UsbDevice::Observer {
97 public:
98 static DevicePermissionsManager* Get(content::BrowserContext* context);
100 // Returns a copy of the DevicePermissions object for a given extension that
101 // can be used by any thread.
102 scoped_ptr<DevicePermissions> GetForExtension(
103 const std::string& extension_id);
105 std::vector<base::string16> GetPermissionMessageStrings(
106 const std::string& extension_id);
108 // TODO(reillyg): AllowUsbDevice should only take the extension ID and
109 // device, with the strings read from the device. This isn't possible now as
110 // the device can not be accessed from the UI thread yet. crbug.com/427985
111 void AllowUsbDevice(const std::string& extension_id,
112 scoped_refptr<device::UsbDevice> device,
113 const base::string16& serial_number,
114 const base::string16& manufacturer_string,
115 const base::string16& product_string);
117 void Clear(const std::string& extension_id);
119 private:
120 friend class DevicePermissionsManagerFactory;
122 DevicePermissionsManager(content::BrowserContext* context);
123 ~DevicePermissionsManager() override;
125 DevicePermissions* Get(const std::string& extension_id) const;
126 DevicePermissions* GetOrInsert(const std::string& extension_id);
128 // ProcessManagerObserver implementation
129 void OnBackgroundHostClose(const std::string& extension_id) override;
131 // device::UsbDevice::Observer implementation
132 void OnDisconnect(scoped_refptr<device::UsbDevice> device) override;
134 content::BrowserContext* context_;
135 std::map<std::string, DevicePermissions*> extension_id_to_device_permissions_;
136 ScopedObserver<ProcessManager, ProcessManagerObserver>
137 process_manager_observer_;
139 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager);
142 class DevicePermissionsManagerFactory
143 : public BrowserContextKeyedServiceFactory {
144 public:
145 static DevicePermissionsManager* GetForBrowserContext(
146 content::BrowserContext* context);
147 static DevicePermissionsManagerFactory* GetInstance();
149 private:
150 friend struct DefaultSingletonTraits<DevicePermissionsManagerFactory>;
152 DevicePermissionsManagerFactory();
153 ~DevicePermissionsManagerFactory() override;
155 // BrowserContextKeyedServiceFactory implementation
156 KeyedService* BuildServiceInstanceFor(
157 content::BrowserContext* context) const override;
158 content::BrowserContext* GetBrowserContextToUse(
159 content::BrowserContext* context) const override;
161 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory);
164 } // namespace extensions
166 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_