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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// 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.
#include "chrome/browser/chromeos/device/input_service_proxy.h"
#include "base/bind_helpers.h"
#include "base/task_runner_util.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
using device::InputServiceLinux;
typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
namespace chromeos {
class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
public:
ServiceObserver() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); }
virtual ~ServiceObserver() { DCHECK(CalledOnValidThread()); }
void Initialize(const base::WeakPtr<InputServiceProxy>& proxy) {
DCHECK(CalledOnValidThread());
InputServiceLinux::GetInstance()->AddObserver(this);
proxy_ = proxy;
}
void Shutdown() {
DCHECK(CalledOnValidThread());
if (InputServiceLinux::HasInstance())
InputServiceLinux::GetInstance()->RemoveObserver(this);
delete this;
}
std::vector<InputDeviceInfo> GetDevices() {
DCHECK(CalledOnValidThread());
std::vector<InputDeviceInfo> devices;
if (InputServiceLinux::HasInstance())
InputServiceLinux::GetInstance()->GetDevices(&devices);
return devices;
}
void GetDeviceInfo(const std::string& id,
const InputServiceProxy::GetDeviceInfoCallback& callback) {
DCHECK(CalledOnValidThread());
bool success = false;
InputDeviceInfo info;
info.id = id;
if (InputServiceLinux::HasInstance())
success = InputServiceLinux::GetInstance()->GetDeviceInfo(id, &info);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, base::Bind(callback, success, info));
}
// InputServiceLinux::Observer implementation:
virtual void OnInputDeviceAdded(
const InputServiceLinux::InputDeviceInfo& info) OVERRIDE {
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&InputServiceProxy::OnDeviceAdded, proxy_, info));
}
virtual void OnInputDeviceRemoved(const std::string& id) OVERRIDE {
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id));
}
private:
bool CalledOnValidThread() const {
return BrowserThread::CurrentlyOn(BrowserThread::FILE);
}
base::WeakPtr<InputServiceProxy> proxy_;
DISALLOW_COPY_AND_ASSIGN(ServiceObserver);
};
InputServiceProxy::InputServiceProxy()
: service_observer_(new ServiceObserver()), weak_factory_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::Initialize,
base::Unretained(service_observer_.get()),
weak_factory_.GetWeakPtr()));
}
InputServiceProxy::~InputServiceProxy() {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::Shutdown,
base::Unretained(service_observer_.release())));
}
// static
void InputServiceProxy::WarmUp() {
content::BrowserThread::PostTask(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(base::IgnoreResult(&InputServiceLinux::GetInstance)));
}
void InputServiceProxy::AddObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
if (observer)
observers_.AddObserver(observer);
}
void InputServiceProxy::RemoveObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
if (observer)
observers_.RemoveObserver(observer);
}
void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::GetDevices,
base::Unretained(service_observer_.get())),
callback);
}
void InputServiceProxy::GetDeviceInfo(const std::string& id,
const GetDeviceInfoCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo,
base::Unretained(service_observer_.release()),
id,
callback));
}
void InputServiceProxy::OnDeviceAdded(
const InputServiceLinux::InputDeviceInfo& info) {
DCHECK(thread_checker_.CalledOnValidThread());
FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info));
}
void InputServiceProxy::OnDeviceRemoved(const std::string& id) {
DCHECK(thread_checker_.CalledOnValidThread());
FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id));
}
} // namespace chromeos
|