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/memory/scoped_ptr.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/shared_memory.h" | |
11 #include "device_motion_shared_memory_reader.h" | |
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData .h" | |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionHand ler.h" | |
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionList ener.h" | |
15 | |
16 namespace content { | |
17 | |
18 DeviceMotionEventPump::DeviceMotionEventPump() | |
19 : listener_(0), | |
20 fire_pending_(false), | |
21 pump_delay_(base::TimeDelta::FromMilliseconds(pump_delay_millis)) | |
22 { | |
jamesr
2013/05/16 19:53:06
This is chromium - the { goes on the previous line
timvolodine
2013/05/20 18:18:48
Done.
| |
23 } | |
24 | |
25 DeviceMotionEventPump::~DeviceMotionEventPump() | |
26 { | |
27 } | |
28 | |
29 void DeviceMotionEventPump::startListeningToDeviceMotion( | |
30 WebKit::WebDeviceMotionListener* listener) | |
31 { | |
32 if (listener_) | |
33 return; | |
34 | |
35 if (!reader_.get()) | |
36 reader_.reset(new DeviceMotionSharedMemoryReader); | |
37 | |
38 reader_->startUpdating(); | |
39 listener_ = listener; | |
40 | |
41 if (!fire_pending_) { | |
42 // Only post a task if the pump is not still running. | |
43 MessageLoop::current()->PostTask( | |
44 FROM_HERE, | |
45 base::Bind(&DeviceMotionEventPump::fire, base::Unretained(this))); | |
jamesr
2013/05/16 19:53:06
unretained? what prevents this task from running a
timvolodine
2013/05/20 18:18:48
switched to RepeatedTimer, looks like this is exac
| |
46 fire_pending_ = true; | |
47 } | |
48 } | |
49 | |
50 void DeviceMotionEventPump::stopListeningToDeviceMotion() | |
51 { | |
52 LOG(INFO) << "stop listening\n"; | |
jamesr
2013/05/16 19:53:06
This is way too useless to spam up the INFO log.
timvolodine
2013/05/20 18:18:48
Done.
| |
53 reader_->stopUpdating(); | |
54 listener_ = 0; | |
55 } | |
56 | |
57 bool DeviceMotionEventPump::shouldFireEvent(WebKit::WebDeviceMotionData& data) | |
58 { | |
59 // Device motion should probably be always fired, | |
60 // however for device orientation a threshold can be provided. | |
61 return true; | |
jamesr
2013/05/16 19:53:06
why does this function take a data parameter if it
timvolodine
2013/05/20 18:18:48
Done.
| |
62 } | |
63 | |
64 void DeviceMotionEventPump::fire() | |
65 { | |
66 fire_pending_ = false; | |
67 last_fire_event_time_ = base::TimeTicks::Now(); | |
68 | |
69 if (!listener_) | |
70 return; | |
71 | |
72 WebKit::WebDeviceMotionData data; | |
73 if (reader_->latestDeviceMotionData(data) && shouldFireEvent(data)) | |
74 listener_->didChangeDeviceMotion(data); | |
75 | |
76 const base::TimeDelta dispatch_time = | |
77 base::TimeTicks::Now() - last_fire_event_time_; | |
78 const base::TimeDelta schedule_delay = | |
79 (dispatch_time < pump_delay_) ? pump_delay_ - dispatch_time | |
80 : base::TimeDelta(); | |
81 | |
82 MessageLoop::current()->PostDelayedTask( | |
83 FROM_HERE, | |
84 base::Bind(&DeviceMotionEventPump::fire, base::Unretained(this)), | |
jamesr
2013/05/16 19:53:06
same thing here re the unretained
timvolodine
2013/05/20 18:18:48
Done.
| |
85 schedule_delay); | |
86 fire_pending_ = true; | |
87 } | |
88 | |
89 | |
90 } // namespace content | |
OLD | NEW |