| 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 #ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ |
| 6 #define CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | 6 #define CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "content/browser/device_orientation/data_fetcher.h" | 14 #include "content/browser/device_orientation/data_fetcher.h" |
| 15 #include "content/browser/device_orientation/orientation.h" | 15 #include "content/browser/device_orientation/orientation.h" |
| 16 #include "content/browser/device_orientation/provider.h" | 16 #include "content/browser/device_orientation/provider.h" |
| 17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 18 | 18 |
| 19 class MessageLoop; | 19 class MessageLoop; |
| 20 | 20 |
| 21 namespace base { | |
| 22 class Thread; | |
| 23 } | |
| 24 | |
| 25 namespace device_orientation { | 21 namespace device_orientation { |
| 26 | 22 |
| 27 class ProviderImpl : public Provider { | 23 class ProviderImpl : public Provider { |
| 28 public: | 24 public: |
| 29 typedef DataFetcher* (*DataFetcherFactory)(); | 25 typedef DataFetcher* (*DataFetcherFactory)(); |
| 30 | 26 |
| 31 // Create a ProviderImpl that uses the NULL-terminated factories array to find | 27 // Create a ProviderImpl that uses the NULL-terminated factories array to find |
| 32 // a DataFetcher that can provide orientation data. | 28 // a DataFetcher that can provide orientation data. |
| 33 CONTENT_EXPORT ProviderImpl(const DataFetcherFactory factories[]); | 29 CONTENT_EXPORT ProviderImpl(const DataFetcherFactory factories[]); |
| 34 | 30 |
| 35 // From Provider. | 31 // From Provider. |
| 36 virtual void AddObserver(Observer* observer) OVERRIDE; | 32 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 37 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 33 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 38 | 34 |
| 39 private: | 35 private: |
| 36 class PollingThread; |
| 37 |
| 40 virtual ~ProviderImpl(); | 38 virtual ~ProviderImpl(); |
| 41 | 39 |
| 42 // Starts or Stops the provider. Called from creator_loop_. | 40 // Starts or Stops the provider. Called from creator_loop_. |
| 43 void Start(); | 41 void Start(); |
| 44 void Stop(); | 42 void Stop(); |
| 45 | 43 |
| 46 // Method for finding a suitable DataFetcher and starting the polling. | |
| 47 // Runs on the polling_thread_. | |
| 48 void DoInitializePollingThread( | |
| 49 const std::vector<DataFetcherFactory>& factories); | |
| 50 void ScheduleInitializePollingThread(); | 44 void ScheduleInitializePollingThread(); |
| 51 | 45 |
| 52 // Method for polling a DataFetcher. Runs on the polling_thread_. | |
| 53 void DoPoll(); | |
| 54 void ScheduleDoPoll(); | |
| 55 | |
| 56 // Method for notifying observers of an orientation update. | 46 // Method for notifying observers of an orientation update. |
| 57 // Runs on the creator_thread_. | 47 // Runs on the creator_thread_. |
| 58 void DoNotify(const Orientation& orientation); | 48 void DoNotify(const Orientation& orientation); |
| 59 void ScheduleDoNotify(const Orientation& orientation); | |
| 60 | |
| 61 static bool SignificantlyDifferent(const Orientation& orientation1, | |
| 62 const Orientation& orientation2); | |
| 63 | |
| 64 enum { kDesiredSamplingIntervalMs = 100 }; | |
| 65 base::TimeDelta SamplingInterval() const; | |
| 66 | 49 |
| 67 // The Message Loop on which this object was created. | 50 // The Message Loop on which this object was created. |
| 68 // Typically the I/O loop, but may be something else during testing. | 51 // Typically the I/O loop, but may be something else during testing. |
| 69 MessageLoop* creator_loop_; | 52 MessageLoop* creator_loop_; |
| 70 | 53 |
| 71 // Members below are only to be used from the creator_loop_. | 54 // Members below are only to be used from the creator_loop_. |
| 72 std::vector<DataFetcherFactory> factories_; | 55 std::vector<DataFetcherFactory> factories_; |
| 73 std::set<Observer*> observers_; | 56 std::set<Observer*> observers_; |
| 74 Orientation last_notification_; | 57 Orientation last_notification_; |
| 75 | 58 |
| 76 // When polling_thread_ is running, members below are only to be used | |
| 77 // from that thread. | |
| 78 scoped_ptr<DataFetcher> data_fetcher_; | |
| 79 Orientation last_orientation_; | |
| 80 base::WeakPtrFactory<ProviderImpl> weak_factory_; | 59 base::WeakPtrFactory<ProviderImpl> weak_factory_; |
| 81 | 60 |
| 82 // Polling is done on this background thread. | 61 // Polling is done on this background thread. PollingThread is owned by |
| 83 scoped_ptr<base::Thread> polling_thread_; | 62 // the ProviderImpl object. But its deletion doesn't happen synchronously |
| 63 // along with deletion of the ProviderImpl. Thus this should be a raw |
| 64 // pointer instead of scoped_ptr. |
| 65 PollingThread* polling_thread_; |
| 84 }; | 66 }; |
| 85 | 67 |
| 86 } // namespace device_orientation | 68 } // namespace device_orientation |
| 87 | 69 |
| 88 #endif // CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | 70 #endif // CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ |
| OLD | NEW |