/src/mozilla-central/netwerk/base/LoadContextInfo.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
27 | 0 | } |
28 | | |
29 | | NS_IMETHODIMP LoadContextInfo::GetIsPrivate(bool *aIsPrivate) |
30 | 0 | { |
31 | 0 | *aIsPrivate = mOriginAttributes.mPrivateBrowsingId > 0; |
32 | 0 | return NS_OK; |
33 | 0 | } |
34 | | |
35 | | NS_IMETHODIMP LoadContextInfo::GetIsAnonymous(bool *aIsAnonymous) |
36 | 0 | { |
37 | 0 | *aIsAnonymous = mIsAnonymous; |
38 | 0 | return NS_OK; |
39 | 0 | } |
40 | | |
41 | | OriginAttributes const* LoadContextInfo::OriginAttributesPtr() |
42 | 0 | { |
43 | 0 | return &mOriginAttributes; |
44 | 0 | } |
45 | | |
46 | | NS_IMETHODIMP LoadContextInfo::GetOriginAttributes(JSContext *aCx, |
47 | | JS::MutableHandle<JS::Value> aVal) |
48 | 0 | { |
49 | 0 | if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) { |
50 | 0 | return NS_ERROR_FAILURE; |
51 | 0 | } |
52 | 0 | return NS_OK; |
53 | 0 | } |
54 | | |
55 | | // LoadContextInfoFactory |
56 | | |
57 | | NS_IMPL_ISUPPORTS(LoadContextInfoFactory, nsILoadContextInfoFactory) |
58 | | |
59 | | NS_IMETHODIMP LoadContextInfoFactory::GetDefault(nsILoadContextInfo * *aDefault) |
60 | 0 | { |
61 | 0 | nsCOMPtr<nsILoadContextInfo> info = |
62 | 0 | GetLoadContextInfo(false, OriginAttributes()); |
63 | 0 | info.forget(aDefault); |
64 | 0 | return NS_OK; |
65 | 0 | } |
66 | | |
67 | | NS_IMETHODIMP LoadContextInfoFactory::GetPrivate(nsILoadContextInfo * *aPrivate) |
68 | 0 | { |
69 | 0 | OriginAttributes attrs; |
70 | 0 | attrs.SyncAttributesWithPrivateBrowsing(true); |
71 | 0 | nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, attrs); |
72 | 0 | info.forget(aPrivate); |
73 | 0 | return NS_OK; |
74 | 0 | } |
75 | | |
76 | | NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous(nsILoadContextInfo * *aAnonymous) |
77 | 0 | { |
78 | 0 | nsCOMPtr<nsILoadContextInfo> info = |
79 | 0 | GetLoadContextInfo(true, OriginAttributes()); |
80 | 0 | info.forget(aAnonymous); |
81 | 0 | return NS_OK; |
82 | 0 | } |
83 | | |
84 | | NS_IMETHODIMP LoadContextInfoFactory::Custom(bool aAnonymous, |
85 | | JS::HandleValue aOriginAttributes, JSContext *cx, |
86 | | nsILoadContextInfo * *_retval) |
87 | 0 | { |
88 | 0 | OriginAttributes attrs; |
89 | 0 | bool status = attrs.Init(cx, aOriginAttributes); |
90 | 0 | NS_ENSURE_TRUE(status, NS_ERROR_FAILURE); |
91 | 0 |
|
92 | 0 | nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aAnonymous, attrs); |
93 | 0 | info.forget(_retval); |
94 | 0 | return NS_OK; |
95 | 0 | } |
96 | | |
97 | | NS_IMETHODIMP LoadContextInfoFactory::FromLoadContext(nsILoadContext *aLoadContext, bool aAnonymous, |
98 | | nsILoadContextInfo * *_retval) |
99 | 0 | { |
100 | 0 | nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aLoadContext, aAnonymous); |
101 | 0 | info.forget(_retval); |
102 | 0 | return NS_OK; |
103 | 0 | } |
104 | | |
105 | | NS_IMETHODIMP LoadContextInfoFactory::FromWindow(nsIDOMWindow *aWindow, bool aAnonymous, |
106 | | nsILoadContextInfo * *_retval) |
107 | 0 | { |
108 | 0 | nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aWindow, aAnonymous); |
109 | 0 | info.forget(_retval); |
110 | 0 | return NS_OK; |
111 | 0 | } |
112 | | |
113 | | // Helper functions |
114 | | |
115 | | LoadContextInfo * |
116 | | GetLoadContextInfo(nsIChannel * aChannel) |
117 | 0 | { |
118 | 0 | nsresult rv; |
119 | 0 |
|
120 | 0 | DebugOnly<bool> pb = NS_UsePrivateBrowsing(aChannel); |
121 | 0 |
|
122 | 0 | bool anon = false; |
123 | 0 | nsLoadFlags loadFlags; |
124 | 0 | rv = aChannel->GetLoadFlags(&loadFlags); |
125 | 0 | if (NS_SUCCEEDED(rv)) { |
126 | 0 | anon = !!(loadFlags & nsIChannel::LOAD_ANONYMOUS); |
127 | 0 | } |
128 | 0 |
|
129 | 0 | OriginAttributes oa; |
130 | 0 | NS_GetOriginAttributes(aChannel, oa); |
131 | 0 | MOZ_ASSERT(pb == (oa.mPrivateBrowsingId > 0)); |
132 | 0 |
|
133 | 0 | return new LoadContextInfo(anon, oa); |
134 | 0 | } |
135 | | |
136 | | LoadContextInfo * |
137 | | GetLoadContextInfo(nsILoadContext *aLoadContext, bool aIsAnonymous) |
138 | 0 | { |
139 | 0 | if (!aLoadContext) { |
140 | 0 | return new LoadContextInfo(aIsAnonymous, OriginAttributes()); |
141 | 0 | } |
142 | 0 | |
143 | 0 | OriginAttributes oa; |
144 | 0 | aLoadContext->GetOriginAttributes(oa); |
145 | 0 |
|
146 | | #ifdef DEBUG |
147 | | nsCOMPtr<nsIDocShellTreeItem> docShell = do_QueryInterface(aLoadContext); |
148 | | if (!docShell || docShell->ItemType() != nsIDocShellTreeItem::typeChrome) { |
149 | | MOZ_ASSERT(aLoadContext->UsePrivateBrowsing() == (oa.mPrivateBrowsingId > 0)); |
150 | | } |
151 | | #endif |
152 | |
|
153 | 0 | return new LoadContextInfo(aIsAnonymous, oa); |
154 | 0 | } |
155 | | |
156 | | LoadContextInfo* |
157 | | GetLoadContextInfo(nsIDOMWindow *aWindow, |
158 | | bool aIsAnonymous) |
159 | 0 | { |
160 | 0 | nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow); |
161 | 0 | nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav); |
162 | 0 |
|
163 | 0 | return GetLoadContextInfo(loadContext, aIsAnonymous); |
164 | 0 | } |
165 | | |
166 | | LoadContextInfo * |
167 | | GetLoadContextInfo(nsILoadContextInfo *aInfo) |
168 | 0 | { |
169 | 0 | return new LoadContextInfo(aInfo->IsAnonymous(), |
170 | 0 | *aInfo->OriginAttributesPtr()); |
171 | 0 | } |
172 | | |
173 | | LoadContextInfo * |
174 | | GetLoadContextInfo(bool const aIsAnonymous, |
175 | | OriginAttributes const &aOriginAttributes) |
176 | 0 | { |
177 | 0 | return new LoadContextInfo(aIsAnonymous, aOriginAttributes); |
178 | 0 | } |
179 | | |
180 | | } // namespace net |
181 | | } // namespace mozilla |