Line data Source code
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 0 : IDBFileRequest::IDBFileRequest(IDBFileHandle* aFileHandle,
28 0 : bool aWrapAsDOMRequest)
29 : : DOMRequest(aFileHandle->GetOwner())
30 : , mFileHandle(aFileHandle)
31 : , mWrapAsDOMRequest(aWrapAsDOMRequest)
32 0 : , mHasEncoding(false)
33 : {
34 0 : MOZ_ASSERT(aFileHandle);
35 0 : aFileHandle->AssertIsOnOwningThread();
36 0 : }
37 :
38 0 : IDBFileRequest::~IDBFileRequest()
39 : {
40 0 : AssertIsOnOwningThread();
41 0 : }
42 :
43 : // static
44 : already_AddRefed<IDBFileRequest>
45 0 : IDBFileRequest::Create(IDBFileHandle* aFileHandle,
46 : bool aWrapAsDOMRequest)
47 : {
48 0 : MOZ_ASSERT(aFileHandle);
49 0 : aFileHandle->AssertIsOnOwningThread();
50 :
51 : RefPtr<IDBFileRequest> request =
52 0 : new IDBFileRequest(aFileHandle, aWrapAsDOMRequest);
53 :
54 0 : return request.forget();
55 : }
56 :
57 : void
58 0 : IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
59 : {
60 0 : AssertIsOnOwningThread();
61 :
62 0 : if (NS_FAILED(CheckInnerWindowCorrectness())) {
63 0 : return;
64 : }
65 :
66 0 : ProgressEventInit init;
67 0 : init.mBubbles = false;
68 0 : init.mCancelable = false;
69 0 : init.mLengthComputable = false;
70 0 : init.mLoaded = aLoaded;
71 0 : init.mTotal = aTotal;
72 :
73 : RefPtr<ProgressEvent> event =
74 0 : ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
75 0 : DispatchTrustedEvent(event);
76 : }
77 :
78 : void
79 0 : IDBFileRequest::SetResultCallback(ResultCallback* aCallback)
80 : {
81 0 : AssertIsOnOwningThread();
82 0 : MOZ_ASSERT(aCallback);
83 :
84 0 : AutoJSAPI autoJS;
85 0 : if (NS_WARN_IF(!autoJS.Init(GetOwner()))) {
86 0 : FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
87 0 : return;
88 : }
89 :
90 0 : JSContext* cx = autoJS.cx();
91 :
92 0 : JS::Rooted<JS::Value> result(cx);
93 0 : nsresult rv = aCallback->GetResult(cx, &result);
94 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
95 0 : FireError(rv);
96 : } else {
97 0 : FireSuccess(result);
98 : }
99 : }
100 :
101 0 : NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
102 0 : NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
103 :
104 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileRequest)
105 0 : NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
106 :
107 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest,
108 : mFileHandle)
109 :
110 : nsresult
111 0 : IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor)
112 : {
113 0 : AssertIsOnOwningThread();
114 :
115 0 : aVisitor.mCanHandle = true;
116 0 : aVisitor.mParentTarget = mFileHandle;
117 0 : return NS_OK;
118 : }
119 :
120 : // virtual
121 : JSObject*
122 0 : IDBFileRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
123 : {
124 0 : AssertIsOnOwningThread();
125 :
126 0 : if (mWrapAsDOMRequest) {
127 0 : return DOMRequest::WrapObject(aCx, aGivenProto);
128 : }
129 0 : return IDBFileRequestBinding::Wrap(aCx, this, aGivenProto);
130 : }
131 :
132 : } // namespace dom
133 : } // namespace mozilla
|