extensions/cookie/nsPopupWindowManager.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/nsPopupWindowManager.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsPopupWindowManager.h"
    1.10 +
    1.11 +#include "nsCRT.h"
    1.12 +#include "nsIServiceManager.h"
    1.13 +#include "nsIPrefService.h"
    1.14 +#include "nsIPrefBranch.h"
    1.15 +#include "nsIPrincipal.h"
    1.16 +#include "nsIURI.h"
    1.17 +
    1.18 +/**
    1.19 + * The Popup Window Manager maintains popup window permissions by website.
    1.20 + */
    1.21 +
    1.22 +static const char kPopupDisablePref[] = "dom.disable_open_during_load";
    1.23 +
    1.24 +//*****************************************************************************
    1.25 +//*** nsPopupWindowManager object management and nsISupports
    1.26 +//*****************************************************************************
    1.27 +
    1.28 +nsPopupWindowManager::nsPopupWindowManager() :
    1.29 +  mPolicy(ALLOW_POPUP)
    1.30 +{
    1.31 +}
    1.32 +
    1.33 +nsPopupWindowManager::~nsPopupWindowManager()
    1.34 +{
    1.35 +}
    1.36 +
    1.37 +NS_IMPL_ISUPPORTS(nsPopupWindowManager, 
    1.38 +                  nsIPopupWindowManager,
    1.39 +                  nsIObserver,
    1.40 +                  nsISupportsWeakReference)
    1.41 +
    1.42 +nsresult
    1.43 +nsPopupWindowManager::Init()
    1.44 +{
    1.45 +  nsresult rv;
    1.46 +  mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
    1.47 +
    1.48 +  nsCOMPtr<nsIPrefBranch> prefBranch =
    1.49 +    do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
    1.50 +  if (NS_SUCCEEDED(rv)) {
    1.51 +    bool permission;
    1.52 +    rv = prefBranch->GetBoolPref(kPopupDisablePref, &permission);
    1.53 +    if (NS_FAILED(rv)) {
    1.54 +      permission = true;
    1.55 +    }
    1.56 +    mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP;
    1.57 +
    1.58 +    prefBranch->AddObserver(kPopupDisablePref, this, true);
    1.59 +  } 
    1.60 +
    1.61 +  return NS_OK;
    1.62 +}
    1.63 +
    1.64 +//*****************************************************************************
    1.65 +//*** nsPopupWindowManager::nsIPopupWindowManager
    1.66 +//*****************************************************************************
    1.67 +
    1.68 +NS_IMETHODIMP
    1.69 +nsPopupWindowManager::TestPermission(nsIPrincipal* aPrincipal,
    1.70 +                                     uint32_t *aPermission)
    1.71 +{
    1.72 +  NS_ENSURE_ARG_POINTER(aPrincipal);
    1.73 +  NS_ENSURE_ARG_POINTER(aPermission);
    1.74 +
    1.75 +  uint32_t permit;
    1.76 +  *aPermission = mPolicy;
    1.77 +
    1.78 +  if (mPermissionManager) {
    1.79 +    if (NS_SUCCEEDED(mPermissionManager->TestPermissionFromPrincipal(aPrincipal, "popup", &permit))) {
    1.80 +      // Share some constants between interfaces?
    1.81 +      if (permit == nsIPermissionManager::ALLOW_ACTION) {
    1.82 +        *aPermission = ALLOW_POPUP;
    1.83 +      } else if (permit == nsIPermissionManager::DENY_ACTION) {
    1.84 +        *aPermission = DENY_POPUP;
    1.85 +      }
    1.86 +    }
    1.87 +  }
    1.88 +
    1.89 +  return NS_OK;
    1.90 +}
    1.91 +
    1.92 +//*****************************************************************************
    1.93 +//*** nsPopupWindowManager::nsIObserver
    1.94 +//*****************************************************************************
    1.95 +NS_IMETHODIMP
    1.96 +nsPopupWindowManager::Observe(nsISupports *aSubject, 
    1.97 +                              const char *aTopic,
    1.98 +                              const char16_t *aData)
    1.99 +{
   1.100 +  nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
   1.101 +  NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
   1.102 +               "unexpected topic - we only deal with pref changes!");
   1.103 +
   1.104 +  if (prefBranch) {
   1.105 +    // refresh our local copy of the "disable popups" pref
   1.106 +    bool permission = true;
   1.107 +    prefBranch->GetBoolPref(kPopupDisablePref, &permission);
   1.108 +
   1.109 +    mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP;
   1.110 +  }
   1.111 +
   1.112 +  return NS_OK;
   1.113 +}

mercurial