summaryrefslogtreecommitdiffstats
path: root/chrome/browser/device_orientation/provider_impl.h
blob: d9840623e52c2ba6f5817ea871512d2528e00292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_
#define CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_

#include <set>
#include <vector>

#include "base/scoped_ptr.h"
#include "base/task.h"
#include "chrome/browser/device_orientation/data_fetcher.h"
#include "chrome/browser/device_orientation/orientation.h"
#include "chrome/browser/device_orientation/provider.h"

class MessageLoop;

namespace base {
class Thread;
}

namespace device_orientation {

class ProviderImpl : public Provider {
 public:
  typedef DataFetcher* (*DataFetcherFactory)();

  // Create a ProviderImpl that uses the NULL-terminated factories array to find
  // a DataFetcher that can provide orientation data.
  ProviderImpl(const DataFetcherFactory factories[]);

  // From Provider.
  virtual void AddObserver(Observer* observer);
  virtual void RemoveObserver(Observer* observer);

 private:
  virtual ~ProviderImpl();

  // Starts or Stops the provider. Called from creator_loop_.
  void Start();
  void Stop();

  // Method for finding a suitable DataFetcher and starting the polling.
  // Runs on the polling_thread_.
  void DoInitializePollingThread(std::vector<DataFetcherFactory> factories);
  void ScheduleInitializePollingThread();

  // Method for polling a DataFetcher. Runs on the polling_thread_.
  void DoPoll();
  void ScheduleDoPoll();

  // Method for notifying observers of an orientation update.
  // Runs on the creator_thread_.
  void DoNotify(Orientation orientation);
  void ScheduleDoNotify(Orientation orientation);

  static bool SignificantlyDifferent(const Orientation& orientation1,
                                     const Orientation& orientation2);

  enum { kDesiredSamplingIntervalMs = 100 };
  int SamplingIntervalMs() const;

  // The Message Loop on which this object was created.
  // Typically the I/O loop, but may be something else during testing.
  MessageLoop* creator_loop_;

  // Members below are only to be used from the creator_loop_.
  std::vector<DataFetcherFactory> factories_;
  std::set<Observer*> observers_;
  Orientation last_notification_;

  // When polling_thread_ is running, members below are only to be used
  // from that thread.
  scoped_ptr<DataFetcher> data_fetcher_;
  Orientation last_orientation_;
  ScopedRunnableMethodFactory<ProviderImpl> do_poll_method_factory_;

  // Polling is done on this background thread.
  scoped_ptr<base::Thread> polling_thread_;
};

}  // namespace device_orientation

#endif  // CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_