Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / filehandle / ActorsParent.h
blob319c50a0cf7399f20dc0975c617ce8b7e82a6daf
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_filehandle_ActorsParent_h
8 #define mozilla_dom_filehandle_ActorsParent_h
10 #include "mozilla/dom/FileHandleStorage.h"
11 #include "mozilla/dom/PBackgroundMutableFileParent.h"
12 #include "mozilla/ipc/BackgroundParent.h"
13 #include "mozilla/UniquePtr.h"
14 #include "nsClassHashtable.h"
15 #include "nsCOMPtr.h"
16 #include "nsHashKeys.h"
17 #include "nsString.h"
18 #include "nsTArrayForwardDeclare.h"
19 #include "nsTHashSet.h"
21 template <class>
22 struct already_AddRefed;
23 class nsIFile;
24 class nsIRunnable;
25 class nsIThreadPool;
27 namespace mozilla {
29 namespace ipc {
31 class PBackgroundParent;
33 } // namespace ipc
35 namespace dom {
37 class BlobImpl;
38 class FileHandle;
39 class FileHandleOp;
41 class FileHandleThreadPool final {
42 class FileHandleQueue;
43 struct DelayedEnqueueInfo;
44 class DirectoryInfo;
45 struct StoragesCompleteCallback;
47 nsCOMPtr<nsIThreadPool> mThreadPool;
48 nsCOMPtr<nsIEventTarget> mOwningEventTarget;
50 nsClassHashtable<nsCStringHashKey, DirectoryInfo> mDirectoryInfos;
52 nsTArray<UniquePtr<StoragesCompleteCallback>> mCompleteCallbacks;
54 bool mShutdownRequested;
55 bool mShutdownComplete;
57 public:
58 static already_AddRefed<FileHandleThreadPool> Create();
60 #ifdef DEBUG
61 void AssertIsOnOwningThread() const;
63 nsIEventTarget* GetThreadPoolEventTarget() const;
64 #else
65 void AssertIsOnOwningThread() const {}
66 #endif
68 void Enqueue(FileHandle* aFileHandle, FileHandleOp* aFileHandleOp,
69 bool aFinish);
71 NS_INLINE_DECL_REFCOUNTING(FileHandleThreadPool)
73 void WaitForDirectoriesToComplete(nsTArray<nsCString>&& aDirectoryIds,
74 nsIRunnable* aCallback);
76 void Shutdown();
78 private:
79 FileHandleThreadPool();
81 // Reference counted.
82 ~FileHandleThreadPool();
84 nsresult Init();
86 void Cleanup();
88 void FinishFileHandle(FileHandle* aFileHandle);
90 bool MaybeFireCallback(StoragesCompleteCallback* aCallback);
93 class BackgroundMutableFileParentBase : public PBackgroundMutableFileParent {
94 friend PBackgroundMutableFileParent;
96 nsTHashSet<FileHandle*> mFileHandles;
97 nsCString mDirectoryId;
98 nsString mFileName;
99 FileHandleStorage mStorage;
100 bool mInvalidated;
101 bool mActorWasAlive;
102 bool mActorDestroyed;
104 protected:
105 nsCOMPtr<nsIFile> mFile;
107 public:
108 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundMutableFileParentBase)
110 void Invalidate();
112 FileHandleStorage Storage() const { return mStorage; }
114 const nsCString& DirectoryId() const { return mDirectoryId; }
116 const nsString& FileName() const { return mFileName; }
118 bool RegisterFileHandle(FileHandle* aFileHandle);
120 void UnregisterFileHandle(FileHandle* aFileHandle);
122 void SetActorAlive();
124 bool IsActorDestroyed() const {
125 mozilla::ipc::AssertIsOnBackgroundThread();
127 return mActorWasAlive && mActorDestroyed;
130 bool IsInvalidated() const {
131 mozilla::ipc::AssertIsOnBackgroundThread();
133 return mInvalidated;
136 virtual void NoteActiveState() {}
138 virtual void NoteInactiveState() {}
140 virtual mozilla::ipc::PBackgroundParent* GetBackgroundParent() const = 0;
142 virtual already_AddRefed<nsISupports> CreateStream(bool aReadOnly);
144 virtual already_AddRefed<BlobImpl> CreateBlobImpl() { return nullptr; }
146 protected:
147 BackgroundMutableFileParentBase(FileHandleStorage aStorage,
148 const nsACString& aDirectoryId,
149 const nsAString& aFileName, nsIFile* aFile);
151 // Reference counted.
152 ~BackgroundMutableFileParentBase();
154 // IPDL methods are only called by IPDL.
155 virtual void ActorDestroy(ActorDestroyReason aWhy) override;
157 virtual PBackgroundFileHandleParent* AllocPBackgroundFileHandleParent(
158 const FileMode& aMode);
160 virtual mozilla::ipc::IPCResult RecvPBackgroundFileHandleConstructor(
161 PBackgroundFileHandleParent* aActor, const FileMode& aMode) override;
163 virtual bool DeallocPBackgroundFileHandleParent(
164 PBackgroundFileHandleParent* aActor);
166 mozilla::ipc::IPCResult RecvDeleteMe();
168 virtual mozilla::ipc::IPCResult RecvGetFileId(int64_t* aFileId);
171 } // namespace dom
172 } // namespace mozilla
174 #endif // mozilla_dom_filehandle_ActorsParent_h