Skip to content

Commit

Permalink
Bug 1197901, ensure sensor events dispatching follows the becoming sp…
Browse files Browse the repository at this point in the history
…ec change, r=bz

--HG--
extra : rebase_source : e44c3e357194067ddae10e8b19ee045fb39d36d6
  • Loading branch information
Olli Pettay authored and Olli Pettay committed Feb 24, 2016
1 parent bc0efb6 commit bdb1f6d
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 4 deletions.
18 changes: 18 additions & 0 deletions dom/base/nsGlobalWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsGlobalWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class nsGlobalWindow : public mozilla::dom::EventTarget,
// 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;

Expand Down
2 changes: 2 additions & 0 deletions dom/base/nsPIDOMWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class nsPIDOMWindow : public T
virtual nsresult RegisterIdleObserver(nsIIdleObserver* aIdleObserver) = 0;
virtual nsresult UnregisterIdleObserver(nsIIdleObserver* aIdleObserver) = 0;

virtual bool IsTopLevelWindowActive() = 0;

// Outer windows only.
virtual void SetActive(bool aActive)
{
Expand Down
75 changes: 72 additions & 3 deletions dom/system/nsDeviceSensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions dom/system/tests/file_bug1197901.html
Original file line number Diff line number Diff line change
@@ -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>
5 changes: 4 additions & 1 deletion dom/system/tests/mochitest.ini
Original file line number Diff line number Diff line change
@@ -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'
96 changes: 96 additions & 0 deletions dom/system/tests/test_bug1197901.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit bdb1f6d

Please sign in to comment.