Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: chrome/browser/chromeos/device/input_service_proxy.cc

Issue 2937253003: cros: Replace BrowserThread::FILE in InputServiceProxy (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/macros.h" 8 #include "base/macros.h"
9 #include "base/sequence_checker.h"
9 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
11 #include "base/task_scheduler/post_task.h"
10 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
11 13
12 using content::BrowserThread; 14 using content::BrowserThread;
13 using device::InputServiceLinux; 15 using device::InputServiceLinux;
14 16
15 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo; 17 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
16 18
17 namespace chromeos { 19 namespace chromeos {
18 20
19 // static 21 namespace {
20 BrowserThread::ID InputServiceProxy::thread_identifier_ = BrowserThread::FILE; 22
23 InputServiceProxy::TaskRunnerFactory* task_runner_factory = nullptr;
24
25 scoped_refptr<base::TaskRunner> GetTaskRunner() {
26 if (task_runner_factory)
27 return task_runner_factory->Run();
28
29 return base::CreateSequencedTaskRunnerWithTraits(
30 {base::TaskPriority::BACKGROUND});
31 }
32
33 } // namespace
21 34
22 class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer { 35 class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
23 public: 36 public:
24 ServiceObserver() { DCHECK_CURRENTLY_ON(BrowserThread::UI); } 37 ServiceObserver() {
25 ~ServiceObserver() override { DCHECK(CalledOnValidThread()); } 38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39
40 // Detach since this object is constructed on UI thread and forever after
41 // used from another sequence.
42 DETACH_FROM_SEQUENCE(sequence_checker_);
43 }
44 ~ServiceObserver() override {
45 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
46 }
26 47
27 void Initialize(const base::WeakPtr<InputServiceProxy>& proxy) { 48 void Initialize(const base::WeakPtr<InputServiceProxy>& proxy) {
28 DCHECK(CalledOnValidThread()); 49 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
29 InputServiceLinux::GetInstance()->AddObserver(this); 50 InputServiceLinux::GetInstance()->AddObserver(this);
30 proxy_ = proxy; 51 proxy_ = proxy;
31 } 52 }
32 53
33 void Shutdown() { 54 void Shutdown() {
34 DCHECK(CalledOnValidThread()); 55 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
35 if (InputServiceLinux::HasInstance()) 56 if (InputServiceLinux::HasInstance())
36 InputServiceLinux::GetInstance()->RemoveObserver(this); 57 InputServiceLinux::GetInstance()->RemoveObserver(this);
37 delete this; 58 delete this;
38 } 59 }
39 60
40 std::vector<InputDeviceInfo> GetDevices() { 61 std::vector<InputDeviceInfo> GetDevices() {
41 DCHECK(CalledOnValidThread()); 62 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
42 std::vector<InputDeviceInfo> devices; 63 std::vector<InputDeviceInfo> devices;
43 if (InputServiceLinux::HasInstance()) 64 if (InputServiceLinux::HasInstance())
44 InputServiceLinux::GetInstance()->GetDevices(&devices); 65 InputServiceLinux::GetInstance()->GetDevices(&devices);
45 return devices; 66 return devices;
46 } 67 }
47 68
48 void GetDeviceInfo(const std::string& id, 69 void GetDeviceInfo(const std::string& id,
49 const InputServiceProxy::GetDeviceInfoCallback& callback) { 70 const InputServiceProxy::GetDeviceInfoCallback& callback) {
50 DCHECK(CalledOnValidThread()); 71 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
51 bool success = false; 72 bool success = false;
52 InputDeviceInfo info; 73 InputDeviceInfo info;
53 info.id = id; 74 info.id = id;
54 if (InputServiceLinux::HasInstance()) 75 if (InputServiceLinux::HasInstance())
55 success = InputServiceLinux::GetInstance()->GetDeviceInfo(id, &info); 76 success = InputServiceLinux::GetInstance()->GetDeviceInfo(id, &info);
56 BrowserThread::PostTask( 77 BrowserThread::PostTask(
57 BrowserThread::UI, FROM_HERE, base::Bind(callback, success, info)); 78 BrowserThread::UI, FROM_HERE, base::Bind(callback, success, info));
58 } 79 }
59 80
60 // InputServiceLinux::Observer implementation: 81 // InputServiceLinux::Observer implementation:
61 void OnInputDeviceAdded( 82 void OnInputDeviceAdded(
62 const InputServiceLinux::InputDeviceInfo& info) override { 83 const InputServiceLinux::InputDeviceInfo& info) override {
63 DCHECK(CalledOnValidThread()); 84 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
64 BrowserThread::PostTask( 85 BrowserThread::PostTask(
65 BrowserThread::UI, 86 BrowserThread::UI,
66 FROM_HERE, 87 FROM_HERE,
67 base::Bind(&InputServiceProxy::OnDeviceAdded, proxy_, info)); 88 base::Bind(&InputServiceProxy::OnDeviceAdded, proxy_, info));
68 } 89 }
69 90
70 void OnInputDeviceRemoved(const std::string& id) override { 91 void OnInputDeviceRemoved(const std::string& id) override {
71 DCHECK(CalledOnValidThread()); 92 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
72 BrowserThread::PostTask( 93 BrowserThread::PostTask(
73 BrowserThread::UI, 94 BrowserThread::UI,
74 FROM_HERE, 95 FROM_HERE,
75 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id)); 96 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id));
76 } 97 }
77 98
78 private: 99 private:
79 bool CalledOnValidThread() const {
80 return BrowserThread::CurrentlyOn(InputServiceProxy::thread_identifier_);
81 }
82
83 base::WeakPtr<InputServiceProxy> proxy_; 100 base::WeakPtr<InputServiceProxy> proxy_;
101 SEQUENCE_CHECKER(sequence_checker_);
84 102
85 DISALLOW_COPY_AND_ASSIGN(ServiceObserver); 103 DISALLOW_COPY_AND_ASSIGN(ServiceObserver);
86 }; 104 };
87 105
88 InputServiceProxy::InputServiceProxy() 106 InputServiceProxy::InputServiceProxy()
89 : service_observer_(new ServiceObserver()), 107 : service_observer_(new ServiceObserver()),
90 task_runner_(BrowserThread::GetTaskRunnerForThread(thread_identifier_)), 108 task_runner_(GetTaskRunner()),
91 weak_factory_(this) { 109 weak_factory_(this) {
92 DCHECK_CURRENTLY_ON(BrowserThread::UI); 110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 task_runner_->PostTask( 111 task_runner_->PostTask(
94 FROM_HERE, 112 FROM_HERE,
95 base::Bind(&InputServiceProxy::ServiceObserver::Initialize, 113 base::Bind(&InputServiceProxy::ServiceObserver::Initialize,
96 base::Unretained(service_observer_.get()), 114 base::Unretained(service_observer_.get()),
97 weak_factory_.GetWeakPtr())); 115 weak_factory_.GetWeakPtr()));
98 } 116 }
99 117
100 InputServiceProxy::~InputServiceProxy() { 118 InputServiceProxy::~InputServiceProxy() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 DCHECK(thread_checker_.CalledOnValidThread()); 150 DCHECK(thread_checker_.CalledOnValidThread());
133 task_runner_->PostTask( 151 task_runner_->PostTask(
134 FROM_HERE, 152 FROM_HERE,
135 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo, 153 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo,
136 base::Unretained(service_observer_.release()), 154 base::Unretained(service_observer_.release()),
137 id, 155 id,
138 callback)); 156 callback));
139 } 157 }
140 158
141 // static 159 // static
142 void InputServiceProxy::SetThreadIdForTesting(BrowserThread::ID thread_id) { 160 void InputServiceProxy::SetTaskRunnerFactoryForTesting(
143 InputServiceProxy::thread_identifier_ = thread_id; 161 TaskRunnerFactory* factory) {
162 task_runner_factory = factory;
144 } 163 }
145 164
146 void InputServiceProxy::OnDeviceAdded( 165 void InputServiceProxy::OnDeviceAdded(
147 const InputServiceLinux::InputDeviceInfo& info) { 166 const InputServiceLinux::InputDeviceInfo& info) {
148 DCHECK(thread_checker_.CalledOnValidThread()); 167 DCHECK(thread_checker_.CalledOnValidThread());
149 for (auto& observer : observers_) 168 for (auto& observer : observers_)
150 observer.OnInputDeviceAdded(info); 169 observer.OnInputDeviceAdded(info);
151 } 170 }
152 171
153 void InputServiceProxy::OnDeviceRemoved(const std::string& id) { 172 void InputServiceProxy::OnDeviceRemoved(const std::string& id) {
154 DCHECK(thread_checker_.CalledOnValidThread()); 173 DCHECK(thread_checker_.CalledOnValidThread());
155 for (auto& observer : observers_) 174 for (auto& observer : observers_)
156 observer.OnInputDeviceRemoved(id); 175 observer.OnInputDeviceRemoved(id);
157 } 176 }
158 177
159 } // namespace chromeos 178 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698