| 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> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/scoped_observer.h" | 16 #include "base/scoped_observer.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" | 19 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "device/usb/usb_service.h" | 22 #include "device/usb/usb_service.h" |
| 22 #include "extensions/browser/process_manager.h" | 23 #include "extensions/browser/process_manager.h" |
| 23 #include "extensions/browser/process_manager_observer.h" | 24 #include "extensions/browser/process_manager_observer.h" |
| 24 | 25 |
| 25 template <typename T> | 26 template <typename T> |
| 26 struct DefaultSingletonTraits; | 27 struct DefaultSingletonTraits; |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class Value; | 30 class Value; |
| 30 } | 31 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 std::set<scoped_refptr<DevicePermissionEntry>> entries_; | 128 std::set<scoped_refptr<DevicePermissionEntry>> entries_; |
| 128 std::map<scoped_refptr<device::UsbDevice>, | 129 std::map<scoped_refptr<device::UsbDevice>, |
| 129 scoped_refptr<DevicePermissionEntry>> ephemeral_devices_; | 130 scoped_refptr<DevicePermissionEntry>> ephemeral_devices_; |
| 130 | 131 |
| 131 DISALLOW_COPY_AND_ASSIGN(DevicePermissions); | 132 DISALLOW_COPY_AND_ASSIGN(DevicePermissions); |
| 132 }; | 133 }; |
| 133 | 134 |
| 134 // Manages saved device permissions for all extensions. | 135 // Manages saved device permissions for all extensions. |
| 135 class DevicePermissionsManager : public KeyedService, | 136 class DevicePermissionsManager : public KeyedService, |
| 136 public base::NonThreadSafe, | 137 public base::NonThreadSafe, |
| 137 public ProcessManagerObserver, | 138 public ProcessManagerObserver { |
| 138 public device::UsbService::Observer { | |
| 139 public: | 139 public: |
| 140 static DevicePermissionsManager* Get(content::BrowserContext* context); | 140 static DevicePermissionsManager* Get(content::BrowserContext* context); |
| 141 | 141 |
| 142 // Returns a copy of the DevicePermissions object for a given extension that | 142 // Returns a copy of the DevicePermissions object for a given extension that |
| 143 // can be used by any thread. | 143 // can be used by any thread. |
| 144 scoped_ptr<DevicePermissions> GetForExtension( | 144 scoped_ptr<DevicePermissions> GetForExtension( |
| 145 const std::string& extension_id); | 145 const std::string& extension_id); |
| 146 | 146 |
| 147 // Equivalent to calling GetForExtension and extracting the permission string | 147 // Equivalent to calling GetForExtension and extracting the permission string |
| 148 // for each entry. | 148 // for each entry. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 scoped_refptr<DevicePermissionEntry> entry); | 164 scoped_refptr<DevicePermissionEntry> entry); |
| 165 | 165 |
| 166 // Revokes permission for the extension to access the given device. | 166 // Revokes permission for the extension to access the given device. |
| 167 void RemoveEntry(const std::string& extension_id, | 167 void RemoveEntry(const std::string& extension_id, |
| 168 scoped_refptr<DevicePermissionEntry> entry); | 168 scoped_refptr<DevicePermissionEntry> entry); |
| 169 | 169 |
| 170 // Revokes permission for the extension to access all allowed devices. | 170 // Revokes permission for the extension to access all allowed devices. |
| 171 void Clear(const std::string& extension_id); | 171 void Clear(const std::string& extension_id); |
| 172 | 172 |
| 173 private: | 173 private: |
| 174 class FileThreadHelper; |
| 175 |
| 174 friend class DevicePermissionsManagerFactory; | 176 friend class DevicePermissionsManagerFactory; |
| 175 FRIEND_TEST_ALL_PREFIXES(DevicePermissionsManagerTest, SuspendExtension); | 177 FRIEND_TEST_ALL_PREFIXES(DevicePermissionsManagerTest, SuspendExtension); |
| 176 | 178 |
| 177 DevicePermissionsManager(content::BrowserContext* context); | 179 DevicePermissionsManager(content::BrowserContext* context); |
| 178 ~DevicePermissionsManager() override; | 180 ~DevicePermissionsManager() override; |
| 179 | 181 |
| 180 DevicePermissions* Get(const std::string& extension_id) const; | 182 DevicePermissions* Get(const std::string& extension_id) const; |
| 181 DevicePermissions* GetOrInsert(const std::string& extension_id); | 183 DevicePermissions* GetOrInsert(const std::string& extension_id); |
| 184 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device); |
| 182 | 185 |
| 183 // ProcessManagerObserver implementation | 186 // ProcessManagerObserver implementation |
| 184 void OnBackgroundHostClose(const std::string& extension_id) override; | 187 void OnBackgroundHostClose(const std::string& extension_id) override; |
| 185 | 188 |
| 186 // device::UsbService::Observer implementation | |
| 187 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; | |
| 188 | |
| 189 content::BrowserContext* context_; | 189 content::BrowserContext* context_; |
| 190 std::map<std::string, DevicePermissions*> extension_id_to_device_permissions_; | 190 std::map<std::string, DevicePermissions*> extension_id_to_device_permissions_; |
| 191 ScopedObserver<ProcessManager, ProcessManagerObserver> | 191 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 192 process_manager_observer_; | 192 process_manager_observer_; |
| 193 ScopedObserver<device::UsbService, device::UsbService::Observer> | 193 FileThreadHelper* helper_; |
| 194 usb_service_observer_; | 194 |
| 195 base::WeakPtrFactory<DevicePermissionsManager> weak_factory_; |
| 195 | 196 |
| 196 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager); | 197 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManager); |
| 197 }; | 198 }; |
| 198 | 199 |
| 199 class DevicePermissionsManagerFactory | 200 class DevicePermissionsManagerFactory |
| 200 : public BrowserContextKeyedServiceFactory { | 201 : public BrowserContextKeyedServiceFactory { |
| 201 public: | 202 public: |
| 202 static DevicePermissionsManager* GetForBrowserContext( | 203 static DevicePermissionsManager* GetForBrowserContext( |
| 203 content::BrowserContext* context); | 204 content::BrowserContext* context); |
| 204 static DevicePermissionsManagerFactory* GetInstance(); | 205 static DevicePermissionsManagerFactory* GetInstance(); |
| 205 | 206 |
| 206 private: | 207 private: |
| 207 friend struct DefaultSingletonTraits<DevicePermissionsManagerFactory>; | 208 friend struct DefaultSingletonTraits<DevicePermissionsManagerFactory>; |
| 208 | 209 |
| 209 DevicePermissionsManagerFactory(); | 210 DevicePermissionsManagerFactory(); |
| 210 ~DevicePermissionsManagerFactory() override; | 211 ~DevicePermissionsManagerFactory() override; |
| 211 | 212 |
| 212 // BrowserContextKeyedServiceFactory implementation | 213 // BrowserContextKeyedServiceFactory implementation |
| 213 KeyedService* BuildServiceInstanceFor( | 214 KeyedService* BuildServiceInstanceFor( |
| 214 content::BrowserContext* context) const override; | 215 content::BrowserContext* context) const override; |
| 215 content::BrowserContext* GetBrowserContextToUse( | 216 content::BrowserContext* GetBrowserContextToUse( |
| 216 content::BrowserContext* context) const override; | 217 content::BrowserContext* context) const override; |
| 217 | 218 |
| 218 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory); | 219 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsManagerFactory); |
| 219 }; | 220 }; |
| 220 | 221 |
| 221 } // namespace extensions | 222 } // namespace extensions |
| 222 | 223 |
| 223 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ | 224 #endif // EXTENSIONS_DEVICE_PERMISSION_MANAGER_H_ |
| OLD | NEW |