Bug 572417 - Release mouse capture in flash subclass after mouse events get delivered...
[mozilla-central.git] / extensions / cookie / nsCookiePromptService.cpp
blob56a7edc03865471d719690be01fc025e17438471
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is cookie manager code.
16 * The Initial Developer of the Original Code is
17 * Michiel van Leeuwen (mvl@exedo.nl).
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 #include "nsCookiePromptService.h"
39 #include "nsICookie.h"
40 #include "nsICookieAcceptDialog.h"
41 #include "nsIDOMWindow.h"
42 #include "nsPIDOMWindow.h"
43 #include "nsIWindowWatcher.h"
44 #include "nsIServiceManager.h"
45 #include "nsString.h"
46 #include "nsIDialogParamBlock.h"
47 #include "nsIMutableArray.h"
49 /****************************************************************
50 ************************ nsCookiePromptService *****************
51 ****************************************************************/
53 NS_IMPL_ISUPPORTS1(nsCookiePromptService, nsICookiePromptService)
55 nsCookiePromptService::nsCookiePromptService() {
58 nsCookiePromptService::~nsCookiePromptService() {
61 NS_IMETHODIMP
62 nsCookiePromptService::CookieDialog(nsIDOMWindow *aParent,
63 nsICookie *aCookie,
64 const nsACString &aHostname,
65 PRInt32 aCookiesFromHost,
66 PRBool aChangingCookie,
67 PRBool *aRememberDecision,
68 PRInt32 *aAccept)
70 nsresult rv;
72 nsCOMPtr<nsIDialogParamBlock> block = do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID,&rv);
73 if (NS_FAILED(rv)) return rv;
75 // since we're setting PRInt32's here, we have to sanitize the PRBool's first.
76 // (myBool != PR_FALSE) is guaranteed to return either 1 or 0.
77 block->SetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, 1);
78 block->SetString(nsICookieAcceptDialog::HOSTNAME, NS_ConvertUTF8toUTF16(aHostname).get());
79 block->SetInt(nsICookieAcceptDialog::COOKIESFROMHOST, aCookiesFromHost);
80 block->SetInt(nsICookieAcceptDialog::CHANGINGCOOKIE, aChangingCookie != PR_FALSE);
82 nsCOMPtr<nsIMutableArray> objects =
83 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
84 if (NS_FAILED(rv)) return rv;
86 rv = objects->AppendElement(aCookie, PR_FALSE);
87 if (NS_FAILED(rv)) return rv;
89 block->SetObjects(objects);
91 nsCOMPtr<nsIWindowWatcher> wwatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
92 if (NS_FAILED(rv)) return rv;
94 nsCOMPtr<nsISupports> arguments = do_QueryInterface(block);
95 nsCOMPtr<nsIDOMWindow> dialog;
97 nsCOMPtr<nsIDOMWindow> parent(aParent);
98 if (!parent) // if no parent provided, consult the window watcher:
99 wwatcher->GetActiveWindow(getter_AddRefs(parent));
101 if (parent) {
102 nsCOMPtr<nsPIDOMWindow> privateParent(do_QueryInterface(parent));
103 if (privateParent)
104 privateParent = privateParent->GetPrivateRoot();
105 parent = do_QueryInterface(privateParent);
108 // The cookie dialog will be modal for the root chrome window rather than the
109 // tab containing the permission-requesting page. This removes confusion
110 // about which monitor is displaying the dialog (see bug 470356), but also
111 // avoids unwanted tab switches (see bug 405239).
112 rv = wwatcher->OpenWindow(parent, "chrome://cookie/content/cookieAcceptDialog.xul", "_blank",
113 "centerscreen,chrome,modal,titlebar", arguments,
114 getter_AddRefs(dialog));
116 if (NS_FAILED(rv)) return rv;
118 // get back output parameters
119 PRBool tempValue;
120 block->GetInt(nsICookieAcceptDialog::ACCEPT_COOKIE, &tempValue);
121 *aAccept = tempValue;
123 // GetInt returns a PRInt32; we need to sanitize it into PRBool
124 block->GetInt(nsICookieAcceptDialog::REMEMBER_DECISION, &tempValue);
125 *aRememberDecision = (tempValue == 1);
127 return rv;