Skip to content

Commit

Permalink
Basic Promise-based lockOrientation() implementation in content/.
Browse files Browse the repository at this point in the history
The only missing bit is to have proper values when the promise is
succussfully resolved.

BUG=162827

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=275058

Review URL: https://codereview.chromium.org/298193003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275168 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mlamouri@chromium.org committed Jun 5, 2014
1 parent e7deba3 commit b6e8531
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool ScreenOrientationDispatcherHost::OnMessageReceived(
bool handled = true;

IPC_BEGIN_MESSAGE_MAP(ScreenOrientationDispatcherHost, message)
IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_Lock, OnLockRequest)
IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_LockRequest, OnLockRequest)
IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_Unlock, OnUnlockRequest)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
Expand All @@ -42,10 +42,20 @@ void ScreenOrientationDispatcherHost::SetProviderForTests(
}

void ScreenOrientationDispatcherHost::OnLockRequest(
blink::WebScreenOrientationLockType orientation) {
if (!provider_.get())
blink::WebScreenOrientationLockType orientation,
int request_id) {
if (!provider_) {
Send(new ScreenOrientationMsg_LockError(
request_id,
blink::WebLockOrientationCallback::ErrorTypeNotAvailable));
return;
}

// TODO(mlamouri): pass real values.
Send(new ScreenOrientationMsg_LockSuccess(
request_id,
0,
blink::WebScreenOrientationPortraitPrimary));
provider_->LockOrientation(orientation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class CONTENT_EXPORT ScreenOrientationDispatcherHost

void SetProviderForTests(ScreenOrientationProvider* provider);

private:
protected:
virtual ~ScreenOrientationDispatcherHost();

void OnLockRequest(blink::WebScreenOrientationLockType orientations);
private:
void OnLockRequest(blink::WebScreenOrientationLockType orientation,
int request_id);
void OnUnlockRequest();

static ScreenOrientationProvider* CreateProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
#include "content/browser/screen_orientation/screen_orientation_dispatcher_host.h"
#include "content/browser/screen_orientation/screen_orientation_provider.h"
#include "content/common/screen_orientation_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
Expand All @@ -21,7 +26,6 @@ class MockScreenOrientationProvider : public ScreenOrientationProvider {
virtual void LockOrientation(blink::WebScreenOrientationLockType orientation)
OVERRIDE {
orientation_ = orientation;

}

virtual void UnlockOrientation() OVERRIDE {
Expand All @@ -45,12 +49,32 @@ class MockScreenOrientationProvider : public ScreenOrientationProvider {
DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider);
};

class ScreenOrientationDispatcherHostWithSink FINAL :
public ScreenOrientationDispatcherHost {
public:
explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink)
: ScreenOrientationDispatcherHost() , sink_(sink) {}

virtual bool Send(IPC::Message* message) OVERRIDE {
return sink_->Send(message);
}

private:
virtual ~ScreenOrientationDispatcherHostWithSink() { }

IPC::TestSink* sink_;
};

class ScreenOrientationDispatcherHostTest : public testing::Test {
protected:
virtual ScreenOrientationDispatcherHost* CreateDispatcher() {
return new ScreenOrientationDispatcherHost();
}

virtual void SetUp() OVERRIDE {
provider_ = new MockScreenOrientationProvider();

dispatcher_ = new ScreenOrientationDispatcherHost();
dispatcher_ = CreateDispatcher();
dispatcher_->SetProviderForTests(provider_);
}

Expand All @@ -59,19 +83,25 @@ class ScreenOrientationDispatcherHostTest : public testing::Test {
scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_;
};

// Test that a NULL provider is correctly handled.
TEST_F(ScreenOrientationDispatcherHostTest, NullProvider) {
dispatcher_->SetProviderForTests(NULL);
class ScreenOrientationDispatcherHostWithSinkTest :
public ScreenOrientationDispatcherHostTest {
protected:
virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE {
return new ScreenOrientationDispatcherHostWithSink(&sink_);
}

bool message_was_handled = dispatcher_->OnMessageReceived(
ScreenOrientationHostMsg_Lock(
blink::WebScreenOrientationLockPortraitPrimary));
EXPECT_TRUE(message_was_handled);
}
const IPC::TestSink& sink() const {
return sink_;
}

IPC::TestSink sink_;
};

// Test that when receiving a lock message, it is correctly dispatched to the
// ScreenOrientationProvider.
TEST_F(ScreenOrientationDispatcherHostTest, ProviderLock) {
// We don't actually need this to be a *WithSinkTest but otherwise the IPC
// messages are detected as leaked.
TEST_F(ScreenOrientationDispatcherHostWithSinkTest, ProviderLock) {
// If we change this array, update |orientationsToTestCount| below.
blink::WebScreenOrientationLockType orientationsToTest[] = {
blink::WebScreenOrientationLockPortraitPrimary,
Expand All @@ -93,7 +123,7 @@ TEST_F(ScreenOrientationDispatcherHostTest, ProviderLock) {
blink::WebScreenOrientationLockType orientation = orientationsToTest[i];

message_was_handled = dispatcher_->OnMessageReceived(
ScreenOrientationHostMsg_Lock(orientation));
ScreenOrientationHostMsg_LockRequest(orientation, 0));

EXPECT_TRUE(message_was_handled);
EXPECT_EQ(orientation, provider_->orientation());
Expand All @@ -110,4 +140,45 @@ TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) {
EXPECT_TRUE(provider_->unlock_called());
}

// Test that when there is no provider, a LockRequest fails with the appropriate
// ErrorType.
TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) {
dispatcher_->SetProviderForTests(NULL);

const int request_id = 3;
dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
blink::WebScreenOrientationLockPortraitPrimary, request_id));

EXPECT_EQ(1u, sink().message_count());

const IPC::Message* msg = sink().GetFirstMessageMatching(
ScreenOrientationMsg_LockError::ID);
EXPECT_TRUE(msg != NULL);

Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params;
ScreenOrientationMsg_LockError::Read(msg, &params);
EXPECT_EQ(request_id, params.a);
EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b);
}

// Test that when there is a provider, we always send a success response back to
// the renderer.
// TODO(mlamouri): we currently do not test the content of the message because
// it currently contains dummy values.
TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) {
const int request_id = 42;
dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
blink::WebScreenOrientationLockPortraitPrimary, request_id));

EXPECT_EQ(1u, sink().message_count());

const IPC::Message* msg = sink().GetFirstMessageMatching(
ScreenOrientationMsg_LockSuccess::ID);
EXPECT_TRUE(msg != NULL);

Tuple3<int, unsigned, blink::WebScreenOrientationType> params;
ScreenOrientationMsg_LockSuccess::Read(msg, &params);
EXPECT_EQ(request_id, params.a);
}

} // namespace content
1 change: 1 addition & 0 deletions content/common/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include_rules = [
"+third_party/WebKit/public/platform/WebIDBCursor.h",
"+third_party/WebKit/public/platform/WebIDBDatabase.h",
"+third_party/WebKit/public/platform/WebIDBTypes.h",
"+third_party/WebKit/public/platform/WebLockOrientationCallback.h",
"+third_party/WebKit/public/platform/WebReferrerPolicy.h",
"+third_party/WebKit/public/platform/WebScreenOrientationLockType.h",
"+third_party/WebKit/public/platform/WebScreenOrientationType.h",
Expand Down
32 changes: 29 additions & 3 deletions content/common/screen_orientation_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "content/common/content_export.h"
#include "ipc/ipc_message_macros.h"
#include "third_party/WebKit/public/platform/WebLockOrientationCallback.h"
#include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
#include "third_party/WebKit/public/platform/WebScreenOrientationType.h"

Expand All @@ -21,16 +22,41 @@ IPC_ENUM_TRAITS_MIN_MAX_VALUE(blink::WebScreenOrientationType,
IPC_ENUM_TRAITS_MIN_MAX_VALUE(blink::WebScreenOrientationLockType,
blink::WebScreenOrientationLockDefault,
blink::WebScreenOrientationLockPortrait)
IPC_ENUM_TRAITS_MIN_MAX_VALUE(
blink::WebLockOrientationCallback::ErrorType,
blink::WebLockOrientationCallback::ErrorTypeNotAvailable,
blink::WebLockOrientationCallback::ErrorTypeCanceled)

// The browser process informs the renderer process that the screen orientation
// has changed. |orientation| contains the new screen orientation in degrees.
IPC_MESSAGE_CONTROL1(ScreenOrientationMsg_OrientationChange,
blink::WebScreenOrientationType /* orientation */ )

// The browser process' response to a ScreenOrientationHostMsg_LockRequest when
// the lock actually succeeded. The message includes the new |angle| and |type|
// of orientation. The |request_id| passed when receiving the request is passed
// back so the renderer process can associate the response to the right request.
IPC_MESSAGE_CONTROL3(ScreenOrientationMsg_LockSuccess,
int, /* request_id */
unsigned, /* angle */
blink::WebScreenOrientationType /* type */)

// The browser process' response to a ScreenOrientationHostMsg_LockRequest when
// the lock actually failed. The message includes the |error| type. The
// |request_id| passed when receiving the request is passed back so the renderer
// process can associate the response to the right request.
IPC_MESSAGE_CONTROL2(ScreenOrientationMsg_LockError,
int, /* request_id */
blink::WebLockOrientationCallback::ErrorType /* error */);

// The renderer process requests the browser process to lock the screen
// orientation to the specified |orientations|.
IPC_MESSAGE_CONTROL1(ScreenOrientationHostMsg_Lock,
blink::WebScreenOrientationLockType /* orientations */ )
// orientation to the specified |orientations|. The request contains a
// |request_id| that will have to be passed back to the renderer process when
// notifying about a success or error (see ScreenOrientationMsg_LockError and
// ScreenOrientationMsg_LockSuccess).
IPC_MESSAGE_CONTROL2(ScreenOrientationHostMsg_LockRequest,
blink::WebScreenOrientationLockType, /* orientation */
int /* request_id */)

// The renderer process requests the browser process to unlock the screen
// orientation.
Expand Down
24 changes: 17 additions & 7 deletions content/renderer/renderer_webkitplatformsupport_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,13 @@ void RendererWebKitPlatformSupportImpl::cancelVibration() {

//------------------------------------------------------------------------------

void RendererWebKitPlatformSupportImpl::EnsureScreenOrientationDispatcher() {
if (screen_orientation_dispatcher_)
return;

screen_orientation_dispatcher_.reset(new ScreenOrientationDispatcher());
}

void RendererWebKitPlatformSupportImpl::setScreenOrientationListener(
blink::WebScreenOrientationListener* listener) {
if (RenderThreadImpl::current() &&
Expand All @@ -1138,22 +1145,23 @@ void RendererWebKitPlatformSupportImpl::setScreenOrientationListener(
return;
}

if (!screen_orientation_dispatcher_) {
screen_orientation_dispatcher_.reset(
new ScreenOrientationDispatcher(RenderThread::Get()));
}

EnsureScreenOrientationDispatcher();
screen_orientation_dispatcher_->setListener(listener);
}

void RendererWebKitPlatformSupportImpl::lockOrientation(
blink::WebScreenOrientationLockType orientation) {
blink::WebScreenOrientationLockType orientation,
blink::WebLockOrientationCallback* callback) {
if (RenderThreadImpl::current() &&
RenderThreadImpl::current()->layout_test_mode()) {
g_test_screen_orientation_controller.Get().UpdateLock(orientation);
return;
}
RenderThread::Get()->Send(new ScreenOrientationHostMsg_Lock(orientation));

EnsureScreenOrientationDispatcher();
screen_orientation_dispatcher_->LockOrientation(
orientation, scoped_ptr<blink::WebLockOrientationCallback>(callback));
}

void RendererWebKitPlatformSupportImpl::unlockOrientation() {
Expand All @@ -1162,7 +1170,9 @@ void RendererWebKitPlatformSupportImpl::unlockOrientation() {
g_test_screen_orientation_controller.Get().ResetLock();
return;
}
RenderThread::Get()->Send(new ScreenOrientationHostMsg_Unlock);

EnsureScreenOrientationDispatcher();
screen_orientation_dispatcher_->UnlockOrientation();
}

// static
Expand Down
4 changes: 3 additions & 1 deletion content/renderer/renderer_webkitplatformsupport_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
virtual void cancelVibration();
virtual void setScreenOrientationListener(
blink::WebScreenOrientationListener*);
virtual void lockOrientation(blink::WebScreenOrientationLockType);
virtual void lockOrientation(blink::WebScreenOrientationLockType,
blink::WebLockOrientationCallback*);
virtual void unlockOrientation();
virtual void setBatteryStatusListener(
blink::WebBatteryStatusListener* listener);
Expand Down Expand Up @@ -196,6 +197,7 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl

private:
bool CheckPreparsedJsCachingEnabled() const;
void EnsureScreenOrientationDispatcher();

scoped_ptr<RendererClipboardClient> clipboard_client_;
scoped_ptr<WebClipboardImpl> clipboard_;
Expand Down
Loading

0 comments on commit b6e8531

Please sign in to comment.