Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / content / renderer / device_orientation_dispatcher.cc
blob4776edf99c5b9fb582ac3ee0903aca0f58a71772
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"
12 namespace content {
14 DeviceOrientationDispatcher::DeviceOrientationDispatcher(
15 RenderViewImpl* render_view)
16 : RenderViewObserver(render_view),
17 controller_(NULL),
18 started_(false) {
21 DeviceOrientationDispatcher::~DeviceOrientationDispatcher() {
22 if (started_)
23 stopUpdating();
26 bool DeviceOrientationDispatcher::OnMessageReceived(const IPC::Message& msg) {
27 bool handled = true;
28 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationDispatcher, msg)
29 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_Updated,
30 OnDeviceOrientationUpdated)
31 IPC_MESSAGE_UNHANDLED(handled = false)
32 IPC_END_MESSAGE_MAP()
33 return handled;
36 void DeviceOrientationDispatcher::setController(
37 WebKit::WebDeviceOrientationController* controller) {
38 controller_.reset(controller);
41 void DeviceOrientationDispatcher::startUpdating() {
42 Send(new DeviceOrientationHostMsg_StartUpdating(routing_id()));
43 started_ = true;
46 void DeviceOrientationDispatcher::stopUpdating() {
47 Send(new DeviceOrientationHostMsg_StopUpdating(routing_id()));
48 started_ = false;
51 WebKit::WebDeviceOrientation DeviceOrientationDispatcher::lastOrientation()
52 const {
53 return last_orientation_;
56 namespace {
57 bool OrientationsEqual(const DeviceOrientationMsg_Updated_Params& a,
58 WebKit::WebDeviceOrientation* b) {
59 if (a.can_provide_alpha != b->canProvideAlpha())
60 return false;
61 if (a.can_provide_alpha && a.alpha != b->alpha())
62 return false;
63 if (a.can_provide_beta != b->canProvideBeta())
64 return false;
65 if (a.can_provide_beta && a.beta != b->beta())
66 return false;
67 if (a.can_provide_gamma != b->canProvideGamma())
68 return false;
69 if (a.can_provide_gamma && a.gamma != b->gamma())
70 return false;
71 if (a.can_provide_absolute != b->canProvideAbsolute())
72 return false;
73 if (a.can_provide_absolute && a.absolute != b->absolute())
74 return false;
76 return true;
78 } // namespace
80 void DeviceOrientationDispatcher::OnDeviceOrientationUpdated(
81 const DeviceOrientationMsg_Updated_Params& p) {
82 if (!last_orientation_.isNull() && OrientationsEqual(p, &last_orientation_))
83 return;
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