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 {
|
2016-02-17 00:46:08 +03:00
|
|
|
|
|
|
|
using namespace mozilla::dom::indexedDB;
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
IDBFileRequest::IDBFileRequest(IDBFileHandle* aFileHandle,
|
2015-09-09 14:15:05 +03:00
|
|
|
bool aWrapAsDOMRequest)
|
2019-03-29 21:05:11 +03:00
|
|
|
: DOMRequest(aFileHandle->GetOwnerGlobal()),
|
2015-09-09 14:15:05 +03:00
|
|
|
mFileHandle(aFileHandle),
|
|
|
|
mWrapAsDOMRequest(aWrapAsDOMRequest),
|
2017-06-07 13:36:42 +03:00
|
|
|
mHasEncoding(false) {
|
|
|
|
MOZ_ASSERT(aFileHandle);
|
|
|
|
aFileHandle->AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBFileRequest::~IDBFileRequest() { AssertIsOnOwningThread(); }
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBFileRequest> IDBFileRequest::Create(
|
2017-06-07 13:36:42 +03:00
|
|
|
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-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDBFileRequest> request =
|
2017-06-07 13:36:42 +03:00
|
|
|
new IDBFileRequest(aFileHandle, aWrapAsDOMRequest);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
void IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal) {
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2019-04-01 19:46:46 +03:00
|
|
|
if (NS_FAILED(CheckCurrentGlobalCorrectness())) {
|
2017-06-07 13:36:42 +03:00
|
|
|
return;
|
2015-09-09 14:15:05 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mLengthComputable = false;
|
|
|
|
init.mLoaded = aLoaded;
|
|
|
|
init.mTotal = aTotal;
|
2015-09-09 14:15:05 +03:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
RefPtr<ProgressEvent> event =
|
|
|
|
ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
|
|
|
|
DispatchTrustedEvent(event);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
void IDBFileRequest::SetResultCallback(ResultCallback* aCallback) {
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aCallback);
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2015-09-09 14:15:05 +03:00
|
|
|
AutoJSAPI autoJS;
|
2019-03-29 21:05:11 +03:00
|
|
|
if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
|
2015-09-09 14:15:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileRequest)
|
2017-06-07 13:36:42 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest, mFileHandle)
|
|
|
|
|
|
|
|
void IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
2014-07-17 20:40:54 +04:00
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
aVisitor.mCanHandle = true;
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.SetParentTarget(mFileHandle, false);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
// virtual
|
|
|
|
JSObject* IDBFileRequest::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2015-09-09 14:15:05 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2017-06-07 13:36:42 +03:00
|
|
|
if (mWrapAsDOMRequest) {
|
|
|
|
return DOMRequest::WrapObject(aCx, aGivenProto);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
2018-06-26 00:20:54 +03:00
|
|
|
return IDBFileRequest_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-17 20:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|