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"
|
2017-06-30 01:06:43 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
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"
|
2015-10-26 05:59:48 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
2015-03-18 19:52:54 +03:00
|
|
|
#include "nsIThreadRetargetableRequest.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-07-05 23:47:10 +03:00
|
|
|
#include "nsIScriptError.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
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-03-18 19:46:38 +03:00
|
|
|
|
|
|
|
namespace serviceWorkerScriptCache {
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
already_AddRefed<CacheStorage>
|
2018-07-26 01:56:14 +03:00
|
|
|
CreateCacheStorage(JSContext* aCx, nsIPrincipal* aPrincipal, ErrorResult& aRv)
|
2015-03-18 19:46:38 +03:00
|
|
|
{
|
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
|
|
|
|
// be in private browing.
|
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-05-11 16:26:18 +03:00
|
|
|
nsresult
|
2017-06-07 09:02:27 +03:00
|
|
|
Initialize(nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
Cache* const aCache);
|
2017-05-09 08:08:06 +03:00
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
void
|
2017-06-07 09:02:27 +03:00
|
|
|
Abort();
|
2017-06-07 08:59:48 +03:00
|
|
|
|
2017-05-09 08:08:06 +03:00
|
|
|
void
|
2017-06-07 09:02:27 +03:00
|
|
|
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
|
2017-05-03 20:38:52 +03:00
|
|
|
{
|
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)
|
|
|
|
: mCN(aCN)
|
2017-06-07 08:56:59 +03:00
|
|
|
, mState(WaitingForInitialization)
|
2017-06-07 09:02:27 +03:00
|
|
|
, mInCache(false)
|
2015-03-18 19:52:54 +03:00
|
|
|
{
|
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-05-11 16:26:18 +03:00
|
|
|
nsresult
|
2017-06-07 08:56:59 +03:00
|
|
|
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);
|
|
|
|
|
2015-03-18 19:52:54 +03:00
|
|
|
void
|
2017-05-24 06:32:59 +03:00
|
|
|
Abort();
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
virtual void
|
2017-05-24 06:32:59 +03:00
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
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:
|
|
|
|
~CompareCache()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
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)
|
2015-03-18 19:52:54 +03:00
|
|
|
{
|
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,
|
2017-05-24 06:32:59 +03:00
|
|
|
const nsAString& aCacheName, nsILoadGroup* aLoadGroup);
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
void
|
2017-05-24 06:32:59 +03:00
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
2015-03-18 19:52:54 +03:00
|
|
|
|
2017-04-07 09:44:35 +03:00
|
|
|
void
|
2017-05-24 06:32:59 +03:00
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
CacheStorage*
|
|
|
|
CacheStorage_()
|
2017-04-07 09:44:35 +03:00
|
|
|
{
|
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:01:50 +03:00
|
|
|
void
|
2017-06-07 09:03:12 +03:00
|
|
|
ComparisonFinished(nsresult aStatus,
|
|
|
|
bool aIsMainScript,
|
|
|
|
bool aIsEqual,
|
|
|
|
const nsACString& aMaxScope,
|
|
|
|
nsLoadFlags aLoadFlags)
|
2017-06-07 09:01:50 +03:00
|
|
|
{
|
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,
|
2017-06-07 09:03:12 +03:00
|
|
|
true /* aSameScripts */,
|
2018-04-02 17:50:22 +03:00
|
|
|
mOnFailure,
|
2017-06-07 09:01:50 +03:00
|
|
|
EmptyString(),
|
|
|
|
mMaxScope,
|
|
|
|
mLoadFlags);
|
|
|
|
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-11 16:26:18 +03:00
|
|
|
void
|
2017-05-24 06:32:59 +03:00
|
|
|
Fail(nsresult aStatus);
|
2017-04-07 09:44:35 +03:00
|
|
|
|
|
|
|
void
|
2017-05-24 06:32:59 +03:00
|
|
|
Cleanup();
|
2017-04-07 09:44:35 +03:00
|
|
|
|
2017-06-07 08:56:59 +03:00
|
|
|
nsresult
|
2017-06-07 09:03:12 +03:00
|
|
|
FetchScript(const nsAString& aURL,
|
|
|
|
bool aIsMainScript,
|
|
|
|
Cache* const aCache = nullptr)
|
2017-06-07 08:56:59 +03:00
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
nsresult rv = cn->Initialize(mPrincipal, aURL, mLoadGroup, 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;
|
|
|
|
if (!JS_GetArrayLength(aCx, obj, &len)) {
|
|
|
|
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-06-07 09:03:12 +03:00
|
|
|
nsresult
|
2017-08-11 04:04:55 +03:00
|
|
|
WriteToCache(JSContext* aCx, Cache* aCache, CompareNetwork* aCN)
|
2015-04-14 07:05:28 +03:00
|
|
|
{
|
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
|
|
|
ErrorResult result;
|
|
|
|
nsCOMPtr<nsIInputStream> body;
|
2016-04-12 11:33:53 +03:00
|
|
|
result = NS_NewCStringInputStream(getter_AddRefs(body),
|
2017-06-07 09:03:12 +03:00
|
|
|
NS_ConvertUTF16toUTF8(aCN->Buffer()));
|
2015-04-14 07:05:28 +03:00
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
MOZ_ASSERT(!result.IsErrorWithMessage());
|
2017-06-07 09:03:12 +03:00
|
|
|
return result.StealNSResult();
|
2015-04-14 07:05:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> ir =
|
2015-04-14 07:05:28 +03:00
|
|
|
new InternalResponse(200, NS_LITERAL_CSTRING("OK"));
|
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 =
|
|
|
|
new Response(aCache->GetGlobalObject(), ir, nullptr);
|
2015-04-14 07:05:28 +03:00
|
|
|
|
|
|
|
RequestOrUSVString request;
|
2017-06-07 09:03:12 +03:00
|
|
|
request.SetAsUSVString().Rebind(aCN->URL().Data(), aCN->URL().Length());
|
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.
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<Promise> cachePromise = aCache->Put(aCx, request, *response, result);
|
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;
|
|
|
|
RefPtr<nsILoadGroup> mLoadGroup;
|
|
|
|
|
|
|
|
// 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-05-11 16:26:18 +03:00
|
|
|
nsresult
|
2017-06-07 09:02:27 +03:00
|
|
|
CompareNetwork::Initialize(nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
Cache* const aCache)
|
2015-10-26 05:59:48 +03:00
|
|
|
{
|
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;
|
2017-05-11 16:26:18 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, nullptr);
|
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.
|
|
|
|
uint32_t secFlags =
|
|
|
|
mIsMainScript ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
|
|
|
|
: nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
|
|
|
|
|
|
|
|
nsContentPolicyType contentPolicyType =
|
|
|
|
mIsMainScript ? nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS;
|
|
|
|
|
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,
|
2018-01-24 19:17:31 +03:00
|
|
|
contentPolicyType,
|
|
|
|
nullptr, /* aPerformanceStorage */
|
|
|
|
loadGroup,
|
|
|
|
nullptr /* aCallbacks */,
|
2017-06-07 08:59:48 +03:00
|
|
|
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(NS_LITERAL_CSTRING("Service-Worker"),
|
|
|
|
NS_LITERAL_CSTRING("script"),
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
rv = mChannel->AsyncOpen2(loader);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:59:48 +03:00
|
|
|
void
|
2017-06-07 09:02:27 +03:00
|
|
|
CompareNetwork::CacheFinish(nsresult aRv)
|
2017-06-07 08:59:48 +03:00
|
|
|
{
|
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
|
|
|
|
CompareNetwork::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
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
|
|
|
|
CompareNetwork::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
2016-01-25 22:03:13 +03:00
|
|
|
MOZ_ASSERT(httpChannel, "How come we don't have an HTTP channel?");
|
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(
|
|
|
|
NS_LITERAL_CSTRING("Service-Worker-Allowed"),
|
|
|
|
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
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
if (!mimeType.LowerCaseEqualsLiteral("text/javascript") &&
|
|
|
|
!mimeType.LowerCaseEqualsLiteral("application/x-javascript") &&
|
|
|
|
!mimeType.LowerCaseEqualsLiteral("application/javascript")) {
|
2016-07-06 05:24:23 +03:00
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
2018-01-31 19:29:49 +03:00
|
|
|
mRegistration->Scope(), "ServiceWorkerRegisterMimeTypeError",
|
|
|
|
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,
|
|
|
|
NS_LITERAL_STRING("UTF-8"), 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-05-11 16:26:18 +03:00
|
|
|
nsresult
|
2017-06-07 08:56:59 +03:00
|
|
|
CompareCache::Initialize(Cache* const aCache, const nsAString& aURL)
|
2015-04-14 07:05:28 +03:00
|
|
|
{
|
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;
|
|
|
|
request.SetAsUSVString().Rebind(aURL.Data(), aURL.Length());
|
|
|
|
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,
|
|
|
|
NS_LITERAL_STRING("UTF-8"),
|
|
|
|
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, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
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-02-23 13:34:16 +03:00
|
|
|
void
|
2017-05-11 16:26:18 +03:00
|
|
|
CompareCache::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
2017-02-23 13:34:16 +03:00
|
|
|
{
|
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),
|
2017-11-22 13:18:57 +03:00
|
|
|
inputStream.forget(),
|
2017-06-30 01:06:43 +03:00
|
|
|
0, /* default segsize */
|
|
|
|
0, /* default segcount */
|
|
|
|
false, /* default closeWhenDone */
|
|
|
|
SystemGroup::EventTargetFor(TaskCategory::Other));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mPump->AsyncRead(loader, nullptr);
|
|
|
|
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,
|
|
|
|
const nsAString& aCacheName,
|
|
|
|
nsILoadGroup* aLoadGroup)
|
|
|
|
{
|
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;
|
|
|
|
mLoadGroup = aLoadGroup;
|
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, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
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 */,
|
2018-04-02 17:50:22 +03:00
|
|
|
mOnFailure,
|
2017-06-07 09:03:12 +03:00
|
|
|
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, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
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());
|
2018-04-02 17:50:22 +03:00
|
|
|
mCallback->ComparisonResult(aStatus, false /* aIsEqual */, mOnFailure,
|
2017-06-07 08:59:48 +03:00
|
|
|
EmptyString(), EmptyCString(), 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-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise =
|
2015-03-18 19:52:54 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:26:18 +03:00
|
|
|
nsresult
|
2015-10-26 05:59:48 +03:00
|
|
|
Compare(ServiceWorkerRegistrationInfo* aRegistration,
|
|
|
|
nsIPrincipal* aPrincipal, const nsAString& aCacheName,
|
2015-06-23 20:50:04 +03:00
|
|
|
const nsAString& aURL, CompareCallback* aCallback,
|
|
|
|
nsILoadGroup* aLoadGroup)
|
2015-03-18 19:52:54 +03:00
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
nsresult rv = cm->Initialize(aPrincipal, aURL, aCacheName, aLoadGroup);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-03-18 19:52:54 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace serviceWorkerScriptCache
|
2015-03-18 19:46:38 +03:00
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|