Bug 1407683 - use singleton factory method for nsHttpHandler. r=mayhemer

nsHttpHandler is designed only for `getService` but we do not protect against `createInstance`.
The singleton of nsHttpHandler will be replaced by new instance created via `createInstance`.
gHttpHandler will hold a dangling pointer after the new instance is destroyed.

MozReview-Commit-ID: DQV6pmb5BUK

--HG--
extra : rebase_source : a6ab90038853e057c632efb5206cc26dcd71b897
This commit is contained in:
Shih-Chiang Chien 2017-10-12 12:13:56 +08:00
parent 2fd13fd9b0
commit 1d51b00021
3 changed files with 24 additions and 7 deletions

View File

@ -266,7 +266,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsFtpProtocolHandler, Init)
namespace mozilla {
namespace net {
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHttpNTLMAuth)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpHandler, Init)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsHttpHandler, nsHttpHandler::GetInstance)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpsHandler, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpAuthManager, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHttpChannelAuthProvider)

View File

@ -30,6 +30,7 @@
#include "nsPrintfCString.h"
#include "nsCOMPtr.h"
#include "nsNetCID.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Printf.h"
#include "mozilla/Sprintf.h"
#include "nsAsyncRedirectVerifyHelper.h"
@ -168,7 +169,20 @@ GetDeviceModelId() {
// nsHttpHandler <public>
//-----------------------------------------------------------------------------
nsHttpHandler *gHttpHandler = nullptr;
StaticRefPtr<nsHttpHandler> gHttpHandler;
/* static */ already_AddRefed<nsHttpHandler>
nsHttpHandler::GetInstance()
{
if (!gHttpHandler) {
gHttpHandler = new nsHttpHandler();
DebugOnly<nsresult> rv = gHttpHandler->Init();
MOZ_ASSERT(NS_SUCCEEDED(rv));
ClearOnShutdown(&gHttpHandler);
}
RefPtr<nsHttpHandler> httpHandler = gHttpHandler;
return httpHandler.forget();
}
nsHttpHandler::nsHttpHandler()
: mHttpVersion(NS_HTTP_VERSION_1_1)
@ -271,7 +285,7 @@ nsHttpHandler::nsHttpHandler()
mUserAgentOverride.SetIsVoid(true);
MOZ_ASSERT(!gHttpHandler, "HTTP handler already created!");
gHttpHandler = this;
nsCOMPtr<nsIXULRuntime> runtime = do_GetService("@mozilla.org/xre/runtime;1");
if (runtime) {
runtime->GetProcessID(&mProcessId);
@ -389,7 +403,6 @@ nsHttpHandler::~nsHttpHandler()
// and it'll segfault. NeckoChild will get cleaned up by process exit.
nsHttp::DestroyAtomTable();
gHttpHandler = nullptr;
}
nsresult

View File

@ -11,6 +11,7 @@
#include "nsHttpConnectionMgr.h"
#include "ASpdySession.h"
#include "mozilla/StaticPtr.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsWeakReference.h"
@ -65,9 +66,8 @@ public:
NS_DECL_NSIOBSERVER
NS_DECL_NSISPECULATIVECONNECT
nsHttpHandler();
static already_AddRefed<nsHttpHandler> GetInstance();
MOZ_MUST_USE nsresult Init();
MOZ_MUST_USE nsresult AddStandardRequestHeaders(nsHttpRequestHead *,
bool isSecure);
MOZ_MUST_USE nsresult AddConnectionHeader(nsHttpRequestHead *,
@ -403,8 +403,12 @@ public:
}
private:
nsHttpHandler();
virtual ~nsHttpHandler();
MOZ_MUST_USE nsresult Init();
//
// Useragent/prefs helper methods
//
@ -684,7 +688,7 @@ public:
MOZ_MUST_USE nsresult NewChannelId(uint64_t& channelId);
};
extern nsHttpHandler *gHttpHandler;
extern StaticRefPtr<nsHttpHandler> gHttpHandler;
//-----------------------------------------------------------------------------
// nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the