Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #ifndef APPS_SAVED_DEVICES_SERVICE_H_ | |
| 6 #define APPS_SAVED_DEVICES_SERVICE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/strings/string16.h" | |
| 17 #include "base/threading/thread_checker.h" | |
| 18 #include "components/keyed_service/core/keyed_service.h" | |
| 19 #include "content/public/browser/notification_observer.h" | |
| 20 #include "content/public/browser/notification_registrar.h" | |
| 21 #include "device/usb/usb_device.h" | |
| 22 | |
| 23 class Profile; | |
| 24 | |
| 25 namespace base { | |
| 26 class Value; | |
| 27 } | |
| 28 | |
| 29 namespace extensions { | |
| 30 class Extension; | |
| 31 } | |
| 32 | |
| 33 namespace apps { | |
| 34 | |
| 35 // Represents a device that a user has given an app permission to access. It | |
| 36 // will be persisted to disk (in the Preferences file) and so should remain | |
| 37 // serializable. | |
| 38 struct SavedDeviceEntry { | |
| 39 SavedDeviceEntry(uint16_t vendor_id, | |
| 40 uint16_t product_id, | |
| 41 const base::string16& serial_number); | |
| 42 | |
| 43 base::Value* ToValue() const; | |
| 44 | |
| 45 // The vendor ID of this device. | |
| 46 uint16_t vendor_id; | |
| 47 | |
| 48 // The product ID of this device. | |
| 49 uint16_t product_id; | |
| 50 | |
| 51 // The serial number (possibly alphanumeric) of this device. | |
| 52 base::string16 serial_number; | |
| 53 }; | |
| 54 | |
| 55 // Tracks the devices that apps have retained access to both while running and | |
| 56 // when suspended. | |
| 57 class SavedDevicesService : public KeyedService, | |
| 58 public content::NotificationObserver { | |
| 59 public: | |
| 60 // Tracks the devices that a particular extension has retained access to. | |
| 61 // Unlike SavedDevicesService the functions of this class can be called from | |
| 62 // the FILE thread. | |
| 63 class SavedDevices : device::UsbDevice::Observer { | |
| 64 public: | |
| 65 bool IsRegistered(scoped_refptr<device::UsbDevice> device) const; | |
| 66 void RegisterDevice(scoped_refptr<device::UsbDevice> device, | |
| 67 /* optional */ base::string16* serial_number); | |
| 68 | |
| 69 private: | |
| 70 friend class SavedDevicesService; | |
| 71 friend void STLDeleteValues<std::map<std::string, SavedDevices*> >( | |
| 72 std::map<std::string, SavedDevices*>* container); | |
| 73 | |
| 74 explicit SavedDevices(Profile* profile, const std::string& extension_id); | |
|
scheib
2014/09/18 20:31:49
explicit is unnecessary for multiple argument cons
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 75 virtual ~SavedDevices(); | |
| 76 | |
| 77 // device::UsbDevice::Observer | |
| 78 virtual void OnDisconnect(scoped_refptr<device::UsbDevice> device) OVERRIDE; | |
| 79 | |
| 80 Profile* profile_; | |
| 81 const std::string extension_id_; | |
| 82 | |
| 83 // Devices with serial numbers are written to the prefs file. | |
| 84 std::vector<SavedDeviceEntry> persistent_devices_; | |
| 85 // Other devices are ephemeral devices and cleared when the extension host | |
| 86 // is destroyed. | |
| 87 std::set<scoped_refptr<device::UsbDevice> > ephemeral_devices_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(SavedDevices); | |
| 90 }; | |
| 91 | |
| 92 explicit SavedDevicesService(Profile* profile); | |
| 93 virtual ~SavedDevicesService(); | |
| 94 | |
| 95 static SavedDevicesService* Get(Profile* profile); | |
| 96 | |
| 97 // Returns the SavedDevices for |extension_id|, creating it if necessary. | |
| 98 SavedDevices* GetOrInsert(const std::string& extension_id); | |
| 99 | |
| 100 std::vector<SavedDeviceEntry> GetAllDevices( | |
| 101 const std::string& extension_id) const; | |
| 102 | |
| 103 // Clears the SavedDevices for |extension_id|. | |
| 104 void Clear(const std::string& extension_id); | |
| 105 | |
| 106 private: | |
| 107 // content::NotificationObserver. | |
| 108 virtual void Observe(int type, | |
| 109 const content::NotificationSource& source, | |
| 110 const content::NotificationDetails& details) OVERRIDE; | |
| 111 | |
| 112 // Returns the SavedDevices for |extension_id| or NULL if one does not exist. | |
| 113 SavedDevices* Get(const std::string& extension_id) const; | |
| 114 | |
| 115 std::map<std::string, SavedDevices*> extension_id_to_saved_devices_; | |
| 116 STLValueDeleter<std::map<std::string, SavedDevices*> > | |
| 117 extension_id_to_saved_devices_deleter_; | |
| 118 Profile* profile_; | |
| 119 content::NotificationRegistrar registrar_; | |
| 120 base::ThreadChecker thread_checker_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(SavedDevicesService); | |
| 123 }; | |
| 124 | |
| 125 } // namespace apps | |
| 126 | |
| 127 #endif // APPS_SAVED_DEVICES_SERVICE_H_ | |
| OLD | NEW |