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(const 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 19 matching lines...) Expand all Loading... | |
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( |
90 const std::vector<DataFetcherFactory>& factories) { | 90 const DataFetcherFactory* factory) { |
91 DCHECK(MessageLoop::current() == message_loop()); | 91 DCHECK(MessageLoop::current() == message_loop()); |
92 | 92 |
93 typedef std::vector<DataFetcherFactory>::const_iterator Iterator; | 93 if (factory != NULL) { |
hans
2012/08/01 19:08:47
i wonder if it would make the code simpler to chec
aousterh
2012/08/02 10:10:38
I don't think it would really be simpler, because
| |
94 for (Iterator i = factories.begin(); i != factories.end(); ++i) { | 94 // Try to use factory to create a fetcher that can provide orientation data. |
95 DataFetcherFactory factory = *i; | 95 DataFetcherFactory data_fetcher_factory = *factory; |
96 scoped_ptr<DataFetcher> fetcher(factory()); | 96 scoped_ptr<DataFetcher> fetcher(data_fetcher_factory()); |
97 Orientation orientation; | 97 Orientation orientation; |
98 | 98 |
99 if (fetcher.get() && fetcher->GetOrientation(&orientation)) { | 99 if (fetcher.get() && fetcher->GetOrientation(&orientation)) { |
100 // Pass ownership of fetcher to provider_. | 100 // Pass ownership of fetcher to provider_. |
101 data_fetcher_.swap(fetcher); | 101 data_fetcher_.swap(fetcher); |
102 last_orientation_ = orientation; | 102 last_orientation_ = orientation; |
103 | 103 |
104 // Notify observers. | 104 // Notify observers. |
105 if (!orientation.is_empty()) | 105 if (!orientation.is_empty()) |
106 ScheduleDoNotify(orientation); | 106 ScheduleDoNotify(orientation); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { | 176 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { |
177 DCHECK(MessageLoop::current() == message_loop()); | 177 DCHECK(MessageLoop::current() == message_loop()); |
178 DCHECK(data_fetcher_.get()); | 178 DCHECK(data_fetcher_.get()); |
179 | 179 |
180 // TODO(erg): There used to be unused code here, that called a default | 180 // TODO(erg): There used to be unused code here, that called a default |
181 // implementation on the DataFetcherInterface that was never defined. I'm | 181 // implementation on the DataFetcherInterface that was never defined. I'm |
182 // removing unused methods from headers. | 182 // removing unused methods from headers. |
183 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); | 183 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); |
184 } | 184 } |
185 | 185 |
186 ProviderImpl::ProviderImpl(const DataFetcherFactory factories[]) | 186 ProviderImpl::ProviderImpl(const DataFetcherFactory* factory) |
187 : creator_loop_(MessageLoop::current()), | 187 : creator_loop_(MessageLoop::current()), |
188 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 188 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
189 polling_thread_(NULL) { | 189 polling_thread_(NULL) { |
190 for (const DataFetcherFactory* fp = factories; *fp; ++fp) | 190 if (factory == NULL) |
191 factories_.push_back(*fp); | 191 factory_ = NULL; |
192 else | |
193 factory_ = new DataFetcherFactory(*factory); | |
hans
2012/08/01 19:08:47
this looks overly complex; i think it should just
aousterh
2012/08/02 10:10:38
Done.
| |
192 } | 194 } |
193 | 195 |
194 ProviderImpl::~ProviderImpl() { | 196 ProviderImpl::~ProviderImpl() { |
195 Stop(); | 197 Stop(); |
196 } | 198 } |
197 | 199 |
198 void ProviderImpl::AddObserver(Observer* observer) { | 200 void ProviderImpl::AddObserver(Observer* observer) { |
199 DCHECK(MessageLoop::current() == creator_loop_); | 201 DCHECK(MessageLoop::current() == creator_loop_); |
200 | 202 |
201 observers_.insert(observer); | 203 observers_.insert(observer); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 } | 246 } |
245 } | 247 } |
246 | 248 |
247 void ProviderImpl::ScheduleInitializePollingThread() { | 249 void ProviderImpl::ScheduleInitializePollingThread() { |
248 DCHECK(MessageLoop::current() == creator_loop_); | 250 DCHECK(MessageLoop::current() == creator_loop_); |
249 | 251 |
250 MessageLoop* polling_loop = polling_thread_->message_loop(); | 252 MessageLoop* polling_loop = polling_thread_->message_loop(); |
251 polling_loop->PostTask(FROM_HERE, | 253 polling_loop->PostTask(FROM_HERE, |
252 base::Bind(&PollingThread::Initialize, | 254 base::Bind(&PollingThread::Initialize, |
253 base::Unretained(polling_thread_), | 255 base::Unretained(polling_thread_), |
254 factories_)); | 256 factory_)); |
255 } | 257 } |
256 | 258 |
257 void ProviderImpl::DoNotify(const Orientation& orientation) { | 259 void ProviderImpl::DoNotify(const Orientation& orientation) { |
258 DCHECK(MessageLoop::current() == creator_loop_); | 260 DCHECK(MessageLoop::current() == creator_loop_); |
259 | 261 |
260 last_notification_ = orientation; | 262 last_notification_ = orientation; |
261 | 263 |
262 typedef std::set<Observer*>::const_iterator Iterator; | 264 typedef std::set<Observer*>::const_iterator Iterator; |
263 for (Iterator i = observers_.begin(); i != observers_.end(); ++i) | 265 for (Iterator i = observers_.begin(); i != observers_.end(); ++i) |
264 (*i)->OnOrientationUpdate(orientation); | 266 (*i)->OnOrientationUpdate(orientation); |
265 | 267 |
266 if (orientation.is_empty()) { | 268 if (orientation.is_empty()) { |
267 // Notify observers about failure to provide data exactly once. | 269 // Notify observers about failure to provide data exactly once. |
268 observers_.clear(); | 270 observers_.clear(); |
269 Stop(); | 271 Stop(); |
270 } | 272 } |
271 } | 273 } |
272 | 274 |
273 } // namespace device_orientation | 275 } // namespace device_orientation |
OLD | NEW |