1 // Copyright (c) 2010 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"
12 DeviceOrientationDispatcher::DeviceOrientationDispatcher(
13 RenderViewImpl
* render_view
)
14 : content::RenderViewObserver(render_view
),
19 DeviceOrientationDispatcher::~DeviceOrientationDispatcher() {
24 bool DeviceOrientationDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
26 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationDispatcher
, msg
)
27 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_Updated
,
28 OnDeviceOrientationUpdated
)
29 IPC_MESSAGE_UNHANDLED(handled
= false)
34 void DeviceOrientationDispatcher::setController(
35 WebKit::WebDeviceOrientationController
* controller
) {
36 controller_
.reset(controller
);
39 void DeviceOrientationDispatcher::startUpdating() {
40 Send(new DeviceOrientationHostMsg_StartUpdating(routing_id()));
44 void DeviceOrientationDispatcher::stopUpdating() {
45 Send(new DeviceOrientationHostMsg_StopUpdating(routing_id()));
49 WebKit::WebDeviceOrientation
DeviceOrientationDispatcher::lastOrientation()
51 if (!last_orientation_
.get())
52 return WebKit::WebDeviceOrientation::nullOrientation();
54 return *last_orientation_
;
58 bool OrientationsEqual(const DeviceOrientationMsg_Updated_Params
& a
,
59 WebKit::WebDeviceOrientation
* b
) {
60 if (a
.can_provide_alpha
!= b
->canProvideAlpha())
62 if (a
.can_provide_alpha
&& a
.alpha
!= b
->alpha())
64 if (a
.can_provide_beta
!= b
->canProvideBeta())
66 if (a
.can_provide_beta
&& a
.beta
!= b
->beta())
68 if (a
.can_provide_gamma
!= b
->canProvideGamma())
70 if (a
.can_provide_gamma
&& a
.gamma
!= b
->gamma())
77 void DeviceOrientationDispatcher::OnDeviceOrientationUpdated(
78 const DeviceOrientationMsg_Updated_Params
& p
) {
79 if (last_orientation_
.get() && OrientationsEqual(p
, last_orientation_
.get()))
82 last_orientation_
.reset(new WebKit::WebDeviceOrientation(p
.can_provide_alpha
,
89 controller_
->didChangeDeviceOrientation(*last_orientation_
);