1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/cookie/nsCookiePromptService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 + 1.9 +#include "nsCookiePromptService.h" 1.10 +#include "nsICookie.h" 1.11 +#include "nsICookieAcceptDialog.h" 1.12 +#include "nsIDOMWindow.h" 1.13 +#include "nsPIDOMWindow.h" 1.14 +#include "nsIWindowWatcher.h" 1.15 +#include "nsIServiceManager.h" 1.16 +#include "nsString.h" 1.17 +#include "nsIDialogParamBlock.h" 1.18 +#include "nsIMutableArray.h" 1.19 + 1.20 +/**************************************************************** 1.21 + ************************ nsCookiePromptService ***************** 1.22 + ****************************************************************/ 1.23 + 1.24 +NS_IMPL_ISUPPORTS(nsCookiePromptService, nsICookiePromptService) 1.25 + 1.26 +nsCookiePromptService::nsCookiePromptService() { 1.27 +} 1.28 + 1.29 +nsCookiePromptService::~nsCookiePromptService() { 1.30 +} 1.31 + 1.32 +NS_IMETHODIMP 1.33 +nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent, 1.34 + nsICookie *aCookie, 1.35 + const nsACString &aHostname, 1.36 + int32_t aCookiesFromHost, 1.37 + bool aChangingCookie, 1.38 + bool *aRememberDecision, 1.39 + int32_t *aAccept) 1.40 +{ 1.41 + nsresult rv; 1.42 + 1.43 + nsCOMPtr<nsIDialogParamBlock> block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv); 1.44 + if (NS_FAILED(rv)) return rv; 1.45 + 1.46 + block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1); 1.47 + block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get()); 1.48 + block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost); 1.49 + block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie ? 1 : 0); 1.50 + 1.51 + nsCOMPtr<nsIMutableArray> objects = 1.52 + do_CreateInstance(NS_ARRAY_CONTRACTID, &rv); 1.53 + if (NS_FAILED(rv)) return rv; 1.54 + 1.55 + rv = objects->AppendElement(aCookie, false); 1.56 + if (NS_FAILED(rv)) return rv; 1.57 + 1.58 + block->SetObjects(objects); 1.59 + 1.60 + nsCOMPtr<nsIWindowWatcher> wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); 1.61 + if (NS_FAILED(rv)) return rv; 1.62 + 1.63 + nsCOMPtr<nsISupports> arguments = do_QueryInterface(block); 1.64 + nsCOMPtr<nsIDOMWindow> dialog; 1.65 + 1.66 + nsCOMPtr<nsIDOMWindow> parent(aParent); 1.67 + if (!parent) // if no parent provided, consult the window watcher: 1.68 + wwatcher->GetActiveWindow(getter_AddRefs(parent)); 1.69 + 1.70 + if (parent) { 1.71 + nsCOMPtr<nsPIDOMWindow> privateParent(do_QueryInterface(parent)); 1.72 + if (privateParent) 1.73 + privateParent = privateParent->GetPrivateRoot(); 1.74 + parent = do_QueryInterface(privateParent); 1.75 + } 1.76 + 1.77 + // The cookie dialog will be modal for the root chrome window rather than the 1.78 + // tab containing the permission-requesting page. This removes confusion 1.79 + // about which monitor is displaying the dialog (see bug 470356), but also 1.80 + // avoids unwanted tab switches (see bug 405239). 1.81 + rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank", 1.82 + "centerscreen,chrome,modal,titlebar", arguments, 1.83 + getter_AddRefs(dialog)); 1.84 + 1.85 + if (NS_FAILED(rv)) return rv; 1.86 + 1.87 + // get back output parameters 1.88 + int32_t tempValue; 1.89 + block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue); 1.90 + *aAccept = tempValue; 1.91 + 1.92 + // GetInt returns a int32_t; we need to sanitize it into bool 1.93 + block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue); 1.94 + *aRememberDecision = (tempValue == 1); 1.95 + 1.96 + return rv; 1.97 +} 1.98 +