OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | |
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | |
7 | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "base/scoped_ptr.h" | |
12 #include "base/task.h" | |
13 #include "chrome/browser/device_orientation/data_fetcher.h" | |
14 #include "chrome/browser/device_orientation/orientation.h" | |
15 #include "chrome/browser/device_orientation/provider.h" | |
16 | |
17 class MessageLoop; | |
18 | |
19 namespace base { | |
20 class Thread; | |
21 } | |
22 | |
23 namespace device_orientation { | |
24 | |
25 class ProviderImpl : public Provider { | |
26 public: | |
27 typedef DataFetcher* (*DataFetcherFactory)(); | |
28 | |
29 // Create a ProviderImpl that uses the NULL-terminated factories array to find | |
30 // a DataFetcher that can provide orientation data. | |
31 ProviderImpl(const DataFetcherFactory factories[]); | |
32 | |
33 // From Provider. | |
34 virtual void AddObserver(Observer* observer); | |
35 virtual void RemoveObserver(Observer* observer); | |
36 | |
37 private: | |
38 virtual ~ProviderImpl(); | |
39 | |
40 // Starts or Stops the provider. Called from creator_loop_. | |
41 void Start(); | |
42 void Stop(); | |
43 | |
44 // Method for finding a suitable DataFetcher and starting the polling. | |
45 // Runs on the polling_thread_. | |
46 void DoInitializePollingThread(std::vector<DataFetcherFactory> factories); | |
47 void ScheduleInitializePollingThread(); | |
48 | |
49 // Method for polling a DataFetcher. Runs on the polling_thread_. | |
50 void DoPoll(); | |
51 void ScheduleDoPoll(); | |
52 | |
53 // Method for notifying observers of an orientation update. | |
54 // Runs on the creator_thread_. | |
55 void DoNotify(const Orientation& orientation); | |
56 void ScheduleDoNotify(const Orientation& orientation); | |
57 | |
58 static bool SignificantlyDifferent(const Orientation& orientation1, | |
59 const Orientation& orientation2); | |
60 | |
61 enum { kDesiredSamplingIntervalMs = 100 }; | |
62 int SamplingIntervalMs() const; | |
63 | |
64 // The Message Loop on which this object was created. | |
65 // Typically the I/O loop, but may be something else during testing. | |
66 MessageLoop* creator_loop_; | |
67 | |
68 // Members below are only to be used from the creator_loop_. | |
69 std::vector<DataFetcherFactory> factories_; | |
70 std::set<Observer*> observers_; | |
71 Orientation last_notification_; | |
72 | |
73 // When polling_thread_ is running, members below are only to be used | |
74 // from that thread. | |
75 scoped_ptr<DataFetcher> data_fetcher_; | |
76 Orientation last_orientation_; | |
77 ScopedRunnableMethodFactory<ProviderImpl> do_poll_method_factory_; | |
78 | |
79 // Polling is done on this background thread. | |
80 scoped_ptr<base::Thread> polling_thread_; | |
81 }; | |
82 | |
83 } // namespace device_orientation | |
84 | |
85 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_ | |
OLD | NEW |