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

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

Issue 2937253003: cros: Replace BrowserThread::FILE in InputServiceProxy (Closed)
Patch Set: rebase 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/lazy_task_runner.h"
12 #include "base/task_scheduler/post_task.h"
10 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
11 14
12 using content::BrowserThread; 15 using content::BrowserThread;
13 using device::InputServiceLinux; 16 using device::InputServiceLinux;
14 17
15 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
16
17 namespace chromeos { 18 namespace chromeos {
18 19
19 // static 20 namespace {
20 BrowserThread::ID InputServiceProxy::thread_identifier_ = BrowserThread::FILE; 21
22 using InputDeviceInfo = device::InputServiceLinux::InputDeviceInfo;
23
24 bool use_ui_thread_for_test = false;
25
26 // SequencedTaskRunner could be used after InputServiceLinux and friends
27 // are updated to check on sequence instead of thread.
fdoray 2017/06/22 12:27:44 Create bug to make InputServiceLinux and friends s
28 base::LazySingleThreadTaskRunner default_input_service_task_runner =
29 LAZY_SINGLE_THREAD_TASK_RUNNER_INITIALIZER(
30 base::TaskTraits({base::TaskPriority::BACKGROUND, base::MayBlock()}),
31 base::SingleThreadTaskRunnerThreadMode::SHARED);
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(BrowserThread::UI, FROM_HERE,
57 BrowserThread::UI, FROM_HERE, base::Bind(callback, success, info)); 78 base::BindOnce(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, FROM_HERE,
66 FROM_HERE, 87 base::BindOnce(&InputServiceProxy::OnDeviceAdded, proxy_, info));
67 base::Bind(&InputServiceProxy::OnDeviceAdded, proxy_, info));
68 } 88 }
69 89
70 void OnInputDeviceRemoved(const std::string& id) override { 90 void OnInputDeviceRemoved(const std::string& id) override {
71 DCHECK(CalledOnValidThread()); 91 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
72 BrowserThread::PostTask( 92 BrowserThread::PostTask(
73 BrowserThread::UI, 93 BrowserThread::UI, FROM_HERE,
74 FROM_HERE, 94 base::BindOnce(&InputServiceProxy::OnDeviceRemoved, proxy_, id));
75 base::Bind(&InputServiceProxy::OnDeviceRemoved, proxy_, id));
76 } 95 }
77 96
78 private: 97 private:
79 bool CalledOnValidThread() const {
80 return BrowserThread::CurrentlyOn(InputServiceProxy::thread_identifier_);
81 }
82
83 base::WeakPtr<InputServiceProxy> proxy_; 98 base::WeakPtr<InputServiceProxy> proxy_;
99 SEQUENCE_CHECKER(sequence_checker_);
84 100
85 DISALLOW_COPY_AND_ASSIGN(ServiceObserver); 101 DISALLOW_COPY_AND_ASSIGN(ServiceObserver);
86 }; 102 };
87 103
88 InputServiceProxy::InputServiceProxy() 104 InputServiceProxy::InputServiceProxy()
89 : service_observer_(new ServiceObserver()), 105 : service_observer_(new ServiceObserver()),
90 task_runner_(BrowserThread::GetTaskRunnerForThread(thread_identifier_)),
91 weak_factory_(this) { 106 weak_factory_(this) {
92 DCHECK_CURRENTLY_ON(BrowserThread::UI); 107 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 task_runner_->PostTask( 108 GetInputServiceTaskRunner()->PostTask(
94 FROM_HERE, 109 FROM_HERE, base::BindOnce(&InputServiceProxy::ServiceObserver::Initialize,
95 base::Bind(&InputServiceProxy::ServiceObserver::Initialize, 110 base::Unretained(service_observer_.get()),
96 base::Unretained(service_observer_.get()), 111 weak_factory_.GetWeakPtr()));
97 weak_factory_.GetWeakPtr()));
98 } 112 }
99 113
100 InputServiceProxy::~InputServiceProxy() { 114 InputServiceProxy::~InputServiceProxy() {
101 DCHECK(thread_checker_.CalledOnValidThread()); 115 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
102 task_runner_->PostTask( 116 GetInputServiceTaskRunner()->PostTask(
103 FROM_HERE, 117 FROM_HERE, base::BindOnce(&InputServiceProxy::ServiceObserver::Shutdown,
104 base::Bind(&InputServiceProxy::ServiceObserver::Shutdown, 118 base::Unretained(service_observer_.release())));
105 base::Unretained(service_observer_.release())));
106 } 119 }
107 120
108 void InputServiceProxy::AddObserver(Observer* observer) { 121 void InputServiceProxy::AddObserver(Observer* observer) {
109 DCHECK(thread_checker_.CalledOnValidThread()); 122 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
110 if (observer) 123 if (observer)
111 observers_.AddObserver(observer); 124 observers_.AddObserver(observer);
112 } 125 }
113 126
114 void InputServiceProxy::RemoveObserver(Observer* observer) { 127 void InputServiceProxy::RemoveObserver(Observer* observer) {
115 DCHECK(thread_checker_.CalledOnValidThread()); 128 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
116 if (observer) 129 if (observer)
117 observers_.RemoveObserver(observer); 130 observers_.RemoveObserver(observer);
118 } 131 }
119 132
120 void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) { 133 void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) {
121 DCHECK(thread_checker_.CalledOnValidThread()); 134 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
122 base::PostTaskAndReplyWithResult( 135 base::PostTaskAndReplyWithResult(
123 task_runner_.get(), 136 GetInputServiceTaskRunner().get(), FROM_HERE,
124 FROM_HERE,
125 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices, 137 base::Bind(&InputServiceProxy::ServiceObserver::GetDevices,
126 base::Unretained(service_observer_.get())), 138 base::Unretained(service_observer_.get())),
127 callback); 139 callback);
128 } 140 }
129 141
130 void InputServiceProxy::GetDeviceInfo(const std::string& id, 142 void InputServiceProxy::GetDeviceInfo(const std::string& id,
131 const GetDeviceInfoCallback& callback) { 143 const GetDeviceInfoCallback& callback) {
132 DCHECK(thread_checker_.CalledOnValidThread()); 144 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
133 task_runner_->PostTask( 145 GetInputServiceTaskRunner()->PostTask(
134 FROM_HERE, 146 FROM_HERE,
135 base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo, 147 base::BindOnce(&InputServiceProxy::ServiceObserver::GetDeviceInfo,
136 base::Unretained(service_observer_.release()), 148 base::Unretained(service_observer_.get()), id, callback));
137 id,
138 callback));
139 } 149 }
140 150
141 // static 151 // static
142 void InputServiceProxy::SetThreadIdForTesting(BrowserThread::ID thread_id) { 152 scoped_refptr<base::SequencedTaskRunner>
143 InputServiceProxy::thread_identifier_ = thread_id; 153 InputServiceProxy::GetInputServiceTaskRunner() {
154 if (use_ui_thread_for_test)
155 return BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
156
157 return default_input_service_task_runner.Get();
158 }
159
160 // static
161 void InputServiceProxy::SetUseUIThreadForTesting(bool use_ui_thread) {
162 use_ui_thread_for_test = use_ui_thread;
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_CALLED_ON_VALID_THREAD(thread_checker_);
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_CALLED_ON_VALID_THREAD(thread_checker_);
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
« no previous file with comments | « chrome/browser/chromeos/device/input_service_proxy.h ('k') | chrome/browser/chromeos/device/input_service_test_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698