OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_orientation/data_fetcher_shared_memory_base.h" | 5 #include "content/browser/device_orientation/data_fetcher_shared_memory_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
| 12 #include "content/common/device_light/device_light_hardware_buffer.h" |
12 #include "content/common/device_orientation/device_motion_hardware_buffer.h" | 13 #include "content/common/device_orientation/device_motion_hardware_buffer.h" |
13 #include "content/common/device_orientation/device_orientation_hardware_buffer.h
" | 14 #include "content/common/device_orientation/device_orientation_hardware_buffer.h
" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 static size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { | 20 static size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { |
20 switch (consumer_type) { | 21 switch (consumer_type) { |
21 case CONSUMER_TYPE_MOTION: | 22 case CONSUMER_TYPE_MOTION: |
22 return sizeof(DeviceMotionHardwareBuffer); | 23 return sizeof(DeviceMotionHardwareBuffer); |
23 case CONSUMER_TYPE_ORIENTATION: | 24 case CONSUMER_TYPE_ORIENTATION: |
24 return sizeof(DeviceOrientationHardwareBuffer); | 25 return sizeof(DeviceOrientationHardwareBuffer); |
| 26 case CONSUMER_TYPE_LIGHT: |
| 27 return sizeof(DeviceLightHardwareBuffer); |
25 default: | 28 default: |
26 NOTREACHED(); | 29 NOTREACHED(); |
27 } | 30 } |
28 return 0; | 31 return 0; |
29 } | 32 } |
30 | 33 |
31 } | 34 } // namespace |
32 | 35 |
33 class DataFetcherSharedMemoryBase::PollingThread : public base::Thread { | 36 class DataFetcherSharedMemoryBase::PollingThread : public base::Thread { |
34 public: | 37 public: |
35 PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher); | 38 PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher); |
36 virtual ~PollingThread(); | 39 virtual ~PollingThread(); |
37 | 40 |
38 void AddConsumer(ConsumerType consumer_type, void* buffer); | 41 void AddConsumer(ConsumerType consumer_type, void* buffer); |
39 void RemoveConsumer(ConsumerType consumer_type); | 42 void RemoveConsumer(ConsumerType consumer_type); |
40 | 43 |
41 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } | 44 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } |
42 bool IsTimerRunning() const { return timer_ ? timer_->IsRunning() : false; } | 45 bool IsTimerRunning() const { return timer_ ? timer_->IsRunning() : false; } |
43 | 46 |
44 private: | 47 private: |
45 | |
46 void DoPoll(); | 48 void DoPoll(); |
47 | 49 |
48 unsigned consumers_bitmask_; | 50 unsigned consumers_bitmask_; |
49 DataFetcherSharedMemoryBase* fetcher_; | 51 DataFetcherSharedMemoryBase* fetcher_; |
50 scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; | 52 scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; |
51 | 53 |
52 DISALLOW_COPY_AND_ASSIGN(PollingThread); | 54 DISALLOW_COPY_AND_ASSIGN(PollingThread); |
53 }; | 55 }; |
54 | 56 |
55 // --- PollingThread methods | 57 // --- PollingThread methods |
(...skipping 26 matching lines...) Expand all Loading... |
82 | 84 |
83 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( | 85 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( |
84 ConsumerType consumer_type) { | 86 ConsumerType consumer_type) { |
85 DCHECK(fetcher_); | 87 DCHECK(fetcher_); |
86 if (!fetcher_->Stop(consumer_type)) | 88 if (!fetcher_->Stop(consumer_type)) |
87 return; | 89 return; |
88 | 90 |
89 consumers_bitmask_ ^= consumer_type; | 91 consumers_bitmask_ ^= consumer_type; |
90 | 92 |
91 if (!consumers_bitmask_) | 93 if (!consumers_bitmask_) |
92 timer_.reset(); // will also stop the timer. | 94 timer_.reset(); // will also stop the timer. |
93 } | 95 } |
94 | 96 |
95 void DataFetcherSharedMemoryBase::PollingThread::DoPoll() { | 97 void DataFetcherSharedMemoryBase::PollingThread::DoPoll() { |
96 DCHECK(fetcher_); | 98 DCHECK(fetcher_); |
97 DCHECK(consumers_bitmask_); | 99 DCHECK(consumers_bitmask_); |
98 fetcher_->Fetch(consumers_bitmask_); | 100 fetcher_->Fetch(consumers_bitmask_); |
99 } | 101 } |
100 | 102 |
101 // --- end of PollingThread methods | 103 // --- end of PollingThread methods |
102 | 104 |
103 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() | 105 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() |
104 : started_consumers_(0) { | 106 : started_consumers_(0) { |
105 } | 107 } |
106 | 108 |
107 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { | 109 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { |
108 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); | 110 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); |
109 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); | 111 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); |
| 112 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); |
110 | 113 |
111 // make sure polling thread stops asap. | 114 // make sure polling thread stops asap. |
112 if (polling_thread_) | 115 if (polling_thread_) |
113 polling_thread_->Stop(); | 116 polling_thread_->Stop(); |
114 | 117 |
115 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), | 118 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), |
116 shared_memory_map_.end()); | 119 shared_memory_map_.end()); |
117 } | 120 } |
118 | 121 |
119 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 122 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 239 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
237 return polling_thread_ ? polling_thread_->message_loop() : NULL; | 240 return polling_thread_ ? polling_thread_->message_loop() : NULL; |
238 } | 241 } |
239 | 242 |
240 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { | 243 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { |
241 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; | 244 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; |
242 } | 245 } |
243 | 246 |
244 | 247 |
245 } // namespace content | 248 } // namespace content |
OLD | NEW |