Bug 1839316: part 5) Guard the "fetchpriority" attribute behind a pref. r=kershaw...
[gecko.git] / docshell / base / BrowsingContextWebProgress.cpp
blob7c915bd6669f9a21dc5325e3a6081a52734a68cc
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/. */
5 #include "BrowsingContextWebProgress.h"
6 #include "mozilla/AlreadyAddRefed.h"
7 #include "mozilla/BounceTrackingState.h"
8 #include "mozilla/dom/CanonicalBrowsingContext.h"
9 #include "mozilla/ErrorNames.h"
10 #include "mozilla/Logging.h"
11 #include "nsCOMPtr.h"
12 #include "nsIWebProgressListener.h"
13 #include "nsString.h"
14 #include "nsPrintfCString.h"
15 #include "nsIChannel.h"
16 #include "xptinfo.h"
17 #include "mozilla/RefPtr.h"
19 namespace mozilla {
20 namespace dom {
22 static mozilla::LazyLogModule gBCWebProgressLog("BCWebProgress");
24 static nsCString DescribeBrowsingContext(CanonicalBrowsingContext* aContext);
25 static nsCString DescribeWebProgress(nsIWebProgress* aWebProgress);
26 static nsCString DescribeRequest(nsIRequest* aRequest);
27 static nsCString DescribeWebProgressFlags(uint32_t aFlags,
28 const nsACString& aPrefix);
29 static nsCString DescribeError(nsresult aError);
31 NS_IMPL_CYCLE_COLLECTION(BrowsingContextWebProgress, mCurrentBrowsingContext)
33 NS_IMPL_CYCLE_COLLECTING_ADDREF(BrowsingContextWebProgress)
34 NS_IMPL_CYCLE_COLLECTING_RELEASE(BrowsingContextWebProgress)
36 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrowsingContextWebProgress)
37 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebProgress)
38 NS_INTERFACE_MAP_ENTRY(nsIWebProgress)
39 NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
40 NS_INTERFACE_MAP_END
42 BrowsingContextWebProgress::BrowsingContextWebProgress(
43 CanonicalBrowsingContext* aBrowsingContext)
44 : mCurrentBrowsingContext(aBrowsingContext) {}
46 BrowsingContextWebProgress::~BrowsingContextWebProgress() = default;
48 NS_IMETHODIMP BrowsingContextWebProgress::AddProgressListener(
49 nsIWebProgressListener* aListener, uint32_t aNotifyMask) {
50 nsWeakPtr listener = do_GetWeakReference(aListener);
51 if (!listener) {
52 return NS_ERROR_INVALID_ARG;
55 if (mListenerInfoList.Contains(listener)) {
56 // The listener is already registered!
57 return NS_ERROR_FAILURE;
60 mListenerInfoList.AppendElement(ListenerInfo(listener, aNotifyMask));
61 return NS_OK;
64 NS_IMETHODIMP BrowsingContextWebProgress::RemoveProgressListener(
65 nsIWebProgressListener* aListener) {
66 nsWeakPtr listener = do_GetWeakReference(aListener);
67 if (!listener) {
68 return NS_ERROR_INVALID_ARG;
71 return mListenerInfoList.RemoveElement(listener) ? NS_OK : NS_ERROR_FAILURE;
74 NS_IMETHODIMP BrowsingContextWebProgress::GetBrowsingContextXPCOM(
75 BrowsingContext** aBrowsingContext) {
76 NS_IF_ADDREF(*aBrowsingContext = mCurrentBrowsingContext);
77 return NS_OK;
80 BrowsingContext* BrowsingContextWebProgress::GetBrowsingContext() {
81 return mCurrentBrowsingContext;
84 NS_IMETHODIMP BrowsingContextWebProgress::GetDOMWindow(
85 mozIDOMWindowProxy** aDOMWindow) {
86 return NS_ERROR_NOT_AVAILABLE;
89 NS_IMETHODIMP BrowsingContextWebProgress::GetIsTopLevel(bool* aIsTopLevel) {
90 *aIsTopLevel = mCurrentBrowsingContext->IsTop();
91 return NS_OK;
94 NS_IMETHODIMP BrowsingContextWebProgress::GetIsLoadingDocument(
95 bool* aIsLoadingDocument) {
96 *aIsLoadingDocument = mIsLoadingDocument;
97 return NS_OK;
100 NS_IMETHODIMP BrowsingContextWebProgress::GetLoadType(uint32_t* aLoadType) {
101 *aLoadType = mLoadType;
102 return NS_OK;
105 NS_IMETHODIMP BrowsingContextWebProgress::GetTarget(nsIEventTarget** aTarget) {
106 return NS_ERROR_NOT_IMPLEMENTED;
109 NS_IMETHODIMP BrowsingContextWebProgress::SetTarget(nsIEventTarget* aTarget) {
110 return NS_ERROR_NOT_IMPLEMENTED;
113 void BrowsingContextWebProgress::UpdateAndNotifyListeners(
114 uint32_t aFlag,
115 const std::function<void(nsIWebProgressListener*)>& aCallback) {
116 RefPtr<BrowsingContextWebProgress> kungFuDeathGrip = this;
118 ListenerArray::ForwardIterator iter(mListenerInfoList);
119 while (iter.HasMore()) {
120 ListenerInfo& info = iter.GetNext();
121 if (!(info.mNotifyMask & aFlag)) {
122 continue;
125 nsCOMPtr<nsIWebProgressListener> listener =
126 do_QueryReferent(info.mWeakListener);
127 if (!listener) {
128 mListenerInfoList.RemoveElement(info);
129 continue;
132 aCallback(listener);
135 mListenerInfoList.Compact();
137 // Notify the parent BrowsingContextWebProgress of the event to continue
138 // propagating.
139 auto* parent = mCurrentBrowsingContext->GetParent();
140 if (parent && parent->GetWebProgress()) {
141 aCallback(parent->GetWebProgress());
145 void BrowsingContextWebProgress::ContextDiscarded() {
146 if (!mIsLoadingDocument) {
147 return;
150 // If our BrowsingContext is being discarded while still loading a document,
151 // fire a synthetic `STATE_STOP` to end the ongoing load.
152 MOZ_LOG(gBCWebProgressLog, LogLevel::Info,
153 ("Discarded while loading %s",
154 DescribeBrowsingContext(mCurrentBrowsingContext).get()));
156 // This matches what nsDocLoader::doStopDocumentLoad does, except we don't
157 // bother notifying for `STATE_STOP | STATE_IS_DOCUMENT`,
158 // nsBrowserStatusFilter would filter it out before it gets to the parent
159 // process.
160 nsCOMPtr<nsIRequest> request = mLoadingDocumentRequest;
161 OnStateChange(this, request, STATE_STOP | STATE_IS_WINDOW | STATE_IS_NETWORK,
162 NS_ERROR_ABORT);
165 void BrowsingContextWebProgress::ContextReplaced(
166 CanonicalBrowsingContext* aNewContext) {
167 mCurrentBrowsingContext = aNewContext;
170 already_AddRefed<BounceTrackingState>
171 BrowsingContextWebProgress::GetBounceTrackingState() {
172 if (!mBounceTrackingState) {
173 mBounceTrackingState = BounceTrackingState::GetOrCreate(this);
175 return do_AddRef(mBounceTrackingState);
178 ////////////////////////////////////////////////////////////////////////////////
179 // nsIWebProgressListener
181 NS_IMETHODIMP
182 BrowsingContextWebProgress::OnStateChange(nsIWebProgress* aWebProgress,
183 nsIRequest* aRequest,
184 uint32_t aStateFlags,
185 nsresult aStatus) {
186 MOZ_LOG(
187 gBCWebProgressLog, LogLevel::Info,
188 ("OnStateChange(%s, %s, %s, %s) on %s",
189 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
190 DescribeWebProgressFlags(aStateFlags, "STATE_"_ns).get(),
191 DescribeError(aStatus).get(),
192 DescribeBrowsingContext(mCurrentBrowsingContext).get()));
194 bool targetIsThis = aWebProgress == this;
196 // We may receive a request from an in-process nsDocShell which doesn't have
197 // `aWebProgress == this` which we should still consider as targeting
198 // ourselves.
199 if (nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(aWebProgress);
200 docShell && docShell->GetBrowsingContext() == mCurrentBrowsingContext) {
201 targetIsThis = true;
202 aWebProgress->GetLoadType(&mLoadType);
205 // Track `mIsLoadingDocument` based on the notifications we've received so far
206 // if the nsIWebProgress being targeted is this one.
207 if (targetIsThis) {
208 constexpr uint32_t startFlags = nsIWebProgressListener::STATE_START |
209 nsIWebProgressListener::STATE_IS_DOCUMENT |
210 nsIWebProgressListener::STATE_IS_REQUEST |
211 nsIWebProgressListener::STATE_IS_WINDOW |
212 nsIWebProgressListener::STATE_IS_NETWORK;
213 constexpr uint32_t stopFlags = nsIWebProgressListener::STATE_STOP |
214 nsIWebProgressListener::STATE_IS_WINDOW;
215 constexpr uint32_t redirectFlags =
216 nsIWebProgressListener::STATE_IS_REDIRECTED_DOCUMENT;
217 if ((aStateFlags & startFlags) == startFlags) {
218 if (mIsLoadingDocument) {
219 // We received a duplicate `STATE_START` notification, silence this
220 // notification until we receive the matching `STATE_STOP` to not fire
221 // duplicate `STATE_START` notifications into frontend on process
222 // switches.
223 return NS_OK;
225 mIsLoadingDocument = true;
227 // Record the request we started the load with, so we can emit a synthetic
228 // `STATE_STOP` notification if the BrowsingContext is discarded before
229 // the notification arrives.
230 mLoadingDocumentRequest = aRequest;
231 } else if ((aStateFlags & stopFlags) == stopFlags) {
232 // We've received a `STATE_STOP` notification targeting this web progress,
233 // clear our loading document flag.
234 mIsLoadingDocument = false;
235 mLoadingDocumentRequest = nullptr;
236 } else if (mIsLoadingDocument &&
237 (aStateFlags & redirectFlags) == redirectFlags) {
238 // If we see a redirected document load, update the loading request which
239 // we'll emit the synthetic STATE_STOP notification with.
240 mLoadingDocumentRequest = aRequest;
244 UpdateAndNotifyListeners(
245 ((aStateFlags >> 16) & nsIWebProgress::NOTIFY_STATE_ALL),
246 [&](nsIWebProgressListener* listener) {
247 listener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
249 return NS_OK;
252 NS_IMETHODIMP
253 BrowsingContextWebProgress::OnProgressChange(nsIWebProgress* aWebProgress,
254 nsIRequest* aRequest,
255 int32_t aCurSelfProgress,
256 int32_t aMaxSelfProgress,
257 int32_t aCurTotalProgress,
258 int32_t aMaxTotalProgress) {
259 MOZ_LOG(
260 gBCWebProgressLog, LogLevel::Info,
261 ("OnProgressChange(%s, %s, %d, %d, %d, %d) on %s",
262 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
263 aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress,
264 DescribeBrowsingContext(mCurrentBrowsingContext).get()));
265 UpdateAndNotifyListeners(
266 nsIWebProgress::NOTIFY_PROGRESS, [&](nsIWebProgressListener* listener) {
267 listener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress,
268 aMaxSelfProgress, aCurTotalProgress,
269 aMaxTotalProgress);
271 return NS_OK;
274 NS_IMETHODIMP
275 BrowsingContextWebProgress::OnLocationChange(nsIWebProgress* aWebProgress,
276 nsIRequest* aRequest,
277 nsIURI* aLocation,
278 uint32_t aFlags) {
279 MOZ_LOG(
280 gBCWebProgressLog, LogLevel::Info,
281 ("OnLocationChange(%s, %s, %s, %s) on %s",
282 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
283 aLocation ? aLocation->GetSpecOrDefault().get() : "<null>",
284 DescribeWebProgressFlags(aFlags, "LOCATION_CHANGE_"_ns).get(),
285 DescribeBrowsingContext(mCurrentBrowsingContext).get()));
286 UpdateAndNotifyListeners(
287 nsIWebProgress::NOTIFY_LOCATION, [&](nsIWebProgressListener* listener) {
288 listener->OnLocationChange(aWebProgress, aRequest, aLocation, aFlags);
290 return NS_OK;
293 NS_IMETHODIMP
294 BrowsingContextWebProgress::OnStatusChange(nsIWebProgress* aWebProgress,
295 nsIRequest* aRequest,
296 nsresult aStatus,
297 const char16_t* aMessage) {
298 MOZ_LOG(
299 gBCWebProgressLog, LogLevel::Info,
300 ("OnStatusChange(%s, %s, %s, \"%s\") on %s",
301 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
302 DescribeError(aStatus).get(), NS_ConvertUTF16toUTF8(aMessage).get(),
303 DescribeBrowsingContext(mCurrentBrowsingContext).get()));
304 UpdateAndNotifyListeners(
305 nsIWebProgress::NOTIFY_STATUS, [&](nsIWebProgressListener* listener) {
306 listener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
308 return NS_OK;
311 NS_IMETHODIMP
312 BrowsingContextWebProgress::OnSecurityChange(nsIWebProgress* aWebProgress,
313 nsIRequest* aRequest,
314 uint32_t aState) {
315 MOZ_LOG(
316 gBCWebProgressLog, LogLevel::Info,
317 ("OnSecurityChange(%s, %s, %x) on %s",
318 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
319 aState, DescribeBrowsingContext(mCurrentBrowsingContext).get()));
320 UpdateAndNotifyListeners(
321 nsIWebProgress::NOTIFY_SECURITY, [&](nsIWebProgressListener* listener) {
322 listener->OnSecurityChange(aWebProgress, aRequest, aState);
324 return NS_OK;
327 NS_IMETHODIMP
328 BrowsingContextWebProgress::OnContentBlockingEvent(nsIWebProgress* aWebProgress,
329 nsIRequest* aRequest,
330 uint32_t aEvent) {
331 MOZ_LOG(
332 gBCWebProgressLog, LogLevel::Info,
333 ("OnContentBlockingEvent(%s, %s, %x) on %s",
334 DescribeWebProgress(aWebProgress).get(), DescribeRequest(aRequest).get(),
335 aEvent, DescribeBrowsingContext(mCurrentBrowsingContext).get()));
336 UpdateAndNotifyListeners(nsIWebProgress::NOTIFY_CONTENT_BLOCKING,
337 [&](nsIWebProgressListener* listener) {
338 listener->OnContentBlockingEvent(aWebProgress,
339 aRequest, aEvent);
341 return NS_OK;
344 NS_IMETHODIMP
345 BrowsingContextWebProgress::GetDocumentRequest(nsIRequest** aRequest) {
346 NS_IF_ADDREF(*aRequest = mLoadingDocumentRequest);
347 return NS_OK;
350 ////////////////////////////////////////////////////////////////////////////////
351 // Helper methods for notification logging
353 static nsCString DescribeBrowsingContext(CanonicalBrowsingContext* aContext) {
354 if (!aContext) {
355 return "<null>"_ns;
358 nsCOMPtr<nsIURI> currentURI = aContext->GetCurrentURI();
359 return nsPrintfCString(
360 "{top:%d, id:%" PRIx64 ", url:%s}", aContext->IsTop(), aContext->Id(),
361 currentURI ? currentURI->GetSpecOrDefault().get() : "<null>");
364 static nsCString DescribeWebProgress(nsIWebProgress* aWebProgress) {
365 if (!aWebProgress) {
366 return "<null>"_ns;
369 bool isTopLevel = false;
370 aWebProgress->GetIsTopLevel(&isTopLevel);
371 bool isLoadingDocument = false;
372 aWebProgress->GetIsLoadingDocument(&isLoadingDocument);
373 return nsPrintfCString("{isTopLevel:%d, isLoadingDocument:%d}", isTopLevel,
374 isLoadingDocument);
377 static nsCString DescribeRequest(nsIRequest* aRequest) {
378 if (!aRequest) {
379 return "<null>"_ns;
382 nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
383 if (!channel) {
384 return "<non-channel>"_ns;
387 nsCOMPtr<nsIURI> originalURI;
388 channel->GetOriginalURI(getter_AddRefs(originalURI));
389 nsCOMPtr<nsIURI> uri;
390 channel->GetURI(getter_AddRefs(uri));
392 return nsPrintfCString(
393 "{URI:%s, originalURI:%s}",
394 uri ? uri->GetSpecOrDefault().get() : "<null>",
395 originalURI ? originalURI->GetSpecOrDefault().get() : "<null>");
398 static nsCString DescribeWebProgressFlags(uint32_t aFlags,
399 const nsACString& aPrefix) {
400 nsCString flags;
401 uint32_t remaining = aFlags;
403 // Hackily fetch the names of each constant from the XPT information used for
404 // reflecting it into JS. This doesn't need to be reliable and just exists as
405 // a logging aid.
407 // If a change to xpt in the future breaks this code, just delete it and
408 // replace it with a normal hex log.
409 if (const auto* ifaceInfo =
410 nsXPTInterfaceInfo::ByIID(NS_GET_IID(nsIWebProgressListener))) {
411 for (uint16_t i = 0; i < ifaceInfo->ConstantCount(); ++i) {
412 const auto& constInfo = ifaceInfo->Constant(i);
413 nsDependentCString name(constInfo.Name());
414 if (!StringBeginsWith(name, aPrefix)) {
415 continue;
418 if (remaining & constInfo.mValue) {
419 remaining &= ~constInfo.mValue;
420 if (!flags.IsEmpty()) {
421 flags.AppendLiteral("|");
423 flags.Append(name);
427 if (remaining != 0 || flags.IsEmpty()) {
428 if (!flags.IsEmpty()) {
429 flags.AppendLiteral("|");
431 flags.AppendInt(remaining, 16);
433 return flags;
436 static nsCString DescribeError(nsresult aError) {
437 nsCString name;
438 GetErrorName(aError, name);
439 return name;
442 } // namespace dom
443 } // namespace mozilla