Bug 1700051: part 35) Reduce accessibility of `mSoftText.mDOMMapping` to `private...
[gecko.git] / dom / base / nsContentPolicyUtils.h
blobbc223dca9bad4f57e322fce34580ff29bd256754
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 * Utility routines for checking content load/process policy settings,
8 * and routines helpful for content policy implementors.
10 * XXXbz it would be nice if some of this stuff could be out-of-lined in
11 * nsContentUtils. That would work for almost all the callers...
14 #ifndef __nsContentPolicyUtils_h__
15 #define __nsContentPolicyUtils_h__
17 #include "mozilla/BasePrincipal.h"
19 #include "nsContentUtils.h"
20 #include "nsIContentPolicy.h"
21 #include "nsIContent.h"
22 #include "nsIURI.h"
23 #include "nsServiceManagerUtils.h"
24 #include "nsStringFwd.h"
25 #include "mozilla/dom/nsCSPService.h"
27 // XXXtw sadly, this makes consumers of nsContentPolicyUtils depend on widget
28 #include "mozilla/dom/Document.h"
29 #include "nsPIDOMWindow.h"
31 #define NS_CONTENTPOLICY_CONTRACTID "@mozilla.org/layout/content-policy;1"
32 #define NS_CONTENTPOLICY_CATEGORY "content-policy"
33 #define NS_CONTENTPOLICY_CID \
34 { \
35 0x0e3afd3d, 0xeb60, 0x4c2b, { \
36 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 \
37 } \
40 /**
41 * Evaluates to true if val is ACCEPT.
43 * @param val the status returned from shouldProcess/shouldLoad
45 #define NS_CP_ACCEPTED(val) ((val) == nsIContentPolicy::ACCEPT)
47 /**
48 * Evaluates to true if val is a REJECT_* status
50 * @param val the status returned from shouldProcess/shouldLoad
52 #define NS_CP_REJECTED(val) ((val) != nsIContentPolicy::ACCEPT)
54 // Offer convenient translations of constants -> const char*
56 // convenience macro to reduce some repetative typing...
57 // name is the name of a constant from this interface
58 #define CASE_RETURN(name) \
59 case nsIContentPolicy::name: \
60 return #name
62 /**
63 * Returns a string corresponding to the name of the response constant, or
64 * "<Unknown Response>" if an unknown response value is given.
66 * The return value is static and must not be freed.
68 * @param response the response code
69 * @return the name of the given response code
71 inline const char* NS_CP_ResponseName(int16_t response) {
72 switch (response) {
73 CASE_RETURN(REJECT_REQUEST);
74 CASE_RETURN(REJECT_TYPE);
75 CASE_RETURN(REJECT_SERVER);
76 CASE_RETURN(REJECT_OTHER);
77 CASE_RETURN(ACCEPT);
78 default:
79 return "<Unknown Response>";
83 /**
84 * Returns a string corresponding to the name of the content type constant, or
85 * "<Unknown Type>" if an unknown content type value is given.
87 * The return value is static and must not be freed.
89 * @param contentType the content type code
90 * @return the name of the given content type code
92 inline const char* NS_CP_ContentTypeName(nsContentPolicyType contentType) {
93 switch (contentType) {
94 CASE_RETURN(TYPE_OTHER);
95 CASE_RETURN(TYPE_SCRIPT);
96 CASE_RETURN(TYPE_IMAGE);
97 CASE_RETURN(TYPE_STYLESHEET);
98 CASE_RETURN(TYPE_OBJECT);
99 CASE_RETURN(TYPE_DOCUMENT);
100 CASE_RETURN(TYPE_SUBDOCUMENT);
101 CASE_RETURN(TYPE_PING);
102 CASE_RETURN(TYPE_XMLHTTPREQUEST);
103 CASE_RETURN(TYPE_OBJECT_SUBREQUEST);
104 CASE_RETURN(TYPE_DTD);
105 CASE_RETURN(TYPE_FONT);
106 CASE_RETURN(TYPE_MEDIA);
107 CASE_RETURN(TYPE_WEBSOCKET);
108 CASE_RETURN(TYPE_CSP_REPORT);
109 CASE_RETURN(TYPE_XSLT);
110 CASE_RETURN(TYPE_BEACON);
111 CASE_RETURN(TYPE_FETCH);
112 CASE_RETURN(TYPE_IMAGESET);
113 CASE_RETURN(TYPE_WEB_MANIFEST);
114 CASE_RETURN(TYPE_INTERNAL_SCRIPT);
115 CASE_RETURN(TYPE_INTERNAL_WORKER);
116 CASE_RETURN(TYPE_INTERNAL_SHARED_WORKER);
117 CASE_RETURN(TYPE_INTERNAL_EMBED);
118 CASE_RETURN(TYPE_INTERNAL_OBJECT);
119 CASE_RETURN(TYPE_INTERNAL_FRAME);
120 CASE_RETURN(TYPE_INTERNAL_IFRAME);
121 CASE_RETURN(TYPE_INTERNAL_AUDIO);
122 CASE_RETURN(TYPE_INTERNAL_VIDEO);
123 CASE_RETURN(TYPE_INTERNAL_TRACK);
124 CASE_RETURN(TYPE_INTERNAL_XMLHTTPREQUEST);
125 CASE_RETURN(TYPE_INTERNAL_EVENTSOURCE);
126 CASE_RETURN(TYPE_INTERNAL_SERVICE_WORKER);
127 CASE_RETURN(TYPE_INTERNAL_SCRIPT_PRELOAD);
128 CASE_RETURN(TYPE_INTERNAL_IMAGE);
129 CASE_RETURN(TYPE_INTERNAL_IMAGE_PRELOAD);
130 CASE_RETURN(TYPE_INTERNAL_IMAGE_FAVICON);
131 CASE_RETURN(TYPE_INTERNAL_STYLESHEET);
132 CASE_RETURN(TYPE_INTERNAL_STYLESHEET_PRELOAD);
133 CASE_RETURN(TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS);
134 CASE_RETURN(TYPE_SAVEAS_DOWNLOAD);
135 CASE_RETURN(TYPE_SPECULATIVE);
136 CASE_RETURN(TYPE_INTERNAL_MODULE);
137 CASE_RETURN(TYPE_INTERNAL_MODULE_PRELOAD);
138 CASE_RETURN(TYPE_INTERNAL_DTD);
139 CASE_RETURN(TYPE_INTERNAL_FORCE_ALLOWED_DTD);
140 CASE_RETURN(TYPE_INTERNAL_AUDIOWORKLET);
141 CASE_RETURN(TYPE_INTERNAL_PAINTWORKLET);
142 CASE_RETURN(TYPE_INTERNAL_FONT_PRELOAD);
143 CASE_RETURN(TYPE_INTERNAL_CHROMEUTILS_COMPILED_SCRIPT);
144 CASE_RETURN(TYPE_INTERNAL_FRAME_MESSAGEMANAGER_SCRIPT);
145 CASE_RETURN(TYPE_INTERNAL_FETCH_PRELOAD);
146 case nsIContentPolicy::TYPE_INVALID:
147 break;
148 // Do not add default: so that compilers can catch the missing case.
150 return "<Unknown Type>";
153 #undef CASE_RETURN
155 inline const char* NS_CP_ContentTypeName(ExtContentPolicyType contentType) {
156 return NS_CP_ContentTypeName(static_cast<nsContentPolicyType>(contentType));
159 /* Passes on parameters from its "caller"'s context. */
160 #define CHECK_CONTENT_POLICY(action) \
161 PR_BEGIN_MACRO \
162 nsCOMPtr<nsIContentPolicy> policy = \
163 do_GetService(NS_CONTENTPOLICY_CONTRACTID); \
164 if (!policy) return NS_ERROR_FAILURE; \
166 return policy->action(contentLocation, loadInfo, mimeType, decision); \
167 PR_END_MACRO
169 /* Passes on parameters from its "caller"'s context. */
170 #define CHECK_CONTENT_POLICY_WITH_SERVICE(action, _policy) \
171 PR_BEGIN_MACRO \
172 return _policy->action(contentLocation, loadInfo, mimeType, decision); \
173 PR_END_MACRO
176 * Check whether we can short-circuit this check and bail out. If not, get the
177 * origin URI to use.
179 * Note: requestOrigin is scoped outside the PR_BEGIN_MACRO/PR_END_MACRO on
180 * purpose */
181 #define CHECK_PRINCIPAL_CSP_AND_DATA(action) \
182 PR_BEGIN_MACRO \
183 if (loadingPrincipal && loadingPrincipal->IsSystemPrincipal()) { \
184 /* We exempt most loads into any document with the system principal \
185 * from content policy (except CSP) checks, mostly as an optimization. \
186 * Which means that we need to apply this check to the loading principal, \
187 * not the principal that triggered the load. */ \
188 /* Check CSP for System Privileged pages */ \
189 CSPService::ConsultCSP(contentLocation, loadInfo, mimeType, decision); \
190 if (NS_CP_REJECTED(*decision)) { \
191 return NS_OK; \
193 if (contentType != nsIContentPolicy::TYPE_DOCUMENT) { \
194 *decision = nsIContentPolicy::ACCEPT; \
195 nsCOMPtr<nsINode> n = do_QueryInterface(context); \
196 if (!n) { \
197 nsCOMPtr<nsPIDOMWindowOuter> win = do_QueryInterface(context); \
198 n = win ? win->GetExtantDoc() : nullptr; \
200 if (n) { \
201 mozilla::dom::Document* d = n->OwnerDoc(); \
202 if (d->IsLoadedAsData() || d->IsBeingUsedAsImage() || \
203 d->IsResourceDoc()) { \
204 nsCOMPtr<nsIContentPolicy> dataPolicy = \
205 do_GetService("@mozilla.org/data-document-content-policy;1"); \
206 if (dataPolicy) { \
207 dataPolicy->action(contentLocation, loadInfo, mimeType, decision); \
212 return NS_OK; \
214 PR_END_MACRO
217 * Alias for calling ShouldLoad on the content policy service. Parameters are
218 * the same as nsIContentPolicy::shouldLoad, except for the loadingPrincipal
219 * and triggeringPrincipal parameters (which should be non-null if possible,
220 * and have the same semantics as in nsLoadInfo), and the last parameter,
221 * which can be used to pass in a pointer to a useful service if the caller
222 * already has it. The origin URI to pass to shouldLoad will be the URI of
223 * loadingPrincipal, unless loadingPrincipal is null (in which case a null
224 * origin URI will be passed).
226 inline nsresult NS_CheckContentLoadPolicy(
227 nsIURI* contentLocation, nsILoadInfo* loadInfo, const nsACString& mimeType,
228 int16_t* decision, nsIContentPolicy* policyService = nullptr) {
229 nsIPrincipal* loadingPrincipal = loadInfo->GetLoadingPrincipal();
230 nsCOMPtr<nsISupports> context = loadInfo->GetLoadingContext();
231 nsContentPolicyType contentType = loadInfo->InternalContentPolicyType();
232 CHECK_PRINCIPAL_CSP_AND_DATA(ShouldLoad);
233 if (policyService) {
234 CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldLoad, policyService);
236 CHECK_CONTENT_POLICY(ShouldLoad);
240 * Alias for calling ShouldProcess on the content policy service.
242 inline nsresult NS_CheckContentProcessPolicy(
243 nsIURI* contentLocation, nsILoadInfo* loadInfo, const nsACString& mimeType,
244 int16_t* decision, nsIContentPolicy* policyService = nullptr) {
245 nsIPrincipal* loadingPrincipal = loadInfo->GetLoadingPrincipal();
246 nsCOMPtr<nsISupports> context = loadInfo->GetLoadingContext();
247 nsContentPolicyType contentType = loadInfo->InternalContentPolicyType();
248 CHECK_PRINCIPAL_CSP_AND_DATA(ShouldProcess);
249 if (policyService) {
250 CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldProcess, policyService);
252 CHECK_CONTENT_POLICY(ShouldProcess);
255 #undef CHECK_CONTENT_POLICY
256 #undef CHECK_CONTENT_POLICY_WITH_SERVICE
259 * Helper function to get an nsIDocShell given a context.
260 * If the context is a document or window, the corresponding docshell will be
261 * returned.
262 * If the context is a non-document DOM node, the docshell of its ownerDocument
263 * will be returned.
265 * @param aContext the context to find a docshell for (can be null)
267 * @return a WEAK pointer to the docshell, or nullptr if it could
268 * not be obtained
270 * @note As of this writing, calls to nsIContentPolicy::Should{Load,Process}
271 * for TYPE_DOCUMENT and TYPE_SUBDOCUMENT pass in an aContext that either
272 * points to the frameElement of the window the load is happening in
273 * (in which case NS_CP_GetDocShellFromContext will return the parent of the
274 * docshell the load is happening in), or points to the window the load is
275 * happening in (in which case NS_CP_GetDocShellFromContext will return
276 * the docshell the load is happening in). It's up to callers to QI aContext
277 * and handle things accordingly if they want the docshell the load is
278 * happening in. These are somewhat odd semantics, and bug 466687 has been
279 * filed to consider improving them.
281 inline nsIDocShell* NS_CP_GetDocShellFromContext(nsISupports* aContext) {
282 if (!aContext) {
283 return nullptr;
286 nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aContext);
288 if (!window) {
289 // Our context might be a document.
290 nsCOMPtr<mozilla::dom::Document> doc = do_QueryInterface(aContext);
291 if (!doc) {
292 // we were not a document after all, get our ownerDocument,
293 // hopefully
294 nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
295 if (content) {
296 doc = content->OwnerDoc();
300 if (doc) {
301 if (doc->GetDisplayDocument()) {
302 doc = doc->GetDisplayDocument();
305 window = doc->GetWindow();
309 if (!window) {
310 return nullptr;
313 return window->GetDocShell();
316 #endif /* __nsContentPolicyUtils_h__ */