Bug 1728760 - move the nsHttpChannel nsITimerCallback implementation to a nested subclass to avoid issues when an object implements both nsIChannel and nsIRequest, r=smaug,necko-reviewers,kershaw.

Differential Revision: https://phabricator.services.mozilla.com/D124385
This commit is contained in:
Florian Quèze 2021-09-07 08:01:18 +00:00
Родитель 998adb18ea
Коммит ba40523537
2 изменённых файлов: 34 добавлений и 16 удалений

Просмотреть файл

@ -3546,7 +3546,8 @@ nsresult nsHttpChannel::OpenCacheEntryInternal(bool isHttps) {
};
// calls nsHttpChannel::Notify after `mCacheOpenDelay` milliseconds
NS_NewTimerWithCallback(getter_AddRefs(mCacheOpenTimer), this,
auto callback = MakeRefPtr<TimerCallback>(this);
NS_NewTimerWithCallback(getter_AddRefs(mCacheOpenTimer), callback,
mCacheOpenDelay, nsITimer::TYPE_ONE_SHOT);
}
NS_ENSURE_SUCCESS(rv, rv);
@ -5351,8 +5352,6 @@ NS_INTERFACE_MAP_BEGIN(nsHttpChannel)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsICorsPreflightCallback)
NS_INTERFACE_MAP_ENTRY(nsIRaceCacheWithNetwork)
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
NS_INTERFACE_MAP_ENTRY(nsINamed)
NS_INTERFACE_MAP_ENTRY(nsIRequestTailUnblockCallback)
NS_INTERFACE_MAP_ENTRY_CONCRETE(nsHttpChannel)
NS_INTERFACE_MAP_END_INHERITING(HttpBaseChannel)
@ -8969,7 +8968,9 @@ nsresult nsHttpChannel::TriggerNetworkWithDelay(uint32_t aDelay) {
if (!mNetworkTriggerTimer) {
mNetworkTriggerTimer = NS_NewTimer();
}
mNetworkTriggerTimer->InitWithCallback(this, aDelay, nsITimer::TYPE_ONE_SHOT);
auto callback = MakeRefPtr<TimerCallback>(this);
mNetworkTriggerTimer->InitWithCallback(callback, aDelay,
nsITimer::TYPE_ONE_SHOT);
return NS_OK;
}
@ -9091,20 +9092,24 @@ nsHttpChannel::Test_triggerNetwork(int32_t aTimeout) {
return TriggerNetworkWithDelay(aTimeout);
}
nsHttpChannel::TimerCallback::TimerCallback(nsHttpChannel* aChannel)
: mChannel(aChannel) {}
NS_IMPL_ISUPPORTS(nsHttpChannel::TimerCallback, nsITimerCallback, nsINamed)
NS_IMETHODIMP
nsHttpChannel::GetName(nsACString& aName) {
nsHttpChannel::TimerCallback::GetName(nsACString& aName) {
aName.AssignLiteral("nsHttpChannel");
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::Notify(nsITimer* aTimer) {
RefPtr<nsHttpChannel> self(this);
if (aTimer == mCacheOpenTimer) {
return Test_triggerDelayedOpenCacheEntry();
nsHttpChannel::TimerCallback::Notify(nsITimer* aTimer) {
if (aTimer == mChannel->mCacheOpenTimer) {
return mChannel->Test_triggerDelayedOpenCacheEntry();
}
if (aTimer == mNetworkTriggerTimer) {
return TriggerNetwork();
if (aTimer == mChannel->mNetworkTriggerTimer) {
return mChannel->TriggerNetwork();
}
MOZ_CRASH("Unknown timer");

Просмотреть файл

@ -74,9 +74,7 @@ class nsHttpChannel final : public HttpBaseChannel,
public nsSupportsWeakReference,
public nsICorsPreflightCallback,
public nsIRaceCacheWithNetwork,
public nsIRequestTailUnblockCallback,
public nsITimerCallback,
public nsINamed {
public nsIRequestTailUnblockCallback {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIREQUESTOBSERVER
@ -93,8 +91,6 @@ class nsHttpChannel final : public HttpBaseChannel,
NS_DECL_NSIDNSLISTENER
NS_DECLARE_STATIC_IID_ACCESSOR(NS_HTTPCHANNEL_IID)
NS_DECL_NSIRACECACHEWITHNETWORK
NS_DECL_NSITIMERCALLBACK
NS_DECL_NSINAMED
NS_DECL_NSIREQUESTTAILUNBLOCKCALLBACK
// nsIHttpAuthenticableChannel. We can't use
@ -734,6 +730,23 @@ class nsHttpChannel final : public HttpBaseChannel,
// True if the channel is reading from cache.
Atomic<bool> mIsReadingFromCache{false};
// nsITimerCallback is implemented on a subclass so that the name attribute
// doesn't conflict with the name attribute of the nsIRequest interface that
// might be present on the same object (as seen from JavaScript code).
class TimerCallback final : public nsITimerCallback, public nsINamed {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSITIMERCALLBACK
NS_DECL_NSINAMED
explicit TimerCallback(nsHttpChannel* aChannel);
private:
~TimerCallback() = default;
RefPtr<nsHttpChannel> mChannel;
};
// These next members are only used in unit tests to delay the call to
// cache->AsyncOpenURI in order to race the cache with the network.
nsCOMPtr<nsITimer> mCacheOpenTimer;