Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / apps / saved_devices_service.h
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
72     SavedDevices(Profile* profile, const std::string& extension_id);
73     virtual ~SavedDevices();
74
75     // device::UsbDevice::Observer
76     virtual void OnDisconnect(scoped_refptr<device::UsbDevice> device) OVERRIDE;
77
78     Profile* profile_;
79     const std::string extension_id_;
80
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_;
86
87     DISALLOW_COPY_AND_ASSIGN(SavedDevices);
88   };
89
90   explicit SavedDevicesService(Profile* profile);
91   virtual ~SavedDevicesService();
92
93   static SavedDevicesService* Get(Profile* profile);
94
95   // Returns the SavedDevices for |extension_id|, creating it if necessary.
96   SavedDevices* GetOrInsert(const std::string& extension_id);
97
98   std::vector<SavedDeviceEntry> GetAllDevices(
99       const std::string& extension_id) const;
100
101   // Clears the SavedDevices for |extension_id|.
102   void Clear(const std::string& extension_id);
103
104  private:
105   // content::NotificationObserver.
106   virtual void Observe(int type,
107                        const content::NotificationSource& source,
108                        const content::NotificationDetails& details) OVERRIDE;
109
110   // Returns the SavedDevices for |extension_id| or NULL if one does not exist.
111   SavedDevices* Get(const std::string& extension_id) const;
112
113   std::map<std::string, SavedDevices*> extension_id_to_saved_devices_;
114   Profile* profile_;
115   content::NotificationRegistrar registrar_;
116   base::ThreadChecker thread_checker_;
117
118   DISALLOW_COPY_AND_ASSIGN(SavedDevicesService);
119 };
120
121 }  // namespace apps
122
123 #endif  // APPS_SAVED_DEVICES_SERVICE_H_