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

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

Issue 14678012: Implement the content/renderer and content/browser part of the Device Motion API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: similarity=70 Created 7 years, 6 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 (c) 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_motion_event_pump.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/shared_memory.h"
12 #include "content/common/device_motion_messages.h"
13 #include "content/public/renderer/render_thread.h"
14 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
15 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h"
16
17 namespace content {
18
19 const double DeviceMotionEventPump::kPumpDelayMillis = 40;
20
21 DeviceMotionEventPump::DeviceMotionEventPump()
22 : listener_(0), state_(STOPPED) {
23 }
24
25 DeviceMotionEventPump::~DeviceMotionEventPump() {
26 }
27
28 bool DeviceMotionEventPump::SetListener(
29 WebKit::WebDeviceMotionListener* listener) {
30 listener_ = listener;
31 if (listener_)
32 return StartFetchingDeviceMotion();
33 return StopFetchingDeviceMotion();
34 }
35
36 void DeviceMotionEventPump::SetDeviceMotionReader(
37 scoped_ptr<DeviceMotionSharedMemoryReader> reader) {
38 reader_.reset(reader.release());
39 }
40
41 bool DeviceMotionEventPump::StartFetchingDeviceMotion() {
42 DVLOG(2) << "start fetching device motion";
43
44 if (state_ != STOPPED)
45 return false;
46
47 DCHECK(!timer_.IsRunning());
48
49 if (RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling())) {
50 state_ = PENDING_START;
51 return true;
52 }
53 return false;
54 }
55
56 bool DeviceMotionEventPump::StopFetchingDeviceMotion() {
57 DVLOG(2) << "stop fetching device motion";
58
59 if (state_ == STOPPED)
60 return true;
61
62 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) ||
63 (state_ == RUNNING && timer_.IsRunning()));
64
65 if (timer_.IsRunning());
66 timer_.Stop();
67 RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling());
68 state_ = STOPPED;
69 return true;
70 }
71
72 void DeviceMotionEventPump::FireEvent() {
73 DCHECK(listener_);
74 WebKit::WebDeviceMotionData data;
75 if (reader_->GetLatestData(&data) && data.allAvailableSensorsActive)
76 listener_->didChangeDeviceMotion(data);
77 }
78
79 void DeviceMotionEventPump::Attach(RenderThread* thread) {
80 if (!thread)
81 return;
82 thread->AddObserver(this);
83 }
84
85 void DeviceMotionEventPump::OnDidStartDeviceMotion(
86 base::SharedMemoryHandle renderer_handle) {
87 DVLOG(2) << "did start fetching device motion";
88
89 if (state_ != PENDING_START)
90 return;
91
92 DCHECK(!timer_.IsRunning());
93 if (!reader_)
94 reader_.reset(new DeviceMotionSharedMemoryReader());
95
96 if (reader_->Initialize(renderer_handle)) {
97 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPumpDelayMillis),
98 this, &DeviceMotionEventPump::FireEvent);
99 state_ = RUNNING;
100 }
101 }
102
103 bool DeviceMotionEventPump::OnControlMessageReceived(
104 const IPC::Message& message) {
105 bool handled = true;
106 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message)
107 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling,
108 OnDidStartDeviceMotion)
109 IPC_MESSAGE_UNHANDLED(handled = false)
110 IPC_END_MESSAGE_MAP()
111 return handled;
112 }
113
114 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698