Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | #include "nsCookiePromptService.h" |
michael@0 | 7 | #include "nsICookie.h" |
michael@0 | 8 | #include "nsICookieAcceptDialog.h" |
michael@0 | 9 | #include "nsIDOMWindow.h" |
michael@0 | 10 | #include "nsPIDOMWindow.h" |
michael@0 | 11 | #include "nsIWindowWatcher.h" |
michael@0 | 12 | #include "nsIServiceManager.h" |
michael@0 | 13 | #include "nsString.h" |
michael@0 | 14 | #include "nsIDialogParamBlock.h" |
michael@0 | 15 | #include "nsIMutableArray.h" |
michael@0 | 16 | |
michael@0 | 17 | /**************************************************************** |
michael@0 | 18 | ************************ nsCookiePromptService ***************** |
michael@0 | 19 | ****************************************************************/ |
michael@0 | 20 | |
michael@0 | 21 | NS_IMPL_ISUPPORTS(nsCookiePromptService, nsICookiePromptService) |
michael@0 | 22 | |
michael@0 | 23 | nsCookiePromptService::nsCookiePromptService() { |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | nsCookiePromptService::~nsCookiePromptService() { |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | NS_IMETHODIMP |
michael@0 | 30 | nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent, |
michael@0 | 31 | nsICookie *aCookie, |
michael@0 | 32 | const nsACString &aHostname, |
michael@0 | 33 | int32_t aCookiesFromHost, |
michael@0 | 34 | bool aChangingCookie, |
michael@0 | 35 | bool *aRememberDecision, |
michael@0 | 36 | int32_t *aAccept) |
michael@0 | 37 | { |
michael@0 | 38 | nsresult rv; |
michael@0 | 39 | |
michael@0 | 40 | nsCOMPtr<nsIDialogParamBlock> block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv); |
michael@0 | 41 | if (NS_FAILED(rv)) return rv; |
michael@0 | 42 | |
michael@0 | 43 | block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1); |
michael@0 | 44 | block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get()); |
michael@0 | 45 | block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost); |
michael@0 | 46 | block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie ? 1 : 0); |
michael@0 | 47 | |
michael@0 | 48 | nsCOMPtr<nsIMutableArray> objects = |
michael@0 | 49 | do_CreateInstance(NS_ARRAY_CONTRACTID, &rv); |
michael@0 | 50 | if (NS_FAILED(rv)) return rv; |
michael@0 | 51 | |
michael@0 | 52 | rv = objects->AppendElement(aCookie, false); |
michael@0 | 53 | if (NS_FAILED(rv)) return rv; |
michael@0 | 54 | |
michael@0 | 55 | block->SetObjects(objects); |
michael@0 | 56 | |
michael@0 | 57 | nsCOMPtr<nsIWindowWatcher> wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
michael@0 | 58 | if (NS_FAILED(rv)) return rv; |
michael@0 | 59 | |
michael@0 | 60 | nsCOMPtr<nsISupports> arguments = do_QueryInterface(block); |
michael@0 | 61 | nsCOMPtr<nsIDOMWindow> dialog; |
michael@0 | 62 | |
michael@0 | 63 | nsCOMPtr<nsIDOMWindow> parent(aParent); |
michael@0 | 64 | if (!parent) // if no parent provided, consult the window watcher: |
michael@0 | 65 | wwatcher->GetActiveWindow(getter_AddRefs(parent)); |
michael@0 | 66 | |
michael@0 | 67 | if (parent) { |
michael@0 | 68 | nsCOMPtr<nsPIDOMWindow> privateParent(do_QueryInterface(parent)); |
michael@0 | 69 | if (privateParent) |
michael@0 | 70 | privateParent = privateParent->GetPrivateRoot(); |
michael@0 | 71 | parent = do_QueryInterface(privateParent); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | // The cookie dialog will be modal for the root chrome window rather than the |
michael@0 | 75 | // tab containing the permission-requesting page. This removes confusion |
michael@0 | 76 | // about which monitor is displaying the dialog (see bug 470356), but also |
michael@0 | 77 | // avoids unwanted tab switches (see bug 405239). |
michael@0 | 78 | rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank", |
michael@0 | 79 | "centerscreen,chrome,modal,titlebar", arguments, |
michael@0 | 80 | getter_AddRefs(dialog)); |
michael@0 | 81 | |
michael@0 | 82 | if (NS_FAILED(rv)) return rv; |
michael@0 | 83 | |
michael@0 | 84 | // get back output parameters |
michael@0 | 85 | int32_t tempValue; |
michael@0 | 86 | block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue); |
michael@0 | 87 | *aAccept = tempValue; |
michael@0 | 88 | |
michael@0 | 89 | // GetInt returns a int32_t; we need to sanitize it into bool |
michael@0 | 90 | block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue); |
michael@0 | 91 | *aRememberDecision = (tempValue == 1); |
michael@0 | 92 | |
michael@0 | 93 | return rv; |
michael@0 | 94 | } |
michael@0 | 95 |