| OLD | NEW |
| 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 "content/browser/device_sensors/data_fetcher_shared_memory_base.h" | 5 #include "device/sensors/data_fetcher_shared_memory_base.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 18 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" | 18 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" |
| 19 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | 19 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" |
| 20 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | 20 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
| 21 | 21 |
| 22 namespace content { | 22 namespace device { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { | 26 size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { |
| 27 switch (consumer_type) { | 27 switch (consumer_type) { |
| 28 case CONSUMER_TYPE_MOTION: | 28 case CONSUMER_TYPE_MOTION: |
| 29 return sizeof(DeviceMotionHardwareBuffer); | 29 return sizeof(DeviceMotionHardwareBuffer); |
| 30 case CONSUMER_TYPE_ORIENTATION: | 30 case CONSUMER_TYPE_ORIENTATION: |
| 31 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | 31 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: |
| 32 return sizeof(DeviceOrientationHardwareBuffer); | 32 return sizeof(DeviceOrientationHardwareBuffer); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 57 unsigned consumers_bitmask_; | 57 unsigned consumers_bitmask_; |
| 58 DataFetcherSharedMemoryBase* fetcher_; | 58 DataFetcherSharedMemoryBase* fetcher_; |
| 59 std::unique_ptr<base::RepeatingTimer> timer_; | 59 std::unique_ptr<base::RepeatingTimer> timer_; |
| 60 | 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(PollingThread); | 61 DISALLOW_COPY_AND_ASSIGN(PollingThread); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 // --- PollingThread methods | 64 // --- PollingThread methods |
| 65 | 65 |
| 66 DataFetcherSharedMemoryBase::PollingThread::PollingThread( | 66 DataFetcherSharedMemoryBase::PollingThread::PollingThread( |
| 67 const char* name, DataFetcherSharedMemoryBase* fetcher) | 67 const char* name, |
| 68 : base::Thread(name), | 68 DataFetcherSharedMemoryBase* fetcher) |
| 69 consumers_bitmask_(0), | 69 : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) {} |
| 70 fetcher_(fetcher) { | |
| 71 } | |
| 72 | 70 |
| 73 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { | 71 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() {} |
| 74 } | |
| 75 | 72 |
| 76 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( | 73 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( |
| 77 ConsumerType consumer_type, void* buffer) { | 74 ConsumerType consumer_type, |
| 75 void* buffer) { |
| 78 DCHECK(fetcher_); | 76 DCHECK(fetcher_); |
| 79 if (!fetcher_->Start(consumer_type, buffer)) | 77 if (!fetcher_->Start(consumer_type, buffer)) |
| 80 return; | 78 return; |
| 81 | 79 |
| 82 consumers_bitmask_ |= consumer_type; | 80 consumers_bitmask_ |= consumer_type; |
| 83 | 81 |
| 84 if (!timer_ && fetcher_->GetType() == FETCHER_TYPE_POLLING_CALLBACK) { | 82 if (!timer_ && fetcher_->GetType() == FETCHER_TYPE_POLLING_CALLBACK) { |
| 85 timer_.reset(new base::RepeatingTimer()); | 83 timer_.reset(new base::RepeatingTimer()); |
| 86 timer_->Start(FROM_HERE, | 84 timer_->Start(FROM_HERE, fetcher_->GetInterval(), this, |
| 87 fetcher_->GetInterval(), | 85 &PollingThread::DoPoll); |
| 88 this, &PollingThread::DoPoll); | |
| 89 } | 86 } |
| 90 } | 87 } |
| 91 | 88 |
| 92 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( | 89 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( |
| 93 ConsumerType consumer_type) { | 90 ConsumerType consumer_type) { |
| 94 DCHECK(fetcher_); | 91 DCHECK(fetcher_); |
| 95 if (!fetcher_->Stop(consumer_type)) | 92 if (!fetcher_->Stop(consumer_type)) |
| 96 return; | 93 return; |
| 97 | 94 |
| 98 consumers_bitmask_ &= ~consumer_type; | 95 consumers_bitmask_ &= ~consumer_type; |
| 99 | 96 |
| 100 if (!consumers_bitmask_) | 97 if (!consumers_bitmask_) |
| 101 timer_.reset(); // will also stop the timer. | 98 timer_.reset(); // will also stop the timer. |
| 102 } | 99 } |
| 103 | 100 |
| 104 void DataFetcherSharedMemoryBase::PollingThread::DoPoll() { | 101 void DataFetcherSharedMemoryBase::PollingThread::DoPoll() { |
| 105 DCHECK(fetcher_); | 102 DCHECK(fetcher_); |
| 106 DCHECK(consumers_bitmask_); | 103 DCHECK(consumers_bitmask_); |
| 107 fetcher_->Fetch(consumers_bitmask_); | 104 fetcher_->Fetch(consumers_bitmask_); |
| 108 } | 105 } |
| 109 | 106 |
| 110 // --- end of PollingThread methods | 107 // --- end of PollingThread methods |
| 111 | 108 |
| 112 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() | 109 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() |
| 113 : started_consumers_(0) { | 110 : started_consumers_(0) {} |
| 114 } | |
| 115 | 111 |
| 116 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { | 112 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { |
| 117 DCHECK_EQ(0u, started_consumers_); | 113 DCHECK_EQ(0u, started_consumers_); |
| 118 | 114 |
| 119 // make sure polling thread stops asap. | 115 // make sure polling thread stops asap. |
| 120 if (polling_thread_) | 116 if (polling_thread_) |
| 121 polling_thread_->Stop(); | 117 polling_thread_->Stop(); |
| 122 } | 118 } |
| 123 | 119 |
| 124 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 120 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 LOG(ERROR) << "Failed to start sensor data polling thread"; | 193 LOG(ERROR) << "Failed to start sensor data polling thread"; |
| 198 return false; | 194 return false; |
| 199 } | 195 } |
| 200 return true; | 196 return true; |
| 201 } | 197 } |
| 202 | 198 |
| 203 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { | 199 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { |
| 204 NOTIMPLEMENTED(); | 200 NOTIMPLEMENTED(); |
| 205 } | 201 } |
| 206 | 202 |
| 207 DataFetcherSharedMemoryBase::FetcherType | 203 DataFetcherSharedMemoryBase::FetcherType DataFetcherSharedMemoryBase::GetType() |
| 208 DataFetcherSharedMemoryBase::GetType() const { | 204 const { |
| 209 return FETCHER_TYPE_DEFAULT; | 205 return FETCHER_TYPE_DEFAULT; |
| 210 } | 206 } |
| 211 | 207 |
| 212 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { | 208 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { |
| 213 return base::TimeDelta::FromMicroseconds(kDeviceSensorIntervalMicroseconds); | 209 return base::TimeDelta::FromMicroseconds(kDeviceSensorIntervalMicroseconds); |
| 214 } | 210 } |
| 215 | 211 |
| 216 void* DataFetcherSharedMemoryBase::GetSharedMemoryBuffer( | 212 void* DataFetcherSharedMemoryBase::GetSharedMemoryBuffer( |
| 217 ConsumerType consumer_type) { | 213 ConsumerType consumer_type) { |
| 218 auto it = shared_memory_map_.find(consumer_type); | 214 auto it = shared_memory_map_.find(consumer_type); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 236 } | 232 } |
| 237 | 233 |
| 238 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 234 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
| 239 return polling_thread_ ? polling_thread_->message_loop() : nullptr; | 235 return polling_thread_ ? polling_thread_->message_loop() : nullptr; |
| 240 } | 236 } |
| 241 | 237 |
| 242 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { | 238 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { |
| 243 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; | 239 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; |
| 244 } | 240 } |
| 245 | 241 |
| 246 } // namespace content | 242 } // namespace device |
| OLD | NEW |