Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "LoadContextInfo.h"
6
7
#include "mozilla/dom/ToJSValue.h"
8
#include "nsDocShell.h"
9
#include "nsIChannel.h"
10
#include "nsILoadContext.h"
11
#include "nsIWebNavigation.h"
12
#include "nsNetUtil.h"
13
14
using namespace mozilla::dom;
15
namespace mozilla {
16
namespace net {
17
18
// LoadContextInfo
19
20
NS_IMPL_ISUPPORTS(LoadContextInfo, nsILoadContextInfo)
21
22
LoadContextInfo::LoadContextInfo(bool aIsAnonymous,
23
OriginAttributes aOriginAttributes)
24
: mIsAnonymous(aIsAnonymous),
25
mOriginAttributes(std::move(aOriginAttributes)) {}
26
27
NS_IMETHODIMP LoadContextInfo::GetIsPrivate(bool* aIsPrivate) {
28
*aIsPrivate = mOriginAttributes.mPrivateBrowsingId > 0;
29
return NS_OK;
30
}
31
32
NS_IMETHODIMP LoadContextInfo::GetIsAnonymous(bool* aIsAnonymous) {
33
*aIsAnonymous = mIsAnonymous;
34
return NS_OK;
35
}
36
37
OriginAttributes const* LoadContextInfo::OriginAttributesPtr() {
38
return &mOriginAttributes;
39
}
40
41
NS_IMETHODIMP LoadContextInfo::GetOriginAttributes(
42
JSContext* aCx, JS::MutableHandle<JS::Value> aVal) {
43
if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) {
44
return NS_ERROR_FAILURE;
45
}
46
return NS_OK;
47
}
48
49
// LoadContextInfoFactory
50
51
NS_IMPL_ISUPPORTS(LoadContextInfoFactory, nsILoadContextInfoFactory)
52
53
NS_IMETHODIMP LoadContextInfoFactory::GetDefault(
54
nsILoadContextInfo** aDefault) {
55
nsCOMPtr<nsILoadContextInfo> info =
56
GetLoadContextInfo(false, OriginAttributes());
57
info.forget(aDefault);
58
return NS_OK;
59
}
60
61
NS_IMETHODIMP LoadContextInfoFactory::GetPrivate(
62
nsILoadContextInfo** aPrivate) {
63
OriginAttributes attrs;
64
attrs.SyncAttributesWithPrivateBrowsing(true);
65
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, attrs);
66
info.forget(aPrivate);
67
return NS_OK;
68
}
69
70
NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous(
71
nsILoadContextInfo** aAnonymous) {
72
nsCOMPtr<nsILoadContextInfo> info =
73
GetLoadContextInfo(true, OriginAttributes());
74
info.forget(aAnonymous);
75
return NS_OK;
76
}
77
78
NS_IMETHODIMP LoadContextInfoFactory::Custom(bool aAnonymous,
79
JS::HandleValue aOriginAttributes,
80
JSContext* cx,
81
nsILoadContextInfo** _retval) {
82
OriginAttributes attrs;
83
bool status = attrs.Init(cx, aOriginAttributes);
84
NS_ENSURE_TRUE(status, NS_ERROR_FAILURE);
85
86
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aAnonymous, attrs);
87
info.forget(_retval);
88
return NS_OK;
89
}
90
91
NS_IMETHODIMP LoadContextInfoFactory::FromLoadContext(
92
nsILoadContext* aLoadContext, bool aAnonymous,
93
nsILoadContextInfo** _retval) {
94
nsCOMPtr<nsILoadContextInfo> info =
95
GetLoadContextInfo(aLoadContext, aAnonymous);
96
info.forget(_retval);
97
return NS_OK;
98
}
99
100
NS_IMETHODIMP LoadContextInfoFactory::FromWindow(nsIDOMWindow* aWindow,
101
bool aAnonymous,
102
nsILoadContextInfo** _retval) {
103
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aWindow, aAnonymous);
104
info.forget(_retval);
105
return NS_OK;
106
}
107
108
// Helper functions
109
110
LoadContextInfo* GetLoadContextInfo(nsIChannel* aChannel) {
111
nsresult rv;
112
113
DebugOnly<bool> pb = NS_UsePrivateBrowsing(aChannel);
114
115
bool anon = false;
116
nsLoadFlags loadFlags;
117
rv = aChannel->GetLoadFlags(&loadFlags);
118
if (NS_SUCCEEDED(rv)) {
119
anon = !!(loadFlags & nsIChannel::LOAD_ANONYMOUS);
120
}
121
122
OriginAttributes oa;
123
NS_GetOriginAttributes(aChannel, oa);
124
MOZ_ASSERT(pb == (oa.mPrivateBrowsingId > 0));
125
126
return new LoadContextInfo(anon, oa);
127
}
128
129
LoadContextInfo* GetLoadContextInfo(nsILoadContext* aLoadContext,
130
bool aIsAnonymous) {
131
if (!aLoadContext) {
132
return new LoadContextInfo(aIsAnonymous, OriginAttributes());
133
}
134
135
OriginAttributes oa;
136
aLoadContext->GetOriginAttributes(oa);
137
138
#ifdef DEBUG
139
nsCOMPtr<nsIDocShellTreeItem> docShell = do_QueryInterface(aLoadContext);
140
if (!docShell || docShell->ItemType() != nsIDocShellTreeItem::typeChrome) {
141
MOZ_ASSERT(aLoadContext->UsePrivateBrowsing() ==
142
(oa.mPrivateBrowsingId > 0));
143
}
144
#endif
145
146
return new LoadContextInfo(aIsAnonymous, oa);
147
}
148
149
LoadContextInfo* GetLoadContextInfo(nsIDOMWindow* aWindow, bool aIsAnonymous) {
150
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
151
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav);
152
153
return GetLoadContextInfo(loadContext, aIsAnonymous);
154
}
155
156
LoadContextInfo* GetLoadContextInfo(nsILoadContextInfo* aInfo) {
157
return new LoadContextInfo(aInfo->IsAnonymous(),
158
*aInfo->OriginAttributesPtr());
159
}
160
161
LoadContextInfo* GetLoadContextInfo(bool const aIsAnonymous,
162
OriginAttributes const& aOriginAttributes) {
163
return new LoadContextInfo(aIsAnonymous, aOriginAttributes);
164
}
165
166
} // namespace net
167
} // namespace mozilla