diff options
author | hans@chromium.org <hans@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-11 14:42:53 +0000 |
---|---|---|
committer | hans@chromium.org <hans@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-11 14:42:53 +0000 |
commit | 57ead35546411f3cb76705c0f21ef139593f5ec8 (patch) | |
tree | 4411a4b3db58b14eb0f3e852550ee73dabc9e273 /chrome/browser/device_orientation | |
parent | bacfe81c2a05ea03178d6764f39b49bee66237de (diff) | |
download | chromium_src-57ead35546411f3cb76705c0f21ef139593f5ec8.zip chromium_src-57ead35546411f3cb76705c0f21ef139593f5ec8.tar.gz chromium_src-57ead35546411f3cb76705c0f21ef139593f5ec8.tar.bz2 |
Chromium plumbing for Device Orientation.
Add the plumbing needed for communicating with the Device Orientation code in WebKit.
RenderView provides an implementation of WebKit::WebDeviceOrientationClient: DeviceOrientationDispatcher. This communicates with the browser-side class device_orientation::DispatcherHost.
device_orientation::Provider, responsible for providing the orientation data, is just an empty shell for now.
BUG=44654
TEST=browser_tests --gtest_filter=DeviceOrientationBrowserTest.BasicTest
Review URL: http://codereview.chromium.org/2858049
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@55724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/device_orientation')
-rw-r--r-- | chrome/browser/device_orientation/device_orientation_browsertest.cc | 70 | ||||
-rw-r--r-- | chrome/browser/device_orientation/dispatcher_host.cc | 80 | ||||
-rw-r--r-- | chrome/browser/device_orientation/dispatcher_host.h | 44 | ||||
-rw-r--r-- | chrome/browser/device_orientation/orientation.h | 47 | ||||
-rw-r--r-- | chrome/browser/device_orientation/provider.cc | 24 | ||||
-rw-r--r-- | chrome/browser/device_orientation/provider.h | 55 |
6 files changed, 320 insertions, 0 deletions
diff --git a/chrome/browser/device_orientation/device_orientation_browsertest.cc b/chrome/browser/device_orientation/device_orientation_browsertest.cc new file mode 100644 index 0000000..332316e --- /dev/null +++ b/chrome/browser/device_orientation/device_orientation_browsertest.cc @@ -0,0 +1,70 @@ +// Copyright (c) 2010 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. + +#include "base/command_line.h" +#include "base/file_path.h" +#include "base/ref_counted.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/device_orientation/orientation.h" +#include "chrome/browser/device_orientation/provider.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" + +namespace device_orientation { + +class MockProvider : public Provider { + public: + explicit MockProvider(const Orientation& orientation) + : orientation_(orientation), + added_observer_(false), + removed_observer_(false) {} + + virtual void AddObserver(Observer* observer) { + added_observer_ = true; + observer->OnOrientationUpdate(orientation_); + } + virtual void RemoveObserver(Observer* observer) { + removed_observer_ = true; + } + Orientation orientation_; + bool added_observer_; + bool removed_observer_; +}; + +class DeviceOrientationBrowserTest : public InProcessBrowserTest { + public: + // From InProcessBrowserTest. + virtual void SetUpCommandLine(CommandLine* command_line) { + command_line->AppendSwitch(switches::kEnableDeviceOrientation); + } + + GURL testUrl(const FilePath::CharType* filename) { + const FilePath kTestDir(FILE_PATH_LITERAL("device_orientation")); + return ui_test_utils::GetTestUrl(kTestDir, FilePath(filename)); + } +}; + +IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicTest) { + const Orientation kTestOrientation(true, 1, true, 2, true, 3); + scoped_refptr<MockProvider> provider = new MockProvider(kTestOrientation); + Provider::SetInstanceForTests(provider.get()); + + // The test page will register an event handler for orientation events, + // expects to get an event with kTestOrientation orientation, + // then removes the event handler and navigates to #pass. + GURL test_url = testUrl(FILE_PATH_LITERAL("device_orientation_test.html")); + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), + test_url, + 2); + + // Check that the page got the event it expected and that the provider + // saw requests for adding and removing an observer. + EXPECT_EQ("pass", browser()->GetSelectedTabContents()->GetURL().ref()); + EXPECT_TRUE(provider->added_observer_); + EXPECT_TRUE(provider->removed_observer_); +} + +} // namespace device_orientation diff --git a/chrome/browser/device_orientation/dispatcher_host.cc b/chrome/browser/device_orientation/dispatcher_host.cc new file mode 100644 index 0000000..605dc82 --- /dev/null +++ b/chrome/browser/device_orientation/dispatcher_host.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2010 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. + +#include "chrome/browser/device_orientation/dispatcher_host.h" + +#include "base/scoped_ptr.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/device_orientation/orientation.h" +#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) { +} + +DispatcherHost::~DispatcherHost() { + if (provider_) + provider_->RemoveObserver(this); +} + +bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, + bool* msg_was_ok) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + bool handled = true; + IPC_BEGIN_MESSAGE_MAP_EX(DispatcherHost, msg, *msg_was_ok) + IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StartUpdating, + OnStartUpdating) + IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StopUpdating, + OnStopUpdating) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + 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_); + provider_ = Provider::GetInstance(); + provider_->AddObserver(this); + } +} + +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; + } +} + +} // namespace device_orientation diff --git a/chrome/browser/device_orientation/dispatcher_host.h b/chrome/browser/device_orientation/dispatcher_host.h new file mode 100644 index 0000000..f43a68b --- /dev/null +++ b/chrome/browser/device_orientation/dispatcher_host.h @@ -0,0 +1,44 @@ +// Copyright (c) 2010 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 CHROME_BROWSER_DEVICE_ORIENTATION_DISPATCHER_HOST_H_ +#define CHROME_BROWSER_DEVICE_ORIENTATION_DISPATCHER_HOST_H_ + +#include <set> + +#include "base/ref_counted.h" +#include "chrome/browser/device_orientation/provider.h" + +namespace IPC { class Message; } + +namespace device_orientation { + +class Orientation; + +class DispatcherHost : public base::RefCountedThreadSafe<DispatcherHost>, + public Provider::Observer { + 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>; + + void OnStartUpdating(int render_view_id); + void OnStopUpdating(int render_view_id); + + int process_id_; + std::set<int> render_view_ids_; + scoped_refptr<Provider> provider_; + + DISALLOW_COPY_AND_ASSIGN(DispatcherHost); +}; + +} // namespace device_orientation + +#endif // CHROME_BROWSER_DEVICE_ORIENTATION_DISPATCHER_HOST_H_ diff --git a/chrome/browser/device_orientation/orientation.h b/chrome/browser/device_orientation/orientation.h new file mode 100644 index 0000000..d43a989 --- /dev/null +++ b/chrome/browser/device_orientation/orientation.h @@ -0,0 +1,47 @@ +// Copyright (c) 2010 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 CHROME_BROWSER_DEVICE_ORIENTATION_ORIENTATION_H_ +#define CHROME_BROWSER_DEVICE_ORIENTATION_ORIENTATION_H_ + +namespace device_orientation { +class Orientation { + public: + // alpha, beta and gamma are the rotations around the axes as specified in + // http://dev.w3.org/geo/api/spec-source-orientation.html + // + // can_provide_{alpha,beta,gamma} is true if data can be provided for that + // variable. + + Orientation(bool can_provide_alpha, double alpha, + bool can_provide_beta, double beta, + bool can_provide_gamma, double gamma) + : alpha_(alpha), + beta_(beta), + gamma_(gamma), + can_provide_alpha_(can_provide_alpha), + can_provide_beta_(can_provide_beta), + can_provide_gamma_(can_provide_gamma) { + } + + Orientation() + : alpha_(0), + beta_(0), + gamma_(0), + can_provide_alpha_(false), + can_provide_beta_(false), + can_provide_gamma_(false) { + } + + double alpha_; + double beta_; + double gamma_; + bool can_provide_alpha_; + bool can_provide_beta_; + bool can_provide_gamma_; +}; + +} // namespace device_orientation + +#endif // CHROME_BROWSER_DEVICE_ORIENTATION_ORIENTATION_H_ diff --git a/chrome/browser/device_orientation/provider.cc b/chrome/browser/device_orientation/provider.cc new file mode 100644 index 0000000..16b24b3 --- /dev/null +++ b/chrome/browser/device_orientation/provider.cc @@ -0,0 +1,24 @@ +// Copyright (c) 2010 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. + +#include "chrome/browser/device_orientation/provider.h" + +namespace device_orientation { + +Provider* Provider::GetInstance() { + if (!instance_) + // TODO(hans) This is not finished. We will create an instance of the real + // Provider implementation once it is implemented. + instance_ = new Provider(); + return instance_; +} + +void Provider::SetInstanceForTests(Provider* provider) { + DCHECK(!instance_); + instance_ = provider; +} + +Provider* Provider::instance_ = NULL; + +} // namespace device_orientation diff --git a/chrome/browser/device_orientation/provider.h b/chrome/browser/device_orientation/provider.h new file mode 100644 index 0000000..aaa5106 --- /dev/null +++ b/chrome/browser/device_orientation/provider.h @@ -0,0 +1,55 @@ +// Copyright (c) 2010 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 CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_ +#define CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_ + +#include "base/logging.h" +#include "base/ref_counted.h" + +namespace device_orientation { + +class Orientation; + +class Provider : public base::RefCounted<Provider> { + public: + class Observer { + public: + // Called when the orientation changes. + // An Observer must not synchronously call Provider::RemoveObserver + // or Provider::AddObserver when this is called. + virtual void OnOrientationUpdate(const Orientation& orientation) = 0; + }; + + // Returns a pointer to the singleton instance of this class. + // The caller should store the returned pointer in a scoped_refptr. + // The Provider instance is lazily constructed when GetInstance() is called, + // and destructed when the last scoped_refptr referring to it is destructed. + static Provider* GetInstance(); + + // Inject a mock Provider for testing. Only a weak pointer to the injected + // object will be held by Provider, i.e. it does not itself contribute to the + // injected object's reference count. + static void SetInstanceForTests(Provider* provider); + + virtual void AddObserver(Observer* observer) {} + virtual void RemoveObserver(Observer* observer) {} + + protected: + Provider() {} + virtual ~Provider() { + DCHECK(instance_ == this); + instance_ = NULL; + } + + private: + friend class base::RefCounted<Provider>; + static Provider* instance_; + + DISALLOW_COPY_AND_ASSIGN(Provider); +}; + +} // namespace device_orientation + +#endif // CHROME_BROWSER_DEVICE_ORIENTATION_PROVIDER_H_ |