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 "content/browser/device_orientation/provider_impl.h" | 5 #include "content/browser/device_orientation/provider_impl.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
15 | 15 |
16 #if defined(OS_ANDROID) | |
17 #include "content/browser/device_orientation/data_fetcher_impl_android.h" | |
18 #elif defined(OS_WIN) | |
19 #include "content/browser/device_orientation/data_fetcher_impl_win.h" | |
20 #endif | |
21 | |
16 namespace { | 22 namespace { |
17 | 23 |
18 void DeleteThread(base::Thread* thread) { | 24 void DeleteThread(base::Thread* thread) { |
19 delete thread; | 25 delete thread; |
20 } | 26 } |
21 | 27 |
22 } | 28 } |
23 | 29 |
24 namespace content { | 30 namespace content { |
25 | 31 |
(...skipping 21 matching lines...) Expand all Loading... | |
47 void ScheduleDoNotify(const scoped_refptr<const DeviceData>& device_data, | 53 void ScheduleDoNotify(const scoped_refptr<const DeviceData>& device_data, |
48 DeviceData::Type device_data_type); | 54 DeviceData::Type device_data_type); |
49 | 55 |
50 enum { kDesiredSamplingIntervalMs = 100 }; | 56 enum { kDesiredSamplingIntervalMs = 100 }; |
51 base::TimeDelta SamplingInterval() const; | 57 base::TimeDelta SamplingInterval() const; |
52 | 58 |
53 // The Message Loop on which this object was created. | 59 // The Message Loop on which this object was created. |
54 // Typically the I/O loop, but may be something else during testing. | 60 // Typically the I/O loop, but may be something else during testing. |
55 base::MessageLoop* creator_loop_; | 61 base::MessageLoop* creator_loop_; |
56 | 62 |
57 scoped_ptr<DataFetcher> data_fetcher_; | 63 DataFetcher* data_fetcher_; |
64 | |
58 std::map<DeviceData::Type, scoped_refptr<const DeviceData> > | 65 std::map<DeviceData::Type, scoped_refptr<const DeviceData> > |
59 last_device_data_map_; | 66 last_device_data_map_; |
60 std::set<DeviceData::Type> polling_data_types_; | 67 std::set<DeviceData::Type> polling_data_types_; |
61 | 68 |
62 base::WeakPtr<ProviderImpl> provider_; | 69 base::WeakPtr<ProviderImpl> provider_; |
63 }; | 70 }; |
64 | 71 |
65 ProviderImpl::PollingThread::PollingThread(const char* name, | 72 ProviderImpl::PollingThread::PollingThread(const char* name, |
66 base::WeakPtr<ProviderImpl> provider, | 73 base::WeakPtr<ProviderImpl> provider, |
67 base::MessageLoop* creator_loop) | 74 base::MessageLoop* creator_loop) |
68 : base::Thread(name), creator_loop_(creator_loop), provider_(provider) {} | 75 : base::Thread(name), creator_loop_(creator_loop), provider_(provider) {} |
69 | 76 |
70 ProviderImpl::PollingThread::~PollingThread() { | 77 ProviderImpl::PollingThread::~PollingThread() { |
78 #if defined(OS_ANDROID) | |
bulach
2013/07/10 16:59:41
// TODO(timvolodine): Remove this platform depende
timvolodine
2013/07/11 14:31:34
N/A anymore.
Done.
| |
79 static_cast<DataFetcherImplAndroid*>(data_fetcher_)->Stop( | |
80 DeviceData::kTypeOrientation); | |
81 #elif defined(OS_WIN) | |
82 static_cast<DataFetcherImplWin*>(data_fetcher_)->Stop(); | |
83 #endif | |
71 Stop(); | 84 Stop(); |
72 } | 85 } |
73 | 86 |
74 void ProviderImpl::PollingThread::DoAddPollingDataType(DeviceData::Type type) { | 87 void ProviderImpl::PollingThread::DoAddPollingDataType(DeviceData::Type type) { |
75 DCHECK(base::MessageLoop::current() == message_loop()); | 88 DCHECK(base::MessageLoop::current() == message_loop()); |
76 | 89 |
77 polling_data_types_.insert(type); | 90 polling_data_types_.insert(type); |
78 } | 91 } |
79 | 92 |
80 void ProviderImpl::PollingThread::Initialize(DataFetcherFactory factory, | 93 void ProviderImpl::PollingThread::Initialize(DataFetcherFactory factory, |
81 DeviceData::Type type) { | 94 DeviceData::Type type) { |
82 DCHECK(base::MessageLoop::current() == message_loop()); | 95 DCHECK(base::MessageLoop::current() == message_loop()); |
83 | 96 |
84 if (factory != NULL) { | 97 if (factory != NULL) { |
85 // Try to use factory to create a fetcher that can provide this type of | 98 // Try to use factory to create a fetcher that can provide this type of |
86 // data. If factory creates a fetcher that provides this type of data, | 99 // data. If factory creates a fetcher that provides this type of data, |
87 // start polling. | 100 // start polling. |
88 scoped_ptr<DataFetcher> fetcher(factory()); | 101 DataFetcher* fetcher = factory(); |
bulach
2013/07/10 16:59:41
hmm... factory() normally means it'll create an ob
timvolodine
2013/07/11 14:31:34
Sure. I've refactored the code and added a new cla
| |
89 | 102 |
90 if (fetcher) { | 103 if (fetcher) { |
91 scoped_refptr<const DeviceData> device_data(fetcher->GetDeviceData(type)); | 104 scoped_refptr<const DeviceData> device_data(fetcher->GetDeviceData(type)); |
92 if (device_data.get() != NULL) { | 105 if (device_data.get() != NULL) { |
93 // Pass ownership of fetcher to provider_. | 106 // Pass ownership of fetcher to provider_. |
bulach
2013/07/10 16:59:41
remove comment?
timvolodine
2013/07/11 14:31:34
N/A anymore.
Done.
| |
94 data_fetcher_.swap(fetcher); | 107 data_fetcher_ = fetcher; |
95 last_device_data_map_[type] = device_data; | 108 last_device_data_map_[type] = device_data; |
96 | 109 |
97 // Notify observers. | 110 // Notify observers. |
98 ScheduleDoNotify(device_data, type); | 111 ScheduleDoNotify(device_data, type); |
99 | 112 |
100 // Start polling. | 113 // Start polling. |
101 ScheduleDoPoll(); | 114 ScheduleDoPoll(); |
102 return; | 115 return; |
103 } | 116 } |
104 } | 117 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 DCHECK(base::MessageLoop::current() == message_loop()); | 164 DCHECK(base::MessageLoop::current() == message_loop()); |
152 | 165 |
153 message_loop()->PostDelayedTask( | 166 message_loop()->PostDelayedTask( |
154 FROM_HERE, | 167 FROM_HERE, |
155 base::Bind(&PollingThread::DoPoll, base::Unretained(this)), | 168 base::Bind(&PollingThread::DoPoll, base::Unretained(this)), |
156 SamplingInterval()); | 169 SamplingInterval()); |
157 } | 170 } |
158 | 171 |
159 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { | 172 base::TimeDelta ProviderImpl::PollingThread::SamplingInterval() const { |
160 DCHECK(base::MessageLoop::current() == message_loop()); | 173 DCHECK(base::MessageLoop::current() == message_loop()); |
161 DCHECK(data_fetcher_.get()); | 174 DCHECK(data_fetcher_); |
162 | 175 |
163 // TODO(erg): There used to be unused code here, that called a default | 176 // TODO(erg): There used to be unused code here, that called a default |
164 // implementation on the DataFetcherInterface that was never defined. I'm | 177 // implementation on the DataFetcherInterface that was never defined. I'm |
165 // removing unused methods from headers. | 178 // removing unused methods from headers. |
166 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); | 179 return base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs); |
167 } | 180 } |
168 | 181 |
169 ProviderImpl::ProviderImpl(DataFetcherFactory factory) | 182 ProviderImpl::ProviderImpl(DataFetcherFactory factory) |
170 : creator_loop_(base::MessageLoop::current()), | 183 : creator_loop_(base::MessageLoop::current()), |
171 factory_(factory), | 184 factory_(factory), |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 observers_.erase(current); | 296 observers_.erase(current); |
284 } | 297 } |
285 | 298 |
286 if (observers_.empty()) | 299 if (observers_.empty()) |
287 Stop(); | 300 Stop(); |
288 } | 301 } |
289 } | 302 } |
290 | 303 |
291 | 304 |
292 } // namespace content | 305 } // namespace content |
OLD | NEW |