2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-03-18 19:46:38 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "ServiceWorkerScriptCache.h"
|
2019-12-11 09:17:44 +03:00
|
|
|
|
2021-07-13 14:52:42 +03:00
|
|
|
#include "js/Array.h" // JS::GetArrayLength
|
|
|
|
#include "js/PropertyAndElement.h" // JS_GetElement
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-18 19:52:54 +03:00
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
2015-03-18 19:46:38 +03:00
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2015-03-18 19:52:54 +03:00
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2015-06-05 07:39:34 +03:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2020-03-04 11:59:08 +03:00
|
|
|
#include "mozilla/net/CookieJarSettings.h"
|
2020-12-07 17:25:59 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2020-07-03 13:14:24 +03:00
|
|
|
#include "mozilla/StaticPrefs_extensions.h"
|
2015-10-26 05:59:48 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsIHttpChannel.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsIStreamLoader.h"
|
2015-03-18 19:52:54 +03:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2019-12-06 12:24:56 +03:00
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsIXPConnect.h"
|
2015-03-18 19:46:38 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "nsIInputStreamPump.h"
|
2015-03-18 19:46:38 +03:00
|
|
|
#include "nsIPrincipal.h"
|
2016-12-19 13:30:29 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsNetUtil.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsStringStream.h"
|
2015-03-18 19:46:38 +03:00
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
using mozilla::dom::cache::Cache;
|
2015-03-18 19:46:38 +03:00
|
|
|
using mozilla::dom::cache::CacheStorage;
|
2017-06-07 08:59:48 +03:00
|
|
|
using mozilla::ipc::PrincipalInfo;
|
2015-03-18 19:46:38 +03:00
|
|
|
|
2022-05-09 23:41:14 +03:00
|
|
|
namespace mozilla::dom::serviceWorkerScriptCache {
|
2015-03-18 19:46:38 +03:00
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
namespace {
|
|
|
|
|
2018-07-26 01:56:14 +03:00
|
|
|
already_AddRefed<CacheStorage> CreateCacheStorage(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:46:38 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
MOZ_ASSERT(xpc, "This should never be null!");
|
2018-07-26 01:56:14 +03:00
|
|
|
JS::Rooted<JSObject*> sandbox(aCx);
|
|
|
|
aRv = xpc->CreateSandbox(aCx, aPrincipal, sandbox.address());
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-03-18 19:46:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:25:49 +03:00
|
|
|
// This is called when the JSContext is not in a realm, so CreateSandbox
|
|
|
|
// returned an unwrapped global.
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(sandbox));
|
|
|
|
|
2018-07-26 01:56:14 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> sandboxGlobalObject = xpc::NativeGlobal(sandbox);
|
2015-03-18 19:46:38 +03:00
|
|
|
if (!sandboxGlobalObject) {
|
2015-03-18 19:52:54 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:40 +03:00
|
|
|
// We assume private browsing is not enabled here. The ScriptLoader
|
|
|
|
// explicitly fails for private browsing so there should never be
|
|
|
|
// a service worker running in private browsing mode. Therefore if
|
|
|
|
// we are purging scripts or running a comparison algorithm we cannot
|
2019-11-25 21:32:35 +03:00
|
|
|
// be in private browsing.
|
2015-06-28 06:19:24 +03:00
|
|
|
//
|
|
|
|
// Also, bypass the CacheStorage trusted origin checks. The ServiceWorker
|
|
|
|
// has validated the origin prior to this point. All the information
|
|
|
|
// to revalidate is not available now.
|
2015-03-18 19:52:54 +03:00
|
|
|
return CacheStorage::CreateOnMainThread(cache::CHROME_ONLY_NAMESPACE,
|
2015-06-27 05:36:40 +03:00
|
|
|
sandboxGlobalObject, aPrincipal,
|
2015-06-28 06:19:24 +03:00
|
|
|
true /* force trusted origin */, aRv);
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class CompareManager;
|
2017-06-07 09:02:27 +03:00
|
|
|
class CompareCache;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
// This class downloads a URL from the network, compare the downloaded script
|
|
|
|
// with an existing cache if provided, and report to CompareManager via calling
|
|
|
|
// ComparisonFinished().
|
2015-05-04 17:18:01 +03:00
|
|
|
class CompareNetwork final : public nsIStreamLoaderObserver,
|
|
|
|
public nsIRequestObserver {
|
2015-03-18 19:52:54 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
2015-05-04 17:18:01 +03:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
CompareNetwork(CompareManager* aManager,
|
2017-06-07 09:03:12 +03:00
|
|
|
ServiceWorkerRegistrationInfo* aRegistration,
|
|
|
|
bool aIsMainScript)
|
2015-03-18 19:52:54 +03:00
|
|
|
: mManager(aManager),
|
2017-06-07 08:59:48 +03:00
|
|
|
mRegistration(aRegistration),
|
|
|
|
mInternalHeaders(new InternalHeaders()),
|
|
|
|
mLoadFlags(nsIChannel::LOAD_BYPASS_SERVICE_WORKER),
|
2017-06-07 09:02:27 +03:00
|
|
|
mState(WaitingForInitialization),
|
|
|
|
mNetworkResult(NS_OK),
|
|
|
|
mCacheResult(NS_OK),
|
2017-10-24 16:24:53 +03:00
|
|
|
mIsMainScript(aIsMainScript),
|
|
|
|
mIsFromCache(false) {
|
2015-03-18 19:52:54 +03:00
|
|
|
MOZ_ASSERT(aManager);
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-09 17:41:32 +03:00
|
|
|
}
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
nsresult Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL,
|
2019-02-15 07:56:12 +03:00
|
|
|
Cache* const aCache);
|
2017-05-09 08:08:06 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
void Abort();
|
2017-06-07 08:59:48 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
void NetworkFinish(nsresult aRv);
|
|
|
|
|
|
|
|
void CacheFinish(nsresult aRv);
|
2017-04-13 10:01:28 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
const nsString& URL() const {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 09:03:12 +03:00
|
|
|
return mURL;
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
const nsString& Buffer() const {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-11 16:26:18 +03:00
|
|
|
return mBuffer;
|
2017-05-03 20:38:52 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
const ChannelInfo& GetChannelInfo() const { return mChannelInfo; }
|
|
|
|
|
|
|
|
already_AddRefed<InternalHeaders> GetInternalHeaders() const {
|
|
|
|
RefPtr<InternalHeaders> internalHeaders = mInternalHeaders;
|
|
|
|
return internalHeaders.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<PrincipalInfo> TakePrincipalInfo() {
|
2018-05-30 22:15:35 +03:00
|
|
|
return std::move(mPrincipalInfo);
|
2017-06-07 08:59:48 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 09:16:21 +03:00
|
|
|
bool Succeeded() const { return NS_SUCCEEDED(mNetworkResult); }
|
|
|
|
|
2017-12-14 21:27:11 +03:00
|
|
|
const nsTArray<nsCString>& URLList() const { return mURLList; }
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
private:
|
|
|
|
~CompareNetwork() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 09:02:27 +03:00
|
|
|
MOZ_ASSERT(!mCC);
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
void Finish();
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
nsresult SetPrincipalInfo(nsIChannel* aChannel);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CompareManager> mManager;
|
2017-06-07 09:02:27 +03:00
|
|
|
RefPtr<CompareCache> mCC;
|
2017-06-07 08:59:48 +03:00
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
nsString mBuffer;
|
2017-06-07 08:59:48 +03:00
|
|
|
nsString mURL;
|
|
|
|
ChannelInfo mChannelInfo;
|
|
|
|
RefPtr<InternalHeaders> mInternalHeaders;
|
|
|
|
UniquePtr<PrincipalInfo> mPrincipalInfo;
|
2017-12-14 21:27:11 +03:00
|
|
|
nsTArray<nsCString> mURLList;
|
2017-06-07 08:59:48 +03:00
|
|
|
|
|
|
|
nsCString mMaxScope;
|
|
|
|
nsLoadFlags mLoadFlags;
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
WaitingForInitialization,
|
|
|
|
WaitingForBothFinished,
|
|
|
|
WaitingForNetworkFinished,
|
|
|
|
WaitingForCacheFinished,
|
|
|
|
Finished
|
|
|
|
} mState;
|
|
|
|
|
|
|
|
nsresult mNetworkResult;
|
|
|
|
nsresult mCacheResult;
|
2017-10-24 16:24:53 +03:00
|
|
|
|
|
|
|
const bool mIsMainScript;
|
|
|
|
bool mIsFromCache;
|
2015-03-18 19:52:54 +03:00
|
|
|
};
|
|
|
|
|
2015-05-04 17:18:01 +03:00
|
|
|
NS_IMPL_ISUPPORTS(CompareNetwork, nsIStreamLoaderObserver, nsIRequestObserver)
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
// This class gets a cached Response from the CacheStorage and then it calls
|
2017-06-07 09:02:27 +03:00
|
|
|
// CacheFinish() in the CompareNetwork.
|
2015-03-18 19:52:54 +03:00
|
|
|
class CompareCache final : public PromiseNativeHandler,
|
2015-04-14 07:05:28 +03:00
|
|
|
public nsIStreamLoaderObserver {
|
2015-03-18 19:52:54 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
explicit CompareCache(CompareNetwork* aCN)
|
2017-06-07 08:56:59 +03:00
|
|
|
: mCN(aCN), mState(WaitingForInitialization), mInCache(false) {
|
2017-06-07 09:02:27 +03:00
|
|
|
MOZ_ASSERT(aCN);
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-09 17:41:32 +03:00
|
|
|
}
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
nsresult Initialize(Cache* const aCache, const nsAString& aURL);
|
2017-05-11 16:26:18 +03:00
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
void Finish(nsresult aStatus, bool aInCache);
|
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void Abort();
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
virtual void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
virtual void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
const nsString& Buffer() const {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:52:54 +03:00
|
|
|
return mBuffer;
|
|
|
|
}
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
bool InCache() { return mInCache; }
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
private:
|
2018-01-27 00:08:59 +03:00
|
|
|
~CompareCache() { MOZ_ASSERT(NS_IsMainThread()); }
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
void ManageValueResult(JSContext* aCx, JS::Handle<JS::Value> aValue);
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
RefPtr<CompareNetwork> mCN;
|
2015-03-18 19:52:54 +03:00
|
|
|
nsCOMPtr<nsIInputStreamPump> mPump;
|
|
|
|
|
|
|
|
nsString mURL;
|
|
|
|
nsString mBuffer;
|
|
|
|
|
|
|
|
enum {
|
2017-06-07 08:56:59 +03:00
|
|
|
WaitingForInitialization,
|
|
|
|
WaitingForScript,
|
|
|
|
Finished,
|
2015-03-18 19:52:54 +03:00
|
|
|
} mState;
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
bool mInCache;
|
2015-03-18 19:52:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CompareCache, nsIStreamLoaderObserver)
|
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
class CompareManager final : public PromiseNativeHandler {
|
2015-03-18 19:52:54 +03:00
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
explicit CompareManager(ServiceWorkerRegistrationInfo* aRegistration,
|
|
|
|
CompareCallback* aCallback)
|
2015-10-26 05:59:48 +03:00
|
|
|
: mRegistration(aRegistration),
|
|
|
|
mCallback(aCallback),
|
2017-06-07 08:59:48 +03:00
|
|
|
mLoadFlags(nsIChannel::LOAD_BYPASS_SERVICE_WORKER),
|
2017-06-07 08:56:59 +03:00
|
|
|
mState(WaitingForInitialization),
|
2017-06-07 09:03:12 +03:00
|
|
|
mPendingCount(0),
|
2018-04-02 17:50:22 +03:00
|
|
|
mOnFailure(OnFailure::DoNothing),
|
2017-06-07 09:03:12 +03:00
|
|
|
mAreScriptsEqual(true) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-26 05:59:48 +03:00
|
|
|
MOZ_ASSERT(aRegistration);
|
2017-05-11 16:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL,
|
2019-02-15 07:56:12 +03:00
|
|
|
const nsAString& aCacheName);
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override;
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
CacheStorage* CacheStorage_() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-11 16:26:18 +03:00
|
|
|
MOZ_ASSERT(mCacheStorage);
|
|
|
|
return mCacheStorage;
|
|
|
|
}
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
void ComparisonFinished(nsresult aStatus, bool aIsMainScript, bool aIsEqual,
|
|
|
|
const nsACString& aMaxScope, nsLoadFlags aLoadFlags) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 09:01:50 +03:00
|
|
|
if (mState == Finished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForScriptOrComparisonResult);
|
2017-06-07 09:01:50 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aStatus))) {
|
|
|
|
Fail(aStatus);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
mAreScriptsEqual = mAreScriptsEqual && aIsEqual;
|
|
|
|
|
|
|
|
if (aIsMainScript) {
|
|
|
|
mMaxScope = aMaxScope;
|
|
|
|
mLoadFlags = aLoadFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether all CompareNetworks finished their jobs.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingCount > 0);
|
|
|
|
if (--mPendingCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAreScriptsEqual) {
|
2017-06-07 09:01:50 +03:00
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
mCallback->ComparisonResult(aStatus, true /* aSameScripts */, mOnFailure,
|
2020-09-23 18:17:15 +03:00
|
|
|
u""_ns, mMaxScope, mLoadFlags);
|
2017-06-07 09:01:50 +03:00
|
|
|
Cleanup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to Cache so ScriptLoader reads succeed.
|
|
|
|
WriteNetworkBufferToNewCache();
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
private:
|
|
|
|
~CompareManager() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_ASSERT(mCNList.Length() == 0);
|
2017-05-11 16:26:18 +03:00
|
|
|
}
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void Fail(nsresult aStatus);
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void Cleanup();
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
nsresult FetchScript(const nsAString& aURL, bool aIsMainScript,
|
|
|
|
Cache* const aCache = nullptr) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForInitialization ||
|
|
|
|
mState == WaitingForScriptOrComparisonResult);
|
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
RefPtr<CompareNetwork> cn =
|
|
|
|
new CompareNetwork(this, mRegistration, aIsMainScript);
|
|
|
|
mCNList.AppendElement(cn);
|
|
|
|
mPendingCount += 1;
|
|
|
|
|
2019-02-15 07:56:12 +03:00
|
|
|
nsresult rv = cn->Initialize(mPrincipal, aURL, aCache);
|
2017-06-07 08:56:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-06-07 09:03:12 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2017-06-07 08:56:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ManageOldCache(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForExistingOpen);
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
// RAII Cleanup when fails.
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
auto guard = MakeScopeExit([&] { Fail(rv); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!aValue.isObject())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mOldCache);
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
if (NS_WARN_IF(!obj) ||
|
|
|
|
NS_WARN_IF(NS_FAILED(UNWRAP_OBJECT(Cache, obj, mOldCache)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<RequestOrUSVString> request;
|
|
|
|
CacheQueryOptions options;
|
|
|
|
ErrorResult error;
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<Promise> promise = mOldCache->Keys(aCx, request, options, error);
|
2017-06-07 08:56:59 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2017-08-11 04:04:55 +03:00
|
|
|
// No exception here because there are no ReadableStreams involved here.
|
|
|
|
MOZ_ASSERT(!error.IsJSException());
|
2017-06-07 08:56:59 +03:00
|
|
|
rv = error.StealNSResult();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = WaitingForExistingKeys;
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
guard.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManageOldKeys(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForExistingKeys);
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
// RAII Cleanup when fails.
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
auto guard = MakeScopeExit([&] { Fail(rv); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!aValue.isObject())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
if (NS_WARN_IF(!obj)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t len = 0;
|
2019-12-11 09:17:44 +03:00
|
|
|
if (!JS::GetArrayLength(aCx, obj, &len)) {
|
2017-06-07 08:56:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch and compare the source scripts.
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_ASSERT(mPendingCount == 0);
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
mState = WaitingForScriptOrComparisonResult;
|
|
|
|
|
2018-04-02 17:50:22 +03:00
|
|
|
bool hasMainScript = false;
|
|
|
|
AutoTArray<nsString, 8> urlList;
|
2017-12-14 21:27:11 +03:00
|
|
|
|
2018-04-02 17:50:22 +03:00
|
|
|
// Extract the list of URLs in the old cache.
|
2017-06-07 08:56:59 +03:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
if (NS_WARN_IF(!JS_GetElement(aCx, obj, i, &val)) ||
|
|
|
|
NS_WARN_IF(!val.isObject())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Request* request;
|
|
|
|
JS::Rooted<JSObject*> requestObj(aCx, &val.toObject());
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(UNWRAP_OBJECT(Request, &requestObj, request)))) {
|
2017-06-07 08:56:59 +03:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2018-04-02 17:50:22 +03:00
|
|
|
nsString url;
|
|
|
|
request->GetUrl(url);
|
|
|
|
|
|
|
|
if (!hasMainScript && url == mURL) {
|
|
|
|
hasMainScript = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
urlList.AppendElement(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the main script is missing, then something has gone wrong. We
|
|
|
|
// will try to continue with the update process to trigger a new
|
|
|
|
// installation. If that fails, however, then uninstall the registration
|
|
|
|
// because it is broken in a way that cannot be fixed.
|
|
|
|
if (!hasMainScript) {
|
|
|
|
mOnFailure = OnFailure::Uninstall;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always make sure to fetch the main script. If the old cache has
|
|
|
|
// no entries or the main script entry is missing, then the loop below
|
|
|
|
// may not trigger it. This should not really happen, but we handle it
|
|
|
|
// gracefully if it does occur. Its possible the bad cache state is due
|
|
|
|
// to a crash or shutdown during an update, etc.
|
|
|
|
rv = FetchScript(mURL, true /* aIsMainScript */, mOldCache);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-07 08:56:59 +03:00
|
|
|
|
2018-04-02 17:50:22 +03:00
|
|
|
for (const auto& url : urlList) {
|
2017-12-14 21:27:11 +03:00
|
|
|
// We explicitly start the fetch for the main script above.
|
2018-04-02 17:50:22 +03:00
|
|
|
if (mURL == url) {
|
2017-12-14 21:27:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-02 17:50:22 +03:00
|
|
|
rv = FetchScript(url, false /* aIsMainScript */, mOldCache);
|
2017-06-07 09:03:12 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
2017-06-07 08:56:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
guard.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManageNewCache(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForOpen);
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
// RAII Cleanup when fails.
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
auto guard = MakeScopeExit([&] { Fail(rv); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!aValue.isObject())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
if (NS_WARN_IF(!obj)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache* cache = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
rv = UNWRAP_OBJECT(Cache, &obj, cache);
|
2017-06-07 08:56:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just to be safe.
|
|
|
|
RefPtr<Cache> kungfuDeathGrip = cache;
|
2017-06-07 09:03:12 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mPendingCount == 0);
|
|
|
|
for (uint32_t i = 0; i < mCNList.Length(); ++i) {
|
|
|
|
// We bail out immediately when something goes wrong.
|
2017-08-11 04:04:55 +03:00
|
|
|
rv = WriteToCache(aCx, cache, mCNList[i]);
|
2017-06-07 09:03:12 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = WaitingForPut;
|
2017-06-07 08:56:59 +03:00
|
|
|
guard.release();
|
|
|
|
}
|
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
void WriteNetworkBufferToNewCache() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_ASSERT(mCNList.Length() != 0);
|
2015-04-14 07:05:28 +03:00
|
|
|
MOZ_ASSERT(mCacheStorage);
|
|
|
|
MOZ_ASSERT(mNewCacheName.IsEmpty());
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
result = serviceWorkerScriptCache::GenerateCacheName(mNewCacheName);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
2017-05-11 16:26:18 +03:00
|
|
|
Fail(result.StealNSResult());
|
2015-04-14 07:05:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> cacheOpenPromise =
|
|
|
|
mCacheStorage->Open(mNewCacheName, result);
|
2015-04-14 07:05:28 +03:00
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
2017-05-11 16:26:18 +03:00
|
|
|
Fail(result.StealNSResult());
|
2015-04-14 07:05:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
mState = WaitingForOpen;
|
2015-04-14 07:05:28 +03:00
|
|
|
cacheOpenPromise->AppendNativeHandler(this);
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
nsresult WriteToCache(JSContext* aCx, Cache* aCache, CompareNetwork* aCN) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-04-14 07:05:28 +03:00
|
|
|
MOZ_ASSERT(aCache);
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_ASSERT(aCN);
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForOpen);
|
2017-05-03 20:38:52 +03:00
|
|
|
|
2017-06-07 09:16:21 +03:00
|
|
|
// We don't have to save any information from a failed CompareNetwork.
|
|
|
|
if (!aCN->Succeeded()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
nsCOMPtr<nsIInputStream> body;
|
2018-10-31 20:30:18 +03:00
|
|
|
nsresult rv = NS_NewCStringInputStream(
|
|
|
|
getter_AddRefs(body), NS_ConvertUTF16toUTF8(aCN->Buffer()));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-04-14 07:05:28 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 16:29:32 +03:00
|
|
|
SafeRefPtr<InternalResponse> ir =
|
|
|
|
MakeSafeRefPtr<InternalResponse>(200, "OK"_ns);
|
2017-06-07 09:03:12 +03:00
|
|
|
ir->SetBody(body, aCN->Buffer().Length());
|
2017-12-14 21:27:11 +03:00
|
|
|
ir->SetURLList(aCN->URLList());
|
2015-04-14 07:05:28 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
ir->InitChannelInfo(aCN->GetChannelInfo());
|
|
|
|
UniquePtr<PrincipalInfo> principalInfo = aCN->TakePrincipalInfo();
|
2017-06-07 08:59:48 +03:00
|
|
|
if (principalInfo) {
|
2018-05-30 22:15:35 +03:00
|
|
|
ir->SetPrincipalInfo(std::move(principalInfo));
|
2015-06-05 07:39:34 +03:00
|
|
|
}
|
2015-05-04 17:18:01 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
RefPtr<InternalHeaders> internalHeaders = aCN->GetInternalHeaders();
|
2018-02-01 22:21:14 +03:00
|
|
|
ir->Headers()->Fill(*(internalHeaders.get()), IgnoreErrors());
|
2017-02-14 18:06:39 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> response =
|
2021-11-08 16:29:32 +03:00
|
|
|
new Response(aCache->GetGlobalObject(), std::move(ir), nullptr);
|
2015-04-14 07:05:28 +03:00
|
|
|
|
|
|
|
RequestOrUSVString request;
|
2019-10-28 23:41:59 +03:00
|
|
|
request.SetAsUSVString().ShareOrDependUpon(aCN->URL());
|
2015-04-14 07:05:28 +03:00
|
|
|
|
|
|
|
// For now we have to wait until the Put Promise is fulfilled before we can
|
|
|
|
// continue since Cache does not yet support starting a read that is being
|
|
|
|
// written to.
|
2018-10-31 20:30:18 +03:00
|
|
|
ErrorResult result;
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<Promise> cachePromise = aCache->Put(aCx, request, *response, result);
|
2018-10-31 20:30:18 +03:00
|
|
|
result.WouldReportJSException();
|
2015-04-14 07:05:28 +03:00
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
2017-08-11 04:04:55 +03:00
|
|
|
// No exception here because there are no ReadableStreams involved here.
|
|
|
|
MOZ_ASSERT(!result.IsJSException());
|
2015-04-14 07:05:28 +03:00
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
2017-06-07 09:03:12 +03:00
|
|
|
return result.StealNSResult();
|
2015-04-14 07:05:28 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
mPendingCount += 1;
|
2015-04-14 07:05:28 +03:00
|
|
|
cachePromise->AppendNativeHandler(this);
|
2017-06-07 09:03:12 +03:00
|
|
|
return NS_OK;
|
2015-04-14 07:05:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CompareCallback> mCallback;
|
|
|
|
RefPtr<CacheStorage> mCacheStorage;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
nsTArray<RefPtr<CompareNetwork>> mCNList;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
nsString mURL;
|
2017-06-07 08:56:59 +03:00
|
|
|
RefPtr<nsIPrincipal> mPrincipal;
|
|
|
|
|
|
|
|
// Used for the old cache where saves the old source scripts.
|
|
|
|
RefPtr<Cache> mOldCache;
|
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
// Only used if the network script has changed and needs to be cached.
|
|
|
|
nsString mNewCacheName;
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
nsCString mMaxScope;
|
2017-06-07 08:59:48 +03:00
|
|
|
nsLoadFlags mLoadFlags;
|
2017-05-11 16:26:18 +03:00
|
|
|
|
2015-04-14 07:05:28 +03:00
|
|
|
enum {
|
2017-06-07 08:56:59 +03:00
|
|
|
WaitingForInitialization,
|
|
|
|
WaitingForExistingOpen,
|
|
|
|
WaitingForExistingKeys,
|
|
|
|
WaitingForScriptOrComparisonResult,
|
2015-04-14 07:05:28 +03:00
|
|
|
WaitingForOpen,
|
2017-06-07 08:56:59 +03:00
|
|
|
WaitingForPut,
|
|
|
|
Finished
|
2015-04-14 07:05:28 +03:00
|
|
|
} mState;
|
2017-06-07 09:03:12 +03:00
|
|
|
|
|
|
|
uint32_t mPendingCount;
|
2018-04-02 17:50:22 +03:00
|
|
|
OnFailure mOnFailure;
|
2017-06-07 09:03:12 +03:00
|
|
|
bool mAreScriptsEqual;
|
2015-03-18 19:52:54 +03:00
|
|
|
};
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(CompareManager)
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
nsresult CompareNetwork::Initialize(nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
|
|
|
Cache* const aCache) {
|
2017-05-09 17:41:32 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-04-13 10:01:28 +03:00
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2019-07-15 16:39:51 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
|
2015-10-26 05:59:48 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-11 16:26:18 +03:00
|
|
|
return rv;
|
2015-10-26 05:59:48 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
mURL = aURL;
|
2017-12-14 21:27:11 +03:00
|
|
|
mURLList.AppendElement(NS_ConvertUTF16toUTF8(mURL));
|
2017-06-07 08:59:48 +03:00
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = NS_NewLoadGroup(getter_AddRefs(loadGroup), aPrincipal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-11 16:26:18 +03:00
|
|
|
return rv;
|
2015-10-26 05:59:48 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
// Update LoadFlags for propagating to ServiceWorkerInfo.
|
2017-08-16 09:18:52 +03:00
|
|
|
mLoadFlags = nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
|
|
|
|
|
|
|
|
ServiceWorkerUpdateViaCache uvc = mRegistration->GetUpdateViaCache();
|
|
|
|
if (uvc == ServiceWorkerUpdateViaCache::None ||
|
|
|
|
(uvc == ServiceWorkerUpdateViaCache::Imports && mIsMainScript)) {
|
|
|
|
mLoadFlags |= nsIRequest::VALIDATE_ALWAYS;
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
if (mRegistration->IsLastUpdateCheckTimeOverOneDay()) {
|
|
|
|
mLoadFlags |= nsIRequest::LOAD_BYPASS_CACHE;
|
2017-05-09 17:41:32 +03:00
|
|
|
}
|
2017-01-04 12:08:58 +03:00
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
// Different settings are needed for fetching imported scripts, since they
|
|
|
|
// might be cross-origin scripts.
|
2020-07-15 14:20:45 +03:00
|
|
|
uint32_t secFlags =
|
|
|
|
mIsMainScript ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
|
|
|
|
: nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT;
|
2017-06-07 09:03:12 +03:00
|
|
|
|
|
|
|
nsContentPolicyType contentPolicyType =
|
|
|
|
mIsMainScript ? nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS;
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
// Create a new cookieJarSettings.
|
|
|
|
nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
|
2021-03-29 14:01:52 +03:00
|
|
|
mozilla::net::CookieJarSettings::Create(aPrincipal);
|
2019-03-08 12:04:11 +03:00
|
|
|
|
2021-11-25 16:11:35 +03:00
|
|
|
// Populate the partitionKey by using the given prinicpal. The ServiceWorkers
|
|
|
|
// are using the foreign partitioned principal, so we can get the partitionKey
|
|
|
|
// from it and the partitionKey will only exist if it's in the third-party
|
|
|
|
// context. In first-party context, we can still use the uri to set the
|
|
|
|
// partitionKey.
|
|
|
|
if (!aPrincipal->OriginAttributesRef().mPartitionKey.IsEmpty()) {
|
|
|
|
net::CookieJarSettings::Cast(cookieJarSettings)
|
|
|
|
->SetPartitionKey(aPrincipal->OriginAttributesRef().mPartitionKey);
|
|
|
|
} else {
|
|
|
|
net::CookieJarSettings::Cast(cookieJarSettings)->SetPartitionKey(uri);
|
|
|
|
}
|
2020-06-10 13:05:24 +03:00
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
// Note that because there is no "serviceworker" RequestContext type, we can
|
|
|
|
// use the TYPE_INTERNAL_SCRIPT content policy types when loading a service
|
|
|
|
// worker.
|
2017-06-07 09:03:12 +03:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel), uri, aPrincipal, secFlags,
|
2020-03-04 11:59:08 +03:00
|
|
|
contentPolicyType, cookieJarSettings,
|
2019-03-08 12:04:11 +03:00
|
|
|
nullptr /* aPerformanceStorage */, loadGroup,
|
|
|
|
nullptr /* aCallbacks */, mLoadFlags);
|
2015-10-26 05:59:48 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-11 16:26:18 +03:00
|
|
|
return rv;
|
2015-10-26 05:59:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
|
|
|
|
if (httpChannel) {
|
2017-09-18 16:36:16 +03:00
|
|
|
// Spec says no redirects allowed for top-level SW scripts.
|
|
|
|
if (mIsMainScript) {
|
|
|
|
rv = httpChannel->SetRedirectionLimit(0);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
2015-10-26 05:59:48 +03:00
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
rv = httpChannel->SetRequestHeader("Service-Worker"_ns, "script"_ns,
|
|
|
|
/* merge */ false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2015-10-26 05:59:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this, this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-11 16:26:18 +03:00
|
|
|
return rv;
|
2015-10-26 05:59:48 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = mChannel->AsyncOpen(loader);
|
2015-10-26 05:59:48 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-11 16:26:18 +03:00
|
|
|
return rv;
|
2017-05-09 08:08:06 +03:00
|
|
|
}
|
2017-05-09 17:41:32 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
// If we do have an existing cache to compare with.
|
|
|
|
if (aCache) {
|
|
|
|
mCC = new CompareCache(this);
|
|
|
|
rv = mCC->Initialize(aCache, aURL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Abort();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = WaitingForBothFinished;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = WaitingForNetworkFinished;
|
2017-05-11 16:26:18 +03:00
|
|
|
return NS_OK;
|
2017-02-23 13:34:16 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
void CompareNetwork::Finish() {
|
|
|
|
if (mState == Finished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool same = true;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// mNetworkResult is prior to mCacheResult, since it's needed for reporting
|
2017-12-14 21:27:11 +03:00
|
|
|
// various errors to web content.
|
2017-06-07 09:02:27 +03:00
|
|
|
if (NS_FAILED(mNetworkResult)) {
|
2017-06-07 09:16:21 +03:00
|
|
|
// An imported script could become offline, since it might no longer be
|
|
|
|
// needed by the new importing script. In that case, the importing script
|
|
|
|
// must be different, and thus, it's okay to report same script found here.
|
|
|
|
rv = mIsMainScript ? mNetworkResult : NS_OK;
|
|
|
|
same = true;
|
2017-06-07 09:02:27 +03:00
|
|
|
} else if (mCC && NS_FAILED(mCacheResult)) {
|
|
|
|
rv = mCacheResult;
|
|
|
|
} else { // Both passed.
|
|
|
|
same = mCC && mCC->InCache() && mCC->Buffer().Equals(mBuffer);
|
|
|
|
}
|
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
mManager->ComparisonFinished(rv, mIsMainScript, same, mMaxScope, mLoadFlags);
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
// We have done with the CompareCache.
|
|
|
|
mCC = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareNetwork::NetworkFinish(nsresult aRv) {
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForBothFinished ||
|
|
|
|
mState == WaitingForNetworkFinished);
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
mNetworkResult = aRv;
|
|
|
|
|
|
|
|
if (mState == WaitingForBothFinished) {
|
|
|
|
mState = WaitingForCacheFinished;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == WaitingForNetworkFinished) {
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareNetwork::CacheFinish(nsresult aRv) {
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForBothFinished ||
|
|
|
|
mState == WaitingForCacheFinished);
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
mCacheResult = aRv;
|
|
|
|
|
|
|
|
if (mState == WaitingForBothFinished) {
|
|
|
|
mState = WaitingForNetworkFinished;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == WaitingForCacheFinished) {
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
2017-06-07 08:59:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void CompareNetwork::Abort() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 06:32:59 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mState != Finished) {
|
|
|
|
mState = Finished;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mChannel);
|
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mChannel = nullptr;
|
|
|
|
|
|
|
|
if (mCC) {
|
|
|
|
mCC->Abort();
|
|
|
|
mCC = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
CompareNetwork::OnStartRequest(nsIRequest* aRequest) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-04 17:18:01 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mState == Finished) {
|
2015-05-13 23:24:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:18:01 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
2017-09-18 16:36:16 +03:00
|
|
|
MOZ_ASSERT_IF(mIsMainScript, channel == mChannel);
|
|
|
|
mChannel = channel;
|
2015-05-04 17:18:01 +03:00
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
MOZ_ASSERT(!mChannelInfo.IsInitialized());
|
|
|
|
mChannelInfo.InitFromChannel(mChannel);
|
|
|
|
|
|
|
|
nsresult rv = SetPrincipalInfo(mChannel);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInternalHeaders->FillResponseHeaders(mChannel);
|
2017-10-24 16:24:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsICacheInfoChannel> cacheChannel(do_QueryInterface(channel));
|
|
|
|
if (cacheChannel) {
|
|
|
|
cacheChannel->IsFromCache(&mIsFromCache);
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CompareNetwork::SetPrincipalInfo(nsIChannel* aChannel) {
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!ssm) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
nsresult rv = ssm->GetChannelResultPrincipal(
|
|
|
|
aChannel, getter_AddRefs(channelPrincipal));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<PrincipalInfo> principalInfo = MakeUnique<PrincipalInfo>();
|
|
|
|
rv = PrincipalToPrincipalInfo(channelPrincipal, principalInfo.get());
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-05-04 17:18:01 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mPrincipalInfo = std::move(principalInfo);
|
2015-05-04 17:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
CompareNetwork::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
|
2015-05-04 17:18:01 +03:00
|
|
|
// Nothing to do here!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CompareNetwork::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext, nsresult aStatus,
|
|
|
|
uint32_t aLen, const uint8_t* aString) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mState == Finished) {
|
2015-03-18 19:52:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
auto guard = MakeScopeExit([&] { NetworkFinish(rv); });
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(aStatus))) {
|
2017-06-07 09:02:27 +03:00
|
|
|
rv = (aStatus == NS_ERROR_REDIRECT_LOOP) ? NS_ERROR_DOM_SECURITY_ERR
|
|
|
|
: aStatus;
|
2015-03-18 19:52:54 +03:00
|
|
|
return NS_OK;
|
2015-03-18 19:46:38 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
2017-06-07 09:02:27 +03:00
|
|
|
rv = aLoader->GetRequest(getter_AddRefs(request));
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:14:24 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
MOZ_ASSERT(channel, "How come we don't have any channel?");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetOriginalURI(getter_AddRefs(uri));
|
|
|
|
bool isExtension = uri->SchemeIs("moz-extension");
|
|
|
|
|
|
|
|
if (isExtension &&
|
|
|
|
!StaticPrefs::extensions_backgroundServiceWorker_enabled_AtStartup()) {
|
|
|
|
// Return earlier with error is the worker script is a moz-extension url
|
|
|
|
// but the feature isn't enabled by prefs.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isExtension) {
|
|
|
|
// NOTE: trying to register any moz-extension use that doesn't ends
|
|
|
|
// with .js/.jsm/.mjs seems to be already completing with an error
|
|
|
|
// in aStatus and they never reach this point.
|
|
|
|
|
|
|
|
// TODO: look into avoid duplicated parts that could be shared with the HTTP
|
|
|
|
// channel scenario.
|
|
|
|
nsCOMPtr<nsIURI> channelURL;
|
|
|
|
rv = channel->GetURI(getter_AddRefs(channelURL));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString channelURLSpec;
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(channelURL->GetSpec(channelURLSpec));
|
|
|
|
|
|
|
|
// Append the final URL (which for an extension worker script is going to
|
|
|
|
// be a file or jar url).
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mURLList.IsEmpty());
|
|
|
|
if (channelURLSpec != mURLList[0]) {
|
|
|
|
mURLList.AppendElement(channelURLSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* buffer = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
rv = ScriptLoader::ConvertToUTF16(channel, aString, aLen, u"UTF-8"_ns,
|
|
|
|
nullptr, buffer, len);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer.Adopt(buffer, len);
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
2021-01-19 18:27:46 +03:00
|
|
|
|
|
|
|
// Main scripts cannot be redirected successfully, however extensions
|
|
|
|
// may successfuly redirect imported scripts to a moz-extension url
|
|
|
|
// (if listed in the web_accessible_resources manifest property).
|
|
|
|
//
|
|
|
|
// When the service worker is initially registered the imported scripts
|
|
|
|
// will be loaded from the child process (see dom/workers/ScriptLoader.cpp)
|
|
|
|
// and in that case this method will only be called for the main script.
|
|
|
|
//
|
|
|
|
// When a registered worker is loaded again (e.g. when the webpage calls
|
|
|
|
// the ServiceWorkerRegistration's update method):
|
|
|
|
//
|
|
|
|
// - both the main and imported scripts are loaded by the
|
|
|
|
// CompareManager::FetchScript
|
|
|
|
// - the update requests for the imported scripts will also be calling this
|
|
|
|
// method and we should expect scripts redirected to an extension script
|
|
|
|
// to have a null httpChannel.
|
|
|
|
//
|
|
|
|
// The request that triggers this method is:
|
|
|
|
//
|
|
|
|
// - the one that is coming from the network (which may be intercepted by
|
|
|
|
// WebRequest listeners in extensions and redirected to a web_accessible
|
|
|
|
// moz-extension url)
|
|
|
|
// - it will then be compared with a previous response that we may have
|
|
|
|
// in the cache
|
|
|
|
//
|
|
|
|
// When the next service worker update occurs, if the request (for an imported
|
|
|
|
// script) is not redirected by an extension the cache entry is invalidated
|
|
|
|
// and a network request is triggered for the import.
|
|
|
|
if (!httpChannel) {
|
|
|
|
// Redirecting a service worker main script should fail before reaching this
|
|
|
|
// method.
|
|
|
|
// If a main script is somehow redirected, the diagnostic assert will crash
|
|
|
|
// in non-release builds. Release builds will return an explicit error.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mIsMainScript,
|
|
|
|
"Unexpected ServiceWorker main script redirected");
|
|
|
|
if (mIsMainScript) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!ssm) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = ssm->GetChannelResultPrincipal(
|
|
|
|
channel, getter_AddRefs(channelPrincipal));
|
|
|
|
|
|
|
|
// An extension did redirect a non-MainScript request to a moz-extension url
|
|
|
|
// (in that case the originalURL is the resolved jar URI and so we have to
|
|
|
|
// look to the channel principal instead).
|
|
|
|
if (channelPrincipal->SchemeIs("moz-extension")) {
|
|
|
|
char16_t* buffer = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
rv = ScriptLoader::ConvertToUTF16(channel, aString, aLen, u"UTF-8"_ns,
|
|
|
|
nullptr, buffer, len);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer.Adopt(buffer, len);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make non-release and debug builds to crash if this happens and fail
|
|
|
|
// explicitly on release builds.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false,
|
|
|
|
"ServiceWorker imported script redirected to an url "
|
|
|
|
"with an unexpected scheme");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-05-06 08:14:50 +03:00
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
bool requestSucceeded;
|
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-05-06 08:14:50 +03:00
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
if (NS_WARN_IF(!requestSucceeded)) {
|
2016-07-05 23:47:10 +03:00
|
|
|
// Get the stringified numeric status code, not statusText which could be
|
|
|
|
// something misleading like OK for a 404.
|
|
|
|
uint32_t status = 0;
|
2016-12-20 06:49:32 +03:00
|
|
|
Unused << httpChannel->GetResponseStatus(
|
|
|
|
&status); // don't care if this fails, use 0.
|
2016-07-05 23:47:10 +03:00
|
|
|
nsAutoString statusAsText;
|
|
|
|
statusAsText.AppendInt(status);
|
|
|
|
|
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
2018-01-31 19:29:49 +03:00
|
|
|
mRegistration->Scope(), "ServiceWorkerRegisterNetworkError",
|
|
|
|
nsTArray<nsString>{NS_ConvertUTF8toUTF16(mRegistration->Scope()),
|
2017-06-07 08:59:48 +03:00
|
|
|
statusAsText, mURL});
|
2017-06-07 09:02:27 +03:00
|
|
|
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2016-01-25 22:03:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-08-20 02:21:25 +03:00
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
// Note: we explicitly don't check for the return value here, because the
|
|
|
|
// absence of the header is not an error condition.
|
2017-06-07 08:59:48 +03:00
|
|
|
Unused << httpChannel->GetResponseHeader("Service-Worker-Allowed"_ns,
|
|
|
|
mMaxScope);
|
2015-10-26 05:59:48 +03:00
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
// [9.2 Update]4.13, If response's cache state is not "local",
|
|
|
|
// set registration's last update check time to the current time
|
2017-10-24 16:24:53 +03:00
|
|
|
if (!mIsFromCache) {
|
2017-06-07 08:59:48 +03:00
|
|
|
mRegistration->RefreshLastUpdateCheckTime();
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
nsAutoCString mimeType;
|
2017-12-14 21:27:11 +03:00
|
|
|
rv = httpChannel->GetContentType(mimeType);
|
2016-01-25 22:03:13 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-07-06 05:24:23 +03:00
|
|
|
// We should only end up here if !mResponseHead in the channel. If headers
|
|
|
|
// were received but no content type was specified, we'll be given
|
|
|
|
// UNKNOWN_CONTENT_TYPE "application/x-unknown-content-type" and so fall
|
|
|
|
// into the next case with its better error message.
|
2017-06-07 09:02:27 +03:00
|
|
|
rv = NS_ERROR_DOM_SECURITY_ERR;
|
2017-12-14 21:27:11 +03:00
|
|
|
return rv;
|
2016-01-25 22:03:13 +03:00
|
|
|
}
|
2015-04-25 09:27:50 +03:00
|
|
|
|
2018-10-29 18:09:40 +03:00
|
|
|
if (mimeType.IsEmpty() ||
|
|
|
|
!nsContentUtils::IsJavascriptMIMEType(NS_ConvertUTF8toUTF16(mimeType))) {
|
2016-07-06 05:24:23 +03:00
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
2018-10-29 18:09:40 +03:00
|
|
|
mRegistration->Scope(), "ServiceWorkerRegisterMimeTypeError2",
|
2018-01-31 19:29:49 +03:00
|
|
|
nsTArray<nsString>{NS_ConvertUTF8toUTF16(mRegistration->Scope()),
|
2017-06-07 08:59:48 +03:00
|
|
|
NS_ConvertUTF8toUTF16(mimeType), mURL});
|
2017-06-07 09:02:27 +03:00
|
|
|
rv = NS_ERROR_DOM_SECURITY_ERR;
|
2017-12-14 21:27:11 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> channelURL;
|
|
|
|
rv = httpChannel->GetURI(getter_AddRefs(channelURL));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString channelURLSpec;
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(channelURL->GetSpec(channelURLSpec));
|
|
|
|
|
|
|
|
// Append the final URL if its different from the original
|
|
|
|
// request URL. This lets us note that a redirect occurred
|
|
|
|
// even though we don't track every redirect URL here.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mURLList.IsEmpty());
|
|
|
|
if (channelURLSpec != mURLList[0]) {
|
|
|
|
mURLList.AppendElement(channelURLSpec);
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* buffer = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
rv = ScriptLoader::ConvertToUTF16(httpChannel, aString, aLen, u"UTF-8"_ns,
|
|
|
|
nullptr, buffer, len);
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer.Adopt(buffer, len);
|
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
rv = NS_OK;
|
2015-03-18 19:52:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
nsresult CompareCache::Initialize(Cache* const aCache, const nsAString& aURL) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 08:56:59 +03:00
|
|
|
MOZ_ASSERT(aCache);
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForInitialization);
|
2015-04-14 07:05:28 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This JSContext will not end up executing JS code because here there are
|
|
|
|
// no ReadableStreams involved.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
RequestOrUSVString request;
|
2019-10-28 23:41:59 +03:00
|
|
|
request.SetAsUSVString().ShareOrDependUpon(aURL);
|
2017-06-07 08:56:59 +03:00
|
|
|
ErrorResult error;
|
|
|
|
CacheQueryOptions params;
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<Promise> promise = aCache->Match(jsapi.cx(), request, params, error);
|
2017-06-07 08:56:59 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2017-08-11 04:04:55 +03:00
|
|
|
// No exception here because there are no ReadableStreams involved here.
|
|
|
|
MOZ_ASSERT(!error.IsJSException());
|
2017-06-07 08:56:59 +03:00
|
|
|
mState = Finished;
|
|
|
|
return error.StealNSResult();
|
2017-05-09 08:07:09 +03:00
|
|
|
}
|
2017-05-09 08:08:06 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
// Retrieve the script from aCache.
|
|
|
|
mState = WaitingForScript;
|
2017-05-11 16:26:18 +03:00
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
return NS_OK;
|
2017-02-23 13:34:16 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
void CompareCache::Finish(nsresult aStatus, bool aInCache) {
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mState != Finished) {
|
|
|
|
mState = Finished;
|
|
|
|
mInCache = aInCache;
|
|
|
|
mCN->CacheFinish(aStatus);
|
|
|
|
}
|
2017-06-07 08:59:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void CompareCache::Abort() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 06:32:59 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mState != Finished) {
|
|
|
|
mState = Finished;
|
2017-05-24 06:32:59 +03:00
|
|
|
|
2017-06-07 09:02:27 +03:00
|
|
|
if (mPump) {
|
|
|
|
mPump->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mPump = nullptr;
|
|
|
|
}
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CompareCache::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
nsresult aStatus, uint32_t aLen,
|
|
|
|
const uint8_t* aString) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
if (mState == Finished) {
|
2015-03-18 19:52:54 +03:00
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aStatus))) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(aStatus, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* buffer = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
nsresult rv = ScriptLoader::ConvertToUTF16(nullptr, aString, aLen,
|
|
|
|
u"UTF-8"_ns, nullptr, buffer, len);
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer.Adopt(buffer, len);
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(NS_OK, true);
|
2015-03-18 19:52:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
void CompareCache::ResolvedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 06:32:59 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
switch (mState) {
|
|
|
|
case Finished:
|
|
|
|
return;
|
|
|
|
case WaitingForScript:
|
|
|
|
ManageValueResult(aCx, aValue);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unacceptable state.");
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
void CompareCache::RejectedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-02-23 13:34:16 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
if (mState != Finished) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(NS_ERROR_FAILURE, false);
|
2017-05-11 16:26:18 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompareCache::ManageValueResult(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
// The cache returns undefined if the object is not stored.
|
|
|
|
if (aValue.isUndefined()) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(NS_OK, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
if (NS_WARN_IF(!obj)) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(NS_ERROR_FAILURE, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Response* response = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(Response, &obj, response);
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(response->Ok());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
response->GetBody(getter_AddRefs(inputStream));
|
|
|
|
MOZ_ASSERT(inputStream);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPump);
|
2017-06-30 01:06:43 +03:00
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream.forget(),
|
|
|
|
0, /* default segsize */
|
|
|
|
0, /* default segcount */
|
|
|
|
false, /* default closeWhenDone */
|
2020-04-07 18:17:07 +03:00
|
|
|
GetMainThreadSerialEventTarget());
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:21:51 +03:00
|
|
|
rv = mPump->AsyncRead(loader);
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mPump = nullptr;
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(mPump);
|
|
|
|
if (rr) {
|
|
|
|
nsCOMPtr<nsIEventTarget> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mPump = nullptr;
|
2017-06-07 08:59:48 +03:00
|
|
|
Finish(rv, false);
|
2015-03-18 19:52:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
nsresult CompareManager::Initialize(nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
2019-02-15 07:56:12 +03:00
|
|
|
const nsAString& aCacheName) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 06:32:59 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_ASSERT(mPendingCount == 0);
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == WaitingForInitialization);
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
// RAII Cleanup when fails.
|
|
|
|
auto guard = MakeScopeExit([&] { Cleanup(); });
|
2017-05-24 06:32:59 +03:00
|
|
|
|
|
|
|
mURL = aURL;
|
2017-06-07 08:56:59 +03:00
|
|
|
mPrincipal = aPrincipal;
|
2017-05-24 06:32:59 +03:00
|
|
|
|
|
|
|
// Always create a CacheStorage since we want to write the network entry to
|
|
|
|
// the cache even if there isn't an existing one.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
ErrorResult result;
|
2018-07-26 01:56:14 +03:00
|
|
|
mCacheStorage = CreateCacheStorage(jsapi.cx(), aPrincipal, result);
|
2017-05-24 06:32:59 +03:00
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
// If there is no existing cache, proceed to fetch the script directly.
|
|
|
|
if (aCacheName.IsEmpty()) {
|
|
|
|
mState = WaitingForScriptOrComparisonResult;
|
2017-06-07 09:03:12 +03:00
|
|
|
nsresult rv = FetchScript(aURL, true /* aIsMainScript */);
|
2017-05-24 06:32:59 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
guard.release();
|
|
|
|
return NS_OK;
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
// Open the cache saving the old source scripts.
|
|
|
|
RefPtr<Promise> promise = mCacheStorage->Open(aCacheName, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = WaitingForExistingOpen;
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
|
|
|
|
guard.release();
|
2017-05-24 06:32:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
// This class manages 4 promises if needed:
|
|
|
|
// 1. Retrieve the Cache object by a given CacheName of OldCache.
|
|
|
|
// 2. Retrieve the URLs saved in OldCache.
|
|
|
|
// 3. Retrieve the Cache object of the NewCache for the newly created SW.
|
|
|
|
// 4. Put the value in the cache.
|
|
|
|
// For this reason we have mState to know what callback we are handling.
|
2017-05-24 06:32:59 +03:00
|
|
|
void CompareManager::ResolvedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 06:32:59 +03:00
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
switch (mState) {
|
|
|
|
case Finished:
|
2017-05-24 06:32:59 +03:00
|
|
|
return;
|
2017-06-07 08:56:59 +03:00
|
|
|
case WaitingForExistingOpen:
|
|
|
|
ManageOldCache(aCx, aValue);
|
2017-05-24 06:32:59 +03:00
|
|
|
return;
|
2017-06-07 08:56:59 +03:00
|
|
|
case WaitingForExistingKeys:
|
|
|
|
ManageOldKeys(aCx, aValue);
|
2017-05-24 06:32:59 +03:00
|
|
|
return;
|
2017-06-07 08:56:59 +03:00
|
|
|
case WaitingForOpen:
|
|
|
|
ManageNewCache(aCx, aValue);
|
|
|
|
return;
|
|
|
|
case WaitingForPut:
|
2017-06-07 09:03:12 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingCount > 0);
|
|
|
|
if (--mPendingCount == 0) {
|
|
|
|
mCallback->ComparisonResult(NS_OK, false /* aIsEqual */, mOnFailure,
|
|
|
|
mNewCacheName, mMaxScope, mLoadFlags);
|
|
|
|
Cleanup();
|
|
|
|
}
|
2017-06-07 08:56:59 +03:00
|
|
|
return;
|
|
|
|
default:
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false);
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareManager::RejectedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 08:56:59 +03:00
|
|
|
switch (mState) {
|
|
|
|
case Finished:
|
|
|
|
return;
|
|
|
|
case WaitingForExistingOpen:
|
|
|
|
NS_WARNING("Could not open the existing cache.");
|
|
|
|
break;
|
|
|
|
case WaitingForExistingKeys:
|
|
|
|
NS_WARNING("Could not get the existing URLs.");
|
|
|
|
break;
|
|
|
|
case WaitingForOpen:
|
|
|
|
NS_WARNING("Could not open cache.");
|
|
|
|
break;
|
|
|
|
case WaitingForPut:
|
|
|
|
NS_WARNING("Could not write to cache.");
|
|
|
|
break;
|
|
|
|
default:
|
2017-06-07 09:06:23 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false);
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
2017-06-07 08:56:59 +03:00
|
|
|
|
2017-05-24 06:32:59 +03:00
|
|
|
Fail(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareManager::Fail(nsresult aStatus) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-09-23 18:17:15 +03:00
|
|
|
mCallback->ComparisonResult(aStatus, false /* aIsEqual */, mOnFailure, u""_ns,
|
|
|
|
""_ns, mLoadFlags);
|
2017-05-24 06:32:59 +03:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareManager::Cleanup() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-07 08:56:59 +03:00
|
|
|
|
|
|
|
if (mState != Finished) {
|
|
|
|
mState = Finished;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
mCallback = nullptr;
|
|
|
|
|
2017-06-07 09:03:12 +03:00
|
|
|
// Abort and release CompareNetworks.
|
|
|
|
for (uint32_t i = 0; i < mCNList.Length(); ++i) {
|
|
|
|
mCNList[i]->Abort();
|
|
|
|
}
|
|
|
|
mCNList.Clear();
|
2017-06-07 08:56:59 +03:00
|
|
|
}
|
2017-05-24 06:32:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
nsresult PurgeCache(nsIPrincipal* aPrincipal, const nsAString& aCacheName) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-18 19:52:54 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
if (aCacheName.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
2015-03-18 19:52:54 +03:00
|
|
|
ErrorResult rv;
|
2018-07-26 01:56:14 +03:00
|
|
|
RefPtr<CacheStorage> cacheStorage =
|
|
|
|
CreateCacheStorage(jsapi.cx(), aPrincipal, rv);
|
2015-03-18 19:52:54 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2015-03-18 19:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We use the ServiceWorker scope as key for the cacheStorage.
|
2015-03-18 19:52:54 +03:00
|
|
|
RefPtr<Promise> promise = cacheStorage->Delete(aCacheName, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2015-03-18 19:46:38 +03:00
|
|
|
}
|
|
|
|
|
2022-02-25 23:11:41 +03:00
|
|
|
// Set [[PromiseIsHandled]] to ensure that if this promise gets rejected,
|
2019-04-02 22:56:27 +03:00
|
|
|
// we don't end up reporting a rejected promise to the console.
|
2022-02-25 23:11:41 +03:00
|
|
|
MOZ_ALWAYS_TRUE(promise->SetAnyPromiseIsHandled());
|
2019-04-02 22:56:27 +03:00
|
|
|
|
2015-03-18 19:46:38 +03:00
|
|
|
// We don't actually care about the result of the delete operation.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult GenerateCacheName(nsAString& aName) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidGenerator =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = uuidGenerator->GenerateUUIDInPlace(&id);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(chars);
|
2016-01-22 20:39:14 +03:00
|
|
|
|
|
|
|
// NSID_LENGTH counts the null terminator.
|
|
|
|
aName.AssignASCII(chars, NSID_LENGTH - 1);
|
2015-03-18 19:46:38 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
nsresult Compare(ServiceWorkerRegistrationInfo* aRegistration,
|
|
|
|
nsIPrincipal* aPrincipal, const nsAString& aCacheName,
|
2019-02-15 07:56:12 +03:00
|
|
|
const nsAString& aURL, CompareCallback* aCallback) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-26 05:59:48 +03:00
|
|
|
MOZ_ASSERT(aRegistration);
|
2015-03-18 19:52:54 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
MOZ_ASSERT(!aURL.IsEmpty());
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
RefPtr<CompareManager> cm = new CompareManager(aRegistration, aCallback);
|
|
|
|
|
2019-02-15 07:56:12 +03:00
|
|
|
nsresult rv = cm->Initialize(aPrincipal, aURL, aCacheName);
|
2017-05-11 16:26:18 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
2022-05-09 23:41:14 +03:00
|
|
|
} // namespace mozilla::dom::serviceWorkerScriptCache
|