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.
5 #include "content/browser/device_orientation/orientation.h"
9 #include "content/common/device_orientation_messages.h"
13 Orientation::Orientation()
14 : can_provide_alpha_(false),
15 can_provide_beta_(false),
16 can_provide_gamma_(false),
17 can_provide_absolute_(false) {
20 Orientation::~Orientation() {
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_
;
29 params
.can_provide_gamma
= can_provide_gamma_
;
30 params
.gamma
= gamma_
;
31 params
.can_provide_absolute
= can_provide_absolute_
;
32 params
.absolute
= absolute_
;
34 return new DeviceOrientationMsg_Updated(render_view_id
, params
);
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
));
43 return IsElementSignificantlyDifferent(can_provide_alpha_
,
44 old_orientation
->can_provide_alpha(),
46 old_orientation
->alpha()) ||
47 IsElementSignificantlyDifferent(can_provide_beta_
,
48 old_orientation
->can_provide_beta(),
50 old_orientation
->beta()) ||
51 IsElementSignificantlyDifferent(can_provide_gamma_
,
52 old_orientation
->can_provide_gamma(),
54 old_orientation
->gamma()) ||
55 can_provide_absolute_
!= old_orientation
->can_provide_absolute() ||
56 absolute_
!= old_orientation
->absolute();
59 bool Orientation::IsElementSignificantlyDifferent(bool can_provide_element1
,
60 bool can_provide_element2
, double element1
, double element2
) {
61 const double kThreshold
= 0.1;
63 if (can_provide_element1
!= can_provide_element2
)
65 if (can_provide_element1
&& std::fabs(element1
- element2
) >= kThreshold
)
70 } // namespace content