Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(993)

Side by Side Diff: content/renderer/device_orientation/device_orientation_event_pump.cc

Issue 20707002: Implement Device Orientation using shared memory in content/renderer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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.
4
5 #include "device_orientation_event_pump.h"
6
7 #include "content/common/device_orientation_messages.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
10
11 namespace content {
12
13 DeviceOrientationEventPump::DeviceOrientationEventPump()
14 : DeviceSensorEventPump(), listener_(0) {
15 }
16
17 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis)
18 : DeviceSensorEventPump(pump_delay_millis), listener_(0) {
19 }
20
21 DeviceOrientationEventPump::~DeviceOrientationEventPump() {
22 }
23
24 bool DeviceOrientationEventPump::SetListener(
25 WebKit::WebDeviceOrientationListener* listener) {
26 listener_ = listener;
27 return listener_ ? RequestStart() : Stop();
28 }
29
30 bool DeviceOrientationEventPump::OnControlMessageReceived(
31 const IPC::Message& message) {
32 bool handled = true;
33 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message)
34 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart)
35 IPC_MESSAGE_UNHANDLED(handled = false)
36 IPC_END_MESSAGE_MAP()
37 return handled;
38 }
39
40 void DeviceOrientationEventPump::FireEvent() {
41 DCHECK(listener_);
42 WebKit::WebDeviceOrientationData data;
43 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) {
44 memcpy(&data_, &data, sizeof(data));
45 listener_->didChangeDeviceOrientation(data);
46 }
47 }
48
49 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1,
50 bool hasAngle2, double angle2) {
51 if (hasAngle1 != hasAngle2)
52 return true;
53 return (hasAngle1 && std::fabs(angle1 - angle2) >=
54 DeviceOrientationEventPump::kOrientationThreshold);
55 }
56
57 bool DeviceOrientationEventPump::ShouldFireEvent(
58 const WebKit::WebDeviceOrientationData& data) const {
59 return data.allAvailableSensorsAreActive &&
60 (IsSignificantlyDifferent(
61 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) ||
62 IsSignificantlyDifferent(
63 data_.hasBeta, data_.beta, data.hasBeta, data.beta) ||
64 IsSignificantlyDifferent(
65 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma));
66 }
67
68 bool DeviceOrientationEventPump::InitializeReader(
69 base::SharedMemoryHandle handle) {
70 memset(&data_, 0, sizeof(data_));
71 if (!reader_)
72 reader_.reset(new DeviceOrientationSharedMemoryReader());
73 return reader_->Initialize(handle);
74 }
75
76 bool DeviceOrientationEventPump::SendStartMessage() {
77 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling());
78 }
79
80 bool DeviceOrientationEventPump::SendStopMessage() {
81 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling());
82 }
83
84 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698