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 #include "chrome/browser/chromeos/device/input_service_proxy.h"
7 #include "base/bind_helpers.h"
8 #include "base/task_runner_util.h"
9 #include "content/public/browser/browser_thread.h"
11 using content::BrowserThread
;
12 using device::InputServiceLinux
;
14 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo
;
19 BrowserThread::ID
InputServiceProxy::thread_identifier_
= BrowserThread::FILE;
21 class InputServiceProxy::ServiceObserver
: public InputServiceLinux::Observer
{
23 ServiceObserver() { DCHECK_CURRENTLY_ON(BrowserThread::UI
); }
24 ~ServiceObserver() override
{ DCHECK(CalledOnValidThread()); }
26 void Initialize(const base::WeakPtr
<InputServiceProxy
>& proxy
) {
27 DCHECK(CalledOnValidThread());
28 InputServiceLinux::GetInstance()->AddObserver(this);
33 DCHECK(CalledOnValidThread());
34 if (InputServiceLinux::HasInstance())
35 InputServiceLinux::GetInstance()->RemoveObserver(this);
39 std::vector
<InputDeviceInfo
> GetDevices() {
40 DCHECK(CalledOnValidThread());
41 std::vector
<InputDeviceInfo
> devices
;
42 if (InputServiceLinux::HasInstance())
43 InputServiceLinux::GetInstance()->GetDevices(&devices
);
47 void GetDeviceInfo(const std::string
& id
,
48 const InputServiceProxy::GetDeviceInfoCallback
& callback
) {
49 DCHECK(CalledOnValidThread());
53 if (InputServiceLinux::HasInstance())
54 success
= InputServiceLinux::GetInstance()->GetDeviceInfo(id
, &info
);
55 BrowserThread::PostTask(
56 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, success
, info
));
59 // InputServiceLinux::Observer implementation:
60 void OnInputDeviceAdded(
61 const InputServiceLinux::InputDeviceInfo
& info
) override
{
62 DCHECK(CalledOnValidThread());
63 BrowserThread::PostTask(
66 base::Bind(&InputServiceProxy::OnDeviceAdded
, proxy_
, info
));
69 void OnInputDeviceRemoved(const std::string
& id
) override
{
70 DCHECK(CalledOnValidThread());
71 BrowserThread::PostTask(
74 base::Bind(&InputServiceProxy::OnDeviceRemoved
, proxy_
, id
));
78 bool CalledOnValidThread() const {
79 return BrowserThread::CurrentlyOn(InputServiceProxy::thread_identifier_
);
82 base::WeakPtr
<InputServiceProxy
> proxy_
;
84 DISALLOW_COPY_AND_ASSIGN(ServiceObserver
);
87 InputServiceProxy::InputServiceProxy()
88 : service_observer_(new ServiceObserver()),
89 task_runner_(BrowserThread::GetMessageLoopProxyForThread(
92 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
93 task_runner_
->PostTask(
95 base::Bind(&InputServiceProxy::ServiceObserver::Initialize
,
96 base::Unretained(service_observer_
.get()),
97 weak_factory_
.GetWeakPtr()));
100 InputServiceProxy::~InputServiceProxy() {
101 DCHECK(thread_checker_
.CalledOnValidThread());
102 task_runner_
->PostTask(
104 base::Bind(&InputServiceProxy::ServiceObserver::Shutdown
,
105 base::Unretained(service_observer_
.release())));
108 void InputServiceProxy::AddObserver(Observer
* observer
) {
109 DCHECK(thread_checker_
.CalledOnValidThread());
111 observers_
.AddObserver(observer
);
114 void InputServiceProxy::RemoveObserver(Observer
* observer
) {
115 DCHECK(thread_checker_
.CalledOnValidThread());
117 observers_
.RemoveObserver(observer
);
120 void InputServiceProxy::GetDevices(const GetDevicesCallback
& callback
) {
121 DCHECK(thread_checker_
.CalledOnValidThread());
122 base::PostTaskAndReplyWithResult(
125 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices
,
126 base::Unretained(service_observer_
.get())),
130 void InputServiceProxy::GetDeviceInfo(const std::string
& id
,
131 const GetDeviceInfoCallback
& callback
) {
132 DCHECK(thread_checker_
.CalledOnValidThread());
133 task_runner_
->PostTask(
135 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo
,
136 base::Unretained(service_observer_
.release()),
142 void InputServiceProxy::SetThreadIdForTesting(BrowserThread::ID thread_id
) {
143 InputServiceProxy::thread_identifier_
= thread_id
;
146 void InputServiceProxy::OnDeviceAdded(
147 const InputServiceLinux::InputDeviceInfo
& info
) {
148 DCHECK(thread_checker_
.CalledOnValidThread());
149 FOR_EACH_OBSERVER(Observer
, observers_
, OnInputDeviceAdded(info
));
152 void InputServiceProxy::OnDeviceRemoved(const std::string
& id
) {
153 DCHECK(thread_checker_
.CalledOnValidThread());
154 FOR_EACH_OBSERVER(Observer
, observers_
, OnInputDeviceRemoved(id
));
157 } // namespace chromeos