chromeos: dbus: add Bluetooth properties support
[chromium-blink-merge.git] / content / renderer / device_orientation_dispatcher.cc
blobdf80f06c41b03181029d9c801fb6eea194b179c5
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),
15 controller_(NULL),
16 started_(false) {
19 DeviceOrientationDispatcher::~DeviceOrientationDispatcher() {
20 if (started_)
21 stopUpdating();
24 bool DeviceOrientationDispatcher::OnMessageReceived(const IPC::Message& msg) {
25 bool handled = true;
26 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationDispatcher, msg)
27 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_Updated,
28 OnDeviceOrientationUpdated)
29 IPC_MESSAGE_UNHANDLED(handled = false)
30 IPC_END_MESSAGE_MAP()
31 return handled;
34 void DeviceOrientationDispatcher::setController(
35 WebKit::WebDeviceOrientationController* controller) {
36 controller_.reset(controller);
39 void DeviceOrientationDispatcher::startUpdating() {
40 Send(new DeviceOrientationHostMsg_StartUpdating(routing_id()));
41 started_ = true;
44 void DeviceOrientationDispatcher::stopUpdating() {
45 Send(new DeviceOrientationHostMsg_StopUpdating(routing_id()));
46 started_ = false;
49 WebKit::WebDeviceOrientation DeviceOrientationDispatcher::lastOrientation()
50 const {
51 if (!last_orientation_.get())
52 return WebKit::WebDeviceOrientation::nullOrientation();
54 return *last_orientation_;
57 namespace {
58 bool OrientationsEqual(const DeviceOrientationMsg_Updated_Params& a,
59 WebKit::WebDeviceOrientation* b) {
60 if (a.can_provide_alpha != b->canProvideAlpha())
61 return false;
62 if (a.can_provide_alpha && a.alpha != b->alpha())
63 return false;
64 if (a.can_provide_beta != b->canProvideBeta())
65 return false;
66 if (a.can_provide_beta && a.beta != b->beta())
67 return false;
68 if (a.can_provide_gamma != b->canProvideGamma())
69 return false;
70 if (a.can_provide_gamma && a.gamma != b->gamma())
71 return false;
73 return true;
75 } // namespace
77 void DeviceOrientationDispatcher::OnDeviceOrientationUpdated(
78 const DeviceOrientationMsg_Updated_Params& p) {
79 if (last_orientation_.get() && OrientationsEqual(p, last_orientation_.get()))
80 return;
82 last_orientation_.reset(new WebKit::WebDeviceOrientation(p.can_provide_alpha,
83 p.alpha,
84 p.can_provide_beta,
85 p.beta,
86 p.can_provide_gamma,
87 p.gamma));
89 controller_->didChangeDeviceOrientation(*last_orientation_);