2014-07-14 21:22:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "nsScreenManagerProxy.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2014-07-14 21:22:26 +04:00
|
|
|
#include "nsIAppShell.h"
|
|
|
|
#include "nsIScreen.h"
|
|
|
|
#include "nsIScreenManager.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsScreenManagerProxy, nsIScreenManager)
|
|
|
|
|
|
|
|
nsScreenManagerProxy::nsScreenManagerProxy()
|
|
|
|
: mNumberOfScreens(-1)
|
|
|
|
, mSystemDefaultScale(1.0)
|
|
|
|
, mCacheValid(true)
|
|
|
|
, mCacheWillInvalidate(false)
|
|
|
|
{
|
|
|
|
bool success = false;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << ContentChild::GetSingleton()->SendPScreenManagerConstructor(
|
2014-07-14 21:22:26 +04:00
|
|
|
this,
|
|
|
|
&mNumberOfScreens,
|
|
|
|
&mSystemDefaultScale,
|
|
|
|
&success);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
// We're in bad shape. We'll return the default values, but we'll basically
|
|
|
|
// be lying.
|
|
|
|
NS_WARNING("Setting up communications with the parent nsIScreenManager failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateCacheOnNextTick();
|
|
|
|
|
|
|
|
// nsScreenManagerProxy is a service, which will always have a reference
|
|
|
|
// held to it by the Component Manager once the service is requested.
|
|
|
|
// However, nsScreenManagerProxy also implements PScreenManagerChild, and
|
|
|
|
// that means that the manager of the PScreenManager protocol (PContent
|
|
|
|
// in this case) needs to know how to deallocate this actor. We AddRef here
|
|
|
|
// so that in the event that PContent tries to deallocate us either before
|
|
|
|
// or after process shutdown, we don't try to do a double-free.
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsIScreenManager
|
|
|
|
**/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::GetPrimaryScreen(nsIScreen** outScreen)
|
|
|
|
{
|
|
|
|
InvalidateCacheOnNextTick();
|
|
|
|
|
|
|
|
if (!mPrimaryScreen) {
|
|
|
|
ScreenDetails details;
|
|
|
|
bool success = false;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendGetPrimaryScreen(&details, &success);
|
2014-07-14 21:22:26 +04:00
|
|
|
if (!success) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrimaryScreen = new ScreenProxy(this, details);
|
|
|
|
}
|
|
|
|
NS_ADDREF(*outScreen = mPrimaryScreen);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::ScreenForId(uint32_t aId, nsIScreen** outScreen)
|
|
|
|
{
|
|
|
|
// At this time, there's no need for child processes to query for
|
|
|
|
// screens by ID.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::ScreenForRect(int32_t inLeft,
|
|
|
|
int32_t inTop,
|
|
|
|
int32_t inWidth,
|
|
|
|
int32_t inHeight,
|
|
|
|
nsIScreen** outScreen)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
ScreenDetails details;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendScreenForRect(inLeft, inTop, inWidth, inHeight, &details, &success);
|
2014-07-14 21:22:26 +04:00
|
|
|
if (!success) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScreenProxy> screen = new ScreenProxy(this, details);
|
2014-07-14 21:22:26 +04:00
|
|
|
NS_ADDREF(*outScreen = screen);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::ScreenForNativeWidget(void* aWidget,
|
|
|
|
nsIScreen** outScreen)
|
|
|
|
{
|
|
|
|
// Because ScreenForNativeWidget can be called numerous times
|
|
|
|
// indirectly from content via the DOM Screen API, we cache the
|
|
|
|
// results for this tick of the event loop.
|
|
|
|
TabChild* tabChild = static_cast<TabChild*>(aWidget);
|
|
|
|
|
|
|
|
// Enumerate the cached screen array, looking for one that has
|
|
|
|
// the TabChild that we're looking for...
|
|
|
|
for (uint32_t i = 0; i < mScreenCache.Length(); ++i) {
|
|
|
|
ScreenCacheEntry& curr = mScreenCache[i];
|
|
|
|
if (curr.mTabChild == aWidget) {
|
|
|
|
NS_ADDREF(*outScreen = static_cast<nsIScreen*>(curr.mScreenProxy));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never cached this screen, so we have to ask the parent process
|
|
|
|
// for it.
|
|
|
|
bool success = false;
|
|
|
|
ScreenDetails details;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendScreenForBrowser(tabChild->GetTabId(), &details, &success);
|
2014-07-14 21:22:26 +04:00
|
|
|
if (!success) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenCacheEntry newEntry;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScreenProxy> screen = new ScreenProxy(this, details);
|
2014-07-14 21:22:26 +04:00
|
|
|
|
|
|
|
newEntry.mScreenProxy = screen;
|
|
|
|
newEntry.mTabChild = tabChild;
|
|
|
|
|
|
|
|
mScreenCache.AppendElement(newEntry);
|
|
|
|
|
|
|
|
NS_ADDREF(*outScreen = screen);
|
|
|
|
|
|
|
|
InvalidateCacheOnNextTick();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::GetNumberOfScreens(uint32_t* aNumberOfScreens)
|
|
|
|
{
|
|
|
|
if (!EnsureCacheIsValid()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aNumberOfScreens = mNumberOfScreens;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerProxy::GetSystemDefaultScale(float *aSystemDefaultScale)
|
|
|
|
{
|
|
|
|
if (!EnsureCacheIsValid()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aSystemDefaultScale = mSystemDefaultScale;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsScreenManagerProxy::EnsureCacheIsValid()
|
|
|
|
{
|
|
|
|
if (mCacheValid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
// Kick off a synchronous IPC call to the parent to get the
|
|
|
|
// most up-to-date information.
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendRefresh(&mNumberOfScreens, &mSystemDefaultScale, &success);
|
2014-07-14 21:22:26 +04:00
|
|
|
if (!success) {
|
|
|
|
NS_WARNING("Refreshing nsScreenManagerProxy failed in the parent process.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCacheValid = true;
|
|
|
|
|
|
|
|
InvalidateCacheOnNextTick();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScreenManagerProxy::InvalidateCacheOnNextTick()
|
|
|
|
{
|
|
|
|
if (mCacheWillInvalidate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCacheWillInvalidate = true;
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r =
|
|
|
|
NS_NewRunnableMethod(this, &nsScreenManagerProxy::InvalidateCache);
|
|
|
|
nsContentUtils::RunInStableState(r.forget());
|
2014-07-14 21:22:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScreenManagerProxy::InvalidateCache()
|
|
|
|
{
|
|
|
|
mCacheValid = false;
|
|
|
|
mCacheWillInvalidate = false;
|
|
|
|
|
|
|
|
if (mPrimaryScreen) {
|
|
|
|
mPrimaryScreen = nullptr;
|
|
|
|
}
|
|
|
|
for (int32_t i = mScreenCache.Length() - 1; i >= 0; --i) {
|
|
|
|
mScreenCache.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|