Bug 1805294 [wpt PR 37463] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / dom / indexedDB / IDBFileRequest.h
blob2de8bbbcae365bc159e4696b009f424c4d028600
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/. */
7 #ifndef mozilla_dom_idbfilerequest_h__
8 #define mozilla_dom_idbfilerequest_h__
10 #include "DOMRequest.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/ScriptSettings.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsString.h"
17 namespace mozilla {
19 class EventChainPreVisitor;
21 namespace dom {
23 class IDBFileHandle;
25 class IDBFileRequest final : public DOMRequest {
26 RefPtr<IDBFileHandle> mFileHandle;
28 nsString mEncoding;
30 bool mWrapAsDOMRequest;
31 bool mHasEncoding;
33 public:
34 [[nodiscard]] static RefPtr<IDBFileRequest> Create(IDBFileHandle* aFileHandle,
35 bool aWrapAsDOMRequest);
37 void SetEncoding(const nsAString& aEncoding) {
38 mEncoding = aEncoding;
39 mHasEncoding = true;
42 const nsAString& GetEncoding() const { return mEncoding; }
44 bool HasEncoding() const { return mHasEncoding; }
46 void FireProgressEvent(uint64_t aLoaded, uint64_t aTotal);
48 template <typename ResultCallback>
49 void SetResult(const ResultCallback& aCallback) {
50 AssertIsOnOwningThread();
52 AutoJSAPI autoJS;
53 if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
54 FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
55 return;
58 JSContext* cx = autoJS.cx();
60 JS::Rooted<JS::Value> result(cx);
61 nsresult rv = aCallback(cx, &result);
62 if (NS_WARN_IF(NS_FAILED(rv))) {
63 FireError(rv);
64 } else {
65 FireSuccess(result);
69 // WebIDL
70 IDBFileHandle* GetFileHandle() const {
71 AssertIsOnOwningThread();
72 return mFileHandle;
75 IDBFileHandle* GetLockedFile() const {
76 AssertIsOnOwningThread();
77 return GetFileHandle();
80 IMPL_EVENT_HANDLER(progress)
82 void AssertIsOnOwningThread() const {
83 NS_ASSERT_OWNINGTHREAD(IDBFileRequest);
86 NS_DECL_ISUPPORTS_INHERITED
87 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileRequest, DOMRequest)
89 // EventTarget
90 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
92 // nsWrapperCache
93 virtual JSObject* WrapObject(JSContext* aCx,
94 JS::Handle<JSObject*> aGivenProto) override;
96 private:
97 IDBFileRequest(IDBFileHandle* aFileHandle, bool aWrapAsDOMRequest);
99 ~IDBFileRequest();
102 } // namespace dom
103 } // namespace mozilla
105 #endif // mozilla_dom_idbfilerequest_h__