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

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: apply review comments 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
(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 virtual void SendFakeDataForTesting(void*) = 0;
31 };
32
33 // PlatformEventObserver<> defines the basic skeleton for an object requesting
34 // the browser process to start/stop listening to some platform/hardware events
35 // and observe the result.
36 // The results are received via IPC, assuming that the object was correctly
37 // registered as an observer via the constructor taking a RenderThread.
38 template <typename ListenerType>
39 class PlatformEventObserver : public PlatformEventObserverBase,
40 public RenderProcessObserver {
41 public:
42 // Creates a PlatformEventObserver that doesn't listen to response from the
Avi (use Gerrit) 2014/08/07 19:56:11 ... listen to responses ...
mlamouri (slow - plz ping) 2014/08/08 18:08:35 Done.
43 // browser process. Can be used for testing purposes or for observers that
44 // have other means to get their results.
45 PlatformEventObserver()
46 : is_observing_(false),
47 listener_(0) {
48 }
49
50 // Creates a PlatformEventObserver that registers to the RenderThread in order
51 // to intercept the received IPC messages (via OnControlMessageReceived). If
52 // |thread| is null, it will not register.
53 explicit PlatformEventObserver(RenderThread* thread)
54 : is_observing_(false),
55 listener_(0) {
56 if (thread)
57 thread->AddObserver(this);
58 }
59
60 // The observer will automatically stop observing when destroyed in case of it
61 // did not stop before.
Avi (use Gerrit) 2014/08/07 19:56:11 ... in case it did not stop before.
mlamouri (slow - plz ping) 2014/08/08 18:08:35 Done.
62 virtual ~PlatformEventObserver() {
63 if (is_observing())
64 Stop();
65 }
66
67 // Called when a new IPC message is received. Must be used to listen to the
68 // responses from the browser process if any is expected.
Avi (use Gerrit) 2014/08/07 19:56:11 ... if any are expected.
mlamouri (slow - plz ping) 2014/08/08 18:08:36 Done.
69 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE = 0;
70
71 // Start observing. Will request the browser process to start listening to the
72 // events. |listener| will receive any response from the browser process.
73 // Note: should not be called if already observing.
74 virtual void Start(blink::WebPlatformEventListener* listener) {
Avi (use Gerrit) 2014/08/07 19:56:11 Can you correctly type this parameter as ListenerT
mlamouri (slow - plz ping) 2014/08/08 18:08:35 Using ListenerType would require the callers to ca
75 DCHECK(!is_observing());
76 listener_ = static_cast<ListenerType*>(listener);
77 is_observing_ = true;
78
79 SendStartMessage();
80 }
81
82 // Stop observing. Will let the browser know that it doesn't need to observe
83 // anymore.
84 virtual void Stop() {
85 DCHECK(is_observing());
86 listener_ = 0;
87 is_observing_ = false;
88
89 SendStopMessage();
90 }
91
92 protected:
93 // This method is expected to send an IPC to the browser process to let it
94 // know that it should start observing.
95 virtual void SendStartMessage() = 0;
Avi (use Gerrit) 2014/08/07 19:56:11 It's "expected" to send? Do you mean that it shoul
mlamouri (slow - plz ping) 2014/08/08 18:08:35 Done.
96
97 // This method is expected to send an IPC to the browser process to let it
98 /// know that it should stop observing.
Avi (use Gerrit) 2014/08/07 19:56:11 Same question as above. Also, fix ///.
mlamouri (slow - plz ping) 2014/08/08 18:08:35 Done.
99 virtual void SendStopMessage() = 0;
100
101 bool is_observing() const {
102 return is_observing_;
103 }
104
105 ListenerType* listener() {
106 return listener_;
107 }
108
109 private:
110 bool is_observing_;
111 ListenerType* listener_;
112
113 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver);
114 };
115
116 } // namespace content
117
118 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698