2014-07-17 20:40:54 +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: */
|
2014-07-17 20:40:54 +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 "IDBFileRequest.h"
|
|
|
|
|
|
|
|
#include "IDBFileHandle.h"
|
|
|
|
#include "js/RootingAPI.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/dom/IDBFileRequestBinding.h"
|
|
|
|
#include "mozilla/dom/ProgressEvent.h"
|
2015-09-09 14:15:05 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-07-17 20:40:54 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
IDBFileRequest::IDBFileRequest(nsPIDOMWindow* aWindow,
|
|
|
|
IDBFileHandle* aFileHandle,
|
|
|
|
bool aWrapAsDOMRequest)
|
|
|
|
: DOMRequest(aWindow)
|
|
|
|
, FileRequestBase(DEBUGONLY(aFileHandle->OwningThread()))
|
|
|
|
, mFileHandle(aFileHandle)
|
|
|
|
, mWrapAsDOMRequest(aWrapAsDOMRequest)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBFileRequest::~IDBFileRequest()
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBFileRequest>
|
|
|
|
IDBFileRequest::Create(nsPIDOMWindow* aOwner, IDBFileHandle* aFileHandle,
|
|
|
|
bool aWrapAsDOMRequest)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
MOZ_ASSERT(aFileHandle);
|
|
|
|
aFileHandle->AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
nsRefPtr<IDBFileRequest> request =
|
|
|
|
new IDBFileRequest(aOwner, aFileHandle, aWrapAsDOMRequest);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileRequest)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest,
|
|
|
|
mFileHandle)
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
nsresult
|
|
|
|
IDBFileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
aVisitor.mParentTarget = mFileHandle;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
// virtual
|
|
|
|
JSObject*
|
|
|
|
IDBFileRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (mWrapAsDOMRequest) {
|
|
|
|
return DOMRequest::WrapObject(aCx, aGivenProto);
|
|
|
|
}
|
|
|
|
return IDBFileRequestBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::dom::FileHandleBase*
|
|
|
|
IDBFileRequest::FileHandle() const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
return mFileHandle;
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
void
|
|
|
|
IDBFileRequest::OnProgress(uint64_t aProgress, uint64_t aProgressMax)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
FireProgressEvent(aProgress, aProgressMax);
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
void
|
|
|
|
IDBFileRequest::SetResultCallback(ResultCallback* aCallback)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aCallback);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
AutoJSAPI autoJS;
|
|
|
|
if (NS_WARN_IF(!autoJS.Init(GetOwner()))) {
|
|
|
|
FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
|
|
|
|
return;
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
JSContext* cx = autoJS.cx();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2015-09-09 14:15:05 +03:00
|
|
|
nsresult rv = aCallback->GetResult(cx, &result);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-07-17 20:40:54 +04:00
|
|
|
FireError(rv);
|
2015-09-09 14:15:05 +03:00
|
|
|
} else {
|
|
|
|
FireSuccess(result);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
void
|
|
|
|
IDBFileRequest::SetError(nsresult aError)
|
2014-07-17 20:40:54 +04:00
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
FireError(aError);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
|
|
|
|
{
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2014-07-17 20:40:54 +04:00
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mLengthComputable = false;
|
|
|
|
init.mLoaded = aLoaded;
|
|
|
|
init.mTotal = aTotal;
|
|
|
|
|
|
|
|
nsRefPtr<ProgressEvent> event =
|
|
|
|
ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
|
|
|
|
DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|