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

Side by Side Diff: content/public/renderer/platform_event_observer.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
« no previous file with comments | « content/content_renderer.gypi ('k') | content/public/renderer/renderer_gamepad_provider.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
6 #define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
7
8 #include "base/logging.h"
9 #include "content/public/renderer/render_process_observer.h"
10 #include "content/public/renderer/render_thread.h"
11
12 namespace blink {
13 class WebPlatformEventListener;
14 }
15
16 namespace content {
17
18 // This class is used as a base class for PlatformEventObserver<ListenerType> to
19 // allow storing PlatformEventObserver<> with different typename in the same
20 // place.
21 class PlatformEventObserverBase {
22 public:
23 virtual ~PlatformEventObserverBase() { }
24
25 // Methods that need to be exposed in PlatformEventObserverBase. Their purpose
26 // is described in PlatformEventObserver<>.
27
28 virtual void Start(blink::WebPlatformEventListener* listener) = 0;
29 virtual void Stop() = 0;
30
31 // Helper method that allows an sub-class to write its own test helper.
32 // The |data| type MUST be known from the caller.
33 virtual void SendFakeDataForTesting(void* data) { }
34 };
35
36 // PlatformEventObserver<> defines the basic skeleton for an object requesting
37 // the browser process to start/stop listening to some platform/hardware events
38 // and observe the result.
39 // The results are received via IPC, assuming that the object was correctly
40 // registered as an observer via the constructor taking a RenderThread.
41 template <typename ListenerType>
42 class PlatformEventObserver : public PlatformEventObserverBase,
43 public RenderProcessObserver {
44 public:
45 // Creates a PlatformEventObserver that doesn't listen to responses from the
46 // browser process. Can be used for testing purposes or for observers that
47 // have other means to get their results.
48 PlatformEventObserver()
49 : is_observing_(false),
50 listener_(0) {
51 }
52
53 // Creates a PlatformEventObserver that registers to the RenderThread in order
54 // to intercept the received IPC messages (via OnControlMessageReceived). If
55 // |thread| is null, it will not register.
56 explicit PlatformEventObserver(RenderThread* thread)
57 : is_observing_(false),
58 listener_(0) {
59 if (thread)
60 thread->AddObserver(this);
61 }
62
63 // The observer will automatically stop observing when destroyed in case it
64 // did not stop before.
65 virtual ~PlatformEventObserver() {
66 if (is_observing())
67 Stop();
68 }
69
70 // Called when a new IPC message is received. Must be used to listen to the
71 // responses from the browser process if any expected.
72 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE {
73 return false;
74 }
75
76 // Start observing. Will request the browser process to start listening to the
77 // events. |listener| will receive any response from the browser process.
78 // Note: should not be called if already observing.
79 virtual void Start(blink::WebPlatformEventListener* listener) {
80 DCHECK(!is_observing());
81 listener_ = static_cast<ListenerType*>(listener);
82 is_observing_ = true;
83
84 SendStartMessage();
85 }
86
87 // Stop observing. Will let the browser know that it doesn't need to observe
88 // anymore.
89 virtual void Stop() {
90 DCHECK(is_observing());
91 listener_ = 0;
92 is_observing_ = false;
93
94 SendStopMessage();
95 }
96
97 protected:
98 // This method is expected to send an IPC to the browser process to let it
99 // know that it should start observing.
100 // It is expected for subclasses to override it.
101 virtual void SendStartMessage() = 0;
102
103 // This method is expected to send an IPC to the browser process to let it
104 // know that it should start observing.
105 // It is expected for subclasses to override it.
106 virtual void SendStopMessage() = 0;
107
108 bool is_observing() const {
109 return is_observing_;
110 }
111
112 ListenerType* listener() {
113 return listener_;
114 }
115
116 private:
117 bool is_observing_;
118 ListenerType* listener_;
119
120 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver);
121 };
122
123 } // namespace content
124
125 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
OLDNEW
« no previous file with comments | « content/content_renderer.gypi ('k') | content/public/renderer/renderer_gamepad_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698