diff --git a/dom/url/URLWorker.cpp b/dom/url/URLWorker.cpp index 269b4bdbe6b0..0e66489033e1 100644 --- a/dom/url/URLWorker.cpp +++ b/dom/url/URLWorker.cpp @@ -11,13 +11,8 @@ #include "WorkerPrivate.h" #include "WorkerRunnable.h" #include "WorkerScope.h" -#include "nsStandardURL.h" -#include "nsURLHelper.h" namespace mozilla { - -using net::nsStandardURL; - namespace dom { using namespace workers; @@ -510,6 +505,24 @@ private: bool mFailed; }; +already_AddRefed +FinishConstructor(JSContext* aCx, WorkerPrivate* aPrivate, + ConstructorRunnable* aRunnable, ErrorResult& aRv) +{ + aRunnable->Dispatch(Terminating, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr proxy = aRunnable->GetURLProxy(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr url = new URLWorker(aPrivate, proxy); + return url.forget(); +} + /* static */ already_AddRefed URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, const Optional& aBase, ErrorResult& aRv) @@ -517,20 +530,26 @@ URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, JSContext* cx = aGlobal.Context(); WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); - RefPtr url = new URLWorker(workerPrivate); - url->Init(aURL, aBase, aRv); + RefPtr runnable = + new ConstructorRunnable(workerPrivate, aURL, aBase); - return aRv.Failed() ? nullptr : url.forget(); + return FinishConstructor(cx, workerPrivate, runnable, aRv); } /* static */ already_AddRefed URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, const nsAString& aBase, ErrorResult& aRv) { + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + Optional base; base = &aBase; - return Constructor(aGlobal, aURL, base, aRv); + RefPtr runnable = + new ConstructorRunnable(workerPrivate, aURL, base); + + return FinishConstructor(cx, workerPrivate, runnable, aRv); } /* static */ void @@ -605,63 +624,12 @@ URLWorker::IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl, return runnable->IsValidURL(); } -URLWorker::URLWorker(WorkerPrivate* aWorkerPrivate) +URLWorker::URLWorker(WorkerPrivate* aWorkerPrivate, URLProxy* aURLProxy) : URL(nullptr) , mWorkerPrivate(aWorkerPrivate) + , mURLProxy(aURLProxy) {} -void -URLWorker::Init(const nsAString& aURL, const Optional& aBase, - ErrorResult& aRv) -{ - nsAutoCString scheme; - nsresult rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aURL), scheme); - if (NS_FAILED(rv)) { - // this may be a relative URL, check baseURL - if (!aBase.WasPassed()) { - aRv.ThrowTypeError(aURL); - return; - } - rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aBase.Value()), scheme); - if (NS_WARN_IF(NS_FAILED(rv))) { - aRv.ThrowTypeError(aURL); - return; - } - } - - if (scheme.Equals(NS_LITERAL_CSTRING("http")) || - scheme.Equals(NS_LITERAL_CSTRING("https"))) { - RefPtr baseURL; - if (aBase.WasPassed()) { - baseURL = new nsStandardURL(); - - // XXXcatalinb: SetSpec only writes a warning to the console on urls - // without a valid scheme. I can't fix that because we've come to rely - // on that behaviour in a bunch of different places. - nsresult rv = baseURL->SetSpec(NS_ConvertUTF16toUTF8(aBase.Value())); - nsAutoCString baseScheme; - baseURL->GetScheme(baseScheme); - if (NS_WARN_IF(NS_FAILED(rv)) || baseScheme.IsEmpty()) { - aRv.ThrowTypeError(aBase.Value()); - return; - } - } - mStdURL = new nsStandardURL(); - aRv = mStdURL->Init(nsIStandardURL::URLTYPE_STANDARD, -1, - NS_ConvertUTF16toUTF8(aURL), nullptr, baseURL); - return; - } - - // create url proxy - RefPtr runnable = - new ConstructorRunnable(mWorkerPrivate, aURL, aBase); - runnable->Dispatch(Terminating, aRv); - if (NS_WARN_IF(aRv.Failed())) { - return; - } - mURLProxy = runnable->GetURLProxy(aRv); -} - URLWorker::~URLWorker() { if (mURLProxy) { @@ -680,77 +648,29 @@ URLWorker::~URLWorker() void URLWorker::GetHref(nsAString& aHref, ErrorResult& aRv) const { - aHref.Truncate(); - if (mStdURL) { - nsAutoCString href; - aRv = mStdURL->GetSpec(href); - if (!aRv.Failed()) { - CopyUTF8toUTF16(href, aHref); - } - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHref, aHref, mURLProxy); + runnable->Dispatch(aRv); } void URLWorker::SetHref(const nsAString& aHref, ErrorResult& aRv) { - nsAutoCString scheme; - nsresult rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aHref), scheme); - if (NS_FAILED(rv)) { - aRv.ThrowTypeError(aHref); - return; - } + RefPtr runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHref, aHref, + mURLProxy); - if (scheme.Equals(NS_LITERAL_CSTRING("http")) || - scheme.Equals(NS_LITERAL_CSTRING("https"))) { - mStdURL = new nsStandardURL(); - aRv = mStdURL->SetSpec(NS_ConvertUTF16toUTF8(aHref)); - if (mURLProxy) { - mWorkerPrivate->AssertIsOnWorkerThread(); - - RefPtr runnable = - new TeardownURLRunnable(mURLProxy); - mURLProxy = nullptr; - - if (NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(runnable)))) { - return; - } - } - - UpdateURLSearchParams(); - return; - } - - mStdURL = nullptr; - // fallback to using a main thread url proxy - if (mURLProxy) { - RefPtr runnable = - new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHref, aHref, - mURLProxy); - - runnable->Dispatch(aRv); - if (NS_WARN_IF(aRv.Failed())) { - return; - } - - UpdateURLSearchParams(); - return; - } - - // create the proxy now - RefPtr runnable = - new ConstructorRunnable(mWorkerPrivate, aHref, Optional()); - runnable->Dispatch(Terminating, aRv); + runnable->Dispatch(aRv); if (NS_WARN_IF(aRv.Failed())) { return; } - mURLProxy = runnable->GetURLProxy(aRv); + + if (runnable->Failed()) { + aRv.ThrowTypeError(aHref); + return; + } UpdateURLSearchParams(); } @@ -758,12 +678,6 @@ URLWorker::SetHref(const nsAString& aHref, ErrorResult& aRv) void URLWorker::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const { - if (mStdURL) { - nsContentUtils::GetUTFOrigin(mStdURL, aOrigin); - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterOrigin, aOrigin, mURLProxy); @@ -774,18 +688,6 @@ URLWorker::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const void URLWorker::GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const { - aProtocol.Truncate(); - nsAutoCString protocol; - if (mStdURL) { - if (NS_SUCCEEDED(mStdURL->GetScheme(protocol))) { - CopyASCIItoUTF16(protocol, aProtocol); - aProtocol.Append(char16_t(':')); - } - - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterProtocol, aProtocol, mURLProxy); @@ -796,30 +698,6 @@ URLWorker::GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const void URLWorker::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) { - if (mStdURL) { - nsAString::const_iterator start, end; - aProtocol.BeginReading(start); - aProtocol.EndReading(end); - nsAString::const_iterator iter(start); - - FindCharInReadable(':', iter, end); - - nsresult rv = mStdURL->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))); - if (NS_WARN_IF(NS_FAILED(rv))) { - return; - } - - nsAutoCString href; - rv = mStdURL->GetSpec(href); - if (NS_WARN_IF(NS_FAILED(rv))) { - return; - } - - SetHref(NS_ConvertUTF8toUTF16(href), aRv); - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterProtocol, aProtocol, mURLProxy); @@ -829,29 +707,9 @@ URLWorker::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) MOZ_ASSERT(!runnable->Failed()); } -#define STDURL_GETTER(value, method) \ - if (mStdURL) { \ - value.Truncate(); \ - nsAutoCString tmp; \ - nsresult rv = mStdURL->method(tmp); \ - if (NS_SUCCEEDED(rv)) { \ - CopyUTF8toUTF16(tmp, value); \ - } \ - return; \ - } - -#define STDURL_SETTER(value, method) \ - if (mStdURL) { \ - aRv = mStdURL->method(NS_ConvertUTF16toUTF8(value)); \ - return; \ - } - void URLWorker::GetUsername(nsAString& aUsername, ErrorResult& aRv) const { - STDURL_GETTER(aUsername, GetUsername); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterUsername, aUsername, mURLProxy); @@ -862,9 +720,6 @@ URLWorker::GetUsername(nsAString& aUsername, ErrorResult& aRv) const void URLWorker::SetUsername(const nsAString& aUsername, ErrorResult& aRv) { - STDURL_SETTER(aUsername, SetUsername); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterUsername, aUsername, mURLProxy); @@ -880,9 +735,6 @@ URLWorker::SetUsername(const nsAString& aUsername, ErrorResult& aRv) void URLWorker::GetPassword(nsAString& aPassword, ErrorResult& aRv) const { - STDURL_GETTER(aPassword, GetPassword); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPassword, aPassword, mURLProxy); @@ -893,9 +745,6 @@ URLWorker::GetPassword(nsAString& aPassword, ErrorResult& aRv) const void URLWorker::SetPassword(const nsAString& aPassword, ErrorResult& aRv) { - STDURL_SETTER(aPassword, SetPassword); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPassword, aPassword, mURLProxy); @@ -911,9 +760,6 @@ URLWorker::SetPassword(const nsAString& aPassword, ErrorResult& aRv) void URLWorker::GetHost(nsAString& aHost, ErrorResult& aRv) const { - STDURL_GETTER(aHost, GetHostPort); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHost, aHost, mURLProxy); @@ -924,9 +770,6 @@ URLWorker::GetHost(nsAString& aHost, ErrorResult& aRv) const void URLWorker::SetHost(const nsAString& aHost, ErrorResult& aRv) { - STDURL_SETTER(aHost, SetHostPort); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHost, aHost, mURLProxy); @@ -942,13 +785,6 @@ URLWorker::SetHost(const nsAString& aHost, ErrorResult& aRv) void URLWorker::GetHostname(nsAString& aHostname, ErrorResult& aRv) const { - aHostname.Truncate(); - if (mStdURL) { - aRv = nsContentUtils::GetHostOrIPv6WithBrackets(mStdURL, aHostname); - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHostname, aHostname, mURLProxy); @@ -959,9 +795,6 @@ URLWorker::GetHostname(nsAString& aHostname, ErrorResult& aRv) const void URLWorker::SetHostname(const nsAString& aHostname, ErrorResult& aRv) { - STDURL_SETTER(aHostname, SetHost); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHostname, aHostname, mURLProxy); @@ -977,20 +810,6 @@ URLWorker::SetHostname(const nsAString& aHostname, ErrorResult& aRv) void URLWorker::GetPort(nsAString& aPort, ErrorResult& aRv) const { - aPort.Truncate(); - - if (mStdURL) { - int32_t port; - nsresult rv = mStdURL->GetPort(&port); - if (NS_SUCCEEDED(rv) && port != -1) { - nsAutoString portStr; - portStr.AppendInt(port, 10); - aPort.Assign(portStr); - } - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPort, aPort, mURLProxy); @@ -1001,24 +820,6 @@ URLWorker::GetPort(nsAString& aPort, ErrorResult& aRv) const void URLWorker::SetPort(const nsAString& aPort, ErrorResult& aRv) { - if (mStdURL) { - nsresult rv; - nsAutoString portStr(aPort); - int32_t port = -1; - - // nsIURI uses -1 as default value. - if (!portStr.IsEmpty()) { - port = portStr.ToInteger(&rv); - if (NS_FAILED(rv)) { - return; - } - } - - mStdURL->SetPort(port); - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPort, aPort, mURLProxy); @@ -1034,18 +835,6 @@ URLWorker::SetPort(const nsAString& aPort, ErrorResult& aRv) void URLWorker::GetPathname(nsAString& aPathname, ErrorResult& aRv) const { - aPathname.Truncate(); - - if (mStdURL) { - nsAutoCString file; - nsresult rv = mStdURL->GetFilePath(file); - if (NS_SUCCEEDED(rv)) { - CopyUTF8toUTF16(file, aPathname); - } - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPathname, aPathname, mURLProxy); @@ -1056,9 +845,6 @@ URLWorker::GetPathname(nsAString& aPathname, ErrorResult& aRv) const void URLWorker::SetPathname(const nsAString& aPathname, ErrorResult& aRv) { - STDURL_SETTER(aPathname, SetFilePath); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPathname, aPathname, mURLProxy); @@ -1074,20 +860,6 @@ URLWorker::SetPathname(const nsAString& aPathname, ErrorResult& aRv) void URLWorker::GetSearch(nsAString& aSearch, ErrorResult& aRv) const { - aSearch.Truncate(); - - if (mStdURL) { - nsAutoCString search; - nsresult rv; - - rv = mStdURL->GetQuery(search); - if (NS_SUCCEEDED(rv) && !search.IsEmpty()) { - CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, aSearch); - } - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterSearch, aSearch, mURLProxy); @@ -1098,18 +870,6 @@ URLWorker::GetSearch(nsAString& aSearch, ErrorResult& aRv) const void URLWorker::GetHash(nsAString& aHash, ErrorResult& aRv) const { - aHash.Truncate(); - if (mStdURL) { - nsAutoCString ref; - nsresult rv = mStdURL->GetRef(ref); - if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) { - aHash.Assign(char16_t('#')); - AppendUTF8toUTF16(ref, aHash); - } - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHash, aHash, mURLProxy); @@ -1120,9 +880,6 @@ URLWorker::GetHash(nsAString& aHash, ErrorResult& aRv) const void URLWorker::SetHash(const nsAString& aHash, ErrorResult& aRv) { - STDURL_SETTER(aHash, SetRef); - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHash, aHash, mURLProxy); @@ -1138,13 +895,6 @@ URLWorker::SetHash(const nsAString& aHash, ErrorResult& aRv) void URLWorker::SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) { - if (mStdURL) { - // URLMainThread ignores failures here. - mStdURL->SetQuery(NS_ConvertUTF16toUTF8(aSearch)); - return; - } - - MOZ_ASSERT(mURLProxy); RefPtr runnable = new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterSearch, aSearch, mURLProxy); @@ -1173,5 +923,12 @@ URLWorker::UpdateURLSearchParams() } } +URLWorker::URLProxy* +URLWorker::GetURLProxy() const +{ + mWorkerPrivate->AssertIsOnWorkerThread(); + return mURLProxy; +} + } // namespace dom } // namespace mozilla diff --git a/dom/url/URLWorker.h b/dom/url/URLWorker.h index bc5b2eeea620..f21ca45e4876 100644 --- a/dom/url/URLWorker.h +++ b/dom/url/URLWorker.h @@ -11,11 +11,6 @@ #include "URLMainThread.h" namespace mozilla { - -namespace net { -class nsStandardURL; -} - namespace dom { namespace workers { @@ -48,11 +43,7 @@ public: IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl, ErrorResult& aRv); - explicit URLWorker(workers::WorkerPrivate* aWorkerPrivate); - - void - Init(const nsAString& aURL, const Optional& aBase, - ErrorResult& aRv); + URLWorker(workers::WorkerPrivate* aWorkerPrivate, URLProxy* aURLProxy); virtual void GetHref(nsAString& aHref, ErrorResult& aRv) const override; @@ -119,12 +110,14 @@ public: virtual void SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) override; + URLProxy* + GetURLProxy() const; + private: ~URLWorker(); workers::WorkerPrivate* mWorkerPrivate; RefPtr mURLProxy; - RefPtr mStdURL; }; } // namespace dom diff --git a/dom/url/moz.build b/dom/url/moz.build index 14cfb568543e..a7d349cc3663 100644 --- a/dom/url/moz.build +++ b/dom/url/moz.build @@ -21,7 +21,6 @@ UNIFIED_SOURCES += [ LOCAL_INCLUDES += [ '../workers', - '/netwerk/base', ] MOCHITEST_MANIFESTS += ['tests/mochitest.ini'] diff --git a/netwerk/base/nsStandardURL.cpp b/netwerk/base/nsStandardURL.cpp index 62d4e28fa2af..db7e5eec4524 100644 --- a/netwerk/base/nsStandardURL.cpp +++ b/netwerk/base/nsStandardURL.cpp @@ -23,7 +23,6 @@ #include "mozilla/MemoryReporting.h" #include "mozilla/ipc/URIUtils.h" #include -#include "mozilla/SyncRunnable.h" #include "nsContentUtils.h" #include "prprf.h" #include "nsReadableUtils.h" @@ -43,9 +42,7 @@ static LazyLogModule gStandardURLLog("nsStandardURL"); #ifdef MOZ_RUST_URLPARSE #include "RustURL.h" - -// Modified on the main thread, read on both main thread and worker threads. -Atomic nsStandardURL::gRustEnabled(false); +bool nsStandardURL::gRustEnabled = false; // Fall back to CPP-parsed URLs if the Rust one doesn't match. #define MOZ_RUST_URLPARSE_FALLBACK @@ -140,16 +137,9 @@ namespace net { static NS_DEFINE_CID(kThisImplCID, NS_THIS_STANDARDURL_IMPL_CID); static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID); -// This will always be initialized and destroyed on the main thread, but -// can be safely used on other threads. nsIIDNService *nsStandardURL::gIDN = nullptr; - -// This value will only be updated on the main thread once. Worker threads -// may race when reading this values, but that's OK because in the worst -// case we will just dispatch a noop runnable to the main thread. bool nsStandardURL::gInitialized = false; - -const char nsStandardURL::gHostLimitDigits[] = { '/', '\\', '?', '#', 0 }; +char nsStandardURL::gHostLimitDigits[] = { '/', '\\', '?', '#', 0 }; // Invalid host characters // We still allow % because it is in the ID of addons. @@ -193,8 +183,6 @@ nsPrefObserver::Observe(nsISupports *subject, const char *topic, const char16_t *data) { - MOZ_ASSERT(NS_IsMainThread()); - if (!strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { nsCOMPtr prefBranch( do_QueryInterface(subject) ); if (prefBranch) { @@ -313,10 +301,8 @@ nsStandardURL::nsStandardURL(bool aSupportsFileURL, bool aTrackURL) { LOG(("Creating nsStandardURL @%p\n", this)); - // gInitialized changes value only once (false->true) on the main thread. - // It's OK to race here because in the worst case we'll just - // dispatch a noop runnable to the main thread. if (!gInitialized) { + gInitialized = true; InitGlobalObjects(); } @@ -369,21 +355,6 @@ DumpLeakedURLs::~DumpLeakedURLs() void nsStandardURL::InitGlobalObjects() { - if (!NS_IsMainThread()) { - RefPtr r = - NS_NewRunnableFunction("nsStandardURL::InitGlobalObjects", - &nsStandardURL::InitGlobalObjects); - SyncRunnable::DispatchToThread(GetMainThreadEventTarget(), r, false); - return; - } - - if (gInitialized) { - return; - } - - MOZ_ASSERT(NS_IsMainThread()); - gInitialized = true; - nsCOMPtr prefBranch( do_GetService(NS_PREFSERVICE_CONTRACTID) ); if (prefBranch) { nsCOMPtr obs( new nsPrefObserver() ); @@ -392,18 +363,11 @@ nsStandardURL::InitGlobalObjects() #endif PrefsChanged(prefBranch, nullptr); } - - nsCOMPtr serv(do_GetService(NS_IDNSERVICE_CONTRACTID)); - if (serv) { - NS_ADDREF(gIDN = serv.get()); - MOZ_ASSERT(gIDN); - } } void nsStandardURL::ShutdownGlobalObjects() { - MOZ_ASSERT(NS_IsMainThread()); NS_IF_RELEASE(gIDN); #ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN @@ -685,6 +649,13 @@ nsStandardURL::NormalizeIDN(const nsACString& host, nsCString& result) NS_ASSERTION(mHostEncoding == eEncoding_ASCII, "unexpected default encoding"); bool isASCII; + if (!gIDN) { + nsCOMPtr serv(do_GetService(NS_IDNSERVICE_CONTRACTID)); + if (serv) { + NS_ADDREF(gIDN = serv.get()); + } + } + result.Truncate(); nsresult rv = NS_ERROR_UNEXPECTED; if (gIDN) { @@ -1278,8 +1249,6 @@ nsStandardURL::WriteSegment(nsIBinaryOutputStream *stream, const URLSegment &seg /* static */ void nsStandardURL::PrefsChanged(nsIPrefBranch *prefs, const char *pref) { - MOZ_ASSERT(NS_IsMainThread()); - LOG(("nsStandardURL::PrefsChanged [pref=%s]\n", pref)); #define PREF_CHANGED(p) ((pref == nullptr) || !strcmp(pref, p)) diff --git a/netwerk/base/nsStandardURL.h b/netwerk/base/nsStandardURL.h index 9e2a1d0824c1..fbf0918ad32f 100644 --- a/netwerk/base/nsStandardURL.h +++ b/netwerk/base/nsStandardURL.h @@ -298,7 +298,7 @@ private: // global objects. don't use COMPtr as its destructor will cause a // coredump if we leak it. static nsIIDNService *gIDN; - static const char gHostLimitDigits[]; + static char gHostLimitDigits[]; static bool gInitialized; public: @@ -307,7 +307,7 @@ public: #endif #ifdef MOZ_RUST_URLPARSE - static Atomic gRustEnabled; + static bool gRustEnabled; RefPtr mRustURL; #endif }; diff --git a/netwerk/dns/nsIDNService.cpp b/netwerk/dns/nsIDNService.cpp index 9076c85db523..f53ced0664ae 100644 --- a/netwerk/dns/nsIDNService.cpp +++ b/netwerk/dns/nsIDNService.cpp @@ -63,9 +63,6 @@ NS_IMPL_ISUPPORTS(nsIDNService, nsresult nsIDNService::Init() { - MOZ_ASSERT(NS_IsMainThread()); - MutexAutoLock lock(mLock); - nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); if (prefs) prefs->GetBranch(NS_NET_PREF_IDNWHITELIST, getter_AddRefs(mIDNWhitelistPrefBranch)); @@ -86,9 +83,6 @@ NS_IMETHODIMP nsIDNService::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData) { - MOZ_ASSERT(NS_IsMainThread()); - MutexAutoLock lock(mLock); - if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { nsCOMPtr prefBranch( do_QueryInterface(aSubject) ); if (prefBranch) @@ -99,9 +93,6 @@ NS_IMETHODIMP nsIDNService::Observe(nsISupports *aSubject, void nsIDNService::prefsChanged(nsIPrefBranch *prefBranch, const char16_t *pref) { - MOZ_ASSERT(NS_IsMainThread()); - mLock.AssertCurrentThreadOwns(); - if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNBLACKLIST).Equals(pref)) { nsCOMPtr blacklist; nsresult rv = prefBranch->GetComplexValue(NS_NET_PREF_IDNBLACKLIST, @@ -140,12 +131,9 @@ void nsIDNService::prefsChanged(nsIPrefBranch *prefBranch, const char16_t *pref) } nsIDNService::nsIDNService() - : mLock("DNService pref value lock") - , mShowPunycode(false) + : mShowPunycode(false) , mIDNUseWhitelist(false) { - MOZ_ASSERT(NS_IsMainThread()); - #ifdef IDNA2008 uint32_t IDNAOptions = UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ; if (!kIDNA2008_TransitionalProcessing) { @@ -164,8 +152,6 @@ nsIDNService::nsIDNService() nsIDNService::~nsIDNService() { - MOZ_ASSERT(NS_IsMainThread()); - #ifdef IDNA2008 uidna_close(mIDNA); #else @@ -395,41 +381,8 @@ NS_IMETHODIMP nsIDNService::Normalize(const nsACString & input, return NS_OK; } -namespace { - -class MOZ_STACK_CLASS MutexSettableAutoUnlock final -{ - Mutex* mMutex; -public: - MutexSettableAutoUnlock() - : mMutex(nullptr) - { } - - void - Acquire(mozilla::Mutex& aMutex) - { - MOZ_ASSERT(!mMutex); - mMutex = &aMutex; - mMutex->Lock(); - } - - ~MutexSettableAutoUnlock() - { - if (mMutex) { - mMutex->Unlock(); - } - } -}; - -} // anonymous namespace - NS_IMETHODIMP nsIDNService::ConvertToDisplayIDN(const nsACString & input, bool * _isASCII, nsACString & _retval) { - MutexSettableAutoUnlock lock; - if (!NS_IsMainThread()) { - lock.Acquire(mLock); - } - // If host is ACE, then convert to UTF-8 if the host is in the IDN whitelist. // Else, if host is already UTF-8, then make sure it is normalized per IDN. @@ -801,10 +754,6 @@ nsresult nsIDNService::decodeACE(const nsACString& in, nsACString& out, bool nsIDNService::isInWhitelist(const nsACString &host) { - if (!NS_IsMainThread()) { - mLock.AssertCurrentThreadOwns(); - } - if (mIDNUseWhitelist && mIDNWhitelistPrefBranch) { nsAutoCString tld(host); // make sure the host is ACE for lookup and check that there are no @@ -831,10 +780,6 @@ bool nsIDNService::isInWhitelist(const nsACString &host) bool nsIDNService::isLabelSafe(const nsAString &label) { - if (!NS_IsMainThread()) { - mLock.AssertCurrentThreadOwns(); - } - if (!isOnlySafeChars(PromiseFlatString(label), mIDNBlacklist)) { return false; } @@ -975,10 +920,6 @@ static const int32_t scriptComboTable[13][9] = { bool nsIDNService::illegalScriptCombo(Script script, int32_t& savedScript) { - if (!NS_IsMainThread()) { - mLock.AssertCurrentThreadOwns(); - } - if (savedScript == -1) { savedScript = findScriptIndex(script); return false; diff --git a/netwerk/dns/nsIDNService.h b/netwerk/dns/nsIDNService.h index d0487f7c0ecd..19aa94da055b 100644 --- a/netwerk/dns/nsIDNService.h +++ b/netwerk/dns/nsIDNService.h @@ -168,25 +168,12 @@ private: idn_nameprep_t mNamePrepHandle; nsCOMPtr mNormalizer; #endif - - // We use this mutex to guard access to: - // |mIDNBlacklist|, |mShowPunycode|, |mRestrictionProfile|, - // |mIDNUseWhitelist|. - // - // These members can only be updated on the main thread and - // read on any thread. Therefore, acquiring the mutex is required - // only for threads other than the main thread. - mozilla::Mutex mLock; - - // guarded by mLock nsXPIDLString mIDNBlacklist; /** * Flag set by the pref network.IDN_show_punycode. When it is true, * IDNs containing non-ASCII characters are always displayed to the * user in punycode - * - * guarded by mLock */ bool mShowPunycode; @@ -200,11 +187,8 @@ private: eHighlyRestrictiveProfile, eModeratelyRestrictiveProfile }; - // guarded by mLock; restrictionProfile mRestrictionProfile; - // guarded by mLock; nsCOMPtr mIDNWhitelistPrefBranch; - // guarded by mLock bool mIDNUseWhitelist; };