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 #ifndef mozilla_dom_idbfilerequest_h__
8 #define mozilla_dom_idbfilerequest_h__
9 
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"
16 
17 namespace mozilla {
18 
19 class EventChainPreVisitor;
20 
21 namespace dom {
22 
23 class IDBFileHandle;
24 
25 class IDBFileRequest final : public DOMRequest {
26   RefPtr<IDBFileHandle> mFileHandle;
27 
28   nsString mEncoding;
29 
30   bool mWrapAsDOMRequest;
31   bool mHasEncoding;
32 
33  public:
34   [[nodiscard]] static RefPtr<IDBFileRequest> Create(IDBFileHandle* aFileHandle,
35                                                      bool aWrapAsDOMRequest);
36 
SetEncoding(const nsAString & aEncoding)37   void SetEncoding(const nsAString& aEncoding) {
38     mEncoding = aEncoding;
39     mHasEncoding = true;
40   }
41 
GetEncoding()42   const nsAString& GetEncoding() const { return mEncoding; }
43 
HasEncoding()44   bool HasEncoding() const { return mHasEncoding; }
45 
46   void FireProgressEvent(uint64_t aLoaded, uint64_t aTotal);
47 
48   template <typename ResultCallback>
SetResult(const ResultCallback & aCallback)49   void SetResult(const ResultCallback& aCallback) {
50     AssertIsOnOwningThread();
51 
52     AutoJSAPI autoJS;
53     if (NS_WARN_IF(!autoJS.Init(GetOwnerGlobal()))) {
54       FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
55       return;
56     }
57 
58     JSContext* cx = autoJS.cx();
59 
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);
66     }
67   }
68 
69   // WebIDL
GetFileHandle()70   IDBFileHandle* GetFileHandle() const {
71     AssertIsOnOwningThread();
72     return mFileHandle;
73   }
74 
GetLockedFile()75   IDBFileHandle* GetLockedFile() const {
76     AssertIsOnOwningThread();
77     return GetFileHandle();
78   }
79 
IMPL_EVENT_HANDLER(progress)80   IMPL_EVENT_HANDLER(progress)
81 
82   void AssertIsOnOwningThread() const {
83     NS_ASSERT_OWNINGTHREAD(IDBFileRequest);
84   }
85 
86   NS_DECL_ISUPPORTS_INHERITED
87   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileRequest, DOMRequest)
88 
89   // EventTarget
90   void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
91 
92   // nsWrapperCache
93   virtual JSObject* WrapObject(JSContext* aCx,
94                                JS::Handle<JSObject*> aGivenProto) override;
95 
96  private:
97   IDBFileRequest(IDBFileHandle* aFileHandle, bool aWrapAsDOMRequest);
98 
99   ~IDBFileRequest();
100 };
101 
102 }  // namespace dom
103 }  // namespace mozilla
104 
105 #endif  // mozilla_dom_idbfilerequest_h__
106