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"
16 #include "nsHashKeys.h"
18 #include "nsTArrayForwardDeclare.h"
19 #include "nsTHashSet.h"
22 struct already_AddRefed
;
31 class PBackgroundParent
;
41 class FileHandleThreadPool final
{
42 class FileHandleQueue
;
43 struct DelayedEnqueueInfo
;
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
;
58 static already_AddRefed
<FileHandleThreadPool
> Create();
61 void AssertIsOnOwningThread() const;
63 nsIEventTarget
* GetThreadPoolEventTarget() const;
65 void AssertIsOnOwningThread() const {}
68 void Enqueue(FileHandle
* aFileHandle
, FileHandleOp
* aFileHandleOp
,
71 NS_INLINE_DECL_REFCOUNTING(FileHandleThreadPool
)
73 void WaitForDirectoriesToComplete(nsTArray
<nsCString
>&& aDirectoryIds
,
74 nsIRunnable
* aCallback
);
79 FileHandleThreadPool();
82 ~FileHandleThreadPool();
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
;
99 FileHandleStorage mStorage
;
102 bool mActorDestroyed
;
105 nsCOMPtr
<nsIFile
> mFile
;
108 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundMutableFileParentBase
)
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();
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; }
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
);
172 } // namespace mozilla
174 #endif // mozilla_dom_filehandle_ActorsParent_h