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" |
(...skipping 18 matching lines...) Expand all Loading... |
29 return 0; | 29 return 0; |
30 } | 30 } |
31 | 31 |
32 } | 32 } |
33 | 33 |
34 class DataFetcherSharedMemoryBase::PollingThread : public base::Thread { | 34 class DataFetcherSharedMemoryBase::PollingThread : public base::Thread { |
35 public: | 35 public: |
36 PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher); | 36 PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher); |
37 virtual ~PollingThread(); | 37 virtual ~PollingThread(); |
38 | 38 |
39 void AddConsumer(ConsumerType consumer_type); | 39 void AddConsumer(ConsumerType consumer_type, void* buffer); |
40 void RemoveConsumer(ConsumerType consumer_type); | 40 void RemoveConsumer(ConsumerType consumer_type); |
41 | 41 |
42 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } | 42 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } |
43 | 43 |
44 private: | 44 private: |
45 | 45 |
46 void DoPoll(); | 46 void DoPoll(); |
47 | 47 |
48 unsigned consumers_bitmask_; | 48 unsigned consumers_bitmask_; |
49 DataFetcherSharedMemoryBase* fetcher_; | 49 DataFetcherSharedMemoryBase* fetcher_; |
50 scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; | 50 scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; |
51 | 51 |
52 DISALLOW_COPY_AND_ASSIGN(PollingThread); | 52 DISALLOW_COPY_AND_ASSIGN(PollingThread); |
53 }; | 53 }; |
54 | 54 |
55 // --- PollingThread methods | 55 // --- PollingThread methods |
56 | 56 |
57 DataFetcherSharedMemoryBase::PollingThread::PollingThread( | 57 DataFetcherSharedMemoryBase::PollingThread::PollingThread( |
58 const char* name, DataFetcherSharedMemoryBase* fetcher) | 58 const char* name, DataFetcherSharedMemoryBase* fetcher) |
59 : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) { | 59 : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) { |
60 } | 60 } |
61 | 61 |
62 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { | 62 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { |
63 } | 63 } |
64 | 64 |
65 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( | 65 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( |
66 ConsumerType consumer_type) { | 66 ConsumerType consumer_type, void* buffer) { |
67 DCHECK(fetcher_); | 67 DCHECK(fetcher_); |
68 if (!fetcher_->Start(consumer_type)) | 68 if (!fetcher_->Start(consumer_type, buffer)) |
69 return; | 69 return; |
70 | 70 |
71 consumers_bitmask_ |= consumer_type; | 71 consumers_bitmask_ |= consumer_type; |
72 | 72 |
73 if (!timer_) { | 73 if (!timer_) { |
74 timer_.reset(new base::RepeatingTimer<PollingThread>()); | 74 timer_.reset(new base::RepeatingTimer<PollingThread>()); |
75 timer_->Start(FROM_HERE, | 75 timer_->Start(FROM_HERE, |
76 base::TimeDelta::FromMilliseconds(kPeriodInMilliseconds), | 76 base::TimeDelta::FromMilliseconds(kPeriodInMilliseconds), |
77 this, &PollingThread::DoPoll); | 77 this, &PollingThread::DoPoll); |
78 } | 78 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 112 |
113 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), | 113 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), |
114 shared_memory_map_.end()); | 114 shared_memory_map_.end()); |
115 } | 115 } |
116 | 116 |
117 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 117 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
118 ConsumerType consumer_type) { | 118 ConsumerType consumer_type) { |
119 if (started_consumers_ & consumer_type) | 119 if (started_consumers_ & consumer_type) |
120 return true; | 120 return true; |
121 | 121 |
122 if (!GetSharedMemoryBuffer(consumer_type)) | 122 void* buffer = GetSharedMemoryBuffer(consumer_type); |
| 123 if (!buffer) |
123 return false; | 124 return false; |
124 | 125 |
125 if (IsPolling()) { | 126 if (IsPolling()) { |
126 if (!InitAndStartPollingThreadIfNecessary()) | 127 if (!InitAndStartPollingThreadIfNecessary()) |
127 return false; | 128 return false; |
128 polling_thread_->message_loop()->PostTask( | 129 polling_thread_->message_loop()->PostTask( |
129 FROM_HERE, | 130 FROM_HERE, |
130 base::Bind(&PollingThread::AddConsumer, | 131 base::Bind(&PollingThread::AddConsumer, |
131 base::Unretained(polling_thread_.get()), | 132 base::Unretained(polling_thread_.get()), |
132 consumer_type)); | 133 consumer_type, buffer)); |
133 } else { | 134 } else { |
134 if (!Start(consumer_type)) | 135 if (!Start(consumer_type, buffer)) |
135 return false; | 136 return false; |
136 } | 137 } |
137 | 138 |
138 started_consumers_ |= consumer_type; | 139 started_consumers_ |= consumer_type; |
139 | 140 |
140 return true; | 141 return true; |
141 } | 142 } |
142 | 143 |
143 bool DataFetcherSharedMemoryBase::StopFetchingDeviceData( | 144 bool DataFetcherSharedMemoryBase::StopFetchingDeviceData( |
144 ConsumerType consumer_type) { | 145 ConsumerType consumer_type) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 if (base::SharedMemory* shared_memory = GetSharedMemory(consumer_type)) | 224 if (base::SharedMemory* shared_memory = GetSharedMemory(consumer_type)) |
224 return shared_memory->memory(); | 225 return shared_memory->memory(); |
225 return NULL; | 226 return NULL; |
226 } | 227 } |
227 | 228 |
228 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 229 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
229 return polling_thread_ ? polling_thread_->message_loop() : NULL; | 230 return polling_thread_ ? polling_thread_->message_loop() : NULL; |
230 } | 231 } |
231 | 232 |
232 } // namespace content | 233 } // namespace content |
OLD | NEW |