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 APPS_SAVED_DEVICES_SERVICE_H_
6 #define APPS_SAVED_DEVICES_SERVICE_H_
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"
29 namespace extensions {
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
38 struct SavedDeviceEntry {
39 SavedDeviceEntry(uint16_t vendor_id,
41 const base::string16& serial_number);
43 base::Value* ToValue() const;
45 // The vendor ID of this device.
48 // The product ID of this device.
51 // The serial number (possibly alphanumeric) of this device.
52 base::string16 serial_number;
55 // Tracks the devices that apps have retained access to both while running and
57 class SavedDevicesService : public KeyedService,
58 public content::NotificationObserver {
60 // Tracks the devices that a particular extension has retained access to.
61 // Unlike SavedDevicesService the functions of this class can be called from
63 class SavedDevices : device::UsbDevice::Observer {
65 bool IsRegistered(scoped_refptr<device::UsbDevice> device) const;
66 void RegisterDevice(scoped_refptr<device::UsbDevice> device,
67 /* optional */ base::string16* serial_number);
70 friend class SavedDevicesService;
72 SavedDevices(Profile* profile, const std::string& extension_id);
73 virtual ~SavedDevices();
75 // device::UsbDevice::Observer
76 virtual void OnDisconnect(scoped_refptr<device::UsbDevice> device) OVERRIDE;
79 const std::string extension_id_;
81 // Devices with serial numbers are written to the prefs file.
82 std::vector<SavedDeviceEntry> persistent_devices_;
83 // Other devices are ephemeral devices and cleared when the extension host
85 std::set<scoped_refptr<device::UsbDevice> > ephemeral_devices_;
87 DISALLOW_COPY_AND_ASSIGN(SavedDevices);
90 explicit SavedDevicesService(Profile* profile);
91 virtual ~SavedDevicesService();
93 static SavedDevicesService* Get(Profile* profile);
95 // Returns the SavedDevices for |extension_id|, creating it if necessary.
96 SavedDevices* GetOrInsert(const std::string& extension_id);
98 std::vector<SavedDeviceEntry> GetAllDevices(
99 const std::string& extension_id) const;
101 // Clears the SavedDevices for |extension_id|.
102 void Clear(const std::string& extension_id);
105 // content::NotificationObserver.
106 virtual void Observe(int type,
107 const content::NotificationSource& source,
108 const content::NotificationDetails& details) OVERRIDE;
110 // Returns the SavedDevices for |extension_id| or NULL if one does not exist.
111 SavedDevices* Get(const std::string& extension_id) const;
113 std::map<std::string, SavedDevices*> extension_id_to_saved_devices_;
115 content::NotificationRegistrar registrar_;
116 base::ThreadChecker thread_checker_;
118 DISALLOW_COPY_AND_ASSIGN(SavedDevicesService);
123 #endif // APPS_SAVED_DEVICES_SERVICE_H_