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: michael@0: #include "nsCookiePromptService.h" michael@0: #include "nsICookie.h" michael@0: #include "nsICookieAcceptDialog.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIWindowWatcher.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsString.h" michael@0: #include "nsIDialogParamBlock.h" michael@0: #include "nsIMutableArray.h" michael@0: michael@0: /**************************************************************** michael@0: ************************ nsCookiePromptService ***************** michael@0: ****************************************************************/ michael@0: michael@0: NS_IMPL_ISUPPORTS(nsCookiePromptService, nsICookiePromptService) michael@0: michael@0: nsCookiePromptService::nsCookiePromptService() { michael@0: } michael@0: michael@0: nsCookiePromptService::~nsCookiePromptService() { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent, michael@0: nsICookie *aCookie, michael@0: const nsACString &aHostname, michael@0: int32_t aCookiesFromHost, michael@0: bool aChangingCookie, michael@0: bool *aRememberDecision, michael@0: int32_t *aAccept) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1); michael@0: block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get()); michael@0: block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost); michael@0: block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie ? 1 : 0); michael@0: michael@0: nsCOMPtr objects = michael@0: do_CreateInstance(NS_ARRAY_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: rv = objects->AppendElement(aCookie, false); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: block->SetObjects(objects); michael@0: michael@0: nsCOMPtr wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr arguments = do_QueryInterface(block); michael@0: nsCOMPtr dialog; michael@0: michael@0: nsCOMPtr parent(aParent); michael@0: if (!parent) // if no parent provided, consult the window watcher: michael@0: wwatcher->GetActiveWindow(getter_AddRefs(parent)); michael@0: michael@0: if (parent) { michael@0: nsCOMPtr privateParent(do_QueryInterface(parent)); michael@0: if (privateParent) michael@0: privateParent = privateParent->GetPrivateRoot(); michael@0: parent = do_QueryInterface(privateParent); michael@0: } michael@0: michael@0: // The cookie dialog will be modal for the root chrome window rather than the michael@0: // tab containing the permission-requesting page. This removes confusion michael@0: // about which monitor is displaying the dialog (see bug 470356), but also michael@0: // avoids unwanted tab switches (see bug 405239). michael@0: rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank", michael@0: "centerscreen,chrome,modal,titlebar", arguments, michael@0: getter_AddRefs(dialog)); michael@0: michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: // get back output parameters michael@0: int32_t tempValue; michael@0: block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue); michael@0: *aAccept = tempValue; michael@0: michael@0: // GetInt returns a int32_t; we need to sanitize it into bool michael@0: block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue); michael@0: *aRememberDecision = (tempValue == 1); michael@0: michael@0: return rv; michael@0: } michael@0: