| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/device/input_service_proxy.h" | 5 #include "chrome/browser/chromeos/device/input_service_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/task_runner_util.h" | 8 #include "base/task_runner_util.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 | 10 |
| 11 using content::BrowserThread; | 11 using content::BrowserThread; |
| 12 using device::InputServiceLinux; | 12 using device::InputServiceLinux; |
| 13 | 13 |
| 14 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo; | 14 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo; |
| 15 | 15 |
| 16 namespace chromeos { | 16 namespace chromeos { |
| 17 | 17 |
| 18 // static |
| 19 BrowserThread::ID InputServiceProxy::thread_identifier_ = BrowserThread::FILE; |
| 20 |
| 18 class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer { | 21 class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer { |
| 19 public: | 22 public: |
| 20 ServiceObserver() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); } | 23 ServiceObserver() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); } |
| 21 ~ServiceObserver() override { DCHECK(CalledOnValidThread()); } | 24 ~ServiceObserver() override { DCHECK(CalledOnValidThread()); } |
| 22 | 25 |
| 23 void Initialize(const base::WeakPtr<InputServiceProxy>& proxy) { | 26 void Initialize(const base::WeakPtr<InputServiceProxy>& proxy) { |
| 24 DCHECK(CalledOnValidThread()); | 27 DCHECK(CalledOnValidThread()); |
| 25 InputServiceLinux::GetInstance()->AddObserver(this); | 28 InputServiceLinux::GetInstance()->AddObserver(this); |
| 26 proxy_ = proxy; | 29 proxy_ = proxy; |
| 27 } | 30 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 void OnInputDeviceRemoved(const std::string& id) override { | 69 void OnInputDeviceRemoved(const std::string& id) override { |
| 67 DCHECK(CalledOnValidThread()); | 70 DCHECK(CalledOnValidThread()); |
| 68 BrowserThread::PostTask( | 71 BrowserThread::PostTask( |
| 69 BrowserThread::UI, | 72 BrowserThread::UI, |
| 70 FROM_HERE, | 73 FROM_HERE, |
| 71 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id)); | 74 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id)); |
| 72 } | 75 } |
| 73 | 76 |
| 74 private: | 77 private: |
| 75 bool CalledOnValidThread() const { | 78 bool CalledOnValidThread() const { |
| 76 return BrowserThread::CurrentlyOn(BrowserThread::FILE); | 79 return BrowserThread::CurrentlyOn(InputServiceProxy::thread_identifier_); |
| 77 } | 80 } |
| 78 | 81 |
| 79 base::WeakPtr<InputServiceProxy> proxy_; | 82 base::WeakPtr<InputServiceProxy> proxy_; |
| 80 | 83 |
| 81 DISALLOW_COPY_AND_ASSIGN(ServiceObserver); | 84 DISALLOW_COPY_AND_ASSIGN(ServiceObserver); |
| 82 }; | 85 }; |
| 83 | 86 |
| 84 InputServiceProxy::InputServiceProxy() | 87 InputServiceProxy::InputServiceProxy() |
| 85 : service_observer_(new ServiceObserver()), weak_factory_(this) { | 88 : service_observer_(new ServiceObserver()), |
| 89 task_runner_(BrowserThread::GetMessageLoopProxyForThread( |
| 90 thread_identifier_)), |
| 91 weak_factory_(this) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 BrowserThread::PostTask( | 93 task_runner_->PostTask( |
| 88 BrowserThread::FILE, | |
| 89 FROM_HERE, | 94 FROM_HERE, |
| 90 base::Bind(&InputServiceProxy::ServiceObserver::Initialize, | 95 base::Bind(&InputServiceProxy::ServiceObserver::Initialize, |
| 91 base::Unretained(service_observer_.get()), | 96 base::Unretained(service_observer_.get()), |
| 92 weak_factory_.GetWeakPtr())); | 97 weak_factory_.GetWeakPtr())); |
| 93 } | 98 } |
| 94 | 99 |
| 95 InputServiceProxy::~InputServiceProxy() { | 100 InputServiceProxy::~InputServiceProxy() { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 BrowserThread::PostTask( | 102 task_runner_->PostTask( |
| 98 BrowserThread::FILE, | |
| 99 FROM_HERE, | 103 FROM_HERE, |
| 100 base::Bind(&InputServiceProxy::ServiceObserver::Shutdown, | 104 base::Bind(&InputServiceProxy::ServiceObserver::Shutdown, |
| 101 base::Unretained(service_observer_.release()))); | 105 base::Unretained(service_observer_.release()))); |
| 102 } | 106 } |
| 103 | 107 |
| 104 // static | 108 // static |
| 105 void InputServiceProxy::WarmUp() { | 109 void InputServiceProxy::WarmUp() { |
| 106 content::BrowserThread::PostTask( | 110 content::BrowserThread::PostTask( |
| 107 content::BrowserThread::FILE, | 111 thread_identifier_, |
| 108 FROM_HERE, | 112 FROM_HERE, |
| 109 base::Bind(base::IgnoreResult(&InputServiceLinux::GetInstance))); | 113 base::Bind(base::IgnoreResult(&InputServiceLinux::GetInstance))); |
| 110 } | 114 } |
| 111 | 115 |
| 112 void InputServiceProxy::AddObserver(Observer* observer) { | 116 void InputServiceProxy::AddObserver(Observer* observer) { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 if (observer) | 118 if (observer) |
| 115 observers_.AddObserver(observer); | 119 observers_.AddObserver(observer); |
| 116 } | 120 } |
| 117 | 121 |
| 118 void InputServiceProxy::RemoveObserver(Observer* observer) { | 122 void InputServiceProxy::RemoveObserver(Observer* observer) { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 if (observer) | 124 if (observer) |
| 121 observers_.RemoveObserver(observer); | 125 observers_.RemoveObserver(observer); |
| 122 } | 126 } |
| 123 | 127 |
| 124 void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) { | 128 void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 BrowserThread::PostTaskAndReplyWithResult( | 130 base::PostTaskAndReplyWithResult( |
| 127 BrowserThread::FILE, | 131 task_runner_.get(), |
| 128 FROM_HERE, | 132 FROM_HERE, |
| 129 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices, | 133 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices, |
| 130 base::Unretained(service_observer_.get())), | 134 base::Unretained(service_observer_.get())), |
| 131 callback); | 135 callback); |
| 132 } | 136 } |
| 133 | 137 |
| 134 void InputServiceProxy::GetDeviceInfo(const std::string& id, | 138 void InputServiceProxy::GetDeviceInfo(const std::string& id, |
| 135 const GetDeviceInfoCallback& callback) { | 139 const GetDeviceInfoCallback& callback) { |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 BrowserThread::PostTask( | 141 task_runner_->PostTask( |
| 138 BrowserThread::FILE, | |
| 139 FROM_HERE, | 142 FROM_HERE, |
| 140 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo, | 143 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo, |
| 141 base::Unretained(service_observer_.release()), | 144 base::Unretained(service_observer_.release()), |
| 142 id, | 145 id, |
| 143 callback)); | 146 callback)); |
| 144 } | 147 } |
| 145 | 148 |
| 149 // static |
| 150 void InputServiceProxy::SetThreadIdForTesting(BrowserThread::ID thread_id) { |
| 151 InputServiceProxy::thread_identifier_ = thread_id; |
| 152 } |
| 153 |
| 146 void InputServiceProxy::OnDeviceAdded( | 154 void InputServiceProxy::OnDeviceAdded( |
| 147 const InputServiceLinux::InputDeviceInfo& info) { | 155 const InputServiceLinux::InputDeviceInfo& info) { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); | 157 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); |
| 150 } | 158 } |
| 151 | 159 |
| 152 void InputServiceProxy::OnDeviceRemoved(const std::string& id) { | 160 void InputServiceProxy::OnDeviceRemoved(const std::string& id) { |
| 153 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 154 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); | 162 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); |
| 155 } | 163 } |
| 156 | 164 |
| 157 } // namespace chromeos | 165 } // namespace chromeos |
| OLD | NEW |