OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <cmath> | 5 #include <cmath> |
6 #include <set> | 6 #include <set> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 namespace device_orientation { | 40 namespace device_orientation { |
41 | 41 |
42 class ProviderImpl::PollingThread : public base::Thread { | 42 class ProviderImpl::PollingThread : public base::Thread { |
43 public: | 43 public: |
44 PollingThread(const char* name, | 44 PollingThread(const char* name, |
45 base::WeakPtr<ProviderImpl> provider, | 45 base::WeakPtr<ProviderImpl> provider, |
46 MessageLoop* creator_loop); | 46 MessageLoop* creator_loop); |
47 virtual ~PollingThread(); | 47 virtual ~PollingThread(); |
48 | 48 |
49 // Method for finding a suitable DataFetcher and starting the polling. | 49 // Method for finding a suitable DataFetcher and starting the polling. |
50 void Initialize(const std::vector<DataFetcherFactory>& factories); | 50 void Initialize(DataFetcherFactory factory); |
51 | 51 |
52 private: | 52 private: |
53 // Method for polling a DataFetcher. | 53 // Method for polling a DataFetcher. |
54 void DoPoll(); | 54 void DoPoll(); |
55 void ScheduleDoPoll(); | 55 void ScheduleDoPoll(); |
56 | 56 |
57 // Schedule a notification to the |provider_| which lives on a different | 57 // Schedule a notification to the |provider_| which lives on a different |
58 // thread (|creator_loop_| is its message loop). | 58 // thread (|creator_loop_| is its message loop). |
59 void ScheduleDoNotify(const Orientation& orientation); | 59 void ScheduleDoNotify(const Orientation& orientation); |
60 | 60 |
(...skipping 18 matching lines...) Expand all Loading... |
79 base::WeakPtr<ProviderImpl> provider, | 79 base::WeakPtr<ProviderImpl> provider, |
80 MessageLoop* creator_loop) | 80 MessageLoop* creator_loop) |
81 : base::Thread(name), | 81 : base::Thread(name), |
82 creator_loop_(creator_loop), | 82 creator_loop_(creator_loop), |
83 provider_(provider) { | 83 provider_(provider) { |
84 } | 84 } |
85 | 85 |
86 ProviderImpl::PollingThread::~PollingThread() { | 86 ProviderImpl::PollingThread::~PollingThread() { |
87 } | 87 } |
88 | 88 |
89 void ProviderImpl::PollingThread::Initialize( | 89 void ProviderImpl::PollingThread::Initialize(DataFetcherFactory factory) { |
90 const std::vector<DataFetcherFactory>& factories) { | |
91 DCHECK(MessageLoop::current() == message_loop()); | 90 DCHECK(MessageLoop::current() == message_loop()); |
92 | 91 |
93 typedef std::vector<DataFetcherFactory>::const_iterator Iterator; | 92 if (factory != NULL) { |
94 for (Iterator i = factories.begin(); i != factories.end(); ++i) { | 93 // Try to use factory to create a fetcher that can provide orientation data. |
95 DataFetcherFactory factory = *i; | |
96 scoped_ptr<DataFetcher> fetcher(factory()); | 94 scoped_ptr<DataFetcher> fetcher(factory()); |
97 Orientation orientation; | 95 Orientation orientation; |
98 | 96 |
99 if (fetcher.get() && fetcher->GetOrientation(&orientation)) { | 97 if (fetcher.get() && fetcher->GetOrientation(&orientation)) { |
100 // Pass ownership of fetcher to provider_. | 98 // Pass ownership of fetcher to provider_. |
101 data_fetcher_.swap(fetcher); | 99 data_fetcher_.swap(fetcher); |
102 last_orientation_ = orientation; | 100 last_orientation_ = orientation; |
103 | 101 |
104 // Notify observers. | 102 // Notify observers. |
105 if (!orientation.is_empty()) | 103 if (!orientation.is_empty()) |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { | 174 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { |
177 DCHECK(MessageLoop::current() == message_loop()); | 175 DCHECK(MessageLoop::current() == message_loop()); |
178 DCHECK(data_fetcher_.get()); | 176 DCHECK(data_fetcher_.get()); |
179 | 177 |
180 // TODO(erg): There used to be unused code here, that called a default | 178 // TODO(erg): There used to be unused code here, that called a default |
181 // implementation on the DataFetcherInterface that was never defined. I'm | 179 // implementation on the DataFetcherInterface that was never defined. I'm |
182 // removing unused methods from headers. | 180 // removing unused methods from headers. |
183 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); | 181 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); |
184 } | 182 } |
185 | 183 |
186 ProviderImpl::ProviderImpl(const DataFetcherFactory factories[]) | 184 ProviderImpl::ProviderImpl(DataFetcherFactory factory) |
187 : creator_loop_(MessageLoop::current()), | 185 : creator_loop_(MessageLoop::current()), |
| 186 factory_(factory), |
188 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 187 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
189 polling_thread_(NULL) { | 188 polling_thread_(NULL) { |
190 for (const DataFetcherFactory* fp = factories; *fp; ++fp) | |
191 factories_.push_back(*fp); | |
192 } | 189 } |
193 | 190 |
194 ProviderImpl::~ProviderImpl() { | 191 ProviderImpl::~ProviderImpl() { |
195 Stop(); | 192 Stop(); |
196 } | 193 } |
197 | 194 |
198 void ProviderImpl::AddObserver(Observer* observer) { | 195 void ProviderImpl::AddObserver(Observer* observer) { |
199 DCHECK(MessageLoop::current() == creator_loop_); | 196 DCHECK(MessageLoop::current() == creator_loop_); |
200 | 197 |
201 observers_.insert(observer); | 198 observers_.insert(observer); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 241 } |
245 } | 242 } |
246 | 243 |
247 void ProviderImpl::ScheduleInitializePollingThread() { | 244 void ProviderImpl::ScheduleInitializePollingThread() { |
248 DCHECK(MessageLoop::current() == creator_loop_); | 245 DCHECK(MessageLoop::current() == creator_loop_); |
249 | 246 |
250 MessageLoop* polling_loop = polling_thread_->message_loop(); | 247 MessageLoop* polling_loop = polling_thread_->message_loop(); |
251 polling_loop->PostTask(FROM_HERE, | 248 polling_loop->PostTask(FROM_HERE, |
252 base::Bind(&PollingThread::Initialize, | 249 base::Bind(&PollingThread::Initialize, |
253 base::Unretained(polling_thread_), | 250 base::Unretained(polling_thread_), |
254 factories_)); | 251 factory_)); |
255 } | 252 } |
256 | 253 |
257 void ProviderImpl::DoNotify(const Orientation& orientation) { | 254 void ProviderImpl::DoNotify(const Orientation& orientation) { |
258 DCHECK(MessageLoop::current() == creator_loop_); | 255 DCHECK(MessageLoop::current() == creator_loop_); |
259 | 256 |
260 last_notification_ = orientation; | 257 last_notification_ = orientation; |
261 | 258 |
262 typedef std::set<Observer*>::const_iterator Iterator; | 259 typedef std::set<Observer*>::const_iterator Iterator; |
263 for (Iterator i = observers_.begin(); i != observers_.end(); ++i) | 260 for (Iterator i = observers_.begin(); i != observers_.end(); ++i) |
264 (*i)->OnOrientationUpdate(orientation); | 261 (*i)->OnOrientationUpdate(orientation); |
265 | 262 |
266 if (orientation.is_empty()) { | 263 if (orientation.is_empty()) { |
267 // Notify observers about failure to provide data exactly once. | 264 // Notify observers about failure to provide data exactly once. |
268 observers_.clear(); | 265 observers_.clear(); |
269 Stop(); | 266 Stop(); |
270 } | 267 } |
271 } | 268 } |
272 | 269 |
273 } // namespace device_orientation | 270 } // namespace device_orientation |
OLD | NEW |