2015-07-31 23:50:08 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 ;*; */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-09-08 17:06:26 +03:00
|
|
|
#include "nsIDocShell.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2017-09-08 17:06:26 +03:00
|
|
|
#include "nsIDocumentLoader.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
#include "nsIObserverService.h"
|
2017-03-23 17:40:36 +03:00
|
|
|
#include "nsIXULRuntime.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2016-04-21 17:59:17 +03:00
|
|
|
#include "RequestContextService.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
2018-10-23 23:07:17 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-07-12 09:44:00 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
#include "mozilla/Services.h"
|
2018-10-23 23:07:17 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2017-08-30 16:32:00 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-09-08 17:06:26 +03:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
2015-07-31 23:50:08 +03:00
|
|
|
#include "mozilla/net/PSpdyPush.h"
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
#include "../protocol/http/nsHttpHandler.h"
|
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2017-07-12 09:44:00 +03:00
|
|
|
LazyLogModule gRequestContextLog("RequestContext");
|
|
|
|
#undef LOG
|
|
|
|
#define LOG(args) MOZ_LOG(gRequestContextLog, LogLevel::Info, args)
|
|
|
|
|
2018-10-23 23:07:17 +03:00
|
|
|
static StaticRefPtr<RequestContextService> gSingleton;
|
|
|
|
|
2017-09-03 13:12:30 +03:00
|
|
|
// This is used to prevent adding tail pending requests after shutdown
|
|
|
|
static bool sShutdown = false;
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
// nsIRequestContext
|
|
|
|
class RequestContext final : public nsIRequestContext, public nsITimerCallback {
|
2015-07-31 23:50:08 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2016-04-21 17:59:17 +03:00
|
|
|
NS_DECL_NSIREQUESTCONTEXT
|
2017-08-30 16:32:00 +03:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-03-23 17:40:36 +03:00
|
|
|
explicit RequestContext(const uint64_t id);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
private:
|
2016-04-21 17:59:17 +03:00
|
|
|
virtual ~RequestContext();
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
void ProcessTailQueue(nsresult aResult);
|
|
|
|
// Reschedules the timer if needed
|
|
|
|
void ScheduleUnblock();
|
|
|
|
// Hard-reschedules the timer
|
|
|
|
void RescheduleUntailTimer(TimeStamp const& now);
|
|
|
|
|
2017-03-23 17:40:36 +03:00
|
|
|
uint64_t mID;
|
2015-07-31 23:50:08 +03:00
|
|
|
Atomic<uint32_t> mBlockingTransactionCount;
|
2020-02-11 19:20:08 +03:00
|
|
|
UniquePtr<SpdyPushCache> mSpdyCache;
|
2017-08-30 16:32:00 +03:00
|
|
|
|
|
|
|
typedef nsCOMPtr<nsIRequestTailUnblockCallback> PendingTailRequest;
|
|
|
|
// Number of known opened non-tailed requets
|
|
|
|
uint32_t mNonTailRequests;
|
|
|
|
// Queue of requests that have been tailed, when conditions are met
|
|
|
|
// we call each of them to unblock and drop the reference
|
|
|
|
nsTArray<PendingTailRequest> mTailQueue;
|
|
|
|
// Loosly scheduled timer, never scheduled further to the future than
|
|
|
|
// mUntailAt time
|
|
|
|
nsCOMPtr<nsITimer> mUntailTimer;
|
|
|
|
// Timestamp when the timer is expected to fire,
|
|
|
|
// always less than or equal to mUntailAt
|
|
|
|
TimeStamp mTimerScheduledAt;
|
|
|
|
// Timestamp when we want to actually untail queued requets based on
|
|
|
|
// the number of request count change in the past; iff this timestamp
|
|
|
|
// is set, we tail requests
|
|
|
|
TimeStamp mUntailAt;
|
|
|
|
|
2018-01-08 07:01:00 +03:00
|
|
|
// Timestamp of the navigation start time, set to Now() in BeginLoad().
|
|
|
|
// This is used to progressively lower the maximum delay time so that
|
|
|
|
// we can't get to a situation when a number of repetitive requests
|
|
|
|
// on the page causes forever tailing.
|
|
|
|
TimeStamp mBeginLoadTime;
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
// This member is true only between DOMContentLoaded notification and
|
|
|
|
// next document load beginning for this request context.
|
|
|
|
// Top level request contexts are recycled.
|
|
|
|
bool mAfterDOMContentLoaded;
|
2015-07-31 23:50:08 +03:00
|
|
|
};
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(RequestContext, nsIRequestContext, nsITimerCallback)
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-03-23 17:40:36 +03:00
|
|
|
RequestContext::RequestContext(const uint64_t aID)
|
|
|
|
: mID(aID),
|
|
|
|
mBlockingTransactionCount(0),
|
2017-08-30 16:32:00 +03:00
|
|
|
mNonTailRequests(0),
|
|
|
|
mAfterDOMContentLoaded(false) {
|
2017-07-12 09:44:00 +03:00
|
|
|
LOG(("RequestContext::RequestContext this=%p id=%" PRIx64, this, mID));
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContext::~RequestContext() {
|
2017-08-30 16:32:00 +03:00
|
|
|
MOZ_ASSERT(mTailQueue.Length() == 0);
|
|
|
|
|
2017-07-12 09:44:00 +03:00
|
|
|
LOG(("RequestContext::~RequestContext this=%p blockers=%u", this,
|
|
|
|
static_cast<uint32_t>(mBlockingTransactionCount)));
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::BeginLoad() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
LOG(("RequestContext::BeginLoad %p", this));
|
|
|
|
|
2017-09-27 02:30:00 +03:00
|
|
|
if (IsNeckoChild()) {
|
2017-08-30 16:32:00 +03:00
|
|
|
// Tailing is not supported on the child process
|
2017-09-27 02:30:00 +03:00
|
|
|
if (gNeckoChild) {
|
|
|
|
gNeckoChild->SendRequestContextLoadBegin(mID);
|
|
|
|
}
|
2017-08-30 16:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAfterDOMContentLoaded = false;
|
2018-01-08 07:01:00 +03:00
|
|
|
mBeginLoadTime = TimeStamp::NowLoRes();
|
2017-08-30 16:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::DOMContentLoaded() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
LOG(("RequestContext::DOMContentLoaded %p", this));
|
|
|
|
|
2017-09-12 15:40:00 +03:00
|
|
|
if (IsNeckoChild()) {
|
2017-08-30 16:32:00 +03:00
|
|
|
// Tailing is not supported on the child process
|
2017-09-12 15:40:00 +03:00
|
|
|
if (gNeckoChild) {
|
|
|
|
gNeckoChild->SendRequestContextAfterDOMContentLoaded(mID);
|
|
|
|
}
|
2017-08-30 16:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAfterDOMContentLoaded) {
|
|
|
|
// There is a possibility of a duplicate notification
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAfterDOMContentLoaded = true;
|
|
|
|
|
|
|
|
// Conditions for the delay calculation has changed.
|
|
|
|
ScheduleUnblock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
NS_IMETHODIMP
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContext::GetBlockingTransactionCount(
|
|
|
|
uint32_t* aBlockingTransactionCount) {
|
2015-07-31 23:50:08 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aBlockingTransactionCount);
|
|
|
|
*aBlockingTransactionCount = mBlockingTransactionCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContext::AddBlockingTransaction() {
|
2015-07-31 23:50:08 +03:00
|
|
|
mBlockingTransactionCount++;
|
2017-07-12 09:44:00 +03:00
|
|
|
LOG(("RequestContext::AddBlockingTransaction this=%p blockers=%u", this,
|
|
|
|
static_cast<uint32_t>(mBlockingTransactionCount)));
|
2015-07-31 23:50:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContext::RemoveBlockingTransaction(uint32_t* outval) {
|
2015-07-31 23:50:08 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(outval);
|
|
|
|
mBlockingTransactionCount--;
|
2017-07-12 09:44:00 +03:00
|
|
|
LOG(("RequestContext::RemoveBlockingTransaction this=%p blockers=%u", this,
|
|
|
|
static_cast<uint32_t>(mBlockingTransactionCount)));
|
2015-07-31 23:50:08 +03:00
|
|
|
*outval = mBlockingTransactionCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-11 19:20:08 +03:00
|
|
|
SpdyPushCache* RequestContext::GetSpdyPushCache() { return mSpdyCache.get(); }
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2018-11-20 04:17:53 +03:00
|
|
|
void RequestContext::SetSpdyPushCache(SpdyPushCache* aSpdyPushCache) {
|
2020-02-11 19:20:08 +03:00
|
|
|
mSpdyCache = WrapUnique(aSpdyPushCache);
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 04:17:53 +03:00
|
|
|
uint64_t RequestContext::GetID() { return mID; }
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::AddNonTailRequest() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
++mNonTailRequests;
|
|
|
|
LOG(("RequestContext::AddNonTailRequest this=%p, cnt=%u", this,
|
|
|
|
mNonTailRequests));
|
|
|
|
|
|
|
|
ScheduleUnblock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::RemoveNonTailRequest() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mNonTailRequests > 0);
|
|
|
|
|
|
|
|
LOG(("RequestContext::RemoveNonTailRequest this=%p, cnt=%u", this,
|
|
|
|
mNonTailRequests - 1));
|
|
|
|
|
|
|
|
--mNonTailRequests;
|
|
|
|
|
|
|
|
ScheduleUnblock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestContext::ScheduleUnblock() {
|
|
|
|
MOZ_ASSERT(!IsNeckoChild());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gHttpHandler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t quantum =
|
|
|
|
gHttpHandler->TailBlockingDelayQuantum(mAfterDOMContentLoaded);
|
|
|
|
uint32_t delayMax = gHttpHandler->TailBlockingDelayMax();
|
2018-01-08 07:01:00 +03:00
|
|
|
uint32_t totalMax = gHttpHandler->TailBlockingTotalMax();
|
|
|
|
|
|
|
|
if (!mBeginLoadTime.IsNull()) {
|
|
|
|
// We decrease the maximum delay progressively with the time since the page
|
|
|
|
// load begin. This seems like a reasonable and clear heuristic allowing us
|
|
|
|
// to start loading tailed requests in a deterministic time after the load
|
|
|
|
// has started.
|
|
|
|
|
|
|
|
uint32_t sinceBeginLoad = static_cast<uint32_t>(
|
|
|
|
(TimeStamp::NowLoRes() - mBeginLoadTime).ToMilliseconds());
|
|
|
|
uint32_t tillTotal = totalMax - std::min(sinceBeginLoad, totalMax);
|
|
|
|
uint32_t proportion = totalMax // values clamped between 0 and 60'000
|
|
|
|
? (delayMax * tillTotal) / totalMax
|
|
|
|
: 0;
|
|
|
|
delayMax = std::min(delayMax, proportion);
|
|
|
|
}
|
2017-08-30 16:32:00 +03:00
|
|
|
|
|
|
|
CheckedInt<uint32_t> delay = quantum * mNonTailRequests;
|
|
|
|
|
|
|
|
if (!mAfterDOMContentLoaded) {
|
|
|
|
// Before DOMContentLoaded notification we want to make sure that tailed
|
|
|
|
// requests don't start when there is a short delay during which we may
|
|
|
|
// not have any active requests on the page happening.
|
|
|
|
delay += quantum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!delay.isValid() || delay.value() > delayMax) {
|
|
|
|
delay = delayMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("RequestContext::ScheduleUnblock this=%p non-tails=%u tail-queue=%zu "
|
|
|
|
"delay=%u after-DCL=%d",
|
|
|
|
this, mNonTailRequests, mTailQueue.Length(), delay.value(),
|
|
|
|
mAfterDOMContentLoaded));
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
mUntailAt = now + TimeDuration::FromMilliseconds(delay.value());
|
|
|
|
|
|
|
|
if (mTimerScheduledAt.IsNull() || mUntailAt < mTimerScheduledAt) {
|
|
|
|
LOG(("RequestContext %p timer would fire too late, rescheduling", this));
|
|
|
|
RescheduleUntailTimer(now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestContext::RescheduleUntailTimer(TimeStamp const& now) {
|
|
|
|
MOZ_ASSERT(mUntailAt >= now);
|
|
|
|
|
|
|
|
if (mUntailTimer) {
|
|
|
|
mUntailTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTailQueue.Length()) {
|
|
|
|
mUntailTimer = nullptr;
|
|
|
|
mTimerScheduledAt = TimeStamp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration interval = mUntailAt - now;
|
|
|
|
if (!mTimerScheduledAt.IsNull() && mUntailAt < mTimerScheduledAt) {
|
|
|
|
// When the number of untailed requests goes down,
|
|
|
|
// let's half the interval, since it's likely we would
|
|
|
|
// reschedule for a shorter time again very soon.
|
|
|
|
// This will likely save rescheduling this timer.
|
|
|
|
interval = interval / int64_t(2);
|
|
|
|
mTimerScheduledAt = mUntailAt - interval;
|
|
|
|
} else {
|
|
|
|
mTimerScheduledAt = mUntailAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t delay = interval.ToMilliseconds();
|
|
|
|
mUntailTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
mUntailTimer->InitWithCallback(this, delay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
LOG(("RequestContext::RescheduleUntailTimer %p in %d", this, delay));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::Notify(nsITimer* timer) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(timer == mUntailTimer);
|
|
|
|
MOZ_ASSERT(!mTimerScheduledAt.IsNull());
|
|
|
|
MOZ_ASSERT(mTailQueue.Length());
|
|
|
|
|
|
|
|
mUntailTimer = nullptr;
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
if (mUntailAt > mTimerScheduledAt && mUntailAt > now) {
|
|
|
|
LOG(("RequestContext %p timer fired too soon, rescheduling", this));
|
|
|
|
RescheduleUntailTimer(now);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must drop to allow re-engage of the timer
|
|
|
|
mTimerScheduledAt = TimeStamp();
|
|
|
|
|
|
|
|
ProcessTailQueue(NS_OK);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::IsContextTailBlocked(nsIRequestTailUnblockCallback* aRequest,
|
|
|
|
bool* aBlocked) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
LOG(("RequestContext::IsContextTailBlocked this=%p, request=%p, queued=%zu",
|
|
|
|
this, aRequest, mTailQueue.Length()));
|
|
|
|
|
|
|
|
*aBlocked = false;
|
|
|
|
|
2017-09-03 13:12:30 +03:00
|
|
|
if (sShutdown) {
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:00:12 +03:00
|
|
|
if (mUntailAt.IsNull()) {
|
2017-08-30 16:32:00 +03:00
|
|
|
LOG((" untail time passed"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAfterDOMContentLoaded && !mNonTailRequests) {
|
|
|
|
LOG((" after DOMContentLoaded and no untailed requests"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gHttpHandler) {
|
|
|
|
// Xpcshell tests may not have http handler
|
|
|
|
LOG((" missing gHttpHandler?"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aBlocked = true;
|
|
|
|
mTailQueue.AppendElement(aRequest);
|
|
|
|
|
|
|
|
LOG((" request queued"));
|
|
|
|
|
|
|
|
if (!mUntailTimer) {
|
|
|
|
ScheduleUnblock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::CancelTailedRequest(nsIRequestTailUnblockCallback* aRequest) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
bool removed = mTailQueue.RemoveElement(aRequest);
|
|
|
|
|
|
|
|
LOG(("RequestContext::CancelTailedRequest %p req=%p removed=%d", this,
|
|
|
|
aRequest, removed));
|
|
|
|
|
2017-09-08 07:05:41 +03:00
|
|
|
// Stop untail timer if all tail requests are canceled.
|
|
|
|
if (removed && mTailQueue.IsEmpty()) {
|
|
|
|
if (mUntailTimer) {
|
|
|
|
mUntailTimer->Cancel();
|
|
|
|
mUntailTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-10 13:34:00 +03:00
|
|
|
// Must drop to allow re-engage of the timer
|
|
|
|
mTimerScheduledAt = TimeStamp();
|
2017-09-08 07:05:41 +03:00
|
|
|
}
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestContext::ProcessTailQueue(nsresult aResult) {
|
|
|
|
LOG(("RequestContext::ProcessTailQueue this=%p, queued=%zu, rv=%" PRIx32,
|
|
|
|
this, mTailQueue.Length(), static_cast<uint32_t>(aResult)));
|
|
|
|
|
|
|
|
if (mUntailTimer) {
|
|
|
|
mUntailTimer->Cancel();
|
|
|
|
mUntailTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must drop to stop tailing requests
|
|
|
|
mUntailAt = TimeStamp();
|
|
|
|
|
|
|
|
nsTArray<PendingTailRequest> queue;
|
|
|
|
queue.SwapElements(mTailQueue);
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
for (const auto& request : queue) {
|
2017-08-30 16:32:00 +03:00
|
|
|
LOG((" untailing %p", request.get()));
|
|
|
|
request->OnTailUnblock(aResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContext::CancelTailPendingRequests(nsresult aResult) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(NS_FAILED(aResult));
|
|
|
|
|
|
|
|
ProcessTailQueue(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-04-21 17:59:17 +03:00
|
|
|
|
|
|
|
// nsIRequestContextService
|
|
|
|
RequestContextService* RequestContextService::sSelf = nullptr;
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
NS_IMPL_ISUPPORTS(RequestContextService, nsIRequestContextService, nsIObserver)
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContextService::RequestContextService()
|
2018-06-14 11:19:07 +03:00
|
|
|
: mRCIDNamespace(0), mNextRCID(1) {
|
2016-04-21 17:59:17 +03:00
|
|
|
MOZ_ASSERT(!sSelf, "multiple rcs instances!");
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sSelf = this;
|
2017-03-23 17:40:36 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIXULRuntime> runtime = do_GetService("@mozilla.org/xre/runtime;1");
|
|
|
|
runtime->GetProcessID(&mRCIDNamespace);
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContextService::~RequestContextService() {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
Shutdown();
|
|
|
|
sSelf = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
nsresult RequestContextService::Init() {
|
2017-08-30 16:32:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
obs->AddObserver(this, "content-document-interactive", false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
void RequestContextService::Shutdown() {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-09-03 13:12:30 +03:00
|
|
|
// We need to do this to prevent the requests from being scheduled after
|
|
|
|
// shutdown.
|
|
|
|
for (auto iter = mTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Data()->CancelTailPendingRequests(NS_ERROR_ABORT);
|
|
|
|
}
|
2015-07-31 23:50:08 +03:00
|
|
|
mTable.Clear();
|
2017-09-03 13:12:30 +03:00
|
|
|
sShutdown = true;
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:11:11 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<nsIRequestContextService>
|
2018-10-23 23:07:17 +03:00
|
|
|
RequestContextService::GetOrCreate() {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-05-21 06:38:14 +03:00
|
|
|
if (sShutdown) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:07:17 +03:00
|
|
|
RefPtr<RequestContextService> svc;
|
|
|
|
if (gSingleton) {
|
|
|
|
svc = gSingleton;
|
|
|
|
} else {
|
|
|
|
svc = new RequestContextService();
|
|
|
|
nsresult rv = svc->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
gSingleton = svc;
|
|
|
|
ClearOnShutdown(&gSingleton);
|
|
|
|
}
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2018-10-23 23:07:17 +03:00
|
|
|
return svc.forget();
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-23 17:40:36 +03:00
|
|
|
RequestContextService::GetRequestContext(const uint64_t rcID,
|
|
|
|
nsIRequestContext** rc) {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-21 17:59:17 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(rc);
|
|
|
|
*rc = nullptr;
|
2015-07-31 23:50:08 +03:00
|
|
|
|
2017-09-03 13:12:30 +03:00
|
|
|
if (sShutdown) {
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2016-04-21 17:59:17 +03:00
|
|
|
if (!mTable.Get(rcID, rc)) {
|
|
|
|
nsCOMPtr<nsIRequestContext> newSC = new RequestContext(rcID);
|
|
|
|
mTable.Put(rcID, newSC);
|
|
|
|
newSC.swap(*rc);
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestContextService::GetRequestContextFromLoadGroup(nsILoadGroup* aLoadGroup,
|
|
|
|
nsIRequestContext** rc) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
uint64_t rcID;
|
|
|
|
rv = aLoadGroup->GetRequestContextID(&rcID);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetRequestContext(rcID, rc);
|
|
|
|
}
|
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-15 07:10:10 +03:00
|
|
|
RequestContextService::NewRequestContext(nsIRequestContext** rc) {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-03-15 07:10:10 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(rc);
|
|
|
|
*rc = nullptr;
|
|
|
|
|
2017-09-03 13:12:30 +03:00
|
|
|
if (sShutdown) {
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:40:36 +03:00
|
|
|
uint64_t rcID =
|
|
|
|
((static_cast<uint64_t>(mRCIDNamespace) << 32) & 0xFFFFFFFF00000000LL) |
|
|
|
|
mNextRCID++;
|
2017-03-15 07:10:10 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRequestContext> newSC = new RequestContext(rcID);
|
|
|
|
mTable.Put(rcID, newSC);
|
|
|
|
newSC.swap(*rc);
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-23 17:40:36 +03:00
|
|
|
RequestContextService::RemoveRequestContext(const uint64_t rcID) {
|
2015-07-31 23:50:08 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-21 17:59:17 +03:00
|
|
|
mTable.Remove(rcID);
|
2015-07-31 23:50:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-04-21 17:59:17 +03:00
|
|
|
RequestContextService::Observe(nsISupports* subject, const char* topic,
|
2015-07-31 23:50:08 +03:00
|
|
|
const char16_t* data_unicode) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, topic)) {
|
|
|
|
Shutdown();
|
2017-08-30 16:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp("content-document-interactive", topic)) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<dom::Document> document(do_QueryInterface(subject));
|
2017-08-30 16:32:00 +03:00
|
|
|
MOZ_ASSERT(document);
|
|
|
|
// We want this be triggered also for iframes, since those track their
|
|
|
|
// own request context ids.
|
|
|
|
if (!document) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsIDocShell* ds = document->GetDocShell();
|
|
|
|
// XML documents don't always have a docshell assigned
|
|
|
|
if (!ds) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocumentLoader> dl(do_QueryInterface(ds));
|
|
|
|
if (!dl) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadGroup> lg;
|
|
|
|
dl->GetLoadGroup(getter_AddRefs(lg));
|
|
|
|
if (!lg) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRequestContext> rc;
|
|
|
|
GetRequestContextFromLoadGroup(lg, getter_AddRefs(rc));
|
|
|
|
if (rc) {
|
|
|
|
rc->DOMContentLoaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2017-08-30 16:32:00 +03:00
|
|
|
MOZ_ASSERT(false, "Unexpected observer topic");
|
2015-07-31 23:50:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2017-07-12 09:44:00 +03:00
|
|
|
|
|
|
|
#undef LOG
|