summaryrefslogtreecommitdiffstats
path: root/chrome/browser/device_orientation/provider_impl.cc
blob: e5e2397d5e8903cf9c1a0b7a5d3e23ce1dc38902 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 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.

#include <cmath>
#include <set>
#include <vector>

#include "base/logging.h"
#include "base/task.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/device_orientation/orientation.h"
#include "chrome/browser/device_orientation/provider_impl.h"

namespace device_orientation {

ProviderImpl::ProviderImpl(const DataFetcherFactory factories[])
    : creator_loop_(MessageLoop::current()),
      ALLOW_THIS_IN_INITIALIZER_LIST(do_poll_method_factory_(this)) {
  for (const DataFetcherFactory* fp = factories; *fp; ++fp)
    factories_.push_back(*fp);
}

ProviderImpl::~ProviderImpl() {
}

void ProviderImpl::AddObserver(Observer* observer) {
  DCHECK(MessageLoop::current() == creator_loop_);

  observers_.insert(observer);
  if (observers_.size() == 1)
    Start();
  else
    observer->OnOrientationUpdate(last_notification_);
}

void ProviderImpl::RemoveObserver(Observer* observer) {
  DCHECK(MessageLoop::current() == creator_loop_);

  observers_.erase(observer);
  if (observers_.empty())
    Stop();
}

void ProviderImpl::Start() {
  DCHECK(MessageLoop::current() == creator_loop_);
  DCHECK(!polling_thread_.get());

  polling_thread_.reset(new base::Thread("Device orientation polling thread"));
  if (!polling_thread_->Start()) {
    LOG(ERROR) << "Failed to start device orientation polling thread";
    polling_thread_.reset();
    return;
  }
  ScheduleInitializePollingThread();
}

void ProviderImpl::Stop() {
  DCHECK(MessageLoop::current() == creator_loop_);
  polling_thread_.reset();
  data_fetcher_.reset();
}

void ProviderImpl::DoInitializePollingThread(
    std::vector<DataFetcherFactory> factories) {
  DCHECK(MessageLoop::current() == polling_thread_->message_loop());

  typedef std::vector<DataFetcherFactory>::const_iterator Iterator;
  for (Iterator i = factories_.begin(), e = factories_.end(); i != e; ++i) {
    DataFetcherFactory factory = *i;
    scoped_ptr<DataFetcher> fetcher(factory());
    Orientation orientation;

    if (fetcher.get() && fetcher->GetOrientation(&orientation)) {
      // Pass ownership of fetcher to provider_.
      data_fetcher_.swap(fetcher);
      last_orientation_ = orientation;

      // Notify observers.
      ScheduleDoNotify(orientation);

      // Start polling.
      ScheduleDoPoll();
      return;
    }
  }

  // When no orientation data can be provided.
  ScheduleDoNotify(Orientation::Empty());
}

void ProviderImpl::ScheduleInitializePollingThread() {
  DCHECK(MessageLoop::current() == creator_loop_);

  Task* task = NewRunnableMethod(this,
                                 &ProviderImpl::DoInitializePollingThread,
                                 factories_);
  MessageLoop* polling_loop = polling_thread_->message_loop();
  polling_loop->PostTask(FROM_HERE, task);
}

void ProviderImpl::DoNotify(const Orientation& orientation) {
  DCHECK(MessageLoop::current() == creator_loop_);

  last_notification_ = orientation;

  typedef std::set<Observer*>::const_iterator Iterator;
  for (Iterator i = observers_.begin(), e = observers_.end(); i != e; ++i)
    (*i)->OnOrientationUpdate(orientation);

  if (orientation.IsEmpty()) {
    // Notify observers about failure to provide data exactly once.
    observers_.clear();
    Stop();
  }
}

void ProviderImpl::ScheduleDoNotify(const Orientation& orientation) {
  DCHECK(MessageLoop::current() == polling_thread_->message_loop());

  Task* task = NewRunnableMethod(this, &ProviderImpl::DoNotify, orientation);
  creator_loop_->PostTask(FROM_HERE, task);
}

void ProviderImpl::DoPoll() {
  DCHECK(MessageLoop::current() == polling_thread_->message_loop());

  Orientation orientation;
  if (!data_fetcher_->GetOrientation(&orientation)) {
    LOG(ERROR) << "Failed to poll device orientation data fetcher.";

    ScheduleDoNotify(Orientation::Empty());
    return;
  }

  if (SignificantlyDifferent(orientation, last_orientation_)) {
    last_orientation_ = orientation;
    ScheduleDoNotify(orientation);
  }

  ScheduleDoPoll();
}

void ProviderImpl::ScheduleDoPoll() {
  DCHECK(MessageLoop::current() == polling_thread_->message_loop());

  Task* task = do_poll_method_factory_.NewRunnableMethod(&ProviderImpl::DoPoll);
  MessageLoop* polling_loop = polling_thread_->message_loop();
  polling_loop->PostDelayedTask(FROM_HERE, task, SamplingIntervalMs());
}

namespace {
bool IsElementSignificantlyDifferent(bool can_provide_element1,
                                     bool can_provide_element2,
                                     double element1,
                                     double element2) {
  const double kThreshold = 0.1;

  if (can_provide_element1 != can_provide_element2)
    return true;
  if (can_provide_element1 &&
      std::fabs(element1 - element2) >= kThreshold)
    return true;
  return false;
}
} // namespace

// Returns true if two orientations are considered different enough that
// observers should be notified of the new orientation.
bool ProviderImpl::SignificantlyDifferent(const Orientation& o1,
                                          const Orientation& o2) {
  return IsElementSignificantlyDifferent(o1.can_provide_alpha_,
                                         o2.can_provide_alpha_,
                                         o1.alpha_,
                                         o2.alpha_) ||
      IsElementSignificantlyDifferent(o1.can_provide_beta_,
                                         o2.can_provide_beta_,
                                         o1.beta_,
                                         o2.beta_) ||
      IsElementSignificantlyDifferent(o1.can_provide_gamma_,
                                         o2.can_provide_gamma_,
                                         o1.gamma_,
                                         o2.gamma_);
}

int ProviderImpl::SamplingIntervalMs() const {
  DCHECK(MessageLoop::current() == polling_thread_->message_loop());
  DCHECK(data_fetcher_.get());

  int fetcher_interval = data_fetcher_->MinSamplingIntervalMs();

  if (fetcher_interval > kDesiredSamplingIntervalMs)
    return fetcher_interval;
  else
    return kDesiredSamplingIntervalMs;
}

}  // namespace device_orientation