Bug 452317 - FeedConverter.js: QueryInterface should throw NS_ERROR_NO_INTERFACE...
[wine-gecko.git] / embedding / components / windowwatcher / public / nsPromptUtils.h
blobb4f9fce05825b13c39ab653d6863bff0b430ee86
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 mozilla.org embedding code.
16 * The Initial Developer of the Original Code is
17 * Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Christian Biesinger <cbiesinger@web.de> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef NSPROMPTUTILS_H_
39 #define NSPROMPTUTILS_H_
41 #include "nsIHttpChannel.h"
43 /**
44 * @file
45 * This file defines some helper functions that simplify interaction
46 * with authentication prompts.
49 /**
50 * Given a username (possibly in DOMAIN\user form) and password, parses the
51 * domain out of the username if necessary and sets domain, username and
52 * password on the auth information object.
54 inline void
55 NS_SetAuthInfo(nsIAuthInformation* aAuthInfo, const nsString& user,
56 const nsString& password)
58 PRUint32 flags;
59 aAuthInfo->GetFlags(&flags);
60 if (flags & nsIAuthInformation::NEED_DOMAIN) {
61 // Domain is separated from username by a backslash
62 PRInt32 idx = user.FindChar(PRUnichar('\\'));
63 if (idx == kNotFound) {
64 aAuthInfo->SetUsername(user);
65 } else {
66 aAuthInfo->SetDomain(Substring(user, 0, idx));
67 aAuthInfo->SetUsername(Substring(user, idx + 1));
69 } else {
70 aAuthInfo->SetUsername(user);
72 aAuthInfo->SetPassword(password);
75 /**
76 * Gets the host and port from a channel and authentication info. This is the
77 * "logical" host and port for this authentication, i.e. for a proxy
78 * authentication it refers to the proxy, while for a host authentication it
79 * is the actual host.
81 * @param machineProcessing
82 * When this parameter is true, the host will be returned in ASCII
83 * (instead of UTF-8; this is relevant when IDN is used). In addition,
84 * the port will be returned as the real port even when it was not
85 * explicitly specified (when false, the port will be returned as -1 in
86 * this case)
88 inline void
89 NS_GetAuthHostPort(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo,
90 PRBool machineProcessing, nsCString& host, PRInt32* port)
92 nsCOMPtr<nsIURI> uri;
93 nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
94 if (NS_FAILED(rv))
95 return;
97 // Have to distinguish proxy auth and host auth here...
98 PRUint32 flags;
99 aAuthInfo->GetFlags(&flags);
100 if (flags & nsIAuthInformation::AUTH_PROXY) {
101 nsCOMPtr<nsIProxiedChannel> proxied(do_QueryInterface(aChannel));
102 NS_ASSERTION(proxied, "proxy auth needs nsIProxiedChannel");
104 nsCOMPtr<nsIProxyInfo> info;
105 proxied->GetProxyInfo(getter_AddRefs(info));
106 NS_ASSERTION(info, "proxy auth needs nsIProxyInfo");
108 nsCAutoString idnhost;
109 info->GetHost(idnhost);
110 info->GetPort(port);
112 if (machineProcessing) {
113 nsCOMPtr<nsIIDNService> idnService =
114 do_GetService(NS_IDNSERVICE_CONTRACTID);
115 if (idnService) {
116 idnService->ConvertUTF8toACE(idnhost, host);
117 } else {
118 // Not much we can do here...
119 host = idnhost;
121 } else {
122 host = idnhost;
124 } else {
125 if (machineProcessing) {
126 uri->GetAsciiHost(host);
127 *port = NS_GetRealPort(uri);
128 } else {
129 uri->GetHost(host);
130 uri->GetPort(port);
136 * Creates the key for looking up passwords in the password manager. This
137 * function uses the same format that Gecko functions have always used, thus
138 * ensuring backwards compatibility.
140 inline void
141 NS_GetAuthKey(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo,
142 nsCString& key)
144 // HTTP does this differently from other protocols
145 nsCOMPtr<nsIHttpChannel> http(do_QueryInterface(aChannel));
146 if (!http) {
147 nsCOMPtr<nsIURI> uri;
148 aChannel->GetURI(getter_AddRefs(uri));
149 uri->GetPrePath(key);
150 return;
153 // NOTE: For backwards-compatibility reasons, this must be the ASCII host.
154 nsCString host;
155 PRInt32 port = -1;
157 NS_GetAuthHostPort(aChannel, aAuthInfo, PR_TRUE, host, &port);
159 nsAutoString realm;
160 aAuthInfo->GetRealm(realm);
162 // Now assemble the key: host:port (realm)
163 key.Append(host);
164 key.Append(':');
165 key.AppendInt(port);
166 key.AppendLiteral(" (");
167 AppendUTF16toUTF8(realm, key);
168 key.Append(')');
171 #endif