1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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/. */
7 #include "IDBFileRequest.h"
9 #include "IDBFileHandle.h"
10 #include "js/RootingAPI.h"
12 #include "MainThreadUtils.h"
13 #include "mozilla/Assertions.h"
14 #include "mozilla/dom/FileHelper.h"
15 #include "mozilla/dom/IDBFileRequestBinding.h"
16 #include "mozilla/dom/ProgressEvent.h"
17 #include "mozilla/EventDispatcher.h"
21 #include "nsIDOMEvent.h"
22 #include "nsIScriptContext.h"
23 #include "nsLiteralString.h"
29 IDBFileRequest::IDBFileRequest(nsPIDOMWindow
* aWindow
)
30 : DOMRequest(aWindow
), mWrapAsDOMRequest(false)
34 IDBFileRequest::~IDBFileRequest()
39 already_AddRefed
<IDBFileRequest
>
40 IDBFileRequest::Create(nsPIDOMWindow
* aOwner
, IDBFileHandle
* aFileHandle
,
41 bool aWrapAsDOMRequest
)
43 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
45 nsRefPtr
<IDBFileRequest
> request
= new IDBFileRequest(aOwner
);
46 request
->mFileHandle
= aFileHandle
;
47 request
->mWrapAsDOMRequest
= aWrapAsDOMRequest
;
49 return request
.forget();
53 IDBFileRequest::PreHandleEvent(EventChainPreVisitor
& aVisitor
)
55 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
57 aVisitor
.mCanHandle
= true;
58 aVisitor
.mParentTarget
= mFileHandle
;
63 IDBFileRequest::OnProgress(uint64_t aProgress
, uint64_t aProgressMax
)
65 FireProgressEvent(aProgress
, aProgressMax
);
69 IDBFileRequest::NotifyHelperCompleted(FileHelper
* aFileHelper
)
71 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
73 nsresult rv
= aFileHelper
->ResultCode();
75 // If the request failed then fire error event and return.
81 // Otherwise we need to get the result from the helper.
82 nsIScriptContext
* sc
= GetContextForEventHandlers(&rv
);
86 MOZ_ASSERT(cx
, "Failed to get a context!");
88 JS::Rooted
<JS::Value
> result(cx
);
90 JS::Rooted
<JSObject
*> global(cx
, sc
->GetWindowProxy());
91 MOZ_ASSERT(global
, "Failed to get global object!");
93 JSAutoCompartment
ac(cx
, global
);
95 rv
= aFileHelper
->GetSuccessResult(cx
, &result
);
97 NS_WARNING("GetSuccessResult failed!");
100 if (NS_SUCCEEDED(rv
)) {
109 NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest
, DOMRequest
,
112 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileRequest
)
113 NS_INTERFACE_MAP_END_INHERITING(DOMRequest
)
115 NS_IMPL_ADDREF_INHERITED(IDBFileRequest
, DOMRequest
)
116 NS_IMPL_RELEASE_INHERITED(IDBFileRequest
, DOMRequest
)
120 IDBFileRequest::WrapObject(JSContext
* aCx
)
122 if (mWrapAsDOMRequest
) {
123 return DOMRequest::WrapObject(aCx
);
125 return IDBFileRequestBinding::Wrap(aCx
, this);
130 IDBFileRequest::GetFileHandle() const
132 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
134 return static_cast<IDBFileHandle
*>(mFileHandle
.get());
138 IDBFileRequest::FireProgressEvent(uint64_t aLoaded
, uint64_t aTotal
)
140 if (NS_FAILED(CheckInnerWindowCorrectness())) {
144 ProgressEventInit init
;
145 init
.mBubbles
= false;
146 init
.mCancelable
= false;
147 init
.mLengthComputable
= false;
148 init
.mLoaded
= aLoaded
;
149 init
.mTotal
= aTotal
;
151 nsRefPtr
<ProgressEvent
> event
=
152 ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init
);
153 DispatchTrustedEvent(event
);
156 } // namespace indexedDB
158 } // namespace mozilla