Bug 1197901, ensure sensor events dispatching follows the becoming spec change, r=bz

--HG--
extra : rebase_source : e44c3e357194067ddae10e8b19ee045fb39d36d6
This commit is contained in:
Olli Pettay 2016-02-24 19:43:07 +02:00
Родитель bc0efb6df0
Коммит bdb1f6d5c6
7 изменённых файлов: 209 добавлений и 4 удалений

Просмотреть файл

@ -9527,6 +9527,24 @@ nsGlobalWindow::SetActive(bool aActive)
NotifyDocumentTree(mDoc, nullptr);
}
bool
nsGlobalWindow::IsTopLevelWindowActive()
{
nsCOMPtr<nsIDocShellTreeItem> treeItem(GetDocShell());
if (!treeItem) {
return false;
}
nsCOMPtr<nsIDocShellTreeItem> rootItem;
treeItem->GetRootTreeItem(getter_AddRefs(rootItem));
if (!rootItem) {
return false;
}
nsCOMPtr<nsPIDOMWindowOuter> domWindow = rootItem->GetWindow();
return domWindow && domWindow->IsActive();
}
void nsGlobalWindow::SetIsBackground(bool aIsBackground)
{
MOZ_ASSERT(IsOuterWindow());

Просмотреть файл

@ -422,6 +422,7 @@ public:
// Outer windows only.
virtual void ActivateOrDeactivate(bool aActivate) override;
virtual void SetActive(bool aActive) override;
virtual bool IsTopLevelWindowActive() override;
virtual void SetIsBackground(bool aIsBackground) override;
virtual void SetChromeEventHandler(mozilla::dom::EventTarget* aChromeEventHandler) override;

Просмотреть файл

@ -122,6 +122,8 @@ public:
virtual nsresult RegisterIdleObserver(nsIIdleObserver* aIdleObserver) = 0;
virtual nsresult UnregisterIdleObserver(nsIIdleObserver* aIdleObserver) = 0;
virtual bool IsTopLevelWindowActive() = 0;
// Outer windows only.
virtual void SetActive(bool aActive)
{

Просмотреть файл

@ -14,6 +14,7 @@
#include "nsIDOMWindow.h"
#include "nsPIDOMWindow.h"
#include "nsIDOMDocument.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIServiceManager.h"
#include "nsIServiceManager.h"
#include "mozilla/Preferences.h"
@ -136,6 +137,37 @@ NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *a
return NS_OK;
}
class DeviceSensorTestEvent : public nsRunnable
{
public:
DeviceSensorTestEvent(nsDeviceSensors* aTarget,
uint32_t aType)
: mTarget(aTarget)
, mType(aType)
{
}
NS_IMETHOD Run()
{
SensorData sensorData;
sensorData.sensor() = static_cast<SensorType>(mType);
sensorData.timestamp() = PR_Now();
sensorData.values().AppendElement(0.5f);
sensorData.values().AppendElement(0.5f);
sensorData.values().AppendElement(0.5f);
sensorData.values().AppendElement(0.5f);
sensorData.accuracy() = SENSOR_ACCURACY_UNRELIABLE;
mTarget->Notify(sensorData);
return NS_OK;
}
private:
RefPtr<nsDeviceSensors> mTarget;
uint32_t mType;
};
static bool sTestSensorEvents = false;
NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
{
if (!mEnabled)
@ -149,6 +181,20 @@ NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *a
}
mWindowListeners[aType]->AppendElement(aWindow);
static bool sPrefCacheInitialized = false;
if (!sPrefCacheInitialized) {
sPrefCacheInitialized = true;
Preferences::AddBoolVarCache(&sTestSensorEvents,
"device.sensors.test.events",
false);
}
if (sTestSensorEvents) {
nsCOMPtr<nsIRunnable> event = new DeviceSensorTestEvent(this, aType);
NS_DispatchToCurrentThread(event);
}
return NS_OK;
}
@ -183,10 +229,29 @@ WindowCannotReceiveSensorEvent (nsPIDOMWindowInner* aWindow)
return true;
}
if (aWindow->GetOuterWindow()->IsBackground()) {
bool disabled = aWindow->GetOuterWindow()->IsBackground() ||
!aWindow->IsTopLevelWindowActive();
if (!disabled) {
nsCOMPtr<nsPIDOMWindowOuter> top = aWindow->GetScriptableTop();
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
nsCOMPtr<nsIScriptObjectPrincipal> topSop = do_QueryInterface(top);
if (!sop || !topSop) {
return true;
}
nsIPrincipal* principal = sop->GetPrincipal();
nsIPrincipal* topPrincipal = topSop->GetPrincipal();
if (!principal || !topPrincipal) {
return true;
}
disabled = !principal->Subsumes(topPrincipal);
}
if (disabled) {
nsCOMPtr<nsIPermissionManager> permMgr =
services::GetPermissionManager();
NS_ENSURE_TRUE(permMgr, false);
NS_ENSURE_TRUE(permMgr, true);
uint32_t permission = nsIPermissionManager::DENY_ACTION;
permMgr->TestPermissionFromWindow(aWindow, "background-sensors", &permission);
return permission != nsIPermissionManager::ALLOW_ACTION;
@ -428,7 +493,11 @@ nsDeviceSensors::FireDOMMotionEvent(nsIDOMDocument *domdoc,
double z)
{
// Attempt to coalesce events
bool fireEvent = TimeStamp::Now() > mLastDOMMotionEventTime + TimeDuration::FromMilliseconds(DEFAULT_SENSOR_POLL);
TimeDuration sensorPollDuration =
TimeDuration::FromMilliseconds(DEFAULT_SENSOR_POLL);
bool fireEvent =
(TimeStamp::Now() > mLastDOMMotionEventTime + sensorPollDuration) ||
sTestSensorEvents;
switch (type) {
case nsIDeviceSensorData::TYPE_LINEAR_ACCELERATION:

Просмотреть файл

@ -0,0 +1,16 @@
<pre>Sensor events testing</pre>
<script>
window.onmessage = function (event) {
if (event.data.command == "addEventListener") {
window.addEventListener(
"devicemotion", function() {
event.source.postMessage({ result: event.data.expected,
message: event.data.message },
"*");
}
);
}
}
</script>

Просмотреть файл

@ -1,6 +1,9 @@
[DEFAULT]
skip-if = buildapp != 'b2g'
support-files =
preload-SystemUpdateManager-jsm.js
file_bug1197901.html
[test_system_update_enabled.html]
skip-if = buildapp != 'b2g'
[test_bug1197901.html]
skip-if = buildapp == 'mulet'

Просмотреть файл

@ -0,0 +1,96 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1197901
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1197901</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
/** Test for Bug 1197901 **/
SimpleTest.requestFlakyTimeout("requestFlakyTimeout is silly");
SimpleTest.waitForExplicitFinish();
window.onload = function() {
SimpleTest.waitForFocus(function() {
SpecialPowers.pushPrefEnv({"set": [['device.sensors.test.events', true]]},
doTest);
}, window);
}
function doTest() {
window.onmessage = function(event) {
ok(event.data.result, event.data.message);
}
// Only same-origin iframe should get the events.
var xo = document.getElementById("cross-origin");
xo.contentWindow.postMessage(
{ command: "addEventListener",
expected: false,
message: "Cross-origin iframe shouldn't get the sensor events."},
"*");
var so = document.getElementById("same-origin");
so.contentWindow.postMessage(
{ command: "addEventListener",
expected: true,
message: "Same-origin iframe should get the sensor events." },
"*");
// We need a timeout here to check that something does not happen.
setTimeout(function() {
so.parentNode.removeChild(so);
xo.parentNode.removeChild(xo);
doWindowTest();
}, 500);
}
function doWindowTest() {
var win = window.open("file_bug1197901.html", "w1", "height=100,width=100");
win.onload = function() {
win.focus();
SimpleTest.waitForFocus(function() {
var win2 = window.open("file_bug1197901.html", "w2", "height=100,width=100,left=100");
win2.onload = function() {
win2.focus();
SimpleTest.waitForFocus(function() {
// Only focused window should get the events.
win.postMessage(
{ command: "addEventListener",
expected: false,
message: "Only focused window should get the sensor events." },
"*");
win2.postMessage(
{ command: "addEventListener",
expected: true,
message: "Focused window should get the sensor events." },
"*");
setTimeout(function() {
window.onmessage = null;
win.close();
win2.close();
SimpleTest.finish();
}, 500);
}, win2);
}
}, win);
}
}
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<iframe src="file_bug1197901.html" id="same-origin"></iframe>
<iframe src="http://example.com/tests/dom/system/tests/file_bug1197901.html" id="cross-origin"></iframe>
</body>
</html>