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/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/BasePrincipal.h"
9 #include "mozilla/dom/ToJSValue.h"
10 #include "mozilla/LoadContext.h"
11 #include "mozilla/Preferences.h"
12 #include "mozilla/dom/Element.h"
13 #include "mozilla/dom/ScriptSettings.h" // for AutoJSAPI
14 #include "mozilla/dom/BrowsingContext.h"
15 #include "nsContentUtils.h"
16 #include "xpcpublic.h"
20 NS_IMPL_ISUPPORTS(LoadContext
, nsILoadContext
, nsIInterfaceRequestor
)
22 LoadContext::LoadContext(const IPC::SerializedLoadContext
& aToCopy
,
23 dom::Element
* aTopFrameElement
,
24 OriginAttributes
& aAttrs
)
25 : mTopFrameElement(do_GetWeakReference(aTopFrameElement
)),
26 mIsContent(aToCopy
.mIsContent
),
27 mUseRemoteTabs(aToCopy
.mUseRemoteTabs
),
28 mUseRemoteSubframes(aToCopy
.mUseRemoteSubframes
),
29 mUseTrackingProtection(aToCopy
.mUseTrackingProtection
),
31 mIsNotNull(aToCopy
.mIsNotNull
),
33 mOriginAttributes(aAttrs
) {
36 LoadContext::LoadContext(OriginAttributes
& aAttrs
)
37 : mTopFrameElement(nullptr),
39 mUseRemoteTabs(false),
40 mUseRemoteSubframes(false),
41 mUseTrackingProtection(false),
45 mOriginAttributes(aAttrs
) {
48 LoadContext::LoadContext(nsIPrincipal
* aPrincipal
,
49 nsILoadContext
* aOptionalBase
)
50 : mTopFrameElement(nullptr),
52 mUseRemoteTabs(false),
53 mUseRemoteSubframes(false),
54 mUseTrackingProtection(false),
58 mOriginAttributes(aPrincipal
->OriginAttributesRef()) {
63 MOZ_ALWAYS_SUCCEEDS(aOptionalBase
->GetIsContent(&mIsContent
));
64 MOZ_ALWAYS_SUCCEEDS(aOptionalBase
->GetUseRemoteTabs(&mUseRemoteTabs
));
66 aOptionalBase
->GetUseRemoteSubframes(&mUseRemoteSubframes
));
68 aOptionalBase
->GetUseTrackingProtection(&mUseTrackingProtection
));
71 LoadContext::~LoadContext() = default;
73 //-----------------------------------------------------------------------------
74 // LoadContext::nsILoadContext
75 //-----------------------------------------------------------------------------
78 LoadContext::GetAssociatedWindow(mozIDOMWindowProxy
**) {
79 MOZ_ASSERT(mIsNotNull
);
81 // can't support this in the parent process
82 return NS_ERROR_UNEXPECTED
;
86 LoadContext::GetTopWindow(mozIDOMWindowProxy
**) {
87 MOZ_ASSERT(mIsNotNull
);
89 // can't support this in the parent process
90 return NS_ERROR_UNEXPECTED
;
94 LoadContext::GetTopFrameElement(dom::Element
** aElement
) {
95 nsCOMPtr
<dom::Element
> element
= do_QueryReferent(mTopFrameElement
);
96 element
.forget(aElement
);
101 LoadContext::GetIsContent(bool* aIsContent
) {
102 MOZ_ASSERT(mIsNotNull
);
104 NS_ENSURE_ARG_POINTER(aIsContent
);
106 *aIsContent
= mIsContent
;
111 LoadContext::GetUsePrivateBrowsing(bool* aUsePrivateBrowsing
) {
112 MOZ_ASSERT(mIsNotNull
);
114 NS_ENSURE_ARG_POINTER(aUsePrivateBrowsing
);
116 *aUsePrivateBrowsing
= mOriginAttributes
.mPrivateBrowsingId
> 0;
121 LoadContext::SetUsePrivateBrowsing(bool aUsePrivateBrowsing
) {
122 MOZ_ASSERT(mIsNotNull
);
124 // We shouldn't need this on parent...
125 return NS_ERROR_UNEXPECTED
;
129 LoadContext::SetPrivateBrowsing(bool aUsePrivateBrowsing
) {
130 MOZ_ASSERT(mIsNotNull
);
132 // We shouldn't need this on parent...
133 return NS_ERROR_UNEXPECTED
;
137 LoadContext::GetUseRemoteTabs(bool* aUseRemoteTabs
) {
138 MOZ_ASSERT(mIsNotNull
);
140 NS_ENSURE_ARG_POINTER(aUseRemoteTabs
);
142 *aUseRemoteTabs
= mUseRemoteTabs
;
147 LoadContext::SetRemoteTabs(bool aUseRemoteTabs
) {
148 MOZ_ASSERT(mIsNotNull
);
150 // We shouldn't need this on parent...
151 return NS_ERROR_UNEXPECTED
;
155 LoadContext::GetUseRemoteSubframes(bool* aUseRemoteSubframes
) {
156 MOZ_ASSERT(mIsNotNull
);
158 NS_ENSURE_ARG_POINTER(aUseRemoteSubframes
);
160 *aUseRemoteSubframes
= mUseRemoteSubframes
;
165 LoadContext::SetRemoteSubframes(bool aUseRemoteSubframes
) {
166 MOZ_ASSERT(mIsNotNull
);
168 // We shouldn't need this on parent...
169 return NS_ERROR_UNEXPECTED
;
173 LoadContext::GetScriptableOriginAttributes(
174 JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aAttrs
) {
175 bool ok
= ToJSValue(aCx
, mOriginAttributes
, aAttrs
);
176 NS_ENSURE_TRUE(ok
, NS_ERROR_FAILURE
);
181 LoadContext::GetOriginAttributes(mozilla::OriginAttributes
& aAttrs
) {
182 aAttrs
= mOriginAttributes
;
186 LoadContext::GetUseTrackingProtection(bool* aUseTrackingProtection
) {
187 MOZ_ASSERT(mIsNotNull
);
189 NS_ENSURE_ARG_POINTER(aUseTrackingProtection
);
191 *aUseTrackingProtection
= mUseTrackingProtection
;
196 LoadContext::SetUseTrackingProtection(bool aUseTrackingProtection
) {
197 MOZ_ASSERT_UNREACHABLE("Should only be set through nsDocShell");
199 return NS_ERROR_UNEXPECTED
;
202 //-----------------------------------------------------------------------------
203 // LoadContext::nsIInterfaceRequestor
204 //-----------------------------------------------------------------------------
206 LoadContext::GetInterface(const nsIID
& aIID
, void** aResult
) {
207 NS_ENSURE_ARG_POINTER(aResult
);
210 if (aIID
.Equals(NS_GET_IID(nsILoadContext
))) {
211 *aResult
= static_cast<nsILoadContext
*>(this);
216 return NS_NOINTERFACE
;
219 static already_AddRefed
<nsILoadContext
> CreateInstance(bool aPrivate
) {
221 oa
.mPrivateBrowsingId
= aPrivate
? 1 : 0;
223 nsCOMPtr
<nsILoadContext
> lc
= new LoadContext(oa
);
228 already_AddRefed
<nsILoadContext
> CreateLoadContext() {
229 return CreateInstance(false);
232 already_AddRefed
<nsILoadContext
> CreatePrivateLoadContext() {
233 return CreateInstance(true);
236 } // namespace mozilla