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
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.
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"
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 }}
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)
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 : \
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
101 NS_CP_ResponseName(PRInt16 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
);
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
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
);
142 return "<Unknown Type>";
146 #endif // defined(PR_LOGGING)
150 /* Passes on parameters from its "caller"'s context. */
151 #define CHECK_CONTENT_POLICY(action) \
153 nsCOMPtr<nsIContentPolicy> policy = \
154 do_GetService(NS_CONTENTPOLICY_CONTRACTID); \
156 return NS_ERROR_FAILURE; \
158 return policy-> action (contentType, contentLocation, requestOrigin, \
159 context, mimeType, extra, decision); \
162 /* Passes on parameters from its "caller"'s context. */
163 #define CHECK_CONTENT_POLICY_WITH_SERVICE(action, _policy) \
165 return _policy-> action (contentType, contentLocation, requestOrigin, \
166 context, mimeType, extra, decision); \
170 * Check whether we can short-circuit this check and bail out. If not, get the
173 * Note: requestOrigin is scoped outside the PR_BEGIN_MACRO/PR_END_MACRO on
175 #define CHECK_PRINCIPAL \
176 nsCOMPtr<nsIURI> requestOrigin; \
178 if (originPrincipal) { \
179 nsCOMPtr<nsIScriptSecurityManager> secMan = aSecMan; \
181 secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID); \
185 nsresult rv = secMan->IsSystemPrincipal(originPrincipal, \
187 NS_ENSURE_SUCCESS(rv, rv); \
189 *decision = nsIContentPolicy::ACCEPT; \
193 nsresult rv = originPrincipal->GetURI(getter_AddRefs(requestOrigin)); \
194 NS_ENSURE_SUCCESS(rv, rv); \
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).
208 NS_CheckContentLoadPolicy(PRUint32 contentType
,
209 nsIURI
*contentLocation
,
210 nsIPrincipal
*originPrincipal
,
211 nsISupports
*context
,
212 const nsACString
&mimeType
,
215 nsIContentPolicy
*policyService
= nsnull
,
216 nsIScriptSecurityManager
* aSecMan
= nsnull
)
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).
235 NS_CheckContentProcessPolicy(PRUint32 contentType
,
236 nsIURI
*contentLocation
,
237 nsIPrincipal
*originPrincipal
,
238 nsISupports
*context
,
239 const nsACString
&mimeType
,
242 nsIContentPolicy
*policyService
= nsnull
,
243 nsIScriptSecurityManager
* aSecMan
= nsnull
)
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
259 * If the context is a non-document DOM node, the docshell of its ownerDocument
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
267 NS_CP_GetDocShellFromContext(nsISupports
*aContext
)
273 nsCOMPtr
<nsPIDOMWindow
> window
= do_QueryInterface(aContext
);
276 // our context might be a document (which also QIs to nsIDOMNode), so
278 nsCOMPtr
<nsIDocument
> doc
= do_QueryInterface(aContext
);
280 // we were not a document after all, get our ownerDocument,
282 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aContext
);
284 doc
= content
->GetOwnerDoc();
289 window
= doc
->GetWindow();
297 return window
->GetDocShell();
300 #endif /* __nsContentPolicyUtils_h__ */