|
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/. */ |
|
4 |
|
5 |
|
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 |
|
17 /**************************************************************** |
|
18 ************************ nsCookiePromptService ***************** |
|
19 ****************************************************************/ |
|
20 |
|
21 NS_IMPL_ISUPPORTS(nsCookiePromptService, nsICookiePromptService) |
|
22 |
|
23 nsCookiePromptService::nsCookiePromptService() { |
|
24 } |
|
25 |
|
26 nsCookiePromptService::~nsCookiePromptService() { |
|
27 } |
|
28 |
|
29 NS_IMETHODIMP |
|
30 nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent, |
|
31 nsICookie *aCookie, |
|
32 const nsACString &aHostname, |
|
33 int32_t aCookiesFromHost, |
|
34 bool aChangingCookie, |
|
35 bool *aRememberDecision, |
|
36 int32_t *aAccept) |
|
37 { |
|
38 nsresult rv; |
|
39 |
|
40 nsCOMPtr<nsIDialogParamBlock> block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv); |
|
41 if (NS_FAILED(rv)) return rv; |
|
42 |
|
43 block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1); |
|
44 block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get()); |
|
45 block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost); |
|
46 block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie ? 1 : 0); |
|
47 |
|
48 nsCOMPtr<nsIMutableArray> objects = |
|
49 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv); |
|
50 if (NS_FAILED(rv)) return rv; |
|
51 |
|
52 rv = objects->AppendElement(aCookie, false); |
|
53 if (NS_FAILED(rv)) return rv; |
|
54 |
|
55 block->SetObjects(objects); |
|
56 |
|
57 nsCOMPtr<nsIWindowWatcher> wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
|
58 if (NS_FAILED(rv)) return rv; |
|
59 |
|
60 nsCOMPtr<nsISupports> arguments = do_QueryInterface(block); |
|
61 nsCOMPtr<nsIDOMWindow> dialog; |
|
62 |
|
63 nsCOMPtr<nsIDOMWindow> parent(aParent); |
|
64 if (!parent) // if no parent provided, consult the window watcher: |
|
65 wwatcher->GetActiveWindow(getter_AddRefs(parent)); |
|
66 |
|
67 if (parent) { |
|
68 nsCOMPtr<nsPIDOMWindow> privateParent(do_QueryInterface(parent)); |
|
69 if (privateParent) |
|
70 privateParent = privateParent->GetPrivateRoot(); |
|
71 parent = do_QueryInterface(privateParent); |
|
72 } |
|
73 |
|
74 // The cookie dialog will be modal for the root chrome window rather than the |
|
75 // tab containing the permission-requesting page. This removes confusion |
|
76 // about which monitor is displaying the dialog (see bug 470356), but also |
|
77 // avoids unwanted tab switches (see bug 405239). |
|
78 rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank", |
|
79 "centerscreen,chrome,modal,titlebar", arguments, |
|
80 getter_AddRefs(dialog)); |
|
81 |
|
82 if (NS_FAILED(rv)) return rv; |
|
83 |
|
84 // get back output parameters |
|
85 int32_t tempValue; |
|
86 block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue); |
|
87 *aAccept = tempValue; |
|
88 |
|
89 // GetInt returns a int32_t; we need to sanitize it into bool |
|
90 block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue); |
|
91 *aRememberDecision = (tempValue == 1); |
|
92 |
|
93 return rv; |
|
94 } |
|
95 |