Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / indexedDB / IDBFileRequest.cpp
blobd774df24aaae0b9a349b761441d54f7f8f82de0d
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"
11 #include "jsapi.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"
18 #include "nsCOMPtr.h"
19 #include "nsDebug.h"
20 #include "nsError.h"
21 #include "nsIDOMEvent.h"
22 #include "nsIScriptContext.h"
23 #include "nsLiteralString.h"
25 namespace mozilla {
26 namespace dom {
27 namespace indexedDB {
29 IDBFileRequest::IDBFileRequest(nsPIDOMWindow* aWindow)
30 : DOMRequest(aWindow), mWrapAsDOMRequest(false)
34 IDBFileRequest::~IDBFileRequest()
38 // static
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();
52 nsresult
53 IDBFileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
55 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
57 aVisitor.mCanHandle = true;
58 aVisitor.mParentTarget = mFileHandle;
59 return NS_OK;
62 void
63 IDBFileRequest::OnProgress(uint64_t aProgress, uint64_t aProgressMax)
65 FireProgressEvent(aProgress, aProgressMax);
68 nsresult
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.
76 if (NS_FAILED(rv)) {
77 FireError(rv);
78 return NS_OK;
81 // Otherwise we need to get the result from the helper.
82 nsIScriptContext* sc = GetContextForEventHandlers(&rv);
83 NS_ENSURE_STATE(sc);
85 AutoJSContext cx;
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);
96 if (NS_FAILED(rv)) {
97 NS_WARNING("GetSuccessResult failed!");
100 if (NS_SUCCEEDED(rv)) {
101 FireSuccess(result);
103 else {
104 FireError(rv);
106 return NS_OK;
109 NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest,
110 mFileHandle)
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)
118 // virtual
119 JSObject*
120 IDBFileRequest::WrapObject(JSContext* aCx)
122 if (mWrapAsDOMRequest) {
123 return DOMRequest::WrapObject(aCx);
125 return IDBFileRequestBinding::Wrap(aCx, this);
129 IDBFileHandle*
130 IDBFileRequest::GetFileHandle() const
132 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
134 return static_cast<IDBFileHandle*>(mFileHandle.get());
137 void
138 IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
140 if (NS_FAILED(CheckInnerWindowCorrectness())) {
141 return;
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
157 } // namespace dom
158 } // namespace mozilla