Chromium Code Reviews| 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..0f73dbd5240eac721a420f2de8b3b9aa22359006 |
| --- /dev/null |
| +++ b/content/public/renderer/platform_event_observer.h |
| @@ -0,0 +1,118 @@ |
| +// 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 |
|
Avi (use Gerrit)
2014/08/07 19:56:11
... listen to responses ...
mlamouri (slow - plz ping)
2014/08/08 18:08:35
Done.
|
| + // browser process. Can be used for testing purposes or for observers that |
| + // have other means to get their results. |
| + PlatformEventObserver() |
| + : is_observing_(false), |
| + listener_(0) { |
| + } |
| + |
| + // 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. |
| + explicit PlatformEventObserver(RenderThread* thread) |
| + : is_observing_(false), |
| + listener_(0) { |
| + if (thread) |
| + thread->AddObserver(this); |
| + } |
| + |
| + // The observer will automatically stop observing when destroyed in case of it |
| + // 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.
|
| + virtual ~PlatformEventObserver() { |
| + if (is_observing()) |
| + 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. |
|
Avi (use Gerrit)
2014/08/07 19:56:11
... if any are expected.
mlamouri (slow - plz ping)
2014/08/08 18:08:36
Done.
|
| + 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) { |
|
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
|
| + DCHECK(!is_observing()); |
| + 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(is_observing()); |
| + 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; |
|
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.
|
| + |
| + // This method is expected to send an IPC to the browser process to let it |
| + /// 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.
|
| + virtual void SendStopMessage() = 0; |
| + |
| + bool is_observing() const { |
| + return is_observing_; |
| + } |
| + |
| + ListenerType* listener() { |
| + return listener_; |
| + } |
| + |
| + private: |
| + bool is_observing_; |
| + ListenerType* listener_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |