diff options
author | hans@chromium.org <hans@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-24 12:54:51 +0000 |
---|---|---|
committer | hans@chromium.org <hans@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-24 12:54:51 +0000 |
commit | fbc7150171f0ea8202a8427bed6944a2a0b60f6e (patch) | |
tree | 0904cd11c6e2cd546be8ef5c4611bb0c13a6a755 /chrome/browser/device_orientation | |
parent | a2dad7cd5803a7f3ac3a15b0e31ac53515c64c07 (diff) | |
download | chromium_src-fbc7150171f0ea8202a8427bed6944a2a0b60f6e.zip chromium_src-fbc7150171f0ea8202a8427bed6944a2a0b60f6e.tar.gz chromium_src-fbc7150171f0ea8202a8427bed6944a2a0b60f6e.tar.bz2 |
device_orientation::DispatcherHost: one Provider observer per render_view_id.
Refactor device_orientation::DispatcherHost to create a 1-to-1 relationship
between a RenderView and an observer of the Provider. The intention is
to remove the need for logic from DispatcherHost, and in effect have the
RenderView observe the Provider.
BUG=44654
TEST=browser_tests --gtest_filter=DeviceOrientationBrowserTest.BasicTest
Review URL: http://codereview.chromium.org/3152043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57176 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/device_orientation')
-rw-r--r-- | chrome/browser/device_orientation/dispatcher_host.cc | 93 | ||||
-rw-r--r-- | chrome/browser/device_orientation/dispatcher_host.h | 18 | ||||
-rw-r--r-- | chrome/browser/device_orientation/provider.h | 2 |
3 files changed, 74 insertions, 39 deletions
diff --git a/chrome/browser/device_orientation/dispatcher_host.cc b/chrome/browser/device_orientation/dispatcher_host.cc index 605dc82..c7d85db 100644 --- a/chrome/browser/device_orientation/dispatcher_host.cc +++ b/chrome/browser/device_orientation/dispatcher_host.cc @@ -10,19 +10,71 @@ #include "chrome/browser/device_orientation/provider.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/renderer_host/render_view_host_notification_task.h" -#include "chrome/common/chrome_switches.h" #include "chrome/common/render_messages.h" #include "ipc/ipc_message.h" namespace device_orientation { DispatcherHost::DispatcherHost(int process_id) - : process_id_(process_id) { + : process_id_(process_id), + observers_map_(), + provider_(NULL) { } DispatcherHost::~DispatcherHost() { - if (provider_) - provider_->RemoveObserver(this); +} + +class DispatcherHost::ObserverDelegate + : public base::RefCounted<ObserverDelegate>, public Provider::Observer { + public: + // Create ObserverDelegate that observes provider and forwards updates to + // render_view_id in process_id. + // Will stop observing provider when destructed. + ObserverDelegate(Provider* provider, + int process_id, + int render_view_id); + + // From Provider::Observer. + virtual void OnOrientationUpdate(const Orientation& orientation); + + private: + friend class base::RefCounted<ObserverDelegate>; + virtual ~ObserverDelegate(); + + scoped_refptr<Provider> provider_; + int process_id_; + int render_view_id_; + + DISALLOW_COPY_AND_ASSIGN(ObserverDelegate); +}; + +DispatcherHost::ObserverDelegate::ObserverDelegate(Provider* provider, + int process_id, + int render_view_id) + : provider_(provider), + process_id_(process_id), + render_view_id_(render_view_id) { + provider_->AddObserver(this); +} + +DispatcherHost::ObserverDelegate::~ObserverDelegate() { + provider_->RemoveObserver(this); +} + +void DispatcherHost::ObserverDelegate::OnOrientationUpdate( + const Orientation& orientation) { + ViewMsg_DeviceOrientationUpdated_Params params; + params.can_provide_alpha = orientation.can_provide_alpha_; + params.alpha = orientation.alpha_; + params.can_provide_beta = orientation.can_provide_beta_; + params.beta = orientation.beta_; + params.can_provide_gamma = orientation.can_provide_gamma_; + params.gamma = orientation.gamma_; + + IPC::Message* message = new ViewMsg_DeviceOrientationUpdated(render_view_id_, + params); + CallRenderViewHost(process_id_, render_view_id_, &RenderViewHost::Send, + message); } bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, @@ -39,42 +91,21 @@ bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, return handled; } -void DispatcherHost::OnOrientationUpdate(const Orientation& orientation) { - ViewMsg_DeviceOrientationUpdated_Params params; - params.can_provide_alpha = orientation.can_provide_alpha_; - params.alpha = orientation.alpha_; - params.can_provide_beta = orientation.can_provide_beta_; - params.beta = orientation.beta_; - params.can_provide_gamma = orientation.can_provide_gamma_; - params.gamma = orientation.gamma_; - - typedef std::set<int>::const_iterator Iterator; - for (Iterator i = render_view_ids_.begin(), e = render_view_ids_.end(); - i != e; ++i) { - IPC::Message* message = new ViewMsg_DeviceOrientationUpdated(*i, params); - CallRenderViewHost(process_id_, *i, &RenderViewHost::Send, message); - } -} - void DispatcherHost::OnStartUpdating(int render_view_id) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); - render_view_ids_.insert(render_view_id); - if (render_view_ids_.size() == 1) { - DCHECK(!provider_); + if (!provider_) provider_ = Provider::GetInstance(); - provider_->AddObserver(this); - } + + observers_map_[render_view_id] = new ObserverDelegate(provider_, + process_id_, + render_view_id); } void DispatcherHost::OnStopUpdating(int render_view_id) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); - render_view_ids_.erase(render_view_id); - if (render_view_ids_.empty()) { - provider_->RemoveObserver(this); - provider_ = NULL; - } + observers_map_.erase(render_view_id); } } // namespace device_orientation diff --git a/chrome/browser/device_orientation/dispatcher_host.h b/chrome/browser/device_orientation/dispatcher_host.h index f43a68b..d30e8201 100644 --- a/chrome/browser/device_orientation/dispatcher_host.h +++ b/chrome/browser/device_orientation/dispatcher_host.h @@ -5,7 +5,7 @@ #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DISPATCHER_HOST_H_ #define CHROME_BROWSER_DEVICE_ORIENTATION_DISPATCHER_HOST_H_ -#include <set> +#include <map> #include "base/ref_counted.h" #include "chrome/browser/device_orientation/provider.h" @@ -16,24 +16,26 @@ namespace device_orientation { class Orientation; -class DispatcherHost : public base::RefCountedThreadSafe<DispatcherHost>, - public Provider::Observer { +class DispatcherHost : public base::RefCounted<DispatcherHost> { public: explicit DispatcherHost(int process_id); bool OnMessageReceived(const IPC::Message& msg, bool* msg_was_ok); - // From Provider::Observer. - virtual void OnOrientationUpdate(const Orientation& orientation); - private: virtual ~DispatcherHost(); - friend class base::RefCountedThreadSafe<DispatcherHost>; + friend class base::RefCounted<DispatcherHost>; void OnStartUpdating(int render_view_id); void OnStopUpdating(int render_view_id); + // Helper class that observes a Provider and forwards updates to a RenderView. + class ObserverDelegate; + int process_id_; - std::set<int> render_view_ids_; + + // map from render_view_id to ObserverDelegate. + std::map<int, scoped_refptr<ObserverDelegate> > observers_map_; + scoped_refptr<Provider> provider_; DISALLOW_COPY_AND_ASSIGN(DispatcherHost); diff --git a/chrome/browser/device_orientation/provider.h b/chrome/browser/device_orientation/provider.h index f32b0f6..c6b1b7a 100644 --- a/chrome/browser/device_orientation/provider.h +++ b/chrome/browser/device_orientation/provider.h @@ -33,6 +33,8 @@ class Provider : public base::RefCountedThreadSafe<Provider> { // injected object's reference count. static void SetInstanceForTests(Provider* provider); + // Note: AddObserver may call back synchronously to the observer with + // orientation data. virtual void AddObserver(Observer* observer) = 0; virtual void RemoveObserver(Observer* observer) = 0; |