OLD | NEW |
---|---|
(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 "device_motion_shared_memory_reader.h" | |
15 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h" | |
16 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" | |
17 | |
18 namespace content { | |
19 | |
20 const double DeviceMotionEventPump::kPumpDelayMillis = 40; | |
21 | |
22 DeviceMotionEventPump::DeviceMotionEventPump() | |
23 : listener_(0), state_(STOPPED) { | |
24 } | |
25 | |
26 DeviceMotionEventPump::~DeviceMotionEventPump() { | |
27 } | |
28 | |
29 bool DeviceMotionEventPump::SetListener( | |
30 WebKit::WebDeviceMotionListener* listener) { | |
31 listener_ = listener; | |
32 if (listener_) | |
33 return StartFetchingDeviceMotion(); | |
34 return StopFetchingDeviceMotion(); | |
35 } | |
36 | |
37 void DeviceMotionEventPump::SetDeviceMotionReader( | |
38 DeviceMotionSharedMemoryReader* reader) { | |
39 reader_.reset(reader); | |
darin (slow to review)
2013/06/17 05:27:46
since this is a transfer of ownership, the argumen
timvolodine
2013/06/17 20:10:55
Done.
| |
40 } | |
41 | |
42 bool DeviceMotionEventPump::StartFetchingDeviceMotion() { | |
43 DVLOG(2) << "start fetching device motion"; | |
44 LOG(INFO) << "PUMP start fetching"; | |
darin (slow to review)
2013/06/17 05:27:46
nit: are both of these log statements necessary?
timvolodine
2013/06/17 20:10:55
sorry this was for debugging, removed all extra lo
| |
45 | |
46 if (state_ != STOPPED) | |
47 return false; | |
48 | |
49 DCHECK(!timer_.IsRunning()); | |
50 | |
51 if (RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling())) { | |
52 state_ = PENDING_START; | |
53 return true; | |
54 } | |
55 return false; | |
56 } | |
57 | |
58 bool DeviceMotionEventPump::StopFetchingDeviceMotion() { | |
59 DVLOG(2) << "stop fetching device motion"; | |
60 LOG(INFO) << "PUMP stop fetching"; | |
darin (slow to review)
2013/06/17 05:27:46
ditto: can you do with only one log statement? do
timvolodine
2013/06/17 20:10:55
Done.
| |
61 | |
62 if (state_ == STOPPED) | |
63 return true; | |
64 | |
65 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) | |
darin (slow to review)
2013/06/17 05:27:46
nit: chromium style is to put the "||" operator at
timvolodine
2013/06/17 20:10:55
Done.
| |
66 || (state_ == RUNNING && timer_.IsRunning())); | |
67 | |
68 if (timer_.IsRunning()); | |
69 timer_.Stop(); | |
70 RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); | |
71 state_ = STOPPED; | |
72 return true; | |
73 } | |
74 | |
75 void DeviceMotionEventPump::FireEvent() { | |
76 DCHECK(listener_); | |
77 LOG(INFO) << "PUMP fire"; | |
darin (slow to review)
2013/06/17 05:27:46
nit: it seems like there could be a lot of these l
timvolodine
2013/06/17 20:10:55
Done.
| |
78 WebKit::WebDeviceMotionData data; | |
79 if (reader_->GetLatestDeviceMotionData(data)) | |
80 listener_->didChangeDeviceMotion(data); | |
81 } | |
82 | |
83 void DeviceMotionEventPump::Attach(RenderThread* thread) { | |
84 if (!thread) | |
85 return; | |
86 thread->AddObserver(this); | |
87 } | |
88 | |
89 void DeviceMotionEventPump::OnDidStartDeviceMotion( | |
90 base::SharedMemoryHandle renderer_handle) { | |
91 DVLOG(2) << "did start fetching device motion"; | |
92 LOG(INFO) << "PUMP did start fetching"; | |
93 | |
94 if (state_ != PENDING_START) | |
95 return; | |
96 | |
97 DCHECK(!timer_.IsRunning()); | |
98 if (!reader_) | |
99 SetDeviceMotionReader(new DeviceMotionSharedMemoryReader); | |
darin (slow to review)
2013/06/17 05:27:46
nit: the code might be clearer if you just directl
timvolodine
2013/06/17 20:10:55
Done.
| |
100 | |
101 if (reader_->Initialize(renderer_handle)) { | |
102 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPumpDelayMillis), | |
103 this, &DeviceMotionEventPump::FireEvent); | |
104 state_ = RUNNING; | |
105 } | |
106 } | |
107 | |
108 bool DeviceMotionEventPump::OnControlMessageReceived( | |
109 const IPC::Message& message) { | |
110 bool handled = true; | |
111 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) | |
112 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, | |
113 OnDidStartDeviceMotion) | |
114 IPC_MESSAGE_UNHANDLED(handled = false) | |
115 IPC_END_MESSAGE_MAP() | |
116 return handled; | |
117 } | |
118 | |
119 } // namespace content | |
OLD | NEW |