Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: content/browser/device_orientation/orientation.cc

Issue 10755002: Refactors DeviceOrientation to make it more extensible (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adds a unit test, responds to other comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/device_orientation/orientation.h"
6
7 #include <cmath>
8
9 #include "content/common/device_orientation_messages.h"
10
11 namespace device_orientation {
12
13 Orientation::Orientation()
14 : can_provide_alpha_(false),
15 can_provide_beta_(false),
16 can_provide_gamma_(false),
17 can_provide_absolute_(false) {
18 }
19
20 Orientation::~Orientation() {
21 }
22
23 IPC::Message* Orientation::CreateIPCMessage(int render_view_id) const {
24 DeviceOrientationMsg_Updated_Params params;
25 params.can_provide_alpha = can_provide_alpha_;
26 params.alpha = alpha_;
27 params.can_provide_beta = can_provide_beta_;
28 params.beta = beta_;
29 params.can_provide_gamma = can_provide_gamma_;
30 params.gamma = gamma_;
31 params.can_provide_absolute = can_provide_absolute_;
32 params.absolute = absolute_;
33
34 return new DeviceOrientationMsg_Updated(render_view_id, params);
35 }
36
37 // Returns true if two orientations are considered different enough that
38 // observers should be notified of the new orientation.
39 bool Orientation::ShouldFireEvent(const DeviceData* old_data) const {
40 scoped_refptr<const Orientation> old_orientation(
41 static_cast<const Orientation*>(old_data));
42
43 return IsElementSignificantlyDifferent(can_provide_alpha_,
44 old_orientation->can_provide_alpha(),
45 alpha_,
46 old_orientation->alpha()) ||
47 IsElementSignificantlyDifferent(can_provide_beta_,
48 old_orientation->can_provide_beta(),
49 beta_,
50 old_orientation->beta()) ||
51 IsElementSignificantlyDifferent(can_provide_gamma_,
52 old_orientation->can_provide_gamma(),
53 gamma_,
54 old_orientation->gamma()) ||
55 can_provide_absolute_ != old_orientation->can_provide_absolute() ||
56 absolute_ != old_orientation->absolute();
57 }
58
59 bool Orientation::IsElementSignificantlyDifferent(bool can_provide_element1,
60 bool can_provide_element2, double element1, double element2) {
61 const double kThreshold = 0.1;
62
63 if (can_provide_element1 != can_provide_element2)
64 return true;
65 if (can_provide_element1 && std::fabs(element1 - element2) >= kThreshold)
66 return true;
67 return false;
68 }
69
70 } // namespace device_orientation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698