| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ | 5 #ifndef EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ |
| 6 #define EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ | 6 #define EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 namespace content { | 33 namespace content { |
| 34 class BrowserContext; | 34 class BrowserContext; |
| 35 } | 35 } |
| 36 | 36 |
| 37 namespace extensions { | 37 namespace extensions { |
| 38 | 38 |
| 39 // Stores information about a device saved with access granted. | 39 // Stores information about a device saved with access granted. |
| 40 class DevicePermissionEntry | 40 class DevicePermissionEntry |
| 41 : public base::RefCountedThreadSafe<DevicePermissionEntry> { | 41 : public base::RefCountedThreadSafe<DevicePermissionEntry> { |
| 42 public: | 42 public: |
| 43 // TODO(reillyg): This function should be able to take only the | 43 DevicePermissionEntry(scoped_refptr<device::UsbDevice> device); |
| 44 // device::UsbDevice and read the strings from there. This is not yet possible | |
| 45 // as the device can not be accessed from the UI thread. crbug.com/427985 | |
| 46 DevicePermissionEntry(scoped_refptr<device::UsbDevice> device, | |
| 47 const base::string16& serial_number, | |
| 48 const base::string16& manufacturer_string, | |
| 49 const base::string16& product_string); | |
| 50 DevicePermissionEntry(uint16_t vendor_id, | 44 DevicePermissionEntry(uint16_t vendor_id, |
| 51 uint16_t product_id, | 45 uint16_t product_id, |
| 52 const base::string16& serial_number, | 46 const base::string16& serial_number, |
| 53 const base::string16& manufacturer_string, | 47 const base::string16& manufacturer_string, |
| 54 const base::string16& product_string, | 48 const base::string16& product_string, |
| 55 const base::Time& last_used); | 49 const base::Time& last_used); |
| 56 | 50 |
| 57 // A persistent device is one that can be recognized when it is reconnected | 51 // A persistent device is one that can be recognized when it is reconnected |
| 58 // and can therefore be remembered persistently by writing information about | 52 // and can therefore be remembered persistently by writing information about |
| 59 // it to ExtensionPrefs. Currently this means it has a serial number string. | 53 // it to ExtensionPrefs. Currently this means it has a serial number string. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // The serial number (possibly alphanumeric) of this device. | 85 // The serial number (possibly alphanumeric) of this device. |
| 92 base::string16 serial_number_; | 86 base::string16 serial_number_; |
| 93 // The manufacturer string read from the device (optional). | 87 // The manufacturer string read from the device (optional). |
| 94 base::string16 manufacturer_string_; | 88 base::string16 manufacturer_string_; |
| 95 // The product string read from the device (optional). | 89 // The product string read from the device (optional). |
| 96 base::string16 product_string_; | 90 base::string16 product_string_; |
| 97 // The last time this device was used by the extension. | 91 // The last time this device was used by the extension. |
| 98 base::Time last_used_; | 92 base::Time last_used_; |
| 99 }; | 93 }; |
| 100 | 94 |
| 101 // Stores a copy of device permissions associated with a particular extension. | 95 // Stores device permissions associated with a particular extension. |
| 102 class DevicePermissions { | 96 class DevicePermissions { |
| 103 public: | 97 public: |
| 104 virtual ~DevicePermissions(); | 98 virtual ~DevicePermissions(); |
| 105 | 99 |
| 106 // Attempts to find a permission entry matching the given device. The device | 100 // Attempts to find a permission entry matching the given device. The device |
| 107 // serial number is presented separately so that this function does not need | 101 // serial number is presented separately so that this function does not need |
| 108 // to call device->GetSerialNumber() which may not be possible on the | 102 // to call device->GetSerialNumber() which may not be possible on the |
| 109 // current thread. | 103 // current thread. |
| 110 scoped_refptr<DevicePermissionEntry> FindEntry( | 104 scoped_refptr<DevicePermissionEntry> FindEntry( |
| 111 scoped_refptr<device::UsbDevice> device, | 105 scoped_refptr<device::UsbDevice> device) const; |
| 112 const base::string16& serial_number) const; | |
| 113 | 106 |
| 114 const std::set<scoped_refptr<DevicePermissionEntry>>& entries() const { | 107 const std::set<scoped_refptr<DevicePermissionEntry>>& entries() const { |
| 115 return entries_; | 108 return entries_; |
| 116 } | 109 } |
| 117 | 110 |
| 118 private: | 111 private: |
| 119 friend class DevicePermissionsManager; | 112 friend class DevicePermissionsManager; |
| 120 | 113 |
| 121 // Reads permissions out of ExtensionPrefs. | 114 // Reads permissions out of ExtensionPrefs. |
| 122 DevicePermissions(content::BrowserContext* context, | 115 DevicePermissions(content::BrowserContext* context, |
| 123 const std::string& extension_id); | 116 const std::string& extension_id); |
| 124 // Does a shallow copy, duplicating the device lists so that the resulting | |
| 125 // object can be used from a different thread. | |
| 126 DevicePermissions(const DevicePermissions* original); | |
| 127 | 117 |
| 128 std::set<scoped_refptr<DevicePermissionEntry>> entries_; | 118 std::set<scoped_refptr<DevicePermissionEntry>> entries_; |
| 129 std::map<scoped_refptr<device::UsbDevice>, | 119 std::map<scoped_refptr<device::UsbDevice>, |
| 130 scoped_refptr<DevicePermissionEntry>> ephemeral_devices_; | 120 scoped_refptr<DevicePermissionEntry>> ephemeral_devices_; |
| 131 | 121 |
| 132 DISALLOW_COPY_AND_ASSIGN(DevicePermissions); | 122 DISALLOW_COPY_AND_ASSIGN(DevicePermissions); |
| 133 }; | 123 }; |
| 134 | 124 |
| 135 // Manages saved device permissions for all extensions. | 125 // Manages saved device permissions for all extensions. |
| 136 class DevicePermissionsManager : public KeyedService, | 126 class DevicePermissionsManager : public KeyedService, |
| 137 public base::NonThreadSafe, | 127 public base::NonThreadSafe, |
| 138 public ProcessManagerObserver { | 128 public ProcessManagerObserver, |
| 129 public device::UsbService::Observer { |
| 139 public: | 130 public: |
| 140 static DevicePermissionsManager* Get(content::BrowserContext* context); | 131 static DevicePermissionsManager* Get(content::BrowserContext* context); |
| 141 | 132 |
| 142 // Returns a copy of the DevicePermissions object for a given extension that | 133 // The DevicePermissions object for a given extension. |
| 143 // can be used by any thread. | 134 DevicePermissions* GetForExtension(const std::string& extension_id); |
| 144 scoped_ptr<DevicePermissions> GetForExtension( | |
| 145 const std::string& extension_id); | |
| 146 | 135 |
| 147 // Equivalent to calling GetForExtension and extracting the permission string | 136 // Equivalent to calling GetForExtension and extracting the permission string |
| 148 // for each entry. | 137 // for each entry. |
| 149 std::vector<base::string16> GetPermissionMessageStrings( | 138 std::vector<base::string16> GetPermissionMessageStrings( |
| 150 const std::string& extension_id) const; | 139 const std::string& extension_id) const; |
| 151 | 140 |
| 152 // TODO(reillyg): AllowUsbDevice should only take the extension ID and | |
| 153 // device, with the strings read from the device. This isn't possible now as | |
| 154 // the device can not be accessed from the UI thread yet. crbug.com/427985 | |
| 155 void AllowUsbDevice(const std::string& extension_id, | 141 void AllowUsbDevice(const std::string& extension_id, |
| 156 scoped_refptr<device::UsbDevice> device, | 142 scoped_refptr<device::UsbDevice> device); |
| 157 const base::string16& serial_number, | |
| 158 const base::string16& manufacturer_string, | |
| 159 const base::string16& product_string); | |
| 160 | 143 |
| 161 // Updates the "last used" timestamp on the given device entry and writes it | 144 // Updates the "last used" timestamp on the given device entry and writes it |
| 162 // out to ExtensionPrefs. | 145 // out to ExtensionPrefs. |
| 163 void UpdateLastUsed(const std::string& extension_id, | 146 void UpdateLastUsed(const std::string& extension_id, |
| 164 scoped_refptr<DevicePermissionEntry> entry); | 147 scoped_refptr<DevicePermissionEntry> entry); |
| 165 | 148 |
| 166 // Revokes permission for the extension to access the given device. | 149 // Revokes permission for the extension to access the given device. |
| 167 void RemoveEntry(const std::string& extension_id, | 150 void RemoveEntry(const std::string& extension_id, |
| 168 scoped_refptr<DevicePermissionEntry> entry); | 151 scoped_refptr<DevicePermissionEntry> entry); |
| 169 | 152 |
| 170 // Revokes permission for the extension to access all allowed devices. | 153 // Revokes permission for the extension to access all allowed devices. |
| 171 void Clear(const std::string& extension_id); | 154 void Clear(const std::string& extension_id); |
| 172 | 155 |
| 173 private: | 156 private: |
| 174 class FileThreadHelper; | |
| 175 | 157 |
| 176 friend class DevicePermissionsManagerFactory; | 158 friend class DevicePermissionsManagerFactory; |
| 177 FRIEND_TEST_ALL_PREFIXES(DevicePermissionsManagerTest, SuspendExtension); | 159 FRIEND_TEST_ALL_PREFIXES(DevicePermissionsManagerTest, SuspendExtension); |
| 178 | 160 |
| 179 DevicePermissionsManager(content::BrowserContext* context); | 161 DevicePermissionsManager(content::BrowserContext* context); |
| 180 ~DevicePermissionsManager() override; | 162 ~DevicePermissionsManager() override; |
| 181 | 163 |
| 182 DevicePermissions* Get(const std::string& extension_id) const; | 164 DevicePermissions* Get(const std::string& extension_id) const; |
| 183 DevicePermissions* GetOrInsert(const std::string& extension_id); | |
| 184 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device); | |
| 185 | 165 |
| 186 // ProcessManagerObserver implementation | 166 // ProcessManagerObserver implementation |
| 187 void OnBackgroundHostClose(const std::string& extension_id) override; | 167 void OnBackgroundHostClose(const std::string& extension_id) override; |
| 188 | 168 |
| 169 // UsbService::Observer implementation |
| 170 void OnDeviceRemovedCleanup(scoped_refptr<device::UsbDevice> device) override; |
| 171 |
| 189 content::BrowserContext* context_; | 172 content::BrowserContext* context_; |
| 190 std::map<std::string, DevicePermissions*> extension_id_to_device_permissions_; | 173 std::map<std::string, DevicePermissions*> extension_id_to_device_permissions_; |
| 191 ScopedObserver<ProcessManager, ProcessManagerObserver> | 174 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 192 process_manager_observer_; | 175 process_manager_observer_; |
| 193 FileThreadHelper* helper_; | 176 ScopedObserver<device::UsbService, device::UsbService::Observer> |
| 194 | 177 usb_service_observer_; |
| 195 base::WeakPtrFactory<DevicePermissionsManager> weak_factory_; | |
| 196 | 178 |
| 197 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager); | 179 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager); |
| 198 }; | 180 }; |
| 199 | 181 |
| 200 class DevicePermissionsManagerFactory | 182 class DevicePermissionsManagerFactory |
| 201 : public BrowserContextKeyedServiceFactory { | 183 : public BrowserContextKeyedServiceFactory { |
| 202 public: | 184 public: |
| 203 static DevicePermissionsManager* GetForBrowserContext( | 185 static DevicePermissionsManager* GetForBrowserContext( |
| 204 content::BrowserContext* context); | 186 content::BrowserContext* context); |
| 205 static DevicePermissionsManagerFactory* GetInstance(); | 187 static DevicePermissionsManagerFactory* GetInstance(); |
| 206 | 188 |
| 207 private: | 189 private: |
| 208 friend struct DefaultSingletonTraits<DevicePermissionsManagerFactory>; | 190 friend struct DefaultSingletonTraits<DevicePermissionsManagerFactory>; |
| 209 | 191 |
| 210 DevicePermissionsManagerFactory(); | 192 DevicePermissionsManagerFactory(); |
| 211 ~DevicePermissionsManagerFactory() override; | 193 ~DevicePermissionsManagerFactory() override; |
| 212 | 194 |
| 213 // BrowserContextKeyedServiceFactory implementation | 195 // BrowserContextKeyedServiceFactory implementation |
| 214 KeyedService* BuildServiceInstanceFor( | 196 KeyedService* BuildServiceInstanceFor( |
| 215 content::BrowserContext* context) const override; | 197 content::BrowserContext* context) const override; |
| 216 content::BrowserContext* GetBrowserContextToUse( | 198 content::BrowserContext* GetBrowserContextToUse( |
| 217 content::BrowserContext* context) const override; | 199 content::BrowserContext* context) const override; |
| 218 | 200 |
| 219 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory); | 201 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory); |
| 220 }; | 202 }; |
| 221 | 203 |
| 222 } // namespace extensions | 204 } // namespace extensions |
| 223 | 205 |
| 224 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ | 206 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ |
| OLD | NEW |