2012-06-03 09:33:52 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 12:32:37 -07:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-03 09:33:52 -07: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/. */
|
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
#include "IDBMutableFile.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "ActorsChild.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "FileInfo.h"
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "FileSnapshot.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "IDBDatabase.h"
|
|
|
|
#include "IDBFactory.h"
|
|
|
|
#include "IDBFileHandle.h"
|
|
|
|
#include "IDBFileRequest.h"
|
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-07-17 09:40:54 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-06-11 20:35:29 -07:00
|
|
|
#include "mozilla/dom/IDBMutableFileBinding.h"
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "mozilla/dom/filehandle/ActorsChild.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "mozilla/dom/quota/FileStreams.h"
|
2014-05-07 07:33:02 -07:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2014-07-17 09:40:54 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "nsIInputStream.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2015-09-09 04:15:05 -07:00
|
|
|
#include "ReportInternalError.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
using namespace mozilla::dom::quota;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
IDBMutableFile::IDBMutableFile(IDBDatabase* aDatabase,
|
2015-09-09 04:15:05 -07:00
|
|
|
BackgroundMutableFileChild* aActor,
|
2014-09-26 16:21:57 -07:00
|
|
|
const nsAString& aName,
|
2015-09-09 04:15:05 -07:00
|
|
|
const nsAString& aType)
|
2014-09-26 16:21:57 -07:00
|
|
|
: DOMEventTargetHelper(aDatabase)
|
2015-09-09 04:15:05 -07:00
|
|
|
, MutableFileBase(DEBUGONLY(aDatabase->OwningThread(),)
|
|
|
|
aActor)
|
2014-09-26 16:21:57 -07:00
|
|
|
, mDatabase(aDatabase)
|
2015-09-09 04:15:05 -07:00
|
|
|
, mName(aName)
|
|
|
|
, mType(aType)
|
2014-09-26 16:21:57 -07:00
|
|
|
, mInvalidated(false)
|
2014-09-17 16:36:01 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aDatabase);
|
|
|
|
aDatabase->AssertIsOnOwningThread();
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
mDatabase->NoteLiveMutableFile(this);
|
2014-01-06 18:53:23 -08:00
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
IDBMutableFile::~IDBMutableFile()
|
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-09-17 16:36:01 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
mDatabase->NoteFinishedMutableFile(this);
|
2014-09-26 16:21:57 -07:00
|
|
|
}
|
2014-09-17 16:36:01 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
int64_t
|
|
|
|
IDBMutableFile::GetFileId() const
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-11-28 00:44:12 -08:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
int64_t fileId;
|
|
|
|
if (!mBackgroundActor ||
|
|
|
|
NS_WARN_IF(!mBackgroundActor->SendGetFileId(&fileId))) {
|
|
|
|
return -1;
|
2014-09-26 16:21:57 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return fileId;
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
void
|
|
|
|
IDBMutableFile::Invalidate()
|
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-09-26 16:21:57 -07:00
|
|
|
MOZ_ASSERT(!mInvalidated);
|
|
|
|
|
|
|
|
mInvalidated = true;
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
AbortFileHandles();
|
2014-05-07 07:33:02 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
void
|
|
|
|
IDBMutableFile::RegisterFileHandle(IDBFileHandle* aFileHandle)
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aFileHandle);
|
|
|
|
aFileHandle->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mFileHandles.Contains(aFileHandle));
|
|
|
|
|
|
|
|
mFileHandles.PutEntry(aFileHandle);
|
2014-05-07 07:33:02 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
void
|
|
|
|
IDBMutableFile::UnregisterFileHandle(IDBFileHandle* aFileHandle)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aFileHandle);
|
|
|
|
aFileHandle->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mFileHandles.Contains(aFileHandle));
|
2014-09-26 16:21:57 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
mFileHandles.RemoveEntry(aFileHandle);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
void
|
|
|
|
IDBMutableFile::AbortFileHandles()
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
class MOZ_STACK_CLASS Helper final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void
|
|
|
|
AbortFileHandles(nsTHashtable<nsPtrHashKey<IDBFileHandle>>& aTable)
|
|
|
|
{
|
|
|
|
if (!aTable.Count()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
nsTArray<RefPtr<IDBFileHandle>> fileHandlesToAbort;
|
2015-09-09 04:15:05 -07:00
|
|
|
fileHandlesToAbort.SetCapacity(aTable.Count());
|
|
|
|
|
|
|
|
for (auto iter = aTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
IDBFileHandle* fileHandle = iter.Get()->GetKey();
|
|
|
|
MOZ_ASSERT(fileHandle);
|
|
|
|
|
|
|
|
fileHandle->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (!fileHandle->IsDone()) {
|
|
|
|
fileHandlesToAbort.AppendElement(iter.Get()->GetKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(fileHandlesToAbort.Length() <= aTable.Count());
|
|
|
|
|
|
|
|
if (fileHandlesToAbort.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
for (RefPtr<IDBFileHandle>& fileHandle : fileHandlesToAbort) {
|
2015-09-09 04:15:05 -07:00
|
|
|
MOZ_ASSERT(fileHandle);
|
|
|
|
|
|
|
|
fileHandle->Abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Helper::AbortFileHandles(mFileHandles);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
IDBDatabase*
|
|
|
|
IDBMutableFile::Database() const
|
2014-02-24 12:56:13 -08:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
return mDatabase;
|
2014-02-24 12:56:13 -08:00
|
|
|
}
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
already_AddRefed<IDBFileHandle>
|
|
|
|
IDBMutableFile::Open(FileMode aMode, ErrorResult& aError)
|
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
if (QuotaManager::IsShuttingDown() ||
|
|
|
|
mDatabase->IsClosed() ||
|
|
|
|
!GetOwner()) {
|
2014-07-17 09:40:54 -07:00
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBFileHandle> fileHandle =
|
2015-09-09 04:15:05 -07:00
|
|
|
IDBFileHandle::Create(this, aMode);
|
|
|
|
if (NS_WARN_IF(!fileHandle)) {
|
2014-07-17 09:40:54 -07:00
|
|
|
aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
BackgroundFileHandleChild* actor =
|
|
|
|
new BackgroundFileHandleChild(DEBUGONLY(mBackgroundActor->OwningThread(),)
|
|
|
|
fileHandle);
|
2014-09-26 16:21:57 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
MOZ_ALWAYS_TRUE(
|
|
|
|
mBackgroundActor->SendPBackgroundFileHandleConstructor(actor, aMode));
|
2014-09-26 16:21:57 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
fileHandle->SetBackgroundActor(actor);
|
2015-05-12 05:09:51 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return fileHandle.forget();
|
2014-09-26 16:21:57 -07:00
|
|
|
}
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
IDBMutableFile::GetFile(ErrorResult& aError)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBFileHandle> fileHandle = Open(FileMode::Readonly, aError);
|
2015-09-09 04:15:05 -07:00
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
2015-03-02 13:14:22 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
FileRequestGetFileParams params;
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBFileRequest> request =
|
2014-09-26 16:21:57 -07:00
|
|
|
IDBFileRequest::Create(GetOwner(),
|
|
|
|
fileHandle,
|
|
|
|
/* aWrapAsDOMRequest */ true);
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
fileHandle->StartRequest(request, params);
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return request.forget();
|
|
|
|
}
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBMutableFile, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBMutableFile, DOMEventTargetHelper)
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBMutableFile)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBMutableFile)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBMutableFile,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->AssertIsOnOwningThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDatabase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBMutableFile,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// Don't unlink mDatabase!
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
IDBMutableFile::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return IDBMutableFileBinding::Wrap(aCx, this, aGivenProto);
|
2014-07-17 09:40:54 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
const nsString&
|
|
|
|
IDBMutableFile::Name() const
|
2014-07-17 09:40:54 -07:00
|
|
|
{
|
2015-09-09 04:15:05 -07:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return mName;
|
|
|
|
}
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
const nsString&
|
|
|
|
IDBMutableFile::Type() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IDBMutableFile::IsInvalidated()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2015-09-09 04:15:05 -07:00
|
|
|
return mInvalidated;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
IDBMutableFile::CreateFileFor(BlobImpl* aBlobImpl,
|
|
|
|
FileHandleBase* aFileHandle)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<BlobImpl> blobImplSnapshot =
|
2015-09-09 04:15:05 -07:00
|
|
|
new BlobImplSnapshot(aBlobImpl, static_cast<IDBFileHandle*>(aFileHandle));
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<File> file = File::Create(GetOwner(), blobImplSnapshot);
|
2015-09-09 04:15:05 -07:00
|
|
|
return file.forget();
|
2014-07-17 09:40:54 -07:00
|
|
|
}
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|