Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsDocShellLoadState_h__
8
#define nsDocShellLoadState_h__
9
10
#include "mozilla/dom/BrowsingContext.h"
11
12
// Helper Classes
13
#include "nsCOMPtr.h"
14
#include "nsString.h"
15
#include "nsDocShellLoadTypes.h"
16
17
class nsIContentSecurityPolicy;
18
class nsIInputStream;
19
class nsISHEntry;
20
class nsIURI;
21
class nsIDocShell;
22
class nsIChannel;
23
class nsIReferrerInfo;
24
class OriginAttibutes;
25
namespace mozilla {
26
namespace dom {
27
class DocShellLoadStateInit;
28
} // namespace dom
29
} // namespace mozilla
30
31
/**
32
* nsDocShellLoadState contains setup information used in a nsIDocShell::loadURI
33
* call.
34
*/
35
class nsDocShellLoadState final {
36
public:
37
NS_INLINE_DECL_REFCOUNTING(nsDocShellLoadState);
38
39
explicit nsDocShellLoadState(nsIURI* aURI);
40
explicit nsDocShellLoadState(
41
const mozilla::dom::DocShellLoadStateInit& aLoadState);
42
43
static nsresult CreateFromPendingChannel(nsIChannel* aPendingChannel,
44
nsDocShellLoadState** aResult);
45
46
static nsresult CreateFromLoadURIOptions(
47
nsISupports* aConsumer, nsIURIFixup* aURIFixup, const nsAString& aURI,
48
const mozilla::dom::LoadURIOptions& aLoadURIOptions,
49
nsDocShellLoadState** aResult);
50
51
// Getters and Setters
52
53
nsIReferrerInfo* GetReferrerInfo() const;
54
55
void SetReferrerInfo(nsIReferrerInfo* aReferrerInfo);
56
57
nsIURI* URI() const;
58
59
void SetURI(nsIURI* aURI);
60
61
nsIURI* OriginalURI() const;
62
63
void SetOriginalURI(nsIURI* aOriginalURI);
64
65
nsIURI* ResultPrincipalURI() const;
66
67
void SetResultPrincipalURI(nsIURI* aResultPrincipalURI);
68
69
bool ResultPrincipalURIIsSome() const;
70
71
void SetResultPrincipalURIIsSome(bool aIsSome);
72
73
bool KeepResultPrincipalURIIfSet() const;
74
75
void SetKeepResultPrincipalURIIfSet(bool aKeep);
76
77
nsIPrincipal* PrincipalToInherit() const;
78
79
void SetPrincipalToInherit(nsIPrincipal* aPrincipalToInherit);
80
81
nsIPrincipal* StoragePrincipalToInherit() const;
82
83
void SetStoragePrincipalToInherit(nsIPrincipal* aStoragePrincipalToInherit);
84
85
bool LoadReplace() const;
86
87
void SetLoadReplace(bool aLoadReplace);
88
89
nsIPrincipal* TriggeringPrincipal() const;
90
91
void SetTriggeringPrincipal(nsIPrincipal* aTriggeringPrincipal);
92
93
nsIContentSecurityPolicy* Csp() const;
94
95
void SetCsp(nsIContentSecurityPolicy* aCsp);
96
97
bool InheritPrincipal() const;
98
99
void SetInheritPrincipal(bool aInheritPrincipal);
100
101
bool PrincipalIsExplicit() const;
102
103
void SetPrincipalIsExplicit(bool aPrincipalIsExplicit);
104
105
bool ForceAllowDataURI() const;
106
107
void SetForceAllowDataURI(bool aForceAllowDataURI);
108
109
bool OriginalFrameSrc() const;
110
111
void SetOriginalFrameSrc(bool aOriginalFrameSrc);
112
113
bool IsFormSubmission() const;
114
115
void SetIsFormSubmission(bool aIsFormSubmission);
116
117
uint32_t LoadType() const;
118
119
void SetLoadType(uint32_t aLoadType);
120
121
nsISHEntry* SHEntry() const;
122
123
void SetSHEntry(nsISHEntry* aSHEntry);
124
125
const nsString& Target() const;
126
127
void SetTarget(const nsAString& aTarget);
128
129
nsIInputStream* PostDataStream() const;
130
131
void SetPostDataStream(nsIInputStream* aStream);
132
133
nsIInputStream* HeadersStream() const;
134
135
void SetHeadersStream(nsIInputStream* aHeadersStream);
136
137
bool IsSrcdocLoad() const;
138
139
const nsString& SrcdocData() const;
140
141
void SetSrcdocData(const nsAString& aSrcdocData);
142
143
nsIDocShell* SourceDocShell() const;
144
145
void SetSourceDocShell(nsIDocShell* aSourceDocShell);
146
147
nsIURI* BaseURI() const;
148
149
void SetBaseURI(nsIURI* aBaseURI);
150
151
// Helper function allowing convenient work with mozilla::Maybe in C++, hiding
152
// resultPrincipalURI and resultPrincipalURIIsSome attributes from the
153
// consumer.
154
void GetMaybeResultPrincipalURI(
155
mozilla::Maybe<nsCOMPtr<nsIURI>>& aRPURI) const;
156
157
void SetMaybeResultPrincipalURI(
158
mozilla::Maybe<nsCOMPtr<nsIURI>> const& aRPURI);
159
160
uint32_t LoadFlags() const;
161
162
void SetLoadFlags(uint32_t aFlags);
163
164
void SetLoadFlag(uint32_t aFlag);
165
166
void UnsetLoadFlag(uint32_t aFlag);
167
168
bool HasLoadFlags(uint32_t aFlag);
169
170
bool FirstParty() const;
171
172
void SetFirstParty(bool aFirstParty);
173
174
const nsCString& TypeHint() const;
175
176
void SetTypeHint(const nsCString& aTypeHint);
177
178
const nsString& FileName() const;
179
180
void SetFileName(const nsAString& aFileName);
181
182
// Give the type of DocShell we're loading into (chrome/content/etc) and
183
// origin attributes for the URI we're loading, figure out if we should
184
// inherit our principal from the document the load was requested from, or
185
// else if the principal should be set up later in the process (after loads).
186
// See comments in function for more info on principal selection algorithm
187
nsresult SetupInheritingPrincipal(
188
mozilla::dom::BrowsingContext::Type aType,
189
const mozilla::OriginAttributes& aOriginAttributes);
190
191
// If no triggering principal exists at the moment, create one using referrer
192
// information and origin attributes.
193
nsresult SetupTriggeringPrincipal(
194
const mozilla::OriginAttributes& aOriginAttributes);
195
196
void SetIsFromProcessingFrameAttributes() {
197
mIsFromProcessingFrameAttributes = true;
198
}
199
bool GetIsFromProcessingFrameAttributes() const {
200
return mIsFromProcessingFrameAttributes;
201
}
202
203
nsIChannel* GetPendingRedirectedChannel() {
204
return mPendingRedirectedChannel;
205
}
206
207
void SetOriginalURIString(const nsCString& aOriginalURI) {
208
mOriginalURIString.emplace(aOriginalURI);
209
}
210
const mozilla::Maybe<nsCString>& GetOriginalURIString() const {
211
return mOriginalURIString;
212
}
213
214
void SetCancelContentJSEpoch(int32_t aCancelEpoch) {
215
mCancelContentJSEpoch.emplace(aCancelEpoch);
216
}
217
const mozilla::Maybe<int32_t>& GetCancelContentJSEpoch() const {
218
return mCancelContentJSEpoch;
219
}
220
221
// When loading a document through nsDocShell::LoadURI(), a special set of
222
// flags needs to be set based on other values in nsDocShellLoadState. This
223
// function calculates those flags, before the LoadState is passed to
224
// nsDocShell::InternalLoad.
225
void CalculateLoadURIFlags();
226
227
mozilla::dom::DocShellLoadStateInit Serialize();
228
229
protected:
230
// Destructor can't be defaulted or inlined, as header doesn't have all type
231
// includes it needs to do so.
232
~nsDocShellLoadState();
233
234
protected:
235
// This is the referrer for the load.
236
nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
237
238
// The URI we are navigating to. Will not be null once set.
239
nsCOMPtr<nsIURI> mURI;
240
241
// The URI to set as the originalURI on the channel that does the load. If
242
// null, aURI will be set as the originalURI.
243
nsCOMPtr<nsIURI> mOriginalURI;
244
245
// The URI to be set to loadInfo.resultPrincipalURI
246
// - When Nothing, there will be no change
247
// - When Some, the principal URI will overwrite even
248
// with a null value.
249
//
250
// Valid only if mResultPrincipalURIIsSome is true (has the same meaning as
251
// isSome() on mozilla::Maybe.)
252
nsCOMPtr<nsIURI> mResultPrincipalURI;
253
bool mResultPrincipalURIIsSome;
254
255
// The principal of the load, that is, the entity responsible for causing the
256
// load to occur. In most cases the referrer and the triggeringPrincipal's URI
257
// will be identical.
258
//
259
// Please note that this is the principal that is used for security checks. If
260
// the argument aURI is provided by the web, then please do not pass a
261
// SystemPrincipal as the triggeringPrincipal.
262
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
263
264
// The CSP of the load, that is, the CSP of the entity responsible for causing
265
// the load to occur. Most likely this is the CSP of the document that started
266
// the load. In case the entity starting the load did not use a CSP, then mCsp
267
// can be null. Please note that this is also the CSP that will be applied to
268
// the load in case the load encounters a server side redirect.
269
nsCOMPtr<nsIContentSecurityPolicy> mCsp;
270
271
// If a refresh is caused by http-equiv="refresh" we want to set
272
// aResultPrincipalURI, but we do not want to overwrite the channel's
273
// ResultPrincipalURI, if it has already been set on the channel by a protocol
274
// handler.
275
bool mKeepResultPrincipalURIIfSet;
276
277
// If set LOAD_REPLACE flag will be set on the channel. If aOriginalURI is
278
// null, this argument is ignored.
279
bool mLoadReplace;
280
281
// If this attribute is true and no triggeringPrincipal is specified,
282
// copy the principal from the referring document.
283
bool mInheritPrincipal;
284
285
// If this attribute is true only ever use the principal specified
286
// by the triggeringPrincipal and inheritPrincipal attributes.
287
// If there are security reasons for why this is unsafe, such
288
// as trying to use a systemprincipal as the triggeringPrincipal
289
// for a content docshell the load fails.
290
bool mPrincipalIsExplicit;
291
292
// Principal we're inheriting. If null, this means the principal should be
293
// inherited from the current document. If set to NullPrincipal, the channel
294
// will fill in principal information later in the load. See internal comments
295
// of SetupInheritingPrincipal for more info.
296
//
297
// When passed to InternalLoad, If this argument is null then
298
// principalToInherit is computed differently. See nsDocShell::InternalLoad
299
// for more comments.
300
301
nsCOMPtr<nsIPrincipal> mPrincipalToInherit;
302
303
nsCOMPtr<nsIPrincipal> mStoragePrincipalToInherit;
304
305
// If this attribute is true, then a top-level navigation
306
// to a data URI will be allowed.
307
bool mForceAllowDataURI;
308
309
// If this attribute is true, this load corresponds to a frame
310
// element loading its original src (or srcdoc) attribute.
311
bool mOriginalFrameSrc;
312
313
// If this attribute is true, then the load was initiated by a
314
// form submission. This is important to know for the CSP directive
315
// navigate-to.
316
bool mIsFormSubmission;
317
318
// Contains a load type as specified by the nsDocShellLoadTypes::load*
319
// constants
320
uint32_t mLoadType;
321
322
// Active Session History entry (if loading from SH)
323
nsCOMPtr<nsISHEntry> mSHEntry;
324
325
// Target for load, like _content, _blank etc.
326
nsString mTarget;
327
328
// Post data stream (if POSTing)
329
nsCOMPtr<nsIInputStream> mPostDataStream;
330
331
// Additional Headers
332
nsCOMPtr<nsIInputStream> mHeadersStream;
333
334
// When set, the load will be interpreted as a srcdoc load, where contents of
335
// this string will be loaded instead of the URI. Setting srcdocData sets
336
// isSrcdocLoad to true
337
nsString mSrcdocData;
338
339
// When set, this is the Source Browsing Context for the navigation.
340
nsCOMPtr<nsIDocShell> mSourceDocShell;
341
342
// Used for srcdoc loads to give view-source knowledge of the load's base URI
343
// as this information isn't embedded in the load's URI.
344
nsCOMPtr<nsIURI> mBaseURI;
345
346
// Set of Load Flags, taken from nsDocShellLoadTypes.h and nsIWebNavigation
347
uint32_t mLoadFlags;
348
349
// Is this a First Party Load?
350
bool mFirstParty;
351
352
// A hint as to the content-type of the resulting data. If no hint, IsVoid()
353
// should return true.
354
nsCString mTypeHint;
355
356
// Non-void when the link should be downloaded as the given filename.
357
// mFileName being non-void but empty means that no filename hint was
358
// specified, but link should still trigger a download. If not a download,
359
// mFileName.IsVoid() should return true.
360
nsString mFileName;
361
362
// This will be true if this load is triggered by attribute changes.
363
// See nsILoadInfo.isFromProcessingFrameAttributes
364
bool mIsFromProcessingFrameAttributes;
365
366
// If set, a pending cross-process redirected channel should be used to
367
// perform the load. The channel will be stored in this value.
368
nsCOMPtr<nsIChannel> mPendingRedirectedChannel;
369
370
// An optional string representation of mURI, before any
371
// fixups were applied, so that we can send it to a search
372
// engine service if needed.
373
mozilla::Maybe<nsCString> mOriginalURIString;
374
375
// An optional value to pass to nsIDocShell::setCancelJSEpoch
376
// when initiating the load.
377
mozilla::Maybe<int32_t> mCancelContentJSEpoch;
378
};
379
380
#endif /* nsDocShellLoadState_h__ */