2013-11-19 01:49:53 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-19 01:49:53 +04: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 "AsmJSCache.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "js/RootingAPI.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-12-05 08:32:52 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
#include "mozilla/CondVar.h"
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
#include "mozilla/dom/asmjscache/PAsmJSCacheEntryChild.h"
|
|
|
|
#include "mozilla/dom/asmjscache/PAsmJSCacheEntryParent.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
|
|
|
#include "mozilla/dom/quota/Client.h"
|
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
|
|
|
#include "mozilla/dom/quota/QuotaObject.h"
|
|
|
|
#include "mozilla/dom/quota/UsageInfo.h"
|
2013-12-05 08:32:52 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2015-08-29 08:45:04 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIFile.h"
|
2015-08-29 08:45:04 +03:00
|
|
|
#include "nsIIPCBackgroundChildCreateCallback.h"
|
2014-03-06 00:47:10 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIRunnable.h"
|
2013-12-05 08:32:52 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "private/pprio.h"
|
2014-04-29 21:27:26 +04:00
|
|
|
#include "mozilla/Services.h"
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
#define ASMJSCACHE_METADATA_FILE_NAME "metadata"
|
|
|
|
#define ASMJSCACHE_ENTRY_FILE_NAME_BASE "module"
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
using mozilla::dom::quota::AssertIsOnIOThread;
|
2015-08-29 08:45:04 +03:00
|
|
|
using mozilla::dom::quota::DirectoryLock;
|
2013-11-19 01:49:53 +04:00
|
|
|
using mozilla::dom::quota::PersistenceType;
|
|
|
|
using mozilla::dom::quota::QuotaManager;
|
|
|
|
using mozilla::dom::quota::QuotaObject;
|
|
|
|
using mozilla::dom::quota::UsageInfo;
|
2015-08-29 08:45:04 +03:00
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
|
|
|
using mozilla::ipc::BackgroundChild;
|
2015-08-29 08:45:12 +03:00
|
|
|
using mozilla::ipc::IsOnBackgroundThread;
|
2015-08-29 08:45:04 +03:00
|
|
|
using mozilla::ipc::PBackgroundChild;
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2013-12-05 08:32:52 +04:00
|
|
|
using mozilla::HashString;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2013-12-05 08:32:52 +04:00
|
|
|
|
|
|
|
MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPRFileDesc, PRFileDesc, PR_Close);
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
namespace dom {
|
|
|
|
namespace asmjscache {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
// Anything smaller should compile fast enough that caching will just add
|
|
|
|
// overhead.
|
|
|
|
static const size_t sMinCachedModuleLength = 10000;
|
|
|
|
|
|
|
|
// The number of characters to hash into the Metadata::Entry::mFastHash.
|
|
|
|
static const unsigned sNumFastHashChars = 4096;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WriteMetadataFile(nsIFile* aMetadataFile, const Metadata& aMetadata)
|
|
|
|
{
|
|
|
|
int32_t openFlags = PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE;
|
|
|
|
|
|
|
|
JS::BuildIdCharVector buildId;
|
|
|
|
bool ok = GetBuildId(&buildId);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
ScopedPRFileDesc fd;
|
|
|
|
nsresult rv = aMetadataFile->OpenNSPRFileDesc(openFlags, 0644, &fd.rwget());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uint32_t length = buildId.length();
|
|
|
|
int32_t bytesWritten = PR_Write(fd, &length, sizeof(length));
|
|
|
|
NS_ENSURE_TRUE(bytesWritten == sizeof(length), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
bytesWritten = PR_Write(fd, buildId.begin(), length);
|
|
|
|
NS_ENSURE_TRUE(bytesWritten == int32_t(length), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
bytesWritten = PR_Write(fd, &aMetadata, sizeof(aMetadata));
|
|
|
|
NS_ENSURE_TRUE(bytesWritten == sizeof(aMetadata), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ReadMetadataFile(nsIFile* aMetadataFile, Metadata& aMetadata)
|
|
|
|
{
|
|
|
|
int32_t openFlags = PR_RDONLY;
|
|
|
|
|
|
|
|
ScopedPRFileDesc fd;
|
|
|
|
nsresult rv = aMetadataFile->OpenNSPRFileDesc(openFlags, 0644, &fd.rwget());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Read the buildid and check that it matches the current buildid
|
|
|
|
|
|
|
|
JS::BuildIdCharVector currentBuildId;
|
|
|
|
bool ok = GetBuildId(¤tBuildId);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
uint32_t length;
|
|
|
|
int32_t bytesRead = PR_Read(fd, &length, sizeof(length));
|
|
|
|
NS_ENSURE_TRUE(bytesRead == sizeof(length), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(currentBuildId.length() == length, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
JS::BuildIdCharVector fileBuildId;
|
|
|
|
ok = fileBuildId.resize(length);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
bytesRead = PR_Read(fd, fileBuildId.begin(), length);
|
|
|
|
NS_ENSURE_TRUE(bytesRead == int32_t(length), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
if (currentBuildId[i] != fileBuildId[i]) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the Metadata struct
|
|
|
|
|
|
|
|
bytesRead = PR_Read(fd, &aMetadata, sizeof(aMetadata));
|
|
|
|
NS_ENSURE_TRUE(bytesRead == sizeof(aMetadata), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-14 04:25:39 +04:00
|
|
|
nsresult
|
|
|
|
GetCacheFile(nsIFile* aDirectory, unsigned aModuleIndex, nsIFile** aCacheFile)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> cacheFile;
|
|
|
|
nsresult rv = aDirectory->Clone(getter_AddRefs(cacheFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsString cacheFileName = NS_LITERAL_STRING(ASMJSCACHE_ENTRY_FILE_NAME_BASE);
|
|
|
|
cacheFileName.AppendInt(aModuleIndex);
|
|
|
|
rv = cacheFile->Append(cacheFileName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
cacheFile.forget(aCacheFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AutoDecreaseUsageForOrigin
|
|
|
|
{
|
|
|
|
const nsACString& mGroup;
|
|
|
|
const nsACString& mOrigin;
|
|
|
|
|
|
|
|
public:
|
|
|
|
uint64_t mFreed;
|
|
|
|
|
|
|
|
AutoDecreaseUsageForOrigin(const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin)
|
|
|
|
|
|
|
|
: mGroup(aGroup),
|
|
|
|
mOrigin(aOrigin),
|
|
|
|
mFreed(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoDecreaseUsageForOrigin()
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
if (!mFreed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(qm, "We are on the QuotaManager's IO thread");
|
|
|
|
|
|
|
|
qm->DecreaseUsageForOrigin(quota::PERSISTENCE_TYPE_TEMPORARY,
|
|
|
|
mGroup, mOrigin, mFreed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
EvictEntries(nsIFile* aDirectory, const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin, uint64_t aNumBytes,
|
|
|
|
Metadata& aMetadata)
|
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
|
|
|
AutoDecreaseUsageForOrigin usage(aGroup, aOrigin);
|
|
|
|
|
|
|
|
for (int i = Metadata::kLastEntry; i >= 0 && usage.mFreed < aNumBytes; i--) {
|
|
|
|
Metadata::Entry& entry = aMetadata.mEntries[i];
|
|
|
|
unsigned moduleIndex = entry.mModuleIndex;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = GetCacheFile(aDirectory, moduleIndex, getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists;
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
int64_t fileSize;
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->Remove(false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage.mFreed += fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
// FileDescriptorHolder owns a file descriptor and its memory mapping.
|
2015-08-29 08:45:04 +03:00
|
|
|
// FileDescriptorHolder is derived by two runnable classes (that is,
|
2015-08-29 08:45:24 +03:00
|
|
|
// (Parent|Child)Runnable.
|
2016-04-26 03:23:21 +03:00
|
|
|
class FileDescriptorHolder : public Runnable
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileDescriptorHolder()
|
|
|
|
: mQuotaObject(nullptr),
|
|
|
|
mFileSize(INT64_MIN),
|
|
|
|
mFileDesc(nullptr),
|
|
|
|
mFileMap(nullptr),
|
|
|
|
mMappedMemory(nullptr)
|
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
~FileDescriptorHolder() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
// These resources should have already been released by Finish().
|
|
|
|
MOZ_ASSERT(!mQuotaObject);
|
|
|
|
MOZ_ASSERT(!mMappedMemory);
|
|
|
|
MOZ_ASSERT(!mFileMap);
|
|
|
|
MOZ_ASSERT(!mFileDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
FileSize() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mFileSize >= 0, "Accessing FileSize of unopened file");
|
|
|
|
return mFileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileDesc*
|
|
|
|
FileDesc() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mFileDesc, "Accessing FileDesc of unopened file");
|
|
|
|
return mFileDesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MapMemory(OpenMode aOpenMode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mFileMap, "Cannot call MapMemory twice");
|
|
|
|
|
|
|
|
PRFileMapProtect mapFlags = aOpenMode == eOpenForRead ? PR_PROT_READONLY
|
|
|
|
: PR_PROT_READWRITE;
|
|
|
|
|
|
|
|
mFileMap = PR_CreateFileMap(mFileDesc, mFileSize, mapFlags);
|
|
|
|
NS_ENSURE_TRUE(mFileMap, false);
|
|
|
|
|
|
|
|
mMappedMemory = PR_MemMap(mFileMap, 0, mFileSize);
|
|
|
|
NS_ENSURE_TRUE(mMappedMemory, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
MappedMemory() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMappedMemory, "Accessing MappedMemory of un-mapped file");
|
|
|
|
return mMappedMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-06-30 15:59:27 +03:00
|
|
|
// This method must be called before the directory lock is released (the lock
|
|
|
|
// is protecting these resources). It is idempotent, so it is ok to call
|
2013-11-19 01:49:53 +04:00
|
|
|
// multiple times (or before the file has been fully opened).
|
|
|
|
void
|
|
|
|
Finish()
|
|
|
|
{
|
|
|
|
if (mMappedMemory) {
|
|
|
|
PR_MemUnmap(mMappedMemory, mFileSize);
|
|
|
|
mMappedMemory = nullptr;
|
|
|
|
}
|
|
|
|
if (mFileMap) {
|
|
|
|
PR_CloseFileMap(mFileMap);
|
|
|
|
mFileMap = nullptr;
|
|
|
|
}
|
|
|
|
if (mFileDesc) {
|
|
|
|
PR_Close(mFileDesc);
|
|
|
|
mFileDesc = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Holding the QuotaObject alive until all the cache files are closed enables
|
|
|
|
// assertions in QuotaManager that the cache entry isn't cleared while we
|
|
|
|
// are working on it.
|
|
|
|
mQuotaObject = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<QuotaObject> mQuotaObject;
|
2013-11-19 01:49:53 +04:00
|
|
|
int64_t mFileSize;
|
|
|
|
PRFileDesc* mFileDesc;
|
|
|
|
PRFileMap* mFileMap;
|
|
|
|
void* mMappedMemory;
|
|
|
|
};
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// A runnable that implements a state machine required to open a cache entry.
|
|
|
|
// It executes in the parent for a cache access originating in the child.
|
|
|
|
// This runnable gets registered as an IPDL subprotocol actor so that it
|
|
|
|
// can communicate with the corresponding ChildRunnable.
|
|
|
|
class ParentRunnable final
|
2015-08-29 08:45:04 +03:00
|
|
|
: public FileDescriptorHolder
|
2015-06-30 15:59:27 +03:00
|
|
|
, public quota::OpenDirectoryListener
|
2015-08-29 08:45:12 +03:00
|
|
|
, public PAsmJSCacheEntryParent
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-08-29 08:45:12 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable(const PrincipalInfo& aPrincipalInfo,
|
|
|
|
OpenMode aOpenMode,
|
|
|
|
WriteParams aWriteParams)
|
2015-08-29 08:45:04 +03:00
|
|
|
: mOwningThread(NS_GetCurrentThread()),
|
|
|
|
mPrincipalInfo(aPrincipalInfo),
|
2013-11-19 01:49:53 +04:00
|
|
|
mOpenMode(aOpenMode),
|
2013-12-05 08:32:52 +04:00
|
|
|
mWriteParams(aWriteParams),
|
2014-03-06 00:47:10 +04:00
|
|
|
mPersistence(quota::PERSISTENCE_TYPE_INVALID),
|
2014-11-04 23:44:56 +03:00
|
|
|
mState(eInitial),
|
2014-10-23 02:28:07 +04:00
|
|
|
mResult(JS::AsmJSCache_InternalError),
|
2014-11-04 23:44:56 +03:00
|
|
|
mIsApp(false),
|
2015-08-29 08:45:12 +03:00
|
|
|
mEnforcingQuota(true),
|
|
|
|
mActorDestroyed(false),
|
|
|
|
mOpened(false)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
private:
|
2016-11-15 14:09:07 +03:00
|
|
|
~ParentRunnable() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == eFinished);
|
2015-06-30 15:59:27 +03:00
|
|
|
MOZ_ASSERT(!mDirectoryLock);
|
2015-08-29 08:45:12 +03:00
|
|
|
MOZ_ASSERT(mActorDestroyed);
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
bool
|
|
|
|
IsOnOwningThread() const
|
2015-08-29 08:45:04 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
2015-08-29 08:45:12 +03:00
|
|
|
|
|
|
|
bool current;
|
|
|
|
return NS_SUCCEEDED(mOwningThread->IsOnCurrentThread(¤t)) && current;
|
2015-08-29 08:45:04 +03:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
AssertIsOnOwningThread() const
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
2015-08-29 08:45:12 +03:00
|
|
|
MOZ_ASSERT(IsOnBackgroundThread());
|
|
|
|
MOZ_ASSERT(IsOnOwningThread());
|
|
|
|
}
|
2013-12-05 08:32:52 +04:00
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
void
|
|
|
|
AssertIsOnNonOwningThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsOnBackgroundThread());
|
|
|
|
MOZ_ASSERT(!IsOnOwningThread());
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// This method is called on the owning thread when no cache entry was found
|
|
|
|
// to open. If we just tried a lookup in persistent storage then we might
|
|
|
|
// still get a hit in temporary storage (for an asm.js module that wasn't
|
|
|
|
// compiled at install-time).
|
2014-03-06 00:47:10 +04:00
|
|
|
void
|
|
|
|
CacheMiss()
|
|
|
|
{
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-03-06 00:47:10 +04:00
|
|
|
MOZ_ASSERT(mState == eFailedToReadMetadata ||
|
|
|
|
mState == eWaitingToOpenCacheFileForRead);
|
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForRead);
|
|
|
|
|
|
|
|
if (mPersistence == quota::PERSISTENCE_TYPE_TEMPORARY) {
|
|
|
|
Fail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try again with a clean slate. InitOnMainThread will see that mPersistence
|
|
|
|
// is initialized and switch to temporary storage.
|
|
|
|
MOZ_ASSERT(mPersistence == quota::PERSISTENCE_TYPE_PERSISTENT);
|
2015-08-29 08:45:04 +03:00
|
|
|
FinishOnOwningThread();
|
2014-03-06 00:47:10 +04:00
|
|
|
mState = eInitial;
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// This method is called on the owning thread when the JS engine is finished
|
|
|
|
// reading/writing the cache entry.
|
2013-11-19 01:49:53 +04:00
|
|
|
void
|
|
|
|
Close()
|
|
|
|
{
|
2015-08-29 08:45:12 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
MOZ_ASSERT(mState == eOpened);
|
2015-08-29 08:45:12 +03:00
|
|
|
|
|
|
|
mState = eFinished;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mOpened);
|
|
|
|
|
|
|
|
FinishOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// This method is called upon any failure that prevents the eventual opening
|
|
|
|
// of the cache entry.
|
2013-11-19 01:49:53 +04:00
|
|
|
void
|
|
|
|
Fail()
|
|
|
|
{
|
2015-08-29 08:45:12 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mState != eFinished);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
mState = eFinished;
|
2013-12-05 08:32:52 +04:00
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
MOZ_ASSERT(!mOpened);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
FinishOnOwningThread();
|
2015-08-29 08:45:12 +03:00
|
|
|
|
|
|
|
if (!mActorDestroyed) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, mResult);
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// The same as method above but is intended to be called off the owning
|
|
|
|
// thread.
|
|
|
|
void
|
|
|
|
FailOnNonOwningThread()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:12 +03:00
|
|
|
AssertIsOnNonOwningThread();
|
|
|
|
MOZ_ASSERT(mState != eOpened &&
|
|
|
|
mState != eFailing &&
|
|
|
|
mState != eFinished);
|
|
|
|
|
|
|
|
mState = eFailing;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
void
|
|
|
|
InitPersistenceType();
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
nsresult
|
|
|
|
InitOnMainThread();
|
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
void
|
|
|
|
OpenDirectory();
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
nsresult
|
2013-12-05 08:32:52 +04:00
|
|
|
ReadMetadata();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenCacheFileForWrite();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenCacheFileForRead();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
void
|
2015-08-29 08:45:04 +03:00
|
|
|
FinishOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
void
|
|
|
|
DispatchToIOThread()
|
|
|
|
{
|
2015-11-22 12:44:06 +03:00
|
|
|
AssertIsOnOwningThread();
|
2015-08-29 08:45:04 +03:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
// If shutdown just started, the QuotaManager may have been deleted.
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
if (!qm) {
|
2015-08-29 08:45:12 +03:00
|
|
|
FailOnNonOwningThread();
|
2013-12-05 08:32:52 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = qm->IOThread()->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-08-29 08:45:12 +03:00
|
|
|
FailOnNonOwningThread();
|
2013-12-05 08:32:52 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
// OpenDirectoryListener overrides.
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-06-30 15:59:27 +03:00
|
|
|
DirectoryLockAcquired(DirectoryLock* aLock) override;
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-06-30 15:59:27 +03:00
|
|
|
DirectoryLockFailed() override;
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// IPDL methods.
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-08-29 08:45:12 +03:00
|
|
|
Recv__delete__(const JS::AsmJSCacheResult& aResult) override
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mState != eFinished);
|
|
|
|
|
|
|
|
if (mOpened) {
|
|
|
|
Close();
|
|
|
|
} else {
|
|
|
|
Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == eFinished);
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActorDestroy(ActorDestroyReason why) override
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mActorDestroyed);
|
|
|
|
|
|
|
|
mActorDestroyed = true;
|
|
|
|
|
|
|
|
// Assume ActorDestroy can happen at any time, so probe the current state to
|
|
|
|
// determine what needs to happen.
|
|
|
|
|
|
|
|
if (mState == eFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOpened) {
|
|
|
|
Close();
|
|
|
|
} else {
|
|
|
|
Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == eFinished);
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-08-29 08:45:12 +03:00
|
|
|
RecvSelectCacheFileToRead(const uint32_t& aModuleIndex) override
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mState == eWaitingToOpenCacheFileForRead);
|
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForRead);
|
|
|
|
|
|
|
|
// A cache entry has been selected to open.
|
|
|
|
|
|
|
|
mModuleIndex = aModuleIndex;
|
2015-11-22 12:44:06 +03:00
|
|
|
mState = eReadyToOpenCacheFileForRead;
|
|
|
|
DispatchToIOThread();
|
2015-08-29 08:45:12 +03:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-08-29 08:45:12 +03:00
|
|
|
RecvCacheMiss() override
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
CacheMiss();
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mOwningThread;
|
|
|
|
const PrincipalInfo mPrincipalInfo;
|
2013-11-19 01:49:53 +04:00
|
|
|
const OpenMode mOpenMode;
|
2013-12-05 08:32:52 +04:00
|
|
|
const WriteParams mWriteParams;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// State initialized during eInitial:
|
2014-03-06 00:47:10 +04:00
|
|
|
quota::PersistenceType mPersistence;
|
2016-06-05 22:42:25 +03:00
|
|
|
nsCString mSuffix;
|
2013-11-19 01:49:53 +04:00
|
|
|
nsCString mGroup;
|
|
|
|
nsCString mOrigin;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DirectoryLock> mDirectoryLock;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
// State initialized during eReadyToReadMetadata
|
|
|
|
nsCOMPtr<nsIFile> mDirectory;
|
|
|
|
nsCOMPtr<nsIFile> mMetadataFile;
|
|
|
|
Metadata mMetadata;
|
|
|
|
|
|
|
|
// State initialized during eWaitingToOpenCacheFileForRead
|
|
|
|
unsigned mModuleIndex;
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
enum State {
|
|
|
|
eInitial, // Just created, waiting to be dispatched to main thread
|
2015-11-22 12:44:06 +03:00
|
|
|
eWaitingToFinishInit, // Waiting to finish initialization
|
|
|
|
eWaitingToOpenDirectory, // Waiting to open directory
|
2015-08-29 08:45:04 +03:00
|
|
|
eWaitingToOpenMetadata, // Waiting to be called back from OpenDirectory
|
2013-12-05 08:32:52 +04:00
|
|
|
eReadyToReadMetadata, // Waiting to read the metadata file on the IO thread
|
2015-08-29 08:45:04 +03:00
|
|
|
eFailedToReadMetadata, // Waiting to be dispatched to owning thread after fail
|
2013-12-05 08:32:52 +04:00
|
|
|
eSendingMetadataForRead, // Waiting to send OnOpenMetadataForRead
|
|
|
|
eWaitingToOpenCacheFileForRead, // Waiting to hear back from child
|
|
|
|
eReadyToOpenCacheFileForRead, // Waiting to open cache file for read
|
2015-08-29 08:45:04 +03:00
|
|
|
eSendingCacheFile, // Waiting to send OnOpenCacheFile on the owning thread
|
|
|
|
eOpened, // Finished calling OnOpenCacheFile, waiting to be closed
|
|
|
|
eFailing, // Just failed, waiting to be dispatched to the owning thread
|
2013-11-19 01:49:53 +04:00
|
|
|
eFinished, // Terminal state
|
|
|
|
};
|
|
|
|
State mState;
|
2014-10-23 02:28:07 +04:00
|
|
|
JS::AsmJSCacheResult mResult;
|
2014-11-04 23:44:56 +03:00
|
|
|
|
|
|
|
bool mIsApp;
|
|
|
|
bool mEnforcingQuota;
|
2015-08-29 08:45:12 +03:00
|
|
|
bool mActorDestroyed;
|
|
|
|
bool mOpened;
|
2013-11-19 01:49:53 +04:00
|
|
|
};
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::InitPersistenceType()
|
2014-11-04 23:44:56 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eInitial);
|
|
|
|
|
|
|
|
if (mOpenMode == eOpenForWrite) {
|
|
|
|
MOZ_ASSERT(mPersistence == quota::PERSISTENCE_TYPE_INVALID);
|
|
|
|
|
|
|
|
// If we are performing install-time caching of an app, we'd like to store
|
|
|
|
// the cache entry in persistent storage so the entry is never evicted,
|
|
|
|
// but we need to check that quota is not enforced for the app.
|
|
|
|
// That justifies us in skipping all quota checks when storing the cache
|
|
|
|
// entry and avoids all the issues around the persistent quota prompt.
|
|
|
|
// If quota is enforced for the app, then we can still cache in temporary
|
|
|
|
// for a likely good first-run experience.
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(mWriteParams.mInstalled, mIsApp);
|
|
|
|
|
|
|
|
if (mWriteParams.mInstalled &&
|
|
|
|
!QuotaManager::IsQuotaEnforced(quota::PERSISTENCE_TYPE_PERSISTENT,
|
2015-01-22 11:40:42 +03:00
|
|
|
mOrigin, mIsApp)) {
|
2014-11-04 23:44:56 +03:00
|
|
|
mPersistence = quota::PERSISTENCE_TYPE_PERSISTENT;
|
|
|
|
} else {
|
|
|
|
mPersistence = quota::PERSISTENCE_TYPE_TEMPORARY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the reasons described above, apps may have cache entries in both
|
|
|
|
// persistent and temporary storage. At lookup time we don't know how and
|
|
|
|
// where the given script was cached, so start the search in persistent
|
|
|
|
// storage and, if that fails, search in temporary storage. (Non-apps can
|
|
|
|
// only be stored in temporary storage.)
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(mPersistence != quota::PERSISTENCE_TYPE_INVALID,
|
|
|
|
mIsApp && mPersistence == quota::PERSISTENCE_TYPE_PERSISTENT);
|
|
|
|
|
|
|
|
if (mPersistence == quota::PERSISTENCE_TYPE_INVALID && mIsApp) {
|
|
|
|
mPersistence = quota::PERSISTENCE_TYPE_PERSISTENT;
|
|
|
|
} else {
|
|
|
|
mPersistence = quota::PERSISTENCE_TYPE_TEMPORARY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
nsresult
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::InitOnMainThread()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eInitial);
|
2015-08-29 08:45:04 +03:00
|
|
|
MOZ_ASSERT(mPrincipalInfo.type() != PrincipalInfo::TNullPrincipalInfo);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
PrincipalInfoToPrincipal(mPrincipalInfo, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2016-06-05 22:42:25 +03:00
|
|
|
rv = QuotaManager::GetInfoFromPrincipal(principal, &mSuffix, &mGroup,
|
|
|
|
&mOrigin, &mIsApp);
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
InitPersistenceType();
|
|
|
|
|
|
|
|
mEnforcingQuota =
|
2015-01-22 11:40:42 +03:00
|
|
|
QuotaManager::IsQuotaEnforced(mPersistence, mOrigin, mIsApp);
|
2014-11-04 23:44:56 +03:00
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
void
|
|
|
|
ParentRunnable::OpenDirectory()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mState == eWaitingToFinishInit ||
|
|
|
|
mState == eWaitingToOpenDirectory);
|
|
|
|
MOZ_ASSERT(QuotaManager::Get());
|
|
|
|
|
|
|
|
mState = eWaitingToOpenMetadata;
|
|
|
|
|
|
|
|
// XXX The exclusive lock shouldn't be needed for read operations.
|
|
|
|
QuotaManager::Get()->OpenDirectory(mPersistence,
|
|
|
|
mGroup,
|
|
|
|
mOrigin,
|
|
|
|
mIsApp,
|
|
|
|
quota::Client::ASMJS,
|
|
|
|
/* aExclusive */ true,
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
nsresult
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::ReadMetadata()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
2013-12-05 08:32:52 +04:00
|
|
|
MOZ_ASSERT(mState == eReadyToReadMetadata);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(qm, "We are on the QuotaManager's IO thread");
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
nsresult rv =
|
2016-06-05 22:42:25 +03:00
|
|
|
qm->EnsureOriginIsInitialized(mPersistence, mSuffix, mGroup, mOrigin,
|
|
|
|
mIsApp, getter_AddRefs(mDirectory));
|
2015-04-17 21:17:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mResult = JS::AsmJSCache_StorageInitFailure;
|
|
|
|
return rv;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mDirectory->Append(NS_LITERAL_STRING(ASMJSCACHE_DIRECTORY_NAME));
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool exists;
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mDirectory->Exists(&exists);
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!exists) {
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
DebugOnly<bool> isDirectory;
|
2013-12-05 08:32:52 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mDirectory->IsDirectory(&isDirectory)));
|
2013-11-19 01:49:53 +04:00
|
|
|
MOZ_ASSERT(isDirectory, "Should have caught this earlier!");
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mDirectory->Clone(getter_AddRefs(mMetadataFile));
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mMetadataFile->Append(NS_LITERAL_STRING(ASMJSCACHE_METADATA_FILE_NAME));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = mMetadataFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exists && NS_FAILED(ReadMetadataFile(mMetadataFile, mMetadata))) {
|
|
|
|
exists = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
// If we are reading, we can't possibly have a cache hit.
|
|
|
|
if (mOpenMode == eOpenForRead) {
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
// Initialize Metadata with a valid empty state for the LRU cache.
|
|
|
|
for (unsigned i = 0; i < Metadata::kNumEntries; i++) {
|
|
|
|
Metadata::Entry& entry = mMetadata.mEntries[i];
|
|
|
|
entry.mModuleIndex = i;
|
2014-02-14 04:25:39 +04:00
|
|
|
entry.clear();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::OpenCacheFileForWrite()
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_ASSERT(mState == eReadyToReadMetadata);
|
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForWrite);
|
|
|
|
|
|
|
|
mFileSize = mWriteParams.mSize;
|
|
|
|
|
|
|
|
// Kick out the oldest entry in the LRU queue in the metadata.
|
|
|
|
mModuleIndex = mMetadata.mEntries[Metadata::kLastEntry].mModuleIndex;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = GetCacheFile(mDirectory, mModuleIndex, getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(qm, "We are on the QuotaManager's IO thread");
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
if (mEnforcingQuota) {
|
2014-03-06 00:47:10 +04:00
|
|
|
// Create the QuotaObject before all file IO and keep it alive until caching
|
|
|
|
// completes to get maximum assertion coverage in QuotaManager against
|
|
|
|
// concurrent removal, etc.
|
|
|
|
mQuotaObject = qm->GetQuotaObject(mPersistence, mGroup, mOrigin, file);
|
|
|
|
NS_ENSURE_STATE(mQuotaObject);
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
if (!mQuotaObject->MaybeUpdateSize(mWriteParams.mSize,
|
|
|
|
/* aTruncate */ false)) {
|
2014-03-06 00:47:10 +04:00
|
|
|
// If the request fails, it might be because mOrigin is using too much
|
2015-02-16 20:48:14 +03:00
|
|
|
// space (MaybeUpdateSize will not evict our own origin since it is
|
2014-03-06 00:47:10 +04:00
|
|
|
// active). Try to make some space by evicting LRU entries until there is
|
|
|
|
// enough space.
|
|
|
|
EvictEntries(mDirectory, mGroup, mOrigin, mWriteParams.mSize, mMetadata);
|
2015-02-16 20:48:14 +03:00
|
|
|
if (!mQuotaObject->MaybeUpdateSize(mWriteParams.mSize,
|
|
|
|
/* aTruncate */ false)) {
|
2014-10-23 02:28:07 +04:00
|
|
|
mResult = JS::AsmJSCache_QuotaExceeded;
|
2014-03-06 00:47:10 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-02-14 04:25:39 +04:00
|
|
|
}
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t openFlags = PR_RDWR | PR_TRUNCATE | PR_CREATE_FILE;
|
|
|
|
rv = file->OpenNSPRFileDesc(openFlags, 0644, &mFileDesc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Move the mModuleIndex's LRU entry to the recent end of the queue.
|
|
|
|
PodMove(mMetadata.mEntries + 1, mMetadata.mEntries, Metadata::kLastEntry);
|
|
|
|
Metadata::Entry& entry = mMetadata.mEntries[0];
|
|
|
|
entry.mFastHash = mWriteParams.mFastHash;
|
|
|
|
entry.mNumChars = mWriteParams.mNumChars;
|
|
|
|
entry.mFullHash = mWriteParams.mFullHash;
|
|
|
|
entry.mModuleIndex = mModuleIndex;
|
|
|
|
|
|
|
|
rv = WriteMetadataFile(mMetadataFile, mMetadata);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::OpenCacheFileForRead()
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_ASSERT(mState == eReadyToOpenCacheFileForRead);
|
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForRead);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = GetCacheFile(mDirectory, mModuleIndex, getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(qm, "We are on the QuotaManager's IO thread");
|
|
|
|
|
2014-11-04 23:44:56 +03:00
|
|
|
if (mEnforcingQuota) {
|
2014-03-06 00:47:10 +04:00
|
|
|
// Even though it's not strictly necessary, create the QuotaObject before
|
|
|
|
// all file IO and keep it alive until caching completes to get maximum
|
|
|
|
// assertion coverage in QuotaManager against concurrent removal, etc.
|
|
|
|
mQuotaObject = qm->GetQuotaObject(mPersistence, mGroup, mOrigin, file);
|
|
|
|
NS_ENSURE_STATE(mQuotaObject);
|
|
|
|
}
|
2013-12-05 08:32:52 +04:00
|
|
|
|
|
|
|
rv = file->GetFileSize(&mFileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
int32_t openFlags = PR_RDONLY | nsIFile::OS_READAHEAD;
|
|
|
|
rv = file->OpenNSPRFileDesc(openFlags, 0644, &mFileDesc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
// Move the mModuleIndex's LRU entry to the recent end of the queue.
|
|
|
|
unsigned lruIndex = 0;
|
|
|
|
while (mMetadata.mEntries[lruIndex].mModuleIndex != mModuleIndex) {
|
|
|
|
if (++lruIndex == Metadata::kNumEntries) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
2013-12-05 08:32:52 +04:00
|
|
|
Metadata::Entry entry = mMetadata.mEntries[lruIndex];
|
|
|
|
PodMove(mMetadata.mEntries + 1, mMetadata.mEntries, lruIndex);
|
|
|
|
mMetadata.mEntries[0] = entry;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = WriteMetadataFile(mMetadataFile, mMetadata);
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::FinishOnOwningThread()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// Per FileDescriptorHolder::Finish()'s comment, call before
|
2015-06-30 15:59:27 +03:00
|
|
|
// releasing the directory lock.
|
2013-11-19 01:49:53 +04:00
|
|
|
FileDescriptorHolder::Finish();
|
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
mDirectoryLock = nullptr;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::Run()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// All success/failure paths must eventually call Finish() to avoid leaving
|
|
|
|
// the parser hanging.
|
|
|
|
switch (mState) {
|
|
|
|
case eInitial: {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
rv = InitOnMainThread();
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-08-29 08:45:12 +03:00
|
|
|
FailOnNonOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
mState = eWaitingToFinishInit;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2015-11-22 12:44:06 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eWaitingToFinishInit: {
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (QuotaManager::IsShuttingDown()) {
|
|
|
|
Fail();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QuotaManager::Get()) {
|
|
|
|
OpenDirectory();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = eWaitingToOpenDirectory;
|
|
|
|
QuotaManager::GetOrCreate(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
case eWaitingToOpenDirectory: {
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!QuotaManager::Get())) {
|
|
|
|
Fail();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
OpenDirectory();
|
2013-12-05 08:32:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eReadyToReadMetadata: {
|
|
|
|
AssertIsOnIOThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = ReadMetadata();
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-03-06 00:47:10 +04:00
|
|
|
mState = eFailedToReadMetadata;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
if (mOpenMode == eOpenForRead) {
|
|
|
|
mState = eSendingMetadataForRead;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2015-08-29 08:45:04 +03:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = OpenCacheFileForWrite();
|
2013-11-19 01:49:53 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-08-29 08:45:12 +03:00
|
|
|
FailOnNonOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
mState = eSendingCacheFile;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-06 00:47:10 +04:00
|
|
|
case eFailedToReadMetadata: {
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-03-06 00:47:10 +04:00
|
|
|
|
2015-04-21 07:18:59 +03:00
|
|
|
if (mOpenMode == eOpenForRead) {
|
|
|
|
CacheMiss();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fail();
|
2014-03-06 00:47:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
case eSendingMetadataForRead: {
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-12-05 08:32:52 +04:00
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForRead);
|
|
|
|
|
|
|
|
mState = eWaitingToOpenCacheFileForRead;
|
2015-08-29 08:45:12 +03:00
|
|
|
|
|
|
|
// Metadata is now open.
|
|
|
|
if (!SendOnOpenMetadataForRead(mMetadata)) {
|
2016-02-12 18:38:31 +03:00
|
|
|
Fail();
|
|
|
|
return NS_OK;
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eReadyToOpenCacheFileForRead: {
|
2013-11-19 01:49:53 +04:00
|
|
|
AssertIsOnIOThread();
|
2013-12-05 08:32:52 +04:00
|
|
|
MOZ_ASSERT(mOpenMode == eOpenForRead);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = OpenCacheFileForRead();
|
2013-11-19 01:49:53 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-08-29 08:45:12 +03:00
|
|
|
FailOnNonOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
mState = eSendingCacheFile;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
case eSendingCacheFile: {
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
mState = eOpened;
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// The entry is now open.
|
|
|
|
MOZ_ASSERT(!mOpened);
|
|
|
|
mOpened = true;
|
|
|
|
|
|
|
|
FileDescriptor::PlatformHandleType handle =
|
|
|
|
FileDescriptor::PlatformHandleType(PR_FileDesc2NativeHandle(mFileDesc));
|
|
|
|
if (!SendOnOpenCacheFile(mFileSize, FileDescriptor(handle))) {
|
2016-02-12 18:38:31 +03:00
|
|
|
Fail();
|
|
|
|
return NS_OK;
|
2015-08-29 08:45:12 +03:00
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
case eFailing: {
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnOwningThread();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
Fail();
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
case eWaitingToOpenMetadata:
|
2013-12-05 08:32:52 +04:00
|
|
|
case eWaitingToOpenCacheFileForRead:
|
2013-11-19 01:49:53 +04:00
|
|
|
case eOpened:
|
|
|
|
case eFinished: {
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Shouldn't Run() in this state");
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Corrupt state");
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::DirectoryLockAcquired(DirectoryLock* aLock)
|
2015-06-30 15:59:27 +03:00
|
|
|
{
|
2015-11-22 12:44:06 +03:00
|
|
|
AssertIsOnOwningThread();
|
2015-06-30 15:59:27 +03:00
|
|
|
MOZ_ASSERT(mState == eWaitingToOpenMetadata);
|
|
|
|
MOZ_ASSERT(!mDirectoryLock);
|
|
|
|
|
|
|
|
mDirectoryLock = aLock;
|
|
|
|
|
|
|
|
mState = eReadyToReadMetadata;
|
|
|
|
DispatchToIOThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ParentRunnable::DirectoryLockFailed()
|
2015-06-30 15:59:27 +03:00
|
|
|
{
|
2015-11-22 12:44:06 +03:00
|
|
|
AssertIsOnOwningThread();
|
2015-06-30 15:59:27 +03:00
|
|
|
MOZ_ASSERT(mState == eWaitingToOpenMetadata);
|
|
|
|
MOZ_ASSERT(!mDirectoryLock);
|
|
|
|
|
2015-11-22 12:44:06 +03:00
|
|
|
Fail();
|
2015-06-30 15:59:27 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(ParentRunnable, FileDescriptorHolder)
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
bool
|
|
|
|
FindHashMatch(const Metadata& aMetadata, const ReadParams& aReadParams,
|
2014-02-14 04:25:39 +04:00
|
|
|
unsigned* aModuleIndex)
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
|
|
|
// Perform a fast hash of the first sNumFastHashChars chars. Each cache entry
|
|
|
|
// also stores an mFastHash of its first sNumFastHashChars so this gives us a
|
|
|
|
// fast way to probabilistically determine whether we have a cache hit. We
|
|
|
|
// still do a full hash of all the chars before returning the cache file to
|
|
|
|
// the engine to avoid penalizing the case where there are multiple cached
|
|
|
|
// asm.js modules where the first sNumFastHashChars are the same. The
|
|
|
|
// mFullHash of each cache entry can have a different mNumChars so the fast
|
|
|
|
// hash allows us to avoid performing up to Metadata::kNumEntries separate
|
|
|
|
// full hashes.
|
|
|
|
uint32_t numChars = aReadParams.mLimit - aReadParams.mBegin;
|
|
|
|
MOZ_ASSERT(numChars > sNumFastHashChars);
|
|
|
|
uint32_t fastHash = HashString(aReadParams.mBegin, sNumFastHashChars);
|
|
|
|
|
2016-11-14 12:31:11 +03:00
|
|
|
for (auto entry : aMetadata.mEntries) {
|
2013-12-05 08:32:52 +04:00
|
|
|
// Compare the "fast hash" first to see whether it is worthwhile to
|
|
|
|
// hash all the chars.
|
|
|
|
if (entry.mFastHash != fastHash) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assuming we have enough characters, hash all the chars it would take
|
|
|
|
// to match this cache entry and compare to the cache entry. If we get a
|
|
|
|
// hit we'll still do a full source match later (in the JS engine), but
|
|
|
|
// the full hash match means this is probably the cache entry we want.
|
|
|
|
if (numChars < entry.mNumChars) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t fullHash = HashString(aReadParams.mBegin, entry.mNumChars);
|
|
|
|
if (entry.mFullHash != fullHash) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aModuleIndex = entry.mModuleIndex;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
PAsmJSCacheEntryParent*
|
|
|
|
AllocEntryParent(OpenMode aOpenMode,
|
2013-12-05 08:32:52 +04:00
|
|
|
WriteParams aWriteParams,
|
2015-08-29 08:45:04 +03:00
|
|
|
const PrincipalInfo& aPrincipalInfo)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:04 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo)) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ParentRunnable> runnable =
|
2015-08-29 08:45:12 +03:00
|
|
|
new ParentRunnable(aPrincipalInfo, aOpenMode, aWriteParams);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToMainThread(runnable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2014-05-07 18:32:02 +04:00
|
|
|
// Transfer ownership to IPDL.
|
|
|
|
return runnable.forget().take();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeallocEntryParent(PAsmJSCacheEntryParent* aActor)
|
|
|
|
{
|
2014-05-07 18:32:02 +04:00
|
|
|
// Transfer ownership back from IPDL.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ParentRunnable> op =
|
2015-08-29 08:45:12 +03:00
|
|
|
dont_AddRef(static_cast<ParentRunnable*>(aActor));
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
// A runnable that presents a single interface to the AsmJSCache ops which need
|
|
|
|
// to wait until the file is open.
|
2015-08-29 08:45:12 +03:00
|
|
|
class ChildRunnable final
|
2015-08-29 08:45:24 +03:00
|
|
|
: public FileDescriptorHolder
|
2015-08-29 08:45:12 +03:00
|
|
|
, public PAsmJSCacheEntryChild
|
|
|
|
, public nsIIPCBackgroundChildCreateCallback
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:04 +03:00
|
|
|
typedef mozilla::ipc::PBackgroundChild PBackgroundChild;
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
public:
|
2015-08-29 08:45:24 +03:00
|
|
|
class AutoClose
|
|
|
|
{
|
|
|
|
ChildRunnable* mChildRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoClose(ChildRunnable* aChildRunnable = nullptr)
|
|
|
|
: mChildRunnable(aChildRunnable)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
Init(ChildRunnable* aChildRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mChildRunnable);
|
|
|
|
mChildRunnable = aChildRunnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildRunnable*
|
|
|
|
operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mChildRunnable);
|
|
|
|
return mChildRunnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Forget(ChildRunnable** aChildRunnable)
|
|
|
|
{
|
|
|
|
*aChildRunnable = mChildRunnable;
|
|
|
|
mChildRunnable = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoClose()
|
|
|
|
{
|
|
|
|
if (mChildRunnable) {
|
|
|
|
mChildRunnable->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2015-08-29 08:45:04 +03:00
|
|
|
NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
ChildRunnable(nsIPrincipal* aPrincipal,
|
|
|
|
OpenMode aOpenMode,
|
|
|
|
WriteParams aWriteParams,
|
|
|
|
ReadParams aReadParams)
|
2013-11-19 01:49:53 +04:00
|
|
|
: mPrincipal(aPrincipal),
|
2013-12-05 08:32:52 +04:00
|
|
|
mWriteParams(aWriteParams),
|
|
|
|
mReadParams(aReadParams),
|
2015-08-29 08:45:24 +03:00
|
|
|
mMutex("ChildRunnable::mMutex"),
|
|
|
|
mCondVar(mMutex, "ChildRunnable::mCondVar"),
|
|
|
|
mOpenMode(aOpenMode),
|
|
|
|
mState(eInitial),
|
|
|
|
mResult(JS::AsmJSCache_InternalError),
|
2013-11-19 01:49:53 +04:00
|
|
|
mActorDestroyed(false),
|
2015-08-29 08:45:24 +03:00
|
|
|
mWaiting(false),
|
|
|
|
mOpened(false)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
JS::AsmJSCacheResult
|
|
|
|
BlockUntilOpen(AutoClose* aCloser)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mWaiting, "Can only call BlockUntilOpen once");
|
|
|
|
MOZ_ASSERT(!mOpened, "Can only call BlockUntilOpen once");
|
|
|
|
|
|
|
|
mWaiting = true;
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToMainThread(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return JS::AsmJSCache_InternalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
while (mWaiting) {
|
|
|
|
mCondVar.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mOpened) {
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're open, we're guaranteed a Close() call. However, we are
|
|
|
|
// not guaranteed someone is holding an outstanding reference until the File
|
|
|
|
// is closed, so we do that ourselves and Release() in OnClose().
|
|
|
|
aCloser->Init(this);
|
|
|
|
AddRef();
|
|
|
|
return JS::AsmJSCache_Success;
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:55:40 +03:00
|
|
|
void Cleanup()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
NoteActorDestroyed();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
private:
|
2016-11-15 14:09:07 +03:00
|
|
|
~ChildRunnable() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:24 +03:00
|
|
|
MOZ_ASSERT(!mWaiting, "Shouldn't be destroyed while thread is waiting");
|
|
|
|
MOZ_ASSERT(!mOpened);
|
2013-11-19 01:49:53 +04:00
|
|
|
MOZ_ASSERT(mState == eFinished);
|
|
|
|
MOZ_ASSERT(mActorDestroyed);
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
// IPDL methods.
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-03-21 19:28:04 +03:00
|
|
|
RecvOnOpenMetadataForRead(const Metadata& aMetadata) override
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eOpening);
|
|
|
|
|
|
|
|
uint32_t moduleIndex;
|
2014-03-06 00:47:10 +04:00
|
|
|
if (FindHashMatch(aMetadata, mReadParams, &moduleIndex)) {
|
2016-11-15 06:26:00 +03:00
|
|
|
if (!SendSelectCacheFileToRead(moduleIndex)) {
|
|
|
|
return IPC_FAIL_NO_REASON(this);
|
|
|
|
}
|
|
|
|
return IPC_OK();
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
if (!SendCacheMiss()) {
|
|
|
|
return IPC_FAIL_NO_REASON(this);
|
|
|
|
}
|
|
|
|
return IPC_OK();
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2013-12-05 08:32:52 +04:00
|
|
|
RecvOnOpenCacheFile(const int64_t& aFileSize,
|
2015-03-21 19:28:04 +03:00
|
|
|
const FileDescriptor& aFileDesc) override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eOpening);
|
|
|
|
|
|
|
|
mFileSize = aFileSize;
|
|
|
|
|
2016-05-27 11:12:51 +03:00
|
|
|
auto rawFD = aFileDesc.ClonePlatformHandle();
|
|
|
|
mFileDesc = PR_ImportFile(PROsfd(rawFD.release()));
|
2013-11-19 01:49:53 +04:00
|
|
|
if (!mFileDesc) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mState = eOpened;
|
2015-08-29 08:45:24 +03:00
|
|
|
Notify(JS::AsmJSCache_Success);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-03-21 19:28:04 +03:00
|
|
|
Recv__delete__(const JS::AsmJSCacheResult& aResult) override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eOpening);
|
|
|
|
|
2014-10-23 02:28:07 +04:00
|
|
|
Fail(aResult);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-21 19:28:04 +03:00
|
|
|
ActorDestroy(ActorDestroyReason why) override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-01-08 15:55:40 +03:00
|
|
|
NoteActorDestroyed();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-29 08:45:24 +03:00
|
|
|
Close()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == eOpened);
|
|
|
|
|
|
|
|
mState = eClosing;
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
void
|
2014-10-23 02:28:07 +04:00
|
|
|
Fail(JS::AsmJSCacheResult aResult)
|
2013-12-05 08:32:52 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eInitial || mState == eOpening);
|
2015-08-29 08:45:24 +03:00
|
|
|
MOZ_ASSERT(aResult != JS::AsmJSCache_Success);
|
2013-12-05 08:32:52 +04:00
|
|
|
|
|
|
|
mState = eFinished;
|
2015-08-29 08:45:24 +03:00
|
|
|
|
|
|
|
FileDescriptorHolder::Finish();
|
|
|
|
Notify(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notify(JS::AsmJSCacheResult aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mWaiting);
|
|
|
|
|
|
|
|
mWaiting = false;
|
|
|
|
mOpened = aResult == JS::AsmJSCache_Success;
|
|
|
|
mResult = aResult;
|
|
|
|
mCondVar.Notify();
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
2016-01-08 15:55:40 +03:00
|
|
|
void NoteActorDestroyed()
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
nsIPrincipal* const mPrincipal;
|
2015-08-29 08:45:04 +03:00
|
|
|
nsAutoPtr<PrincipalInfo> mPrincipalInfo;
|
2013-12-05 08:32:52 +04:00
|
|
|
WriteParams mWriteParams;
|
|
|
|
ReadParams mReadParams;
|
2015-08-29 08:45:24 +03:00
|
|
|
Mutex mMutex;
|
|
|
|
CondVar mCondVar;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
// Couple enums and bools together
|
|
|
|
const OpenMode mOpenMode;
|
2013-11-19 01:49:53 +04:00
|
|
|
enum State {
|
2015-08-29 08:45:04 +03:00
|
|
|
eInitial, // Just created, waiting to be dispatched to the main thread
|
|
|
|
eBackgroundChildPending, // Waiting for the background child to be created
|
2013-11-19 01:49:53 +04:00
|
|
|
eOpening, // Waiting for the parent process to respond
|
|
|
|
eOpened, // Parent process opened the entry and sent it back
|
|
|
|
eClosing, // Waiting to be dispatched to the main thread to Send__delete__
|
|
|
|
eFinished // Terminal state
|
|
|
|
};
|
|
|
|
State mState;
|
2015-08-29 08:45:24 +03:00
|
|
|
JS::AsmJSCacheResult mResult;
|
|
|
|
|
|
|
|
bool mActorDestroyed;
|
|
|
|
bool mWaiting;
|
|
|
|
bool mOpened;
|
2013-11-19 01:49:53 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-08-29 08:45:12 +03:00
|
|
|
ChildRunnable::Run()
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
switch (mState) {
|
|
|
|
case eInitial: {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-08-12 08:19:29 +03:00
|
|
|
if (mPrincipal->GetIsNullPrincipal()) {
|
2015-08-29 08:45:04 +03:00
|
|
|
NS_WARNING("AsmsJSCache not supported on null principal.");
|
|
|
|
Fail(JS::AsmJSCache_InternalError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<PrincipalInfo> principalInfo(new PrincipalInfo());
|
2016-08-12 08:19:29 +03:00
|
|
|
nsresult rv = PrincipalToPrincipalInfo(mPrincipal, principalInfo);
|
2015-08-29 08:45:04 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(JS::AsmJSCache_InternalError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrincipalInfo = Move(principalInfo);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
PBackgroundChild* actor = BackgroundChild::GetForCurrentThread();
|
|
|
|
if (actor) {
|
|
|
|
ActorCreated(actor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
if (NS_WARN_IF(!BackgroundChild::GetOrCreateForCurrentThread(this))) {
|
2014-10-23 02:28:07 +04:00
|
|
|
Fail(JS::AsmJSCache_InternalError);
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
mState = eBackgroundChildPending;
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eClosing: {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Per FileDescriptorHolder::Finish()'s comment, call before
|
2015-06-30 15:59:27 +03:00
|
|
|
// releasing the directory lock (which happens in the parent upon receipt
|
|
|
|
// of the Send__delete__ message).
|
2015-08-29 08:45:24 +03:00
|
|
|
FileDescriptorHolder::Finish();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mOpened);
|
|
|
|
mOpened = false;
|
|
|
|
|
|
|
|
// Match the AddRef in BlockUntilOpen(). The main thread event loop still
|
|
|
|
// holds an outstanding ref which will keep 'this' alive until returning to
|
|
|
|
// the event loop.
|
|
|
|
Release();
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
if (!mActorDestroyed) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, JS::AsmJSCache_Success);
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mState = eFinished;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
case eBackgroundChildPending:
|
2013-11-19 01:49:53 +04:00
|
|
|
case eOpening:
|
|
|
|
case eOpened:
|
|
|
|
case eFinished: {
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Shouldn't Run() in this state");
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Corrupt state");
|
2013-11-19 01:49:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:04 +03:00
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ChildRunnable::ActorCreated(PBackgroundChild* aActor)
|
2015-08-29 08:45:04 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!aActor->SendPAsmJSCacheEntryConstructor(this, mOpenMode, mWriteParams,
|
|
|
|
*mPrincipalInfo)) {
|
|
|
|
// Unblock the parsing thread with a failure.
|
|
|
|
|
|
|
|
Fail(JS::AsmJSCache_InternalError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRef to keep this runnable alive until IPDL deallocates the
|
|
|
|
// subprotocol (DeallocEntryChild).
|
|
|
|
AddRef();
|
|
|
|
|
|
|
|
mState = eOpening;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-29 08:45:12 +03:00
|
|
|
ChildRunnable::ActorFailed()
|
2015-08-29 08:45:04 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mState == eBackgroundChildPending);
|
|
|
|
|
|
|
|
Fail(JS::AsmJSCache_InternalError);
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(ChildRunnable,
|
2015-08-29 08:45:04 +03:00
|
|
|
FileDescriptorHolder,
|
|
|
|
nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
void
|
|
|
|
DeallocEntryChild(PAsmJSCacheEntryChild* aActor)
|
|
|
|
{
|
|
|
|
// Match the AddRef before SendPAsmJSCacheEntryConstructor.
|
2015-08-29 08:45:12 +03:00
|
|
|
static_cast<ChildRunnable*>(aActor)->Release();
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-10-23 02:28:07 +04:00
|
|
|
JS::AsmJSCacheResult
|
2013-12-03 23:31:30 +04:00
|
|
|
OpenFile(nsIPrincipal* aPrincipal,
|
2013-11-19 01:49:53 +04:00
|
|
|
OpenMode aOpenMode,
|
2013-12-05 08:32:52 +04:00
|
|
|
WriteParams aWriteParams,
|
|
|
|
ReadParams aReadParams,
|
2015-08-29 08:45:24 +03:00
|
|
|
ChildRunnable::AutoClose* aChildRunnable)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2013-12-05 08:32:52 +04:00
|
|
|
MOZ_ASSERT_IF(aOpenMode == eOpenForRead, aWriteParams.mSize == 0);
|
|
|
|
MOZ_ASSERT_IF(aOpenMode == eOpenForWrite, aReadParams.mBegin == nullptr);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// There are three reasons we don't attempt caching from the main thread:
|
|
|
|
// 1. In the parent process: QuotaManager::WaitForOpenAllowed prevents
|
|
|
|
// synchronous waiting on the main thread requiring a runnable to be
|
|
|
|
// dispatched to the main thread.
|
|
|
|
// 2. In the child process: the IPDL PContent messages we need to
|
|
|
|
// synchronously wait on are dispatched to the main thread.
|
|
|
|
// 3. While a cache lookup *should* be much faster than compilation, IO
|
|
|
|
// operations can be unpredictably slow and we'd like to avoid the
|
|
|
|
// occasional janks on the main thread.
|
|
|
|
// We could use a nested event loop to address 1 and 2, but we're potentially
|
|
|
|
// in the middle of running JS (eval()) and nested event loops can be
|
|
|
|
// semantically observable.
|
|
|
|
if (NS_IsMainThread()) {
|
2014-10-23 02:28:07 +04:00
|
|
|
return JS::AsmJSCache_SynchronousScript;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2016-09-02 07:03:36 +03:00
|
|
|
// Check to see whether the principal reflects a private browsing session.
|
|
|
|
// Since AsmJSCache requires disk access at the moment, caching should be
|
|
|
|
// disabled in private browsing situations. Failing here will cause later
|
|
|
|
// read/write requests to also fail.
|
|
|
|
uint32_t pbId;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aPrincipal->GetPrivateBrowsingId(&pbId)))) {
|
|
|
|
return JS::AsmJSCache_InternalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pbId > 0) {
|
|
|
|
return JS::AsmJSCache_Disabled_PrivateBrowsing;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:12 +03:00
|
|
|
// We need to synchronously call into the parent to open the file and
|
|
|
|
// interact with the QuotaManager. The child can then map the file into its
|
|
|
|
// address space to perform I/O.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ChildRunnable> childRunnable =
|
2015-08-29 08:45:12 +03:00
|
|
|
new ChildRunnable(aPrincipal, aOpenMode, aWriteParams, aReadParams);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
JS::AsmJSCacheResult openResult =
|
|
|
|
childRunnable->BlockUntilOpen(aChildRunnable);
|
2014-10-23 02:28:07 +04:00
|
|
|
if (openResult != JS::AsmJSCache_Success) {
|
2016-01-08 15:55:40 +03:00
|
|
|
childRunnable->Cleanup();
|
2014-10-23 02:28:07 +04:00
|
|
|
return openResult;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
if (!childRunnable->MapMemory(aOpenMode)) {
|
2014-10-23 02:28:07 +04:00
|
|
|
return JS::AsmJSCache_InternalError;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:28:07 +04:00
|
|
|
return JS::AsmJSCache_Success;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
typedef uint32_t AsmJSCookieType;
|
|
|
|
static const uint32_t sAsmJSCookie = 0x600d600d;
|
|
|
|
|
|
|
|
bool
|
2013-12-03 23:31:30 +04:00
|
|
|
OpenEntryForRead(nsIPrincipal* aPrincipal,
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* aBegin,
|
|
|
|
const char16_t* aLimit,
|
2013-11-19 01:49:53 +04:00
|
|
|
size_t* aSize,
|
|
|
|
const uint8_t** aMemory,
|
2015-08-29 08:45:24 +03:00
|
|
|
intptr_t* aHandle)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2013-11-19 02:02:05 +04:00
|
|
|
if (size_t(aLimit - aBegin) < sMinCachedModuleLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
ReadParams readParams;
|
|
|
|
readParams.mBegin = aBegin;
|
|
|
|
readParams.mLimit = aLimit;
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
ChildRunnable::AutoClose childRunnable;
|
2013-12-05 08:32:52 +04:00
|
|
|
WriteParams notAWrite;
|
2014-10-23 02:28:07 +04:00
|
|
|
JS::AsmJSCacheResult openResult =
|
2015-08-29 08:45:24 +03:00
|
|
|
OpenFile(aPrincipal, eOpenForRead, notAWrite, readParams, &childRunnable);
|
2014-10-23 02:28:07 +04:00
|
|
|
if (openResult != JS::AsmJSCache_Success) {
|
2013-11-19 01:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Although we trust that the stored cache files have not been arbitrarily
|
|
|
|
// corrupted, it is possible that a previous execution aborted in the middle
|
|
|
|
// of writing a cache file (crash, OOM-killer, etc). To protect against
|
|
|
|
// partially-written cache files, we use the following scheme:
|
|
|
|
// - Allocate an extra word at the beginning of every cache file which
|
|
|
|
// starts out 0 (OpenFile opens with PR_TRUNCATE).
|
|
|
|
// - After the asm.js serialization is complete, PR_SyncMemMap to write
|
|
|
|
// everything to disk and then store a non-zero value (sAsmJSCookie)
|
|
|
|
// in the first word.
|
|
|
|
// - When attempting to read a cache file, check whether the first word is
|
|
|
|
// sAsmJSCookie.
|
2015-08-29 08:45:24 +03:00
|
|
|
if (childRunnable->FileSize() < sizeof(AsmJSCookieType) ||
|
|
|
|
*(AsmJSCookieType*)childRunnable->MappedMemory() != sAsmJSCookie) {
|
2013-11-19 01:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
*aSize = childRunnable->FileSize() - sizeof(AsmJSCookieType);
|
|
|
|
*aMemory = (uint8_t*) childRunnable->MappedMemory() + sizeof(AsmJSCookieType);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// The caller guarnatees a call to CloseEntryForRead (on success or
|
|
|
|
// failure) at which point the file will be closed.
|
2015-08-29 08:45:24 +03:00
|
|
|
childRunnable.Forget(reinterpret_cast<ChildRunnable**>(aHandle));
|
2013-11-19 01:49:53 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 21:58:00 +04:00
|
|
|
CloseEntryForRead(size_t aSize,
|
2013-11-19 01:49:53 +04:00
|
|
|
const uint8_t* aMemory,
|
2015-08-29 08:45:24 +03:00
|
|
|
intptr_t aHandle)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:24 +03:00
|
|
|
ChildRunnable::AutoClose childRunnable(
|
|
|
|
reinterpret_cast<ChildRunnable*>(aHandle));
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
MOZ_ASSERT(aSize + sizeof(AsmJSCookieType) == childRunnable->FileSize());
|
|
|
|
MOZ_ASSERT(aMemory - sizeof(AsmJSCookieType) ==
|
|
|
|
childRunnable->MappedMemory());
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:28:07 +04:00
|
|
|
JS::AsmJSCacheResult
|
2013-12-03 23:31:30 +04:00
|
|
|
OpenEntryForWrite(nsIPrincipal* aPrincipal,
|
2014-03-06 00:47:10 +04:00
|
|
|
bool aInstalled,
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* aBegin,
|
|
|
|
const char16_t* aEnd,
|
2013-11-19 01:49:53 +04:00
|
|
|
size_t aSize,
|
|
|
|
uint8_t** aMemory,
|
2015-08-29 08:45:24 +03:00
|
|
|
intptr_t* aHandle)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2013-11-19 02:02:05 +04:00
|
|
|
if (size_t(aEnd - aBegin) < sMinCachedModuleLength) {
|
2014-10-23 02:28:07 +04:00
|
|
|
return JS::AsmJSCache_ModuleTooSmall;
|
2013-11-19 02:02:05 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
// Add extra space for the AsmJSCookieType (see OpenEntryForRead).
|
|
|
|
aSize += sizeof(AsmJSCookieType);
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
static_assert(sNumFastHashChars < sMinCachedModuleLength, "HashString safe");
|
|
|
|
|
|
|
|
WriteParams writeParams;
|
2014-03-06 00:47:10 +04:00
|
|
|
writeParams.mInstalled = aInstalled;
|
2013-12-05 08:32:52 +04:00
|
|
|
writeParams.mSize = aSize;
|
|
|
|
writeParams.mFastHash = HashString(aBegin, sNumFastHashChars);
|
|
|
|
writeParams.mNumChars = aEnd - aBegin;
|
|
|
|
writeParams.mFullHash = HashString(aBegin, writeParams.mNumChars);
|
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
ChildRunnable::AutoClose childRunnable;
|
2013-12-05 08:32:52 +04:00
|
|
|
ReadParams notARead;
|
2014-10-23 02:28:07 +04:00
|
|
|
JS::AsmJSCacheResult openResult =
|
2015-08-29 08:45:24 +03:00
|
|
|
OpenFile(aPrincipal, eOpenForWrite, writeParams, notARead, &childRunnable);
|
2014-10-23 02:28:07 +04:00
|
|
|
if (openResult != JS::AsmJSCache_Success) {
|
|
|
|
return openResult;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off the AsmJSCookieType from the buffer returned to the caller,
|
|
|
|
// which expects a buffer of aSize, not a buffer of sizeWithCookie starting
|
|
|
|
// with a cookie.
|
2015-08-29 08:45:24 +03:00
|
|
|
*aMemory = (uint8_t*) childRunnable->MappedMemory() + sizeof(AsmJSCookieType);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// The caller guarnatees a call to CloseEntryForWrite (on success or
|
|
|
|
// failure) at which point the file will be closed
|
2015-08-29 08:45:24 +03:00
|
|
|
childRunnable.Forget(reinterpret_cast<ChildRunnable**>(aHandle));
|
2014-10-23 02:28:07 +04:00
|
|
|
return JS::AsmJSCache_Success;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 21:58:00 +04:00
|
|
|
CloseEntryForWrite(size_t aSize,
|
2013-11-19 01:49:53 +04:00
|
|
|
uint8_t* aMemory,
|
2015-08-29 08:45:24 +03:00
|
|
|
intptr_t aHandle)
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2015-08-29 08:45:24 +03:00
|
|
|
ChildRunnable::AutoClose childRunnable(
|
|
|
|
reinterpret_cast<ChildRunnable*>(aHandle));
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2015-08-29 08:45:24 +03:00
|
|
|
MOZ_ASSERT(aSize + sizeof(AsmJSCookieType) == childRunnable->FileSize());
|
|
|
|
MOZ_ASSERT(aMemory - sizeof(AsmJSCookieType) ==
|
|
|
|
childRunnable->MappedMemory());
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
// Flush to disk before writing the cookie (see OpenEntryForRead).
|
2015-08-29 08:45:24 +03:00
|
|
|
if (PR_SyncMemMap(childRunnable->FileDesc(),
|
|
|
|
childRunnable->MappedMemory(),
|
|
|
|
childRunnable->FileSize()) == PR_SUCCESS) {
|
|
|
|
*(AsmJSCookieType*)childRunnable->MappedMemory() = sAsmJSCookie;
|
2013-11-19 01:49:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Client : public quota::Client
|
|
|
|
{
|
2016-11-15 14:09:07 +03:00
|
|
|
~Client() override = default;
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2013-11-19 01:49:53 +04:00
|
|
|
public:
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
2015-03-21 19:28:04 +03:00
|
|
|
AddRef() override;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
2015-03-21 19:28:04 +03:00
|
|
|
Release() override;
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
Type
|
2015-03-21 19:28:04 +03:00
|
|
|
GetType() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
return ASMJS;
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
nsresult
|
2013-11-19 01:49:53 +04:00
|
|
|
InitOrigin(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin,
|
2015-03-21 19:28:04 +03:00
|
|
|
UsageInfo* aUsageInfo) override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
2014-03-06 00:47:10 +04:00
|
|
|
if (!aUsageInfo) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-19 01:49:53 +04:00
|
|
|
return GetUsageForOrigin(aPersistenceType, aGroup, aOrigin, aUsageInfo);
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
nsresult
|
2013-11-19 01:49:53 +04:00
|
|
|
GetUsageForOrigin(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin,
|
2015-03-21 19:28:04 +03:00
|
|
|
UsageInfo* aUsageInfo) override
|
2013-11-19 01:49:53 +04:00
|
|
|
{
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_ASSERT(qm, "We were being called by the QuotaManager");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> directory;
|
|
|
|
nsresult rv = qm->GetDirectoryForOrigin(aPersistenceType, aOrigin,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(directory, "We're here because the origin directory exists");
|
|
|
|
|
|
|
|
rv = directory->Append(NS_LITERAL_STRING(ASMJSCACHE_DIRECTORY_NAME));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
DebugOnly<bool> exists;
|
2013-11-19 01:49:53 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(directory->Exists(&exists)) && exists);
|
|
|
|
|
2013-12-05 08:32:52 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> entries;
|
|
|
|
rv = directory->GetDirectoryEntries(getter_AddRefs(entries));
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-03-06 00:47:10 +04:00
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED((rv = entries->HasMoreElements(&hasMore))) &&
|
|
|
|
hasMore && !aUsageInfo->Canceled()) {
|
2013-12-05 08:32:52 +04:00
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
rv = entries->GetNext(getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(entry);
|
|
|
|
NS_ENSURE_TRUE(file, NS_NOINTERFACE);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
int64_t fileSize;
|
2013-12-05 08:32:52 +04:00
|
|
|
rv = file->GetFileSize(&fileSize);
|
2013-11-19 01:49:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
MOZ_ASSERT(fileSize >= 0, "Negative size?!");
|
|
|
|
|
|
|
|
// Since the client is not explicitly storing files, append to database
|
|
|
|
// usage which represents implicit storage allocation.
|
|
|
|
aUsageInfo->AppendToDatabaseUsage(uint64_t(fileSize));
|
|
|
|
}
|
2014-03-06 00:47:10 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-11-19 01:49:53 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2013-11-19 01:49:53 +04:00
|
|
|
OnOriginClearCompleted(PersistenceType aPersistenceType,
|
2014-11-28 11:44:12 +03:00
|
|
|
const nsACString& aOrigin)
|
2015-03-21 19:28:04 +03:00
|
|
|
override
|
2013-11-19 01:49:53 +04:00
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-03-21 19:28:04 +03:00
|
|
|
ReleaseIOThreadObjects() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-06-30 15:59:27 +03:00
|
|
|
AbortOperations(const nsACString& aOrigin) override
|
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-06-30 15:59:27 +03:00
|
|
|
AbortOperationsForProcess(ContentParentId aContentParentId) override
|
|
|
|
{ }
|
2013-11-19 01:49:53 +04:00
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-11-22 12:44:06 +03:00
|
|
|
StartIdleMaintenance() override
|
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-11-22 12:44:06 +03:00
|
|
|
StopIdleMaintenance() override
|
2015-04-30 23:46:51 +03:00
|
|
|
{ }
|
|
|
|
|
2016-11-15 14:09:07 +03:00
|
|
|
void
|
2015-04-14 11:57:41 +03:00
|
|
|
ShutdownWorkThreads() override
|
2013-11-19 01:49:53 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoRefCnt mRefCnt;
|
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(asmjscache::Client)
|
|
|
|
NS_IMPL_RELEASE(asmjscache::Client)
|
|
|
|
|
|
|
|
quota::Client*
|
|
|
|
CreateClient()
|
|
|
|
{
|
|
|
|
return new Client();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace asmjscache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-12-05 08:32:52 +04:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
using mozilla::dom::asmjscache::Metadata;
|
|
|
|
using mozilla::dom::asmjscache::WriteParams;
|
|
|
|
|
|
|
|
void
|
|
|
|
ParamTraits<Metadata>::Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
2016-11-14 12:31:11 +03:00
|
|
|
for (auto entry : aParam.mEntries) {
|
2013-12-05 08:32:52 +04:00
|
|
|
WriteParam(aMsg, entry.mFastHash);
|
|
|
|
WriteParam(aMsg, entry.mNumChars);
|
|
|
|
WriteParam(aMsg, entry.mFullHash);
|
|
|
|
WriteParam(aMsg, entry.mModuleIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-04-21 07:09:15 +03:00
|
|
|
ParamTraits<Metadata>::Read(const Message* aMsg, PickleIterator* aIter,
|
2013-12-05 08:32:52 +04:00
|
|
|
paramType* aResult)
|
|
|
|
{
|
2016-11-14 12:31:11 +03:00
|
|
|
for (auto& entry : aResult->mEntries) {
|
2013-12-05 08:32:52 +04:00
|
|
|
if (!ReadParam(aMsg, aIter, &entry.mFastHash) ||
|
|
|
|
!ReadParam(aMsg, aIter, &entry.mNumChars) ||
|
|
|
|
!ReadParam(aMsg, aIter, &entry.mFullHash) ||
|
|
|
|
!ReadParam(aMsg, aIter, &entry.mModuleIndex))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParamTraits<Metadata>::Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
2016-11-14 12:31:11 +03:00
|
|
|
for (auto entry : aParam.mEntries) {
|
2013-12-05 08:32:52 +04:00
|
|
|
LogParam(entry.mFastHash, aLog);
|
|
|
|
LogParam(entry.mNumChars, aLog);
|
|
|
|
LogParam(entry.mFullHash, aLog);
|
|
|
|
LogParam(entry.mModuleIndex, aLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParamTraits<WriteParams>::Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
|
|
|
WriteParam(aMsg, aParam.mSize);
|
|
|
|
WriteParam(aMsg, aParam.mFastHash);
|
|
|
|
WriteParam(aMsg, aParam.mNumChars);
|
|
|
|
WriteParam(aMsg, aParam.mFullHash);
|
2014-03-06 00:47:10 +04:00
|
|
|
WriteParam(aMsg, aParam.mInstalled);
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-04-21 07:09:15 +03:00
|
|
|
ParamTraits<WriteParams>::Read(const Message* aMsg, PickleIterator* aIter,
|
2013-12-05 08:32:52 +04:00
|
|
|
paramType* aResult)
|
|
|
|
{
|
|
|
|
return ReadParam(aMsg, aIter, &aResult->mSize) &&
|
|
|
|
ReadParam(aMsg, aIter, &aResult->mFastHash) &&
|
|
|
|
ReadParam(aMsg, aIter, &aResult->mNumChars) &&
|
2014-03-06 00:47:10 +04:00
|
|
|
ReadParam(aMsg, aIter, &aResult->mFullHash) &&
|
|
|
|
ReadParam(aMsg, aIter, &aResult->mInstalled);
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParamTraits<WriteParams>::Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
|
|
|
LogParam(aParam.mSize, aLog);
|
|
|
|
LogParam(aParam.mFastHash, aLog);
|
|
|
|
LogParam(aParam.mNumChars, aLog);
|
|
|
|
LogParam(aParam.mFullHash, aLog);
|
2014-03-06 00:47:10 +04:00
|
|
|
LogParam(aParam.mInstalled, aLog);
|
2013-12-05 08:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace IPC
|