2012-06-03 20:33:52 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "FileHelper.h"
|
|
|
|
|
|
|
|
#include "FileHandle.h"
|
|
|
|
#include "FileRequest.h"
|
|
|
|
#include "FileService.h"
|
2014-05-07 18:32:12 +04:00
|
|
|
#include "js/Value.h"
|
|
|
|
#include "MainThreadUtils.h"
|
2014-05-07 18:33:02 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-06-12 07:35:29 +04:00
|
|
|
#include "MutableFile.h"
|
2014-05-07 18:32:12 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsIRequest.h"
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-05-07 18:32:12 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
FileHandleBase* gCurrentFileHandle = nullptr;
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
FileHelper::FileHelper(FileHandleBase* aFileHandle,
|
|
|
|
FileRequestBase* aFileRequest)
|
|
|
|
: mMutableFile(aFileHandle->MutableFile()),
|
2014-06-12 07:35:29 +04:00
|
|
|
mFileHandle(aFileHandle),
|
2012-06-03 20:33:52 +04:00
|
|
|
mFileRequest(aFileRequest),
|
|
|
|
mResultCode(NS_OK),
|
|
|
|
mFinished(false)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHelper::~FileHelper()
|
|
|
|
{
|
2014-06-12 07:35:29 +04:00
|
|
|
MOZ_ASSERT(!mMutableFile && !mFileHandle && !mFileRequest && !mListener &&
|
2014-05-07 18:33:02 +04:00
|
|
|
!mRequest, "Should have cleared this!");
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(FileHelper, nsIRequestObserver)
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileHelper::Enqueue()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
FileService* service = FileService::GetOrCreate();
|
|
|
|
NS_ENSURE_TRUE(service, NS_ERROR_FAILURE);
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
nsresult rv = service->Enqueue(mFileHandle, this);
|
2012-06-03 20:33:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
if (mFileHandle) {
|
|
|
|
mFileHandle->OnNewRequest();
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileHelper::AsyncRun(FileHelperListener* aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
// Assign the listener early, so we can use it synchronously if the code
|
|
|
|
// below fails.
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> stream;
|
2014-07-17 20:40:54 +04:00
|
|
|
if (mFileHandle->mRequestMode == FileHandleBase::PARALLEL) {
|
2014-06-12 07:35:29 +04:00
|
|
|
rv = mFileHandle->CreateParallelStream(getter_AddRefs(stream));
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-06-12 07:35:29 +04:00
|
|
|
rv = mFileHandle->GetOrCreateStream(getter_AddRefs(stream));
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(stream, "This should never be null!");
|
|
|
|
|
|
|
|
rv = DoAsyncRun(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mResultCode = NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR;
|
|
|
|
Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileHelper::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileHelper::OnStopRequest(nsIRequest* aRequest, nsISupports* aCtxt,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2014-04-02 21:14:14 +04:00
|
|
|
if (aStatus == NS_ERROR_FILE_NO_DEVICE_SPACE) {
|
|
|
|
mResultCode = NS_ERROR_DOM_FILEHANDLE_QUOTA_ERR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mResultCode = NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR;
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Finish();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
FileHelper::OnStreamProgress(uint64_t aProgress, uint64_t aProgressMax)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
if (mFileHandle->IsAborted()) {
|
2012-06-03 20:33:52 +04:00
|
|
|
NS_ASSERTION(mRequest, "Should have a request!\n");
|
|
|
|
|
|
|
|
nsresult rv = mRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to cancel the request!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFileRequest) {
|
|
|
|
mFileRequest->OnProgress(aProgress, aProgressMax);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-07-17 20:40:54 +04:00
|
|
|
FileHandleBase*
|
2014-06-12 07:35:29 +04:00
|
|
|
FileHelper::GetCurrentFileHandle()
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
return gCurrentFileHandle;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileHelper::GetSuccessResult(JSContext* aCx,
|
2014-01-23 23:49:40 +04:00
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-01-23 23:49:40 +04:00
|
|
|
aVal.setUndefined();
|
2012-06-03 20:33:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileHelper::ReleaseObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
mMutableFile = nullptr;
|
2014-05-07 18:33:02 +04:00
|
|
|
mFileHandle = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFileRequest = nullptr;
|
|
|
|
mListener = nullptr;
|
|
|
|
mRequest = nullptr;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileHelper::Finish()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (mFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFinished = true;
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
if (mFileHandle->IsAborted()) {
|
2012-06-03 20:33:52 +04:00
|
|
|
// Always fire a "error" event with ABORT_ERR if the transaction was
|
|
|
|
// aborted, even if the request succeeded or failed with another error.
|
|
|
|
mResultCode = NS_ERROR_DOM_FILEHANDLE_ABORT_ERR;
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
FileHandleBase* oldFileHandle = gCurrentFileHandle;
|
2014-06-12 07:35:29 +04:00
|
|
|
gCurrentFileHandle = mFileHandle;
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
if (mFileRequest) {
|
|
|
|
nsresult rv = mFileRequest->NotifyHelperCompleted(this);
|
|
|
|
if (NS_SUCCEEDED(mResultCode) && NS_FAILED(rv)) {
|
|
|
|
mResultCode = rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
MOZ_ASSERT(gCurrentFileHandle == mFileHandle, "Should be unchanged!");
|
|
|
|
gCurrentFileHandle = oldFileHandle;
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
mFileHandle->OnRequestFinished();
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
mListener->OnFileHelperComplete(this);
|
|
|
|
|
|
|
|
ReleaseObjects();
|
|
|
|
|
2014-06-12 07:35:29 +04:00
|
|
|
MOZ_ASSERT(!(mMutableFile || mFileHandle || mFileRequest || mListener ||
|
2014-05-07 18:33:02 +04:00
|
|
|
mRequest), "Subclass didn't call FileHelper::ReleaseObjects!");
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
}
|
2013-09-19 17:54:39 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
FileHelper::OnStreamClose()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileHelper::OnStreamDestroy()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
Finish();
|
|
|
|
}
|
2014-05-07 18:32:12 +04:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|