Commit 796d2bd1 authored by mlamouri@chromium.org's avatar mlamouri@chromium.org

Poll for Display rotation on Android 4.0 and 4.1 when needed.

In this context, when needed means when Screen Orientation API is being
used.

BUG=400158

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

Cr-Commit-Position: refs/heads/master@{#288243}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288243 0039d316-1c4b-4281-b951-d872f2087c98
parent a940c00d
......@@ -152,6 +152,7 @@
#if defined(OS_ANDROID)
#include "content/browser/media/android/browser_demuxer_android.h"
#include "content/browser/renderer_host/compositor_impl_android.h"
#include "content/browser/screen_orientation/screen_orientation_message_filter_android.h"
#include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h"
#endif
......@@ -893,6 +894,9 @@ void RenderProcessHostImpl::CreateMessageFilters() {
AddFilter(new PushMessagingMessageFilter(
GetID(), storage_partition_impl_->GetServiceWorkerContext()));
AddFilter(new BatteryStatusMessageFilter());
#if defined(OS_ANDROID)
AddFilter(new ScreenOrientationMessageFilterAndroid());
#endif
}
int RenderProcessHostImpl::GetNextRoutingID() {
......
// 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.
#include "content/browser/screen_orientation/screen_orientation_message_filter_android.h"
#include "content/browser/screen_orientation/screen_orientation_provider_android.h"
#include "content/common/screen_orientation_messages.h"
namespace content {
ScreenOrientationMessageFilterAndroid::ScreenOrientationMessageFilterAndroid()
: BrowserMessageFilter(ScreenOrientationMsgStart)
, listeners_count_(0) {
}
ScreenOrientationMessageFilterAndroid::~ScreenOrientationMessageFilterAndroid()
{
if (listeners_count_ > 0)
ScreenOrientationProviderAndroid::StopAccurateListening();
}
bool ScreenOrientationMessageFilterAndroid::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ScreenOrientationMessageFilterAndroid, message)
IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_StartListening,
OnStartListening)
IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_StopListening,
OnStopListening)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void ScreenOrientationMessageFilterAndroid::OnStartListening() {
++listeners_count_;
if (listeners_count_ == 1)
ScreenOrientationProviderAndroid::StartAccurateListening();
}
void ScreenOrientationMessageFilterAndroid::OnStopListening() {
DCHECK(listeners_count_ > 0);
--listeners_count_;
if (listeners_count_ == 0)
ScreenOrientationProviderAndroid::StopAccurateListening();
}
} // namespace content
// 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_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_MESSAGE_FILTER_ANDROID_H_
#define CONTENT_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_MESSAGE_FILTER_ANDROID_H_
#include "content/public/browser/browser_message_filter.h"
namespace content {
class ScreenOrientationMessageFilterAndroid : public BrowserMessageFilter {
public:
ScreenOrientationMessageFilterAndroid();
// BrowserMessageFilter implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
private:
virtual ~ScreenOrientationMessageFilterAndroid();
void OnStartListening();
void OnStopListening();
int listeners_count_;
DISALLOW_COPY_AND_ASSIGN(ScreenOrientationMessageFilterAndroid);
};
} // namespace content
#endif // CONTENT_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_MESSAGE_FILTER_ANDROID_H_
......@@ -202,6 +202,18 @@ ScreenOrientationProviderAndroid::GetNaturalLockType() const {
return blink::WebScreenOrientationLockDefault;
}
// static
void ScreenOrientationProviderAndroid::StartAccurateListening() {
Java_ScreenOrientationProvider_startAccurateListening(
base::android::AttachCurrentThread());
}
// static
void ScreenOrientationProviderAndroid::StopAccurateListening() {
Java_ScreenOrientationProvider_stopAccurateListening(
base::android::AttachCurrentThread());
}
// static
ScreenOrientationProvider* ScreenOrientationProvider::Create(
ScreenOrientationDispatcherHost* dispatcher,
......
......@@ -33,6 +33,16 @@ class ScreenOrientationProviderAndroid : public ScreenOrientationProvider,
// WebContentsObserver
virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) OVERRIDE;
// Ask the ScreenOrientationListener (Java) to start accurately listening to
// the screen orientation. It keep track of the number of start request if it
// is already running an accurate listening.
static void StartAccurateListening();
// Ask the ScreenOrientationListener (Java) to stop accurately listening to
// the screen orientation. It will actually stop only if the number of stop
// requests matches the number of start requests.
static void StopAccurateListening();
private:
WebContentsImpl* web_contents_impl();
......
......@@ -60,3 +60,17 @@ IPC_MESSAGE_ROUTED2(ScreenOrientationHostMsg_LockRequest,
// The renderer process requests the browser process to unlock the screen
// orientation.
IPC_MESSAGE_ROUTED0(ScreenOrientationHostMsg_Unlock)
// The renderer process is now using the Screen Orientation API and informs the
// browser process that it should start accurately listening to the screen
// orientation if it wasn't already.
// This is only expected to be acted upon when the underlying platform requires
// heavy work in order to accurately know the screen orientation.
IPC_MESSAGE_CONTROL0(ScreenOrientationHostMsg_StartListening)
// The renderer process is no longer using the Screen Orientation API and
// informs the browser process that it can stop accurately listening to the
// screen orientation if no other process cares about it.
// This is only expected to be acted upon when the underlying platform requires
// heavy work in order to accurately know the screen orientation.
IPC_MESSAGE_CONTROL0(ScreenOrientationHostMsg_StopListening)
......@@ -1111,6 +1111,8 @@
'browser/safe_util_win.h',
'browser/screen_orientation/screen_orientation_dispatcher_host.cc',
'browser/screen_orientation/screen_orientation_dispatcher_host.h',
'browser/screen_orientation/screen_orientation_message_filter_android.h',
'browser/screen_orientation/screen_orientation_message_filter_android.cc',
'browser/screen_orientation/screen_orientation_provider.h',
'browser/screen_orientation/screen_orientation_provider_android.h',
'browser/screen_orientation/screen_orientation_provider_android.cc',
......
......@@ -60,6 +60,19 @@ public class ScreenOrientationListener {
* when the last observer is removed.
*/
void stopListening();
/**
* Toggle the accurate mode if it wasn't already doing so. The backend
* will keep track of the number of times this has been called.
*/
void startAccurateListening();
/**
* Request to stop the accurate mode. It will effectively be stopped
* only if this method is called as many times as
* startAccurateListening().
*/
void stopAccurateListening();
}
/**
......@@ -69,10 +82,16 @@ public class ScreenOrientationListener {
*
* This method is known to not correctly detect 180 degrees changes but it
* is the only method that will work before API Level 17 (excluding polling).
* When toggleAccurateMode() is called, it will start polling in order to
* find out if the display has changed.
*/
private class ScreenOrientationConfigurationListener
implements ScreenOrientationListenerBackend, ComponentCallbacks {
private static final long POLLING_DELAY = 500;
private int mAccurateCount = 0;
// ScreenOrientationListenerBackend implementation:
@Override
......@@ -85,6 +104,36 @@ public class ScreenOrientationListener {
mAppContext.unregisterComponentCallbacks(this);
}
@Override
public void startAccurateListening() {
++mAccurateCount;
if (mAccurateCount > 1)
return;
// Start polling if we went from 0 to 1. The polling will
// automatically stop when mAccurateCount reaches 0.
final ScreenOrientationConfigurationListener self = this;
ThreadUtils.postOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
self.onConfigurationChanged(null);
if (self.mAccurateCount < 1)
return;
ThreadUtils.postOnUiThreadDelayed(this,
ScreenOrientationConfigurationListener.POLLING_DELAY);
}
}, POLLING_DELAY);
}
@Override
public void stopAccurateListening() {
--mAccurateCount;
assert mAccurateCount >= 0;
}
// ComponentCallbacks implementation:
@Override
......@@ -123,6 +172,16 @@ public class ScreenOrientationListener {
displayManager.unregisterDisplayListener(this);
}
@Override
public void startAccurateListening() {
// Always accurate. Do nothing.
}
@Override
public void stopAccurateListening() {
// Always accurate. Do nothing.
}
// DisplayListener implementation:
@Override
......@@ -234,6 +293,22 @@ public class ScreenOrientationListener {
}
}
/**
* Toggle the accurate mode if it wasn't already doing so. The backend will
* keep track of the number of times this has been called.
*/
public void startAccurateListening() {
mBackend.startAccurateListening();
}
/**
* Request to stop the accurate mode. It will effectively be stopped only if
* this method is called as many times as startAccurateListening().
*/
public void stopAccurateListening() {
mBackend.stopAccurateListening();
}
/**
* This should be called by classes extending ScreenOrientationListener when
* it is possible that there is a screen orientation change. If there is an
......@@ -243,12 +318,12 @@ public class ScreenOrientationListener {
int previousOrientation = mOrientation;
updateOrientation();
DeviceDisplayInfo.create(mAppContext).updateNativeSharedDisplayInfo();
if (mOrientation == previousOrientation) {
return;
}
DeviceDisplayInfo.create(mAppContext).updateNativeSharedDisplayInfo();
for (ScreenOrientationObserver observer : mObservers) {
observer.onScreenOrientationChanged(mOrientation);
}
......
......@@ -12,6 +12,7 @@ import android.util.Log;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.ThreadUtils;
import org.chromium.content.common.ScreenOrientationValues;
/**
......@@ -80,6 +81,26 @@ class ScreenOrientationProvider {
}
}
@CalledByNative
static void startAccurateListening() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
ScreenOrientationListener.getInstance().startAccurateListening();
}
});
}
@CalledByNative
static void stopAccurateListening() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
ScreenOrientationListener.getInstance().stopAccurateListening();
}
});
}
private ScreenOrientationProvider() {
}
}
......@@ -33,6 +33,7 @@
#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
#include "content/common/gpu/gpu_process_launch_causes.h"
#include "content/common/mime_registry_messages.h"
#include "content/common/screen_orientation_messages.h"
#include "content/common/view_messages.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/webplugininfo.h"
......@@ -1074,6 +1075,9 @@ void RendererWebKitPlatformSupportImpl::startListening(
SetGamepadListener(
static_cast<blink::WebGamepadListener*>(listener));
break;
case blink::WebPlatformEventScreenOrientation:
RenderThread::Get()->Send(new ScreenOrientationHostMsg_StartListening());
break;
default:
// A default statement is required to prevent compilation errors when Blink
// adds a new type.
......@@ -1100,6 +1104,9 @@ void RendererWebKitPlatformSupportImpl::stopListening(
case blink::WebPlatformEventGamepad:
SetGamepadListener(0);
break;
case blink::WebPlatformEventScreenOrientation:
RenderThread::Get()->Send(new ScreenOrientationHostMsg_StopListening());
break;
default:
// A default statement is required to prevent compilation errors when Blink
// adds a new type.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment