Bumping gaia.json for 1 gaia revision(s) a=gaia-bump
[gecko.git] / extensions / cookie / nsCookiePromptService.cpp
blobf50c077ce8f0fff76f1cc3a8d3619b985f657418
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsCookiePromptService.h"
7 #include "nsICookie.h"
8 #include "nsICookieAcceptDialog.h"
9 #include "nsIDOMWindow.h"
10 #include "nsPIDOMWindow.h"
11 #include "nsIWindowWatcher.h"
12 #include "nsIServiceManager.h"
13 #include "nsString.h"
14 #include "nsIDialogParamBlock.h"
15 #include "nsIMutableArray.h"
16 #include "mozilla/dom/ScriptSettings.h"
18 /****************************************************************
19 ************************ nsCookiePromptService *****************
20 ****************************************************************/
22 NS_IMPL_ISUPPORTS(nsCookiePromptService, nsICookiePromptService)
24 nsCookiePromptService::nsCookiePromptService() {
27 nsCookiePromptService::~nsCookiePromptService() {
30 NS_IMETHODIMP
31 nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent,
32 nsICookie *aCookie,
33 const nsACString &aHostname,
34 int32_t aCookiesFromHost,
35 bool aChangingCookie,
36 bool *aRememberDecision,
37 int32_t *aAccept)
39 nsresult rv;
41 nsCOMPtr<nsIDialogParamBlock> block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv);
42 if (NS_FAILED(rv)) return rv;
44 block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1);
45 block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get());
46 block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost);
47 block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie ? 1 : 0);
49 nsCOMPtr<nsIMutableArray> objects =
50 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
51 if (NS_FAILED(rv)) return rv;
53 rv = objects->AppendElement(aCookie, false);
54 if (NS_FAILED(rv)) return rv;
56 block->SetObjects(objects);
58 nsCOMPtr<nsIWindowWatcher> wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
59 if (NS_FAILED(rv)) return rv;
61 nsCOMPtr<nsISupports> arguments = do_QueryInterface(block);
62 nsCOMPtr<nsIDOMWindow> dialog;
64 nsCOMPtr<nsIDOMWindow> parent(aParent);
65 if (!parent) // if no parent provided, consult the window watcher:
66 wwatcher->GetActiveWindow(getter_AddRefs(parent));
68 if (parent) {
69 nsCOMPtr<nsPIDOMWindow> privateParent(do_QueryInterface(parent));
70 if (privateParent)
71 privateParent = privateParent->GetPrivateRoot();
72 parent = do_QueryInterface(privateParent);
75 // We're opening a chrome window and passing in a nsIDialogParamBlock. Setting
76 // the nsIDialogParamBlock as the .arguments property on the chrome window
77 // requires system principals on the stack, so we use an AutoNoJSAPI for that.
78 mozilla::dom::AutoNoJSAPI nojsapi;
80 // The cookie dialog will be modal for the root chrome window rather than the
81 // tab containing the permission-requesting page. This removes confusion
82 // about which monitor is displaying the dialog (see bug 470356), but also
83 // avoids unwanted tab switches (see bug 405239).
84 rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank",
85 "centerscreen,chrome,modal,titlebar", arguments,
86 getter_AddRefs(dialog));
88 if (NS_FAILED(rv)) return rv;
90 // get back output parameters
91 int32_t tempValue;
92 block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue);
93 *aAccept = tempValue;
95 // GetInt returns a int32_t; we need to sanitize it into bool
96 block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue);
97 *aRememberDecision = (tempValue == 1);
99 return rv;