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