BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / device / input_service_proxy.cc
blob2779d4cf79766b816861aa797e08a9c90a0612cb
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;
16 namespace chromeos {
18 // static
19 BrowserThread::ID InputServiceProxy::thread_identifier_ = BrowserThread::FILE;
21 class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
22 public:
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);
29 proxy_ = proxy;
32 void Shutdown() {
33 DCHECK(CalledOnValidThread());
34 if (InputServiceLinux::HasInstance())
35 InputServiceLinux::GetInstance()->RemoveObserver(this);
36 delete this;
39 std::vector<InputDeviceInfo> GetDevices() {
40 DCHECK(CalledOnValidThread());
41 std::vector<InputDeviceInfo> devices;
42 if (InputServiceLinux::HasInstance())
43 InputServiceLinux::GetInstance()->GetDevices(&devices);
44 return devices;
47 void GetDeviceInfo(const std::string& id,
48 const InputServiceProxy::GetDeviceInfoCallback& callback) {
49 DCHECK(CalledOnValidThread());
50 bool success = false;
51 InputDeviceInfo info;
52 info.id = id;
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(
64 BrowserThread::UI,
65 FROM_HERE,
66 base::Bind(&InputServiceProxy::OnDeviceAdded, proxy_, info));
69 void OnInputDeviceRemoved(const std::string& id) override {
70 DCHECK(CalledOnValidThread());
71 BrowserThread::PostTask(
72 BrowserThread::UI,
73 FROM_HERE,
74 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id));
77 private:
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(
90 thread_identifier_)),
91 weak_factory_(this) {
92 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 task_runner_->PostTask(
94 FROM_HERE,
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(
103 FROM_HERE,
104 base::Bind(&InputServiceProxy::ServiceObserver::Shutdown,
105 base::Unretained(service_observer_.release())));
108 void InputServiceProxy::AddObserver(Observer* observer) {
109 DCHECK(thread_checker_.CalledOnValidThread());
110 if (observer)
111 observers_.AddObserver(observer);
114 void InputServiceProxy::RemoveObserver(Observer* observer) {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 if (observer)
117 observers_.RemoveObserver(observer);
120 void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 base::PostTaskAndReplyWithResult(
123 task_runner_.get(),
124 FROM_HERE,
125 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices,
126 base::Unretained(service_observer_.get())),
127 callback);
130 void InputServiceProxy::GetDeviceInfo(const std::string& id,
131 const GetDeviceInfoCallback& callback) {
132 DCHECK(thread_checker_.CalledOnValidThread());
133 task_runner_->PostTask(
134 FROM_HERE,
135 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo,
136 base::Unretained(service_observer_.release()),
138 callback));
141 // static
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