1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All
7  * rights reserved.
8  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9  * (http://www.torchmobile.com/)
10  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
11  * Copyright (C) 2013 Google Inc. All rights reserved.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_DOCUMENT_INIT_H_
31 #define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_DOCUMENT_INIT_H_
32 
33 #include "services/network/public/mojom/ip_address_space.mojom-shared.h"
34 #include "third_party/blink/public/common/frame/frame_policy.h"
35 #include "third_party/blink/public/mojom/feature_policy/feature_policy.mojom-blink.h"
36 #include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-blink-forward.h"
37 #include "third_party/blink/renderer/core/core_export.h"
38 #include "third_party/blink/renderer/core/execution_context/security_context.h"
39 #include "third_party/blink/renderer/core/frame/sandbox_flags.h"
40 #include "third_party/blink/renderer/core/html/custom/v0_custom_element_registration_context.h"
41 #include "third_party/blink/renderer/platform/graphics/color.h"
42 #include "third_party/blink/renderer/platform/heap/handle.h"
43 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
44 
45 namespace blink {
46 
47 class ContentSecurityPolicy;
48 class Document;
49 class DocumentLoader;
50 class LocalFrame;
51 class HTMLImportsController;
52 class Settings;
53 class UseCounter;
54 class WindowAgentFactory;
55 
56 class CORE_EXPORT DocumentInit final {
57   STACK_ALLOCATED();
58 
59  public:
60   // Use either of the following methods to create a DocumentInit instance, and
61   // then add a chain of calls to the .WithFooBar() methods to add optional
62   // parameters to it.
63   //
64   // Example:
65   //
66   //   DocumentInit init = DocumentInit::Create()
67   //       .WithDocumentLoader(loader)
68   //       .WithContextDocument(context_document)
69   //       .WithURL(url);
70   //   Document* document = MakeGarbageCollected<Document>(init);
71   static DocumentInit Create();
72   static DocumentInit CreateWithImportsController(HTMLImportsController*);
73 
74   DocumentInit(const DocumentInit&);
75   ~DocumentInit();
76 
77   enum class Type {
78     kHTML,
79     kXHTML,
80     kImage,
81     kPlugin,
82     kMedia,
83     kSVG,
84     kXML,
85     kViewSource,
86     kText,
87     kUnspecified
88   };
89 
ImportsController()90   HTMLImportsController* ImportsController() const {
91     return imports_controller_;
92   }
93 
HasSecurityContext()94   bool HasSecurityContext() const { return MasterDocumentLoader(); }
95   bool IsSrcdocDocument() const;
96   bool ShouldSetURL() const;
97   mojom::blink::WebSandboxFlags GetSandboxFlags() const;
98   mojom::blink::InsecureRequestPolicy GetInsecureRequestPolicy() const;
99   const SecurityContext::InsecureNavigationsSet* InsecureNavigationsToUpgrade()
100       const;
GrantLoadLocalResources()101   bool GrantLoadLocalResources() const { return grant_load_local_resources_; }
102 
103   Settings* GetSettings() const;
104 
105   DocumentInit& WithDocumentLoader(DocumentLoader*);
106   LocalFrame* GetFrame() const;
107   UseCounter* GetUseCounter() const;
108 
109   DocumentInit& WithTypeFrom(const String& type);
GetType()110   Type GetType() const { return type_; }
GetMimeType()111   const String& GetMimeType() const { return mime_type_; }
IsForExternalHandler()112   bool IsForExternalHandler() const { return is_for_external_handler_; }
GetPluginBackgroundColor()113   Color GetPluginBackgroundColor() const { return plugin_background_color_; }
114 
115   // Used by the DOMImplementation and DOMParser to pass their parent Document
116   // so that the created Document will return the Document when the
117   // ContextDocument() method is called.
118   DocumentInit& WithContextDocument(Document*);
119   Document* ContextDocument() const;
120 
121   DocumentInit& WithURL(const KURL&);
Url()122   const KURL& Url() const { return url_; }
123 
124   scoped_refptr<SecurityOrigin> GetDocumentOrigin() const;
125 
126   // Specifies the Document to inherit security configurations from.
127   DocumentInit& WithOwnerDocument(Document*);
OwnerDocument()128   Document* OwnerDocument() const { return owner_document_; }
129 
130   // Specifies the SecurityOrigin in which the URL was requested. This is
131   // relevant for determining properties of the resulting document's origin
132   // when loading data: and about: schemes.
133   DocumentInit& WithInitiatorOrigin(
134       scoped_refptr<const SecurityOrigin> initiator_origin);
135 
136   DocumentInit& WithOriginToCommit(
137       scoped_refptr<SecurityOrigin> origin_to_commit);
OriginToCommit()138   const scoped_refptr<SecurityOrigin>& OriginToCommit() const {
139     return origin_to_commit_;
140   }
141 
142   DocumentInit& WithIPAddressSpace(
143       network::mojom::IPAddressSpace ip_address_space);
144   network::mojom::IPAddressSpace GetIPAddressSpace() const;
145 
146   DocumentInit& WithSrcdocDocument(bool is_srcdoc_document);
147   DocumentInit& WithBlockedByCSP(bool blocked_by_csp);
148   DocumentInit& WithGrantLoadLocalResources(bool grant_load_local_resources);
149 
150   DocumentInit& WithRegistrationContext(V0CustomElementRegistrationContext*);
151   V0CustomElementRegistrationContext* RegistrationContext(Document*) const;
152   DocumentInit& WithNewRegistrationContext();
153 
154   DocumentInit& WithFeaturePolicyHeader(const String& header);
FeaturePolicyHeader()155   const String& FeaturePolicyHeader() const { return feature_policy_header_; }
156 
157   DocumentInit& WithReportOnlyFeaturePolicyHeader(const String& header);
ReportOnlyFeaturePolicyHeader()158   const String& ReportOnlyFeaturePolicyHeader() const {
159     return report_only_feature_policy_header_;
160   }
161 
162   DocumentInit& WithOriginTrialsHeader(const String& header);
OriginTrialsHeader()163   const String& OriginTrialsHeader() const { return origin_trials_header_; }
164 
165   DocumentInit& WithSandboxFlags(mojom::blink::WebSandboxFlags flags);
166 
167   DocumentInit& WithContentSecurityPolicy(ContentSecurityPolicy* policy);
168   DocumentInit& WithContentSecurityPolicyFromContextDoc();
169   ContentSecurityPolicy* GetContentSecurityPolicy() const;
170 
171   DocumentInit& WithFramePolicy(
172       const base::Optional<FramePolicy>& frame_policy);
GetFramePolicy()173   const base::Optional<FramePolicy>& GetFramePolicy() const {
174     return frame_policy_;
175   }
176 
177   DocumentInit& WithDocumentPolicy(
178       const DocumentPolicy::ParsedDocumentPolicy& document_policy);
GetDocumentPolicy()179   const DocumentPolicy::ParsedDocumentPolicy& GetDocumentPolicy() const {
180     return document_policy_;
181   }
182 
183   DocumentInit& WithReportOnlyDocumentPolicyHeader(const String& header);
ReportOnlyDocumentPolicyHeader()184   const String& ReportOnlyDocumentPolicyHeader() const {
185     return report_only_document_policy_header_;
186   }
187 
188   DocumentInit& WithWebBundleClaimedUrl(const KURL& web_bundle_claimed_url);
GetWebBundleClaimedUrl()189   const KURL& GetWebBundleClaimedUrl() const { return web_bundle_claimed_url_; }
190 
191   WindowAgentFactory* GetWindowAgentFactory() const;
192   Settings* GetSettingsForWindowAgentFactory() const;
193 
194  private:
195   DocumentInit(HTMLImportsController*);
196 
197   // For a Document associated directly with a frame, this will be the
198   // DocumentLoader driving the commit. For an import, XSLT-generated
199   // document, etc., it will be the DocumentLoader that drove the commit
200   // of its owning Document.
201   DocumentLoader* MasterDocumentLoader() const;
202 
203   Type type_ = Type::kUnspecified;
204   String mime_type_;
205 
206   DocumentLoader* document_loader_ = nullptr;
207   Document* parent_document_ = nullptr;
208 
209   HTMLImportsController* imports_controller_ = nullptr;
210 
211   Document* context_document_ = nullptr;
212   KURL url_;
213   Document* owner_document_ = nullptr;
214 
215   // Initiator origin is used for calculating the document origin when the
216   // navigation is started in a different process. In such cases, the document
217   // which initiates the navigation sends its origin to the browser process and
218   // it is provided by the browser process here. It is used for cases such as
219   // data: URLs, which inherit their origin from the initiator of the
220   // navigation.
221   // Note: about:blank should also behave this way, however currently it
222   // inherits its origin from the parent frame or opener, regardless of whether
223   // it is the initiator or not.
224   scoped_refptr<const SecurityOrigin> initiator_origin_;
225 
226   // The |origin_to_commit_| is to be used directly without calculating the
227   // document origin at initialization time. It is specified by the browser
228   // process for session history navigations. This allows us to preserve
229   // the origin across session history and ensure the exact same origin
230   // is present on such navigations to URLs that inherit their origins (e.g.
231   // about:blank and data: URLs).
232   scoped_refptr<SecurityOrigin> origin_to_commit_;
233 
234   // Whether we should treat the new document as "srcdoc" document. This
235   // affects security checks, since srcdoc's content comes directly from
236   // the parent document, not from loading a URL.
237   bool is_srcdoc_document_ = false;
238 
239   // Whether the actual document was blocked by csp and we are creating a dummy
240   // empty document instead.
241   bool blocked_by_csp_ = false;
242 
243   // Whether the document should be able to access local file:// resources.
244   bool grant_load_local_resources_ = false;
245 
246   V0CustomElementRegistrationContext* registration_context_ = nullptr;
247   bool create_new_registration_context_;
248 
249   // The feature policy set via response header.
250   String feature_policy_header_;
251   String report_only_feature_policy_header_;
252 
253   // The origin trial set via response header.
254   String origin_trials_header_;
255 
256   // Additional sandbox flags
257   mojom::blink::WebSandboxFlags sandbox_flags_ =
258       mojom::blink::WebSandboxFlags::kNone;
259 
260   // Loader's CSP
261   ContentSecurityPolicy* content_security_policy_ = nullptr;
262   bool content_security_policy_from_context_doc_;
263 
264   network::mojom::IPAddressSpace ip_address_space_ =
265       network::mojom::IPAddressSpace::kUnknown;
266 
267   // The frame policy snapshot from the beginning of navigation.
268   base::Optional<FramePolicy> frame_policy_ = base::nullopt;
269 
270   // The document policy set via response header.
271   DocumentPolicy::ParsedDocumentPolicy document_policy_;
272   String report_only_document_policy_header_;
273 
274   // The claimed URL inside Web Bundle file from which the document is loaded.
275   // This URL is used for window.location and document.URL and relative path
276   // computation in the document.
277   KURL web_bundle_claimed_url_;
278 
279   bool is_for_external_handler_ = false;
280   Color plugin_background_color_;
281 };
282 
283 }  // namespace blink
284 
285 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_DOCUMENT_INIT_H_
286