SW: Move TRACE_EVENT_ASYNC_END to avoid early return and fix typo
[chromium-blink-merge.git] / apps / saved_devices_service.h
blob52a187401722534fce194750f115f7b48d5e3102
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
23 class Profile;
25 namespace base {
26 class Value;
29 namespace extensions {
30 class Extension;
33 namespace apps {
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);
43 base::Value* ToValue() const;
45 // The vendor ID of this device.
46 uint16_t vendor_id;
48 // The product ID of this device.
49 uint16_t product_id;
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
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);
69 private:
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;
78 Profile* profile_;
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
84 // is destroyed.
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);
104 private:
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_;
114 Profile* profile_;
115 content::NotificationRegistrar registrar_;
116 base::ThreadChecker thread_checker_;
118 DISALLOW_COPY_AND_ASSIGN(SavedDevicesService);
121 } // namespace apps
123 #endif // APPS_SAVED_DEVICES_SERVICE_H_