Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
* You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "IDBFileRequest.h"
8
9
#include "IDBFileHandle.h"
10
#include "js/RootingAPI.h"
11
#include "jsapi.h"
12
#include "mozilla/Assertions.h"
13
#include "mozilla/dom/IDBFileRequestBinding.h"
14
#include "mozilla/dom/ProgressEvent.h"
15
#include "mozilla/dom/ScriptSettings.h"
16
#include "mozilla/EventDispatcher.h"
17
#include "nsCOMPtr.h"
18
#include "nsDebug.h"
19
#include "nsError.h"
20
#include "nsLiteralString.h"
21
22
namespace mozilla {
23
namespace dom {
24
25
using namespace mozilla::dom::indexedDB;
26
27
IDBFileRequest::IDBFileRequest(IDBFileHandle* aFileHandle,
28
bool aWrapAsDOMRequest)
29
: DOMRequest(aFileHandle->GetOwnerGlobal()),
30
mFileHandle(aFileHandle),
31
mWrapAsDOMRequest(aWrapAsDOMRequest),
32
mHasEncoding(false) {
33
MOZ_ASSERT(aFileHandle);
34
aFileHandle->AssertIsOnOwningThread();
35
}
36
37
IDBFileRequest::~IDBFileRequest() { AssertIsOnOwningThread(); }
38
39
// static
40
already_AddRefed<IDBFileRequest> IDBFileRequest::Create(
41
IDBFileHandle* aFileHandle, bool aWrapAsDOMRequest) {
42
MOZ_ASSERT(aFileHandle);
43
aFileHandle->AssertIsOnOwningThread();
44
45
RefPtr<IDBFileRequest> request =
46
new IDBFileRequest(aFileHandle, aWrapAsDOMRequest);
47
48
return request.forget();
49
}
50
51
void IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal) {
52
AssertIsOnOwningThread();
53
54
if (NS_FAILED(CheckCurrentGlobalCorrectness())) {
55
return;
56
}
57
58
ProgressEventInit init;
59
init.mBubbles = false;
60
init.mCancelable = false;
61
init.mLengthComputable = false;
62
init.mLoaded = aLoaded;
63
init.mTotal = aTotal;
64
65
RefPtr<ProgressEvent> event =
66
ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
67
DispatchTrustedEvent(event);
68
}
69
70
void IDBFileRequest::SetResultCallback(ResultCallback* aCallback) {
71
AssertIsOnOwningThread();
72
MOZ_ASSERT(aCallback);
73
74
AutoJSAPI autoJS;
75
if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
76
FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
77
return;
78
}
79
80
JSContext* cx = autoJS.cx();
81
82
JS::Rooted<JS::Value> result(cx);
83
nsresult rv = aCallback->GetResult(cx, &result);
84
if (NS_WARN_IF(NS_FAILED(rv))) {
85
FireError(rv);
86
} else {
87
FireSuccess(result);
88
}
89
}
90
91
NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
92
NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
93
94
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileRequest)
95
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
96
97
NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest, mFileHandle)
98
99
void IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
100
AssertIsOnOwningThread();
101
102
aVisitor.mCanHandle = true;
103
aVisitor.SetParentTarget(mFileHandle, false);
104
}
105
106
// virtual
107
JSObject* IDBFileRequest::WrapObject(JSContext* aCx,
108
JS::Handle<JSObject*> aGivenProto) {
109
AssertIsOnOwningThread();
110
111
if (mWrapAsDOMRequest) {
112
return DOMRequest::WrapObject(aCx, aGivenProto);
113
}
114
return IDBFileRequest_Binding::Wrap(aCx, this, aGivenProto);
115
}
116
117
} // namespace dom
118
} // namespace mozilla