Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / base / src / nsJVMAuthTools.cpp
blobea819ca0b5f603e3a6d50a3fea792cd85d836d29
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Sun Microsystem.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Joshua Xia <joshua.xia@sun.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "plstr.h"
40 #include "nsNetUtil.h"
41 #include "nsJVMAuthTools.h"
42 #include "nsIHttpAuthManager.h"
44 static NS_DEFINE_CID(kHttpAuthManagerCID, NS_HTTPAUTHMANAGER_CID);
46 //---------------------------------------------------
47 // implementation of interface nsIAuthenticationInfo
48 // --------------------------------------------------
49 NS_IMPL_ISUPPORTS1(nsAuthenticationInfoImp, nsIAuthenticationInfo)
51 nsAuthenticationInfoImp::nsAuthenticationInfoImp(char* username,
52 char* password)
54 mUserName = username;
55 mPassWord = password;
58 nsAuthenticationInfoImp::~nsAuthenticationInfoImp()
60 if (mUserName)
61 nsMemory::Free(mUserName);
63 if (mPassWord)
64 nsMemory::Free(mPassWord);
67 /* readonly attribute string username; */
68 NS_IMETHODIMP nsAuthenticationInfoImp::GetUsername(const char * *aUsername)
70 *aUsername = mUserName;
71 return NS_OK;
74 /* readonly attribute string password; */
75 NS_IMETHODIMP nsAuthenticationInfoImp::GetPassword(const char * *aPassword)
77 *aPassword = mPassWord;
78 return NS_OK;
82 //---------------------------------------------------
83 // implementation of interface nsIJVMAuthTools
84 // --------------------------------------------------
85 NS_IMPL_AGGREGATED(nsJVMAuthTools)
87 nsJVMAuthTools::nsJVMAuthTools(nsISupports* outer)
89 NS_INIT_AGGREGATED(outer);
92 nsJVMAuthTools::~nsJVMAuthTools(void)
96 NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsJVMAuthTools)
97 NS_INTERFACE_MAP_ENTRY(nsIJVMAuthTools)
98 NS_INTERFACE_MAP_END
100 NS_METHOD
101 nsJVMAuthTools::GetAuthenticationInfo(const char* protocol,
102 const char* host,
103 PRInt32 port,
104 const char* scheme,
105 const char* realm,
106 nsIAuthenticationInfo **_retval)
108 if (!protocol || !host || !scheme || !realm)
109 return NS_ERROR_INVALID_ARG;
111 if (!PL_strcasecmp(protocol, "HTTP") && !PL_strcasecmp(protocol, "HTTPS"))
112 return NS_ERROR_INVALID_ARG;
114 nsCOMPtr<nsIHttpAuthManager> authManager = do_GetService(kHttpAuthManagerCID);
115 if (!authManager)
116 return NS_ERROR_FAILURE;
118 nsDependentCString protocolString(protocol);
119 nsDependentCString hostString(host);
120 nsDependentCString schemeString(scheme);
121 nsDependentCString realmString(realm);
122 nsAutoString domainString, username, password;
124 nsresult rv = authManager->GetAuthIdentity(protocolString,
125 hostString,
126 port,
127 schemeString,
128 realmString,
129 EmptyCString(),
130 domainString,
131 username,
132 password);
133 if (NS_FAILED(rv))
134 return NS_ERROR_FAILURE;
136 nsAuthenticationInfoImp* authInfo = new nsAuthenticationInfoImp(
137 ToNewUTF8String(username),
138 ToNewUTF8String(password));
139 NS_ENSURE_TRUE(authInfo, NS_ERROR_OUT_OF_MEMORY);
140 NS_ADDREF(authInfo);
141 *_retval = authInfo;
143 return NS_OK;
146 NS_METHOD
147 nsJVMAuthTools::SetAuthenticationInfo(const char* protocol,
148 const char* host,
149 PRInt32 port,
150 const char* scheme,
151 const char* realm,
152 const char *username,
153 const char *password)
155 if (!protocol || !host || !scheme || !realm)
156 return NS_ERROR_INVALID_ARG;
158 if (!PL_strcasecmp(protocol, "HTTP") && !PL_strcasecmp(protocol, "HTTPS"))
159 return NS_ERROR_INVALID_ARG;
161 nsCOMPtr<nsIHttpAuthManager> authManager = do_GetService(kHttpAuthManagerCID);
162 if (!authManager)
163 return NS_ERROR_FAILURE;
165 nsDependentCString protocolString(protocol);
166 nsDependentCString hostString(host);
167 nsDependentCString schemeString(scheme);
168 nsDependentCString realmString(realm);
170 nsresult rv = authManager->SetAuthIdentity(protocolString,
171 hostString,
172 port,
173 schemeString,
174 realmString,
175 EmptyCString(),
176 EmptyString(),
177 NS_ConvertUTF8toUTF16(username),
178 NS_ConvertUTF8toUTF16(password));
179 return rv;