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
#include "mozilla/Assertions.h"
8
#include "mozilla/BasePrincipal.h"
9
#include "mozilla/LoadContext.h"
10
#include "mozilla/Preferences.h"
11
#include "mozilla/dom/Element.h"
12
#include "mozilla/dom/ScriptSettings.h" // for AutoJSAPI
13
#include "nsContentUtils.h"
14
#include "xpcpublic.h"
15
16
namespace mozilla {
17
18
NS_IMPL_ISUPPORTS(LoadContext, nsILoadContext, nsIInterfaceRequestor)
19
20
LoadContext::LoadContext(nsIPrincipal* aPrincipal,
21
nsILoadContext* aOptionalBase)
22
: mTopFrameElement(nullptr),
23
mNestedFrameId(0),
24
mIsContent(true),
25
mUseRemoteTabs(false),
26
mUseRemoteSubframes(false),
27
mUseTrackingProtection(false),
28
#ifdef DEBUG
29
mIsNotNull(true),
30
#endif
31
mOriginAttributes(aPrincipal->OriginAttributesRef()) {
32
if (!aOptionalBase) {
33
return;
34
}
35
36
MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetIsContent(&mIsContent));
37
MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetUseRemoteTabs(&mUseRemoteTabs));
38
MOZ_ALWAYS_SUCCEEDS(
39
aOptionalBase->GetUseRemoteSubframes(&mUseRemoteSubframes));
40
MOZ_ALWAYS_SUCCEEDS(
41
aOptionalBase->GetUseTrackingProtection(&mUseTrackingProtection));
42
}
43
44
//-----------------------------------------------------------------------------
45
// LoadContext::nsILoadContext
46
//-----------------------------------------------------------------------------
47
48
NS_IMETHODIMP
49
LoadContext::GetAssociatedWindow(mozIDOMWindowProxy**) {
50
MOZ_ASSERT(mIsNotNull);
51
52
// can't support this in the parent process
53
return NS_ERROR_UNEXPECTED;
54
}
55
56
NS_IMETHODIMP
57
LoadContext::GetTopWindow(mozIDOMWindowProxy**) {
58
MOZ_ASSERT(mIsNotNull);
59
60
// can't support this in the parent process
61
return NS_ERROR_UNEXPECTED;
62
}
63
64
NS_IMETHODIMP
65
LoadContext::GetTopFrameElement(dom::Element** aElement) {
66
nsCOMPtr<dom::Element> element = do_QueryReferent(mTopFrameElement);
67
element.forget(aElement);
68
return NS_OK;
69
}
70
71
NS_IMETHODIMP
72
LoadContext::GetNestedFrameId(uint64_t* aId) {
73
NS_ENSURE_ARG(aId);
74
*aId = mNestedFrameId;
75
return NS_OK;
76
}
77
78
NS_IMETHODIMP
79
LoadContext::GetIsContent(bool* aIsContent) {
80
MOZ_ASSERT(mIsNotNull);
81
82
NS_ENSURE_ARG_POINTER(aIsContent);
83
84
*aIsContent = mIsContent;
85
return NS_OK;
86
}
87
88
NS_IMETHODIMP
89
LoadContext::GetUsePrivateBrowsing(bool* aUsePrivateBrowsing) {
90
MOZ_ASSERT(mIsNotNull);
91
92
NS_ENSURE_ARG_POINTER(aUsePrivateBrowsing);
93
94
*aUsePrivateBrowsing = mOriginAttributes.mPrivateBrowsingId > 0;
95
return NS_OK;
96
}
97
98
NS_IMETHODIMP
99
LoadContext::SetUsePrivateBrowsing(bool aUsePrivateBrowsing) {
100
MOZ_ASSERT(mIsNotNull);
101
102
// We shouldn't need this on parent...
103
return NS_ERROR_UNEXPECTED;
104
}
105
106
NS_IMETHODIMP
107
LoadContext::SetPrivateBrowsing(bool aUsePrivateBrowsing) {
108
MOZ_ASSERT(mIsNotNull);
109
110
// We shouldn't need this on parent...
111
return NS_ERROR_UNEXPECTED;
112
}
113
114
NS_IMETHODIMP
115
LoadContext::GetUseRemoteTabs(bool* aUseRemoteTabs) {
116
MOZ_ASSERT(mIsNotNull);
117
118
NS_ENSURE_ARG_POINTER(aUseRemoteTabs);
119
120
*aUseRemoteTabs = mUseRemoteTabs;
121
return NS_OK;
122
}
123
124
NS_IMETHODIMP
125
LoadContext::SetRemoteTabs(bool aUseRemoteTabs) {
126
MOZ_ASSERT(mIsNotNull);
127
128
// We shouldn't need this on parent...
129
return NS_ERROR_UNEXPECTED;
130
}
131
132
NS_IMETHODIMP
133
LoadContext::GetUseRemoteSubframes(bool* aUseRemoteSubframes) {
134
MOZ_ASSERT(mIsNotNull);
135
136
NS_ENSURE_ARG_POINTER(aUseRemoteSubframes);
137
138
*aUseRemoteSubframes = mUseRemoteSubframes;
139
return NS_OK;
140
}
141
142
NS_IMETHODIMP
143
LoadContext::SetRemoteSubframes(bool aUseRemoteSubframes) {
144
MOZ_ASSERT(mIsNotNull);
145
146
// We shouldn't need this on parent...
147
return NS_ERROR_UNEXPECTED;
148
}
149
150
NS_IMETHODIMP
151
LoadContext::GetScriptableOriginAttributes(JSContext* aCx,
152
JS::MutableHandleValue aAttrs) {
153
bool ok = ToJSValue(aCx, mOriginAttributes, aAttrs);
154
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
155
return NS_OK;
156
}
157
158
NS_IMETHODIMP_(void)
159
LoadContext::GetOriginAttributes(mozilla::OriginAttributes& aAttrs) {
160
aAttrs = mOriginAttributes;
161
}
162
163
NS_IMETHODIMP
164
LoadContext::GetUseTrackingProtection(bool* aUseTrackingProtection) {
165
MOZ_ASSERT(mIsNotNull);
166
167
NS_ENSURE_ARG_POINTER(aUseTrackingProtection);
168
169
*aUseTrackingProtection = mUseTrackingProtection;
170
return NS_OK;
171
}
172
173
NS_IMETHODIMP
174
LoadContext::SetUseTrackingProtection(bool aUseTrackingProtection) {
175
MOZ_ASSERT_UNREACHABLE("Should only be set through nsDocShell");
176
177
return NS_ERROR_UNEXPECTED;
178
}
179
180
//-----------------------------------------------------------------------------
181
// LoadContext::nsIInterfaceRequestor
182
//-----------------------------------------------------------------------------
183
NS_IMETHODIMP
184
LoadContext::GetInterface(const nsIID& aIID, void** aResult) {
185
NS_ENSURE_ARG_POINTER(aResult);
186
*aResult = nullptr;
187
188
if (aIID.Equals(NS_GET_IID(nsILoadContext))) {
189
*aResult = static_cast<nsILoadContext*>(this);
190
NS_ADDREF_THIS();
191
return NS_OK;
192
}
193
194
return NS_NOINTERFACE;
195
}
196
197
static already_AddRefed<nsILoadContext> CreateInstance(bool aPrivate) {
198
OriginAttributes oa;
199
oa.mPrivateBrowsingId = aPrivate ? 1 : 0;
200
201
nsCOMPtr<nsILoadContext> lc = new LoadContext(oa);
202
203
return lc.forget();
204
}
205
206
already_AddRefed<nsILoadContext> CreateLoadContext() {
207
return CreateInstance(false);
208
}
209
210
already_AddRefed<nsILoadContext> CreatePrivateLoadContext() {
211
return CreateInstance(true);
212
}
213
214
} // namespace mozilla