Bug 720799 - Don't use mDocShell in nsScreen. r=jst,jlebar

This commit is contained in:
Mounir Lamouri 2012-03-14 19:14:53 +01:00
parent e1b61ee6bf
commit e27c8b19cf
4 changed files with 52 additions and 40 deletions

View File

@ -56,6 +56,23 @@ using namespace mozilla::dom;
/* static */ bool nsScreen::sAllowScreenEnabledProperty = false;
/* static */ bool nsScreen::sAllowScreenBrightnessProperty = false;
namespace {
bool
IsChromeType(nsIDocShell *aDocShell)
{
nsCOMPtr<nsIDocShellTreeItem> ds = do_QueryInterface(aDocShell);
if (!ds) {
return false;
}
PRInt32 itemType;
ds->GetItemType(&itemType);
return itemType == nsIDocShellTreeItem::typeChrome;
}
} // anonymous namespace
/* static */ void
nsScreen::Initialize()
{
@ -89,7 +106,7 @@ nsScreen::Create(nsPIDOMWindow* aWindow)
nsRefPtr<nsScreen> screen = new nsScreen();
screen->BindToOwner(aWindow);
screen->mDocShell = aWindow->GetDocShell();
screen->mIsChrome = IsChromeType(aWindow->GetDocShell());
hal::RegisterScreenOrientationObserver(screen);
hal::GetCurrentScreenOrientation(&(screen->mOrientation));
@ -133,6 +150,28 @@ NS_IMPL_RELEASE_INHERITED(nsScreen, nsDOMEventTargetHelper)
NS_IMPL_EVENT_HANDLER(nsScreen, mozorientationchange)
bool
nsScreen::IsWhiteListed() {
if (mIsChrome) {
return true;
}
if (!GetOwner()) {
return false;
}
nsCOMPtr<nsIDocument> doc = do_GetInterface(GetOwner()->GetDocShell());
if (!doc) {
return false;
}
nsIPrincipal *principal = doc->NodePrincipal();
nsCOMPtr<nsIURI> principalURI;
principal->GetURI(getter_AddRefs(principalURI));
return nsContentUtils::URIIsChromeOrInPref(principalURI,
"dom.mozScreenWhitelist");
}
NS_IMETHODIMP
nsScreen::GetTop(PRInt32* aTop)
{
@ -251,7 +290,7 @@ nsScreen::GetAvailTop(PRInt32* aAvailTop)
nsDeviceContext*
nsScreen::GetDeviceContext()
{
return nsLayoutUtils::GetDeviceContextForScreenInfo(mDocShell);
return nsLayoutUtils::GetDeviceContextForScreenInfo(GetOwner());
}
nsresult
@ -292,39 +331,10 @@ nsScreen::GetAvailRect(nsRect& aRect)
return NS_OK;
}
namespace {
bool IsWhiteListed(nsIDocShell *aDocShell) {
nsCOMPtr<nsIDocShellTreeItem> ds = do_QueryInterface(aDocShell);
if (!ds) {
return false;
}
PRInt32 itemType;
ds->GetItemType(&itemType);
if (itemType == nsIDocShellTreeItem::typeChrome) {
return true;
}
nsCOMPtr<nsIDocument> doc = do_GetInterface(aDocShell);
nsIPrincipal *principal = doc->NodePrincipal();
nsCOMPtr<nsIURI> principalURI;
principal->GetURI(getter_AddRefs(principalURI));
if (nsContentUtils::URIIsChromeOrInPref(principalURI,
"dom.mozScreenWhitelist")) {
return true;
}
return false;
}
} // anonymous namespace
nsresult
nsScreen::GetMozEnabled(bool *aEnabled)
{
if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
*aEnabled = true;
return NS_OK;
}
@ -336,7 +346,7 @@ nsScreen::GetMozEnabled(bool *aEnabled)
nsresult
nsScreen::SetMozEnabled(bool aEnabled)
{
if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
return NS_OK;
}
@ -349,7 +359,7 @@ nsScreen::SetMozEnabled(bool aEnabled)
nsresult
nsScreen::GetMozBrightness(double *aBrightness)
{
if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
*aBrightness = 1;
return NS_OK;
}
@ -361,7 +371,7 @@ nsScreen::GetMozBrightness(double *aBrightness)
nsresult
nsScreen::SetMozBrightness(double aBrightness)
{
if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
return NS_OK;
}

View File

@ -74,7 +74,7 @@ protected:
nsresult GetRect(nsRect& aRect);
nsresult GetAvailRect(nsRect& aRect);
nsIDocShell* mDocShell; // Weak Reference
bool mIsChrome;
mozilla::dom::ScreenOrientation mOrientation;
@ -88,6 +88,8 @@ private:
static void Initialize();
bool IsWhiteListed();
NS_DECL_EVENT_HANDLER(mozorientationchange)
};

View File

@ -4054,9 +4054,9 @@ nsLayoutUtils::GetRectDifferenceStrips(const nsRect& aR1, const nsRect& aR2,
}
nsDeviceContext*
nsLayoutUtils::GetDeviceContextForScreenInfo(nsIDocShell* aDocShell)
nsLayoutUtils::GetDeviceContextForScreenInfo(nsPIDOMWindow* aWindow)
{
nsCOMPtr<nsIDocShell> docShell = aDocShell;
nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell();
while (docShell) {
// Now make sure our size is up to date. That will mean that the device
// context does the right thing on multi-monitor systems when we return it to

View File

@ -1330,11 +1330,11 @@ public:
/**
* Get a device context that can be used to get up-to-date device
* dimensions for the given docshell. For some reason, this is more
* dimensions for the given window. For some reason, this is more
* complicated than it ought to be in multi-monitor situations.
*/
static nsDeviceContext*
GetDeviceContextForScreenInfo(nsIDocShell* aDocShell);
GetDeviceContextForScreenInfo(nsPIDOMWindow* aWindow);
/**
* Some frames with 'position: fixed' (nsStylePosition::mDisplay ==