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::dom {
23 
24 using namespace mozilla::dom::indexedDB;
25 
IDBFileRequest(IDBFileHandle * aFileHandle,bool aWrapAsDOMRequest)26 IDBFileRequest::IDBFileRequest(IDBFileHandle* aFileHandle,
27                                bool aWrapAsDOMRequest)
28     : DOMRequest(aFileHandle->GetOwnerGlobal()),
29       mFileHandle(aFileHandle),
30       mWrapAsDOMRequest(aWrapAsDOMRequest),
31       mHasEncoding(false) {
32   MOZ_ASSERT(aFileHandle);
33   aFileHandle->AssertIsOnOwningThread();
34 }
35 
~IDBFileRequest()36 IDBFileRequest::~IDBFileRequest() { AssertIsOnOwningThread(); }
37 
38 // static
Create(IDBFileHandle * aFileHandle,bool aWrapAsDOMRequest)39 RefPtr<IDBFileRequest> IDBFileRequest::Create(IDBFileHandle* aFileHandle,
40                                               bool aWrapAsDOMRequest) {
41   MOZ_ASSERT(aFileHandle);
42   aFileHandle->AssertIsOnOwningThread();
43 
44   return new IDBFileRequest(aFileHandle, aWrapAsDOMRequest);
45 }
46 
FireProgressEvent(uint64_t aLoaded,uint64_t aTotal)47 void IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal) {
48   AssertIsOnOwningThread();
49 
50   if (NS_FAILED(CheckCurrentGlobalCorrectness())) {
51     return;
52   }
53 
54   ProgressEventInit init;
55   init.mBubbles = false;
56   init.mCancelable = false;
57   init.mLengthComputable = false;
58   init.mLoaded = aLoaded;
59   init.mTotal = aTotal;
60 
61   RefPtr<ProgressEvent> event =
62       ProgressEvent::Constructor(this, u"progress"_ns, init);
63   DispatchTrustedEvent(event);
64 }
65 
NS_IMPL_ADDREF_INHERITED(IDBFileRequest,DOMRequest)66 NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
67 NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
68 
69 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileRequest)
70 NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
71 
72 NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest, mFileHandle)
73 
74 void IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
75   AssertIsOnOwningThread();
76 
77   aVisitor.mCanHandle = true;
78   aVisitor.SetParentTarget(mFileHandle, false);
79 }
80 
81 // virtual
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)82 JSObject* IDBFileRequest::WrapObject(JSContext* aCx,
83                                      JS::Handle<JSObject*> aGivenProto) {
84   AssertIsOnOwningThread();
85 
86   if (mWrapAsDOMRequest) {
87     return DOMRequest::WrapObject(aCx, aGivenProto);
88   }
89   return IDBFileRequest_Binding::Wrap(aCx, this, aGivenProto);
90 }
91 
92 }  // namespace mozilla::dom
93