michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsPopupWindowManager.h" michael@0: michael@0: #include "nsCRT.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIPrefService.h" michael@0: #include "nsIPrefBranch.h" michael@0: #include "nsIPrincipal.h" michael@0: #include "nsIURI.h" michael@0: michael@0: /** michael@0: * The Popup Window Manager maintains popup window permissions by website. michael@0: */ michael@0: michael@0: static const char kPopupDisablePref[] = "dom.disable_open_during_load"; michael@0: michael@0: //***************************************************************************** michael@0: //*** nsPopupWindowManager object management and nsISupports michael@0: //***************************************************************************** michael@0: michael@0: nsPopupWindowManager::nsPopupWindowManager() : michael@0: mPolicy(ALLOW_POPUP) michael@0: { michael@0: } michael@0: michael@0: nsPopupWindowManager::~nsPopupWindowManager() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsPopupWindowManager, michael@0: nsIPopupWindowManager, michael@0: nsIObserver, michael@0: nsISupportsWeakReference) michael@0: michael@0: nsresult michael@0: nsPopupWindowManager::Init() michael@0: { michael@0: nsresult rv; michael@0: mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); michael@0: michael@0: nsCOMPtr prefBranch = michael@0: do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: bool permission; michael@0: rv = prefBranch->GetBoolPref(kPopupDisablePref, &permission); michael@0: if (NS_FAILED(rv)) { michael@0: permission = true; michael@0: } michael@0: mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP; michael@0: michael@0: prefBranch->AddObserver(kPopupDisablePref, this, true); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //***************************************************************************** michael@0: //*** nsPopupWindowManager::nsIPopupWindowManager michael@0: //***************************************************************************** michael@0: michael@0: NS_IMETHODIMP michael@0: nsPopupWindowManager::TestPermission(nsIPrincipal* aPrincipal, michael@0: uint32_t *aPermission) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aPrincipal); michael@0: NS_ENSURE_ARG_POINTER(aPermission); michael@0: michael@0: uint32_t permit; michael@0: *aPermission = mPolicy; michael@0: michael@0: if (mPermissionManager) { michael@0: if (NS_SUCCEEDED(mPermissionManager->TestPermissionFromPrincipal(aPrincipal, "popup", &permit))) { michael@0: // Share some constants between interfaces? michael@0: if (permit == nsIPermissionManager::ALLOW_ACTION) { michael@0: *aPermission = ALLOW_POPUP; michael@0: } else if (permit == nsIPermissionManager::DENY_ACTION) { michael@0: *aPermission = DENY_POPUP; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //***************************************************************************** michael@0: //*** nsPopupWindowManager::nsIObserver michael@0: //***************************************************************************** michael@0: NS_IMETHODIMP michael@0: nsPopupWindowManager::Observe(nsISupports *aSubject, michael@0: const char *aTopic, michael@0: const char16_t *aData) michael@0: { michael@0: nsCOMPtr prefBranch = do_QueryInterface(aSubject); michael@0: NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic), michael@0: "unexpected topic - we only deal with pref changes!"); michael@0: michael@0: if (prefBranch) { michael@0: // refresh our local copy of the "disable popups" pref michael@0: bool permission = true; michael@0: prefBranch->GetBoolPref(kPopupDisablePref, &permission); michael@0: michael@0: mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP; michael@0: } michael@0: michael@0: return NS_OK; michael@0: }