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/renderer/device_orientation_dispatcher.h"
7 #include "content/common/device_orientation_messages.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientationController.h"
14 DeviceOrientationDispatcher::DeviceOrientationDispatcher(
15 RenderViewImpl
* render_view
)
16 : RenderViewObserver(render_view
),
21 DeviceOrientationDispatcher::~DeviceOrientationDispatcher() {
26 bool DeviceOrientationDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
28 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationDispatcher
, msg
)
29 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_Updated
,
30 OnDeviceOrientationUpdated
)
31 IPC_MESSAGE_UNHANDLED(handled
= false)
36 void DeviceOrientationDispatcher::setController(
37 WebKit::WebDeviceOrientationController
* controller
) {
38 controller_
.reset(controller
);
41 void DeviceOrientationDispatcher::startUpdating() {
42 Send(new DeviceOrientationHostMsg_StartUpdating(routing_id()));
46 void DeviceOrientationDispatcher::stopUpdating() {
47 Send(new DeviceOrientationHostMsg_StopUpdating(routing_id()));
51 WebKit::WebDeviceOrientation
DeviceOrientationDispatcher::lastOrientation()
53 return last_orientation_
;
57 bool OrientationsEqual(const DeviceOrientationMsg_Updated_Params
& a
,
58 WebKit::WebDeviceOrientation
* b
) {
59 if (a
.can_provide_alpha
!= b
->canProvideAlpha())
61 if (a
.can_provide_alpha
&& a
.alpha
!= b
->alpha())
63 if (a
.can_provide_beta
!= b
->canProvideBeta())
65 if (a
.can_provide_beta
&& a
.beta
!= b
->beta())
67 if (a
.can_provide_gamma
!= b
->canProvideGamma())
69 if (a
.can_provide_gamma
&& a
.gamma
!= b
->gamma())
71 if (a
.can_provide_absolute
!= b
->canProvideAbsolute())
73 if (a
.can_provide_absolute
&& a
.absolute
!= b
->absolute())
80 void DeviceOrientationDispatcher::OnDeviceOrientationUpdated(
81 const DeviceOrientationMsg_Updated_Params
& p
) {
82 if (!last_orientation_
.isNull() && OrientationsEqual(p
, &last_orientation_
))
85 last_orientation_
.setNull(false);
86 if (p
.can_provide_alpha
)
87 last_orientation_
.setAlpha(p
.alpha
);
88 if (p
.can_provide_beta
)
89 last_orientation_
.setBeta(p
.beta
);
90 if (p
.can_provide_gamma
)
91 last_orientation_
.setGamma(p
.gamma
);
92 if (p
.can_provide_absolute
)
93 last_orientation_
.setAbsolute(p
.absolute
);
94 controller_
->didChangeDeviceOrientation(last_orientation_
);
97 } // namespace content