Index: content/public/renderer/platform_event_observer.h |
diff --git a/content/public/renderer/platform_event_observer.h b/content/public/renderer/platform_event_observer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb62d1d2cbca28ba30b8516b6eec8887557ce608 |
--- /dev/null |
+++ b/content/public/renderer/platform_event_observer.h |
@@ -0,0 +1,115 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |
+#define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |
+ |
+#include "base/logging.h" |
+#include "content/public/renderer/render_process_observer.h" |
+#include "content/public/renderer/render_thread.h" |
+ |
+namespace blink { |
+class WebPlatformEventListener; |
+} |
+ |
+namespace content { |
+ |
+// This class is used as a base class for PlatformEventObserver<ListenerType> to |
+// allow storing PlatformEventObserver<> with different typename in the same |
+// place. |
+class PlatformEventObserverBase { |
+ public: |
+ virtual ~PlatformEventObserverBase() { } |
+ |
+ // Methods that need to be exposed in PlatformEventObserverBase. Their purpose |
+ // is described in PlatformEventObserver<>. |
+ |
+ virtual void Start(blink::WebPlatformEventListener* listener) = 0; |
+ virtual void Stop() = 0; |
+ virtual void SendFakeDataForTesting(void*) = 0; |
+}; |
+ |
+// PlatformEventObserver<> defines the basic skeleton for an object requesting |
+// the browser process to start/stop listening to some platform/hardware events |
+// and observe the result. |
+// The results are received via IPC, assuming that the object was correctly |
+// registered as an observer via the constructor taking a RenderThread. |
+template <typename ListenerType> |
+class PlatformEventObserver : public PlatformEventObserverBase, |
+ public RenderProcessObserver { |
+ public: |
+ // Creates a PlatformEventObserver that doesn't listen to response from the |
+ // browser process. Can be used for testing purposes or for observers that |
+ // have other means to get their results. |
+ PlatformEventObserver() |
+ : listener_(0), |
+ is_observing(false) { |
+ } |
+ |
+ // Creates a PlatformEventObserver that registers to the RenderThread in order |
+ // to intercept the received IPC messages (via OnControlMessageReceived). If |
+ // |thread| is null, it will not register. |
+ PlatformEventObserver(RenderThread* thread) |
scottmg
2014/08/05 21:09:18
explicit
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
|
+ : listener_(0), |
+ is_observing(false) { |
+ if (thread) |
+ thread->AddObserver(this); |
+ } |
+ |
+ // The observer will automatically stop observing when destroyed in case of it |
+ // did not stop before. |
+ ~PlatformEventObserver() { |
scottmg
2014/08/05 21:09:18
virtual, and out of line
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Forgot the virtual... However, having it out of li
scottmg
2014/08/05 21:33:31
Sorry, template, duh.
|
+ if (IsObserving()) |
+ Stop(); |
+ } |
+ |
+ // Called when a new IPC message is received. Must be used to listen to the |
+ // responses from the browser process if any is expected. |
+ virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE = 0; |
+ |
+ // Start observing. Will request the browser process to start listening to the |
+ // events. |listener| will receive any response from the browser process. |
+ // Note: should not be called if already observing. |
+ virtual void Start(blink::WebPlatformEventListener* listener) { |
+ DCHECK(!IsObserving()); |
+ listener_ = static_cast<ListenerType*>(listener); |
+ is_observing = true; |
+ |
+ SendStartMessage(); |
+ } |
+ |
+ // Stop observing. Will let the browser know that it doesn't need to observe |
+ // anymore. |
+ virtual void Stop() { |
+ DCHECK(IsObserving()); |
+ listener_ = 0; |
+ is_observing = false; |
+ |
+ SendStopMessage(); |
+ } |
+ |
+ protected: |
+ // This method is expected to send an IPC to the browser process to let it |
+ // know that it should start observing. |
+ virtual void SendStartMessage() = 0; |
+ |
+ // This method is expected to send an IPC to the browser process to let it |
+ /// know that it should stop observing. |
+ virtual void SendStopMessage() = 0; |
+ |
+ bool IsObserving() const { |
+ return is_observing; |
+ } |
+ |
+ ListenerType* listener_; |
scottmg
2014/08/05 21:09:18
Oh, I see now. I don't know is this is against our
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
|
+ |
+ private: |
+ bool is_observing; |
scottmg
2014/08/05 21:09:18
is_observing_
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |