| 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" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 DCHECK(base::MessageLoop::current() == message_loop()); | 82 DCHECK(base::MessageLoop::current() == message_loop()); |
| 83 | 83 |
| 84 if (factory != NULL) { | 84 if (factory != NULL) { |
| 85 // Try to use factory to create a fetcher that can provide this type of | 85 // 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, | 86 // data. If factory creates a fetcher that provides this type of data, |
| 87 // start polling. | 87 // start polling. |
| 88 scoped_ptr<DataFetcher> fetcher(factory()); | 88 scoped_ptr<DataFetcher> fetcher(factory()); |
| 89 | 89 |
| 90 if (fetcher) { | 90 if (fetcher) { |
| 91 scoped_refptr<const DeviceData> device_data(fetcher->GetDeviceData(type)); | 91 scoped_refptr<const DeviceData> device_data(fetcher->GetDeviceData(type)); |
| 92 if (device_data != NULL) { | 92 if (device_data.get() != NULL) { |
| 93 // Pass ownership of fetcher to provider_. | 93 // Pass ownership of fetcher to provider_. |
| 94 data_fetcher_.swap(fetcher); | 94 data_fetcher_.swap(fetcher); |
| 95 last_device_data_map_[type] = device_data; | 95 last_device_data_map_[type] = device_data; |
| 96 | 96 |
| 97 // Notify observers. | 97 // Notify observers. |
| 98 ScheduleDoNotify(device_data, type); | 98 ScheduleDoNotify(device_data, type); |
| 99 | 99 |
| 100 // Start polling. | 100 // Start polling. |
| 101 ScheduleDoPoll(); | 101 ScheduleDoPoll(); |
| 102 return; | 102 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 122 DCHECK(base::MessageLoop::current() == message_loop()); | 122 DCHECK(base::MessageLoop::current() == message_loop()); |
| 123 | 123 |
| 124 // Poll the fetcher for each type of data. | 124 // Poll the fetcher for each type of data. |
| 125 typedef std::set<DeviceData::Type>::const_iterator SetIterator; | 125 typedef std::set<DeviceData::Type>::const_iterator SetIterator; |
| 126 for (SetIterator i = polling_data_types_.begin(); | 126 for (SetIterator i = polling_data_types_.begin(); |
| 127 i != polling_data_types_.end(); ++i) { | 127 i != polling_data_types_.end(); ++i) { |
| 128 DeviceData::Type device_data_type = *i; | 128 DeviceData::Type device_data_type = *i; |
| 129 scoped_refptr<const DeviceData> device_data(data_fetcher_->GetDeviceData( | 129 scoped_refptr<const DeviceData> device_data(data_fetcher_->GetDeviceData( |
| 130 device_data_type)); | 130 device_data_type)); |
| 131 | 131 |
| 132 if (device_data == NULL) { | 132 if (device_data.get() == NULL) { |
| 133 LOG(ERROR) << "Failed to poll device data fetcher."; | 133 LOG(ERROR) << "Failed to poll device data fetcher."; |
| 134 ScheduleDoNotify(NULL, device_data_type); | 134 ScheduleDoNotify(NULL, device_data_type); |
| 135 continue; | 135 continue; |
| 136 } | 136 } |
| 137 | 137 |
| 138 const DeviceData* old_data = last_device_data_map_[device_data_type]; | 138 const DeviceData* old_data = last_device_data_map_[device_data_type].get(); |
| 139 if (old_data != NULL && !device_data->ShouldFireEvent(old_data)) | 139 if (old_data != NULL && !device_data->ShouldFireEvent(old_data)) |
| 140 continue; | 140 continue; |
| 141 | 141 |
| 142 // Update the last device data of this type and notify observers. | 142 // Update the last device data of this type and notify observers. |
| 143 last_device_data_map_[device_data_type] = device_data; | 143 last_device_data_map_[device_data_type] = device_data; |
| 144 ScheduleDoNotify(device_data, device_data_type); | 144 ScheduleDoNotify(device_data, device_data_type); |
| 145 } | 145 } |
| 146 | 146 |
| 147 ScheduleDoPoll(); | 147 ScheduleDoPoll(); |
| 148 } | 148 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 void ProviderImpl::AddObserver(Observer* observer) { | 190 void ProviderImpl::AddObserver(Observer* observer) { |
| 191 DCHECK(base::MessageLoop::current() == creator_loop_); | 191 DCHECK(base::MessageLoop::current() == creator_loop_); |
| 192 | 192 |
| 193 DeviceData::Type type = observer->device_data_type(); | 193 DeviceData::Type type = observer->device_data_type(); |
| 194 | 194 |
| 195 observers_.insert(observer); | 195 observers_.insert(observer); |
| 196 if (observers_.size() == 1) | 196 if (observers_.size() == 1) |
| 197 Start(type); | 197 Start(type); |
| 198 else { | 198 else { |
| 199 // Notify observer of most recent notification if one exists. | 199 // Notify observer of most recent notification if one exists. |
| 200 const DeviceData *last_notification = last_notifications_map_[type]; | 200 const DeviceData* last_notification = last_notifications_map_[type].get(); |
| 201 if (last_notification != NULL) | 201 if (last_notification != NULL) |
| 202 observer->OnDeviceDataUpdate(last_notification, type); | 202 observer->OnDeviceDataUpdate(last_notification, type); |
| 203 } | 203 } |
| 204 | 204 |
| 205 ScheduleDoAddPollingDataType(type); | 205 ScheduleDoAddPollingDataType(type); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void ProviderImpl::RemoveObserver(Observer* observer) { | 208 void ProviderImpl::RemoveObserver(Observer* observer) { |
| 209 DCHECK(base::MessageLoop::current() == creator_loop_); | 209 DCHECK(base::MessageLoop::current() == creator_loop_); |
| 210 | 210 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // Update last notification of this type. | 266 // Update last notification of this type. |
| 267 last_notifications_map_[device_data_type] = data; | 267 last_notifications_map_[device_data_type] = data; |
| 268 | 268 |
| 269 // Notify observers of this type of the new data. | 269 // Notify observers of this type of the new data. |
| 270 typedef std::set<Observer*>::const_iterator ConstIterator; | 270 typedef std::set<Observer*>::const_iterator ConstIterator; |
| 271 for (ConstIterator i = observers_.begin(); i != observers_.end(); ++i) { | 271 for (ConstIterator i = observers_.begin(); i != observers_.end(); ++i) { |
| 272 if ((*i)->device_data_type() == device_data_type) | 272 if ((*i)->device_data_type() == device_data_type) |
| 273 (*i)->OnDeviceDataUpdate(data.get(), device_data_type); | 273 (*i)->OnDeviceDataUpdate(data.get(), device_data_type); |
| 274 } | 274 } |
| 275 | 275 |
| 276 if (data == NULL) { | 276 if (data.get() == NULL) { |
| 277 // Notify observers exactly once about failure to provide data. | 277 // Notify observers exactly once about failure to provide data. |
| 278 typedef std::set<Observer*>::iterator Iterator; | 278 typedef std::set<Observer*>::iterator Iterator; |
| 279 Iterator i = observers_.begin(); | 279 Iterator i = observers_.begin(); |
| 280 while (i != observers_.end()) { | 280 while (i != observers_.end()) { |
| 281 Iterator current = i++; | 281 Iterator current = i++; |
| 282 if ((*current)->device_data_type() == device_data_type) | 282 if ((*current)->device_data_type() == device_data_type) |
| 283 observers_.erase(current); | 283 observers_.erase(current); |
| 284 } | 284 } |
| 285 | 285 |
| 286 if (observers_.empty()) | 286 if (observers_.empty()) |
| 287 Stop(); | 287 Stop(); |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 | 290 |
| 291 | 291 |
| 292 } // namespace content | 292 } // namespace content |
| OLD | NEW |