| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "base/task.h" | |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "content/browser/device_orientation/orientation.h" | 14 #include "content/browser/device_orientation/orientation.h" |
| 15 #include "content/browser/device_orientation/provider_impl.h" | 15 #include "content/browser/device_orientation/provider_impl.h" |
| 16 | 16 |
| 17 namespace device_orientation { | 17 namespace device_orientation { |
| 18 | 18 |
| 19 ProviderImpl::ProviderImpl(const DataFetcherFactory factories[]) | 19 ProviderImpl::ProviderImpl(const DataFetcherFactory factories[]) |
| 20 : creator_loop_(MessageLoop::current()), | 20 : creator_loop_(MessageLoop::current()), |
| 21 ALLOW_THIS_IN_INITIALIZER_LIST(do_poll_method_factory_(this)) { | 21 ALLOW_THIS_IN_INITIALIZER_LIST(do_poll_method_factory_(this)) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 // When no orientation data can be provided. | 94 // When no orientation data can be provided. |
| 95 ScheduleDoNotify(Orientation::Empty()); | 95 ScheduleDoNotify(Orientation::Empty()); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void ProviderImpl::ScheduleInitializePollingThread() { | 98 void ProviderImpl::ScheduleInitializePollingThread() { |
| 99 DCHECK(MessageLoop::current() == creator_loop_); | 99 DCHECK(MessageLoop::current() == creator_loop_); |
| 100 | 100 |
| 101 Task* task = NewRunnableMethod(this, | |
| 102 &ProviderImpl::DoInitializePollingThread, | |
| 103 factories_); | |
| 104 MessageLoop* polling_loop = polling_thread_->message_loop(); | 101 MessageLoop* polling_loop = polling_thread_->message_loop(); |
| 105 polling_loop->PostTask(FROM_HERE, task); | 102 polling_loop->PostTask(FROM_HERE, |
| 103 base::Bind(&ProviderImpl::DoInitializePollingThread, |
| 104 this, |
| 105 factories_)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void ProviderImpl::DoNotify(const Orientation& orientation) { | 108 void ProviderImpl::DoNotify(const Orientation& orientation) { |
| 109 DCHECK(MessageLoop::current() == creator_loop_); | 109 DCHECK(MessageLoop::current() == creator_loop_); |
| 110 | 110 |
| 111 last_notification_ = orientation; | 111 last_notification_ = orientation; |
| 112 | 112 |
| 113 typedef std::set<Observer*>::const_iterator Iterator; | 113 typedef std::set<Observer*>::const_iterator Iterator; |
| 114 for (Iterator i = observers_.begin(), e = observers_.end(); i != e; ++i) | 114 for (Iterator i = observers_.begin(), e = observers_.end(); i != e; ++i) |
| 115 (*i)->OnOrientationUpdate(orientation); | 115 (*i)->OnOrientationUpdate(orientation); |
| 116 | 116 |
| 117 if (orientation.IsEmpty()) { | 117 if (orientation.IsEmpty()) { |
| 118 // Notify observers about failure to provide data exactly once. | 118 // Notify observers about failure to provide data exactly once. |
| 119 observers_.clear(); | 119 observers_.clear(); |
| 120 Stop(); | 120 Stop(); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 void ProviderImpl::ScheduleDoNotify(const Orientation& orientation) { | 124 void ProviderImpl::ScheduleDoNotify(const Orientation& orientation) { |
| 125 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | 125 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); |
| 126 | 126 |
| 127 Task* task = NewRunnableMethod(this, &ProviderImpl::DoNotify, orientation); | 127 creator_loop_->PostTask( |
| 128 creator_loop_->PostTask(FROM_HERE, task); | 128 FROM_HERE, base::Bind(&ProviderImpl::DoNotify, this, orientation)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void ProviderImpl::DoPoll() { | 131 void ProviderImpl::DoPoll() { |
| 132 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | 132 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); |
| 133 | 133 |
| 134 Orientation orientation; | 134 Orientation orientation; |
| 135 if (!data_fetcher_->GetOrientation(&orientation)) { | 135 if (!data_fetcher_->GetOrientation(&orientation)) { |
| 136 LOG(ERROR) << "Failed to poll device orientation data fetcher."; | 136 LOG(ERROR) << "Failed to poll device orientation data fetcher."; |
| 137 | 137 |
| 138 ScheduleDoNotify(Orientation::Empty()); | 138 ScheduleDoNotify(Orientation::Empty()); |
| 139 return; | 139 return; |
| 140 } | 140 } |
| 141 | 141 |
| 142 if (SignificantlyDifferent(orientation, last_orientation_)) { | 142 if (SignificantlyDifferent(orientation, last_orientation_)) { |
| 143 last_orientation_ = orientation; | 143 last_orientation_ = orientation; |
| 144 ScheduleDoNotify(orientation); | 144 ScheduleDoNotify(orientation); |
| 145 } | 145 } |
| 146 | 146 |
| 147 ScheduleDoPoll(); | 147 ScheduleDoPoll(); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void ProviderImpl::ScheduleDoPoll() { | 150 void ProviderImpl::ScheduleDoPoll() { |
| 151 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | 151 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); |
| 152 | 152 |
| 153 Task* task = do_poll_method_factory_.NewRunnableMethod(&ProviderImpl::DoPoll); | |
| 154 MessageLoop* polling_loop = polling_thread_->message_loop(); | 153 MessageLoop* polling_loop = polling_thread_->message_loop(); |
| 155 polling_loop->PostDelayedTask(FROM_HERE, task, SamplingIntervalMs()); | 154 polling_loop->PostDelayedTask( |
| 155 FROM_HERE, |
| 156 base::Bind(&ProviderImpl::DoPoll, do_poll_method_factory_.GetWeakPtr()), |
| 157 SamplingIntervalMs()); |
| 156 } | 158 } |
| 157 | 159 |
| 158 namespace { | 160 namespace { |
| 159 | 161 |
| 160 bool IsElementSignificantlyDifferent(bool can_provide_element1, | 162 bool IsElementSignificantlyDifferent(bool can_provide_element1, |
| 161 bool can_provide_element2, | 163 bool can_provide_element2, |
| 162 double element1, | 164 double element1, |
| 163 double element2) { | 165 double element2) { |
| 164 const double kThreshold = 0.1; | 166 const double kThreshold = 0.1; |
| 165 | 167 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 194 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | 196 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); |
| 195 DCHECK(data_fetcher_.get()); | 197 DCHECK(data_fetcher_.get()); |
| 196 | 198 |
| 197 // TODO(erg): There used to be unused code here, that called a default | 199 // TODO(erg): There used to be unused code here, that called a default |
| 198 // implementation on the DataFetcherInterface that was never defined. I'm | 200 // implementation on the DataFetcherInterface that was never defined. I'm |
| 199 // removing unused methods from headers. | 201 // removing unused methods from headers. |
| 200 return kDesiredSamplingIntervalMs; | 202 return kDesiredSamplingIntervalMs; |
| 201 } | 203 } |
| 202 | 204 |
| 203 } // namespace device_orientation | 205 } // namespace device_orientation |
| OLD | NEW |