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

Side by Side Diff: content/renderer/device_sensors/device_sensor_event_pump.h

Issue 446603002: Refactor code listening to platform events in content/renderer/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webkitplatform_impl_start_stop
Patch Set: rebase Created 6 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ 5 #ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_
6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ 6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_
7 7
8 #include "base/memory/shared_memory.h" 8 #include "base/memory/shared_memory.h"
9 #include "base/timer/timer.h" 9 #include "base/timer/timer.h"
10 #include "content/public/renderer/render_process_observer.h" 10 #include "content/public/renderer/platform_event_observer.h"
11 11
12 namespace content { 12 namespace content {
13 class RenderThread;
14 13
15 class CONTENT_EXPORT DeviceSensorEventPump : public RenderProcessObserver { 14 template <typename ListenerType>
15 class CONTENT_EXPORT DeviceSensorEventPump
16 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) {
16 public: 17 public:
17 // Default delay between subsequent firing of events. 18 // Default delay between subsequent firing of events.
18 static const int kDefaultPumpDelayMillis; 19 static const int kDefaultPumpDelayMillis = 50;
19 20
20 int GetDelayMillis() const; 21 // PlatformEventObserver
22 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE {
23 DVLOG(2) << "requested start";
21 24
22 void Attach(RenderThread* thread); 25 if (state_ != STOPPED)
23 virtual bool OnControlMessageReceived(const IPC::Message& message) = 0; 26 return;
27
28 DCHECK(!timer_.IsRunning());
29
30 PlatformEventObserver<ListenerType>::Start(listener);
31 state_ = PENDING_START;
32 }
33
34 virtual void Stop() OVERRIDE {
35 DVLOG(2) << "stop";
36
37 if (state_ == STOPPED)
38 return;
39
40 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) ||
41 (state_ == RUNNING && timer_.IsRunning()));
42
43 if (timer_.IsRunning())
44 timer_.Stop();
45 PlatformEventObserver<ListenerType>::Stop();
46 state_ = STOPPED;
47 }
24 48
25 protected: 49 protected:
26 // Constructor for a pump with default delay. 50 explicit DeviceSensorEventPump(RenderThread* thread)
27 DeviceSensorEventPump(); 51 : PlatformEventObserver<ListenerType>(thread),
52 pump_delay_millis_(kDefaultPumpDelayMillis),
53 state_(STOPPED) {
54 }
28 55
29 // Constructor for a pump with a given delay. 56 virtual ~DeviceSensorEventPump() {
30 explicit DeviceSensorEventPump(int pump_delay_millis); 57 }
31 virtual ~DeviceSensorEventPump();
32 58
33 // The pump is a tri-state automaton with allowed transitions as follows: 59 // The pump is a tri-state automaton with allowed transitions as follows:
34 // STOPPED -> PENDING_START 60 // STOPPED -> PENDING_START
35 // PENDING_START -> RUNNING 61 // PENDING_START -> RUNNING
36 // PENDING_START -> STOPPED 62 // PENDING_START -> STOPPED
37 // RUNNING -> STOPPED 63 // RUNNING -> STOPPED
38 enum PumpState { 64 enum PumpState {
39 STOPPED, 65 STOPPED,
40 RUNNING, 66 RUNNING,
41 PENDING_START 67 PENDING_START
42 }; 68 };
43 69
44 bool RequestStart(); 70 void OnDidStart(base::SharedMemoryHandle handle) {
45 void OnDidStart(base::SharedMemoryHandle handle); 71 DVLOG(2) << "did start sensor event pump";
46 bool Stop(); 72
73 if (state_ != PENDING_START)
74 return;
75
76 DCHECK(!timer_.IsRunning());
77
78 if (InitializeReader(handle)) {
79 timer_.Start(FROM_HERE,
80 base::TimeDelta::FromMilliseconds(pump_delay_millis_),
81 this, &DeviceSensorEventPump::FireEvent);
82 state_ = RUNNING;
83 }
84 }
47 85
48 virtual void FireEvent() = 0; 86 virtual void FireEvent() = 0;
49 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; 87 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0;
50 virtual bool SendStartMessage() = 0;
51 virtual bool SendStopMessage() = 0;
52 88
53 int pump_delay_millis_; 89 int pump_delay_millis_;
54 PumpState state_; 90 PumpState state_;
55 base::RepeatingTimer<DeviceSensorEventPump> timer_; 91 base::RepeatingTimer<DeviceSensorEventPump> timer_;
92
93 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump);
56 }; 94 };
57 95
58 } // namespace content 96 } // namespace content
59 97
60 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ 98 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698