Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / content / base / public / nsContentPolicyUtils.h
blob8a9515f6b8f5f1514e7dfcdb15409d4783ef35b6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 code.
17 * The Initial Developer of the Original Code is
18 * Zero-Knowledge Systems, Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Timothy Watt <riceman+moz@mail.rit.edu>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or 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 ***** */
40 * Utility routines for checking content load/process policy settings,
41 * and routines helpful for content policy implementors.
43 * XXXbz it would be nice if some of this stuff could be out-of-lined in
44 * nsContentUtils. That would work for almost all the callers...
47 #ifndef __nsContentPolicyUtils_h__
48 #define __nsContentPolicyUtils_h__
50 #include "nsIContentPolicy.h"
51 #include "nsIServiceManager.h"
52 #include "nsIContent.h"
53 #include "nsIScriptSecurityManager.h"
54 #include "nsIPrincipal.h"
56 //XXXtw sadly, this makes consumers of nsContentPolicyUtils depend on widget
57 #include "nsIDocument.h"
58 #include "nsPIDOMWindow.h"
60 class nsACString;
62 #define NS_CONTENTPOLICY_CONTRACTID "@mozilla.org/layout/content-policy;1"
63 #define NS_CONTENTPOLICY_CATEGORY "content-policy"
64 #define NS_CONTENTPOLICY_CID \
65 {0x0e3afd3d, 0xeb60, 0x4c2b, \
66 { 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
68 /**
69 * Evaluates to true if val is ACCEPT.
71 * @param val the status returned from shouldProcess/shouldLoad
73 #define NS_CP_ACCEPTED(val) ((val) == nsIContentPolicy::ACCEPT)
75 /**
76 * Evaluates to true if val is a REJECT_* status
78 * @param val the status returned from shouldProcess/shouldLoad
80 #define NS_CP_REJECTED(val) ((val) != nsIContentPolicy::ACCEPT)
82 // Offer convenient translations of constants -> const char*
84 // convenience macro to reduce some repetative typing...
85 // name is the name of a constant from this interface
86 #define CASE_RETURN(name) \
87 case nsIContentPolicy:: name : \
88 return #name
90 #ifdef PR_LOGGING
91 /**
92 * Returns a string corresponding to the name of the response constant, or
93 * "<Unknown Response>" if an unknown response value is given.
95 * The return value is static and must not be freed.
97 * @param response the response code
98 * @return the name of the given response code
100 inline const char *
101 NS_CP_ResponseName(PRInt16 response)
103 switch (response) {
104 CASE_RETURN( REJECT_REQUEST );
105 CASE_RETURN( REJECT_TYPE );
106 CASE_RETURN( REJECT_SERVER );
107 CASE_RETURN( REJECT_OTHER );
108 CASE_RETURN( ACCEPT );
109 default:
110 return "<Unknown Response>";
115 * Returns a string corresponding to the name of the content type constant, or
116 * "<Unknown Type>" if an unknown content type value is given.
118 * The return value is static and must not be freed.
120 * @param contentType the content type code
121 * @return the name of the given content type code
123 inline const char *
124 NS_CP_ContentTypeName(PRUint32 contentType)
126 switch (contentType) {
127 CASE_RETURN( TYPE_OTHER );
128 CASE_RETURN( TYPE_SCRIPT );
129 CASE_RETURN( TYPE_IMAGE );
130 CASE_RETURN( TYPE_STYLESHEET );
131 CASE_RETURN( TYPE_OBJECT );
132 CASE_RETURN( TYPE_DOCUMENT );
133 CASE_RETURN( TYPE_SUBDOCUMENT );
134 CASE_RETURN( TYPE_REFRESH );
135 CASE_RETURN( TYPE_XBL );
136 CASE_RETURN( TYPE_PING );
137 CASE_RETURN( TYPE_XMLHTTPREQUEST );
138 CASE_RETURN( TYPE_OBJECT_SUBREQUEST );
139 CASE_RETURN( TYPE_DTD );
140 CASE_RETURN( TYPE_FONT );
141 default:
142 return "<Unknown Type>";
146 #endif // defined(PR_LOGGING)
148 #undef CASE_RETURN
150 /* Passes on parameters from its "caller"'s context. */
151 #define CHECK_CONTENT_POLICY(action) \
152 PR_BEGIN_MACRO \
153 nsCOMPtr<nsIContentPolicy> policy = \
154 do_GetService(NS_CONTENTPOLICY_CONTRACTID); \
155 if (!policy) \
156 return NS_ERROR_FAILURE; \
158 return policy-> action (contentType, contentLocation, requestOrigin, \
159 context, mimeType, extra, decision); \
160 PR_END_MACRO
162 /* Passes on parameters from its "caller"'s context. */
163 #define CHECK_CONTENT_POLICY_WITH_SERVICE(action, _policy) \
164 PR_BEGIN_MACRO \
165 return _policy-> action (contentType, contentLocation, requestOrigin, \
166 context, mimeType, extra, decision); \
167 PR_END_MACRO
170 * Check whether we can short-circuit this check and bail out. If not, get the
171 * origin URI to use.
173 * Note: requestOrigin is scoped outside the PR_BEGIN_MACRO/PR_END_MACRO on
174 * purpose */
175 #define CHECK_PRINCIPAL \
176 nsCOMPtr<nsIURI> requestOrigin; \
177 PR_BEGIN_MACRO \
178 if (originPrincipal) { \
179 nsCOMPtr<nsIScriptSecurityManager> secMan = aSecMan; \
180 if (!secMan) { \
181 secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID); \
183 if (secMan) { \
184 PRBool isSystem; \
185 nsresult rv = secMan->IsSystemPrincipal(originPrincipal, \
186 &isSystem); \
187 NS_ENSURE_SUCCESS(rv, rv); \
188 if (isSystem) { \
189 *decision = nsIContentPolicy::ACCEPT; \
190 return NS_OK; \
193 nsresult rv = originPrincipal->GetURI(getter_AddRefs(requestOrigin)); \
194 NS_ENSURE_SUCCESS(rv, rv); \
196 PR_END_MACRO
199 * Alias for calling ShouldLoad on the content policy service. Parameters are
200 * the same as nsIContentPolicy::shouldLoad, except for the originPrincipal
201 * parameter, which should be non-null if possible, and the last two
202 * parameters, which can be used to pass in pointer to some useful services if
203 * the caller already has them. The origin URI to pass to shouldLoad will be
204 * the URI of originPrincipal, unless originPrincipal is null (in which case a
205 * null origin URI will be passed).
207 inline nsresult
208 NS_CheckContentLoadPolicy(PRUint32 contentType,
209 nsIURI *contentLocation,
210 nsIPrincipal *originPrincipal,
211 nsISupports *context,
212 const nsACString &mimeType,
213 nsISupports *extra,
214 PRInt16 *decision,
215 nsIContentPolicy *policyService = nsnull,
216 nsIScriptSecurityManager* aSecMan = nsnull)
218 CHECK_PRINCIPAL;
219 if (policyService) {
220 CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldLoad, policyService);
222 CHECK_CONTENT_POLICY(ShouldLoad);
226 * Alias for calling ShouldProcess on the content policy service. Parameters
227 * are the same as nsIContentPolicy::shouldLoad, except for the originPrincipal
228 * parameter, which should be non-null if possible, and the last two
229 * parameters, which can be used to pass in pointer to some useful services if
230 * the caller already has them. The origin URI to pass to shouldLoad will be
231 * the URI of originPrincipal, unless originPrincipal is null (in which case a
232 * null origin URI will be passed).
234 inline nsresult
235 NS_CheckContentProcessPolicy(PRUint32 contentType,
236 nsIURI *contentLocation,
237 nsIPrincipal *originPrincipal,
238 nsISupports *context,
239 const nsACString &mimeType,
240 nsISupports *extra,
241 PRInt16 *decision,
242 nsIContentPolicy *policyService = nsnull,
243 nsIScriptSecurityManager* aSecMan = nsnull)
245 CHECK_PRINCIPAL;
246 if (policyService) {
247 CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldProcess, policyService);
249 CHECK_CONTENT_POLICY(ShouldProcess);
252 #undef CHECK_CONTENT_POLICY
253 #undef CHECK_CONTENT_POLICY_WITH_SERVICE
256 * Helper function to get an nsIDocShell given a context.
257 * If the context is a document or window, the corresponding docshell will be
258 * returned.
259 * If the context is a non-document DOM node, the docshell of its ownerDocument
260 * will be returned.
262 * @param aContext the context to find a docshell for (can be null)
263 * @return a WEAK pointer to the docshell, or nsnull if it could
264 * not be obtained
266 inline nsIDocShell*
267 NS_CP_GetDocShellFromContext(nsISupports *aContext)
269 if (!aContext) {
270 return nsnull;
273 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
275 if (!window) {
276 // our context might be a document (which also QIs to nsIDOMNode), so
277 // try that first
278 nsCOMPtr<nsIDocument> doc = do_QueryInterface(aContext);
279 if (!doc) {
280 // we were not a document after all, get our ownerDocument,
281 // hopefully
282 nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
283 if (content) {
284 doc = content->GetOwnerDoc();
288 if (doc) {
289 window = doc->GetWindow();
293 if (!window) {
294 return nsnull;
297 return window->GetDocShell();
300 #endif /* __nsContentPolicyUtils_h__ */