summaryrefslogtreecommitdiff
path: root/dom/filehandle/ActorsParent.h
blob: 5d251715ed99f68d6b512828cee2705b224690ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_filehandle_ActorsParent_h
#define mozilla_dom_filehandle_ActorsParent_h

#include "mozilla/dom/FileHandleStorage.h"
#include "mozilla/dom/PBackgroundMutableFileParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "nsAutoPtr.h"
#include "nsClassHashtable.h"
#include "nsCOMPtr.h"
#include "nsHashKeys.h"
#include "nsString.h"
#include "nsTArrayForwardDeclare.h"
#include "nsTHashtable.h"

template <class> struct already_AddRefed;
class nsIFile;
class nsIRunnable;
class nsIThreadPool;

namespace mozilla {

namespace ipc {

class PBackgroundParent;

} // namespace ipc

namespace dom {

class BlobImpl;
class FileHandle;
class FileHandleOp;

class FileHandleThreadPool final
{
  class FileHandleQueue;
  struct DelayedEnqueueInfo;
  class DirectoryInfo;
  struct StoragesCompleteCallback;

  nsCOMPtr<nsIThreadPool> mThreadPool;
  nsCOMPtr<nsIEventTarget> mOwningThread;

  nsClassHashtable<nsCStringHashKey, DirectoryInfo> mDirectoryInfos;

  nsTArray<nsAutoPtr<StoragesCompleteCallback>> mCompleteCallbacks;

  bool mShutdownRequested;
  bool mShutdownComplete;

public:
  static already_AddRefed<FileHandleThreadPool>
  Create();

#ifdef DEBUG
  void
  AssertIsOnOwningThread() const;

  nsIEventTarget*
  GetThreadPoolEventTarget() const;
#else
  void
  AssertIsOnOwningThread() const
  { }
#endif

  void
  Enqueue(FileHandle* aFileHandle,
          FileHandleOp* aFileHandleOp,
          bool aFinish);

  NS_INLINE_DECL_REFCOUNTING(FileHandleThreadPool)

  void
  WaitForDirectoriesToComplete(nsTArray<nsCString>&& aDirectoryIds,
                               nsIRunnable* aCallback);

  void
  Shutdown();

private:
  FileHandleThreadPool();

  // Reference counted.
  ~FileHandleThreadPool();

  nsresult
  Init();

  void
  Cleanup();

  void
  FinishFileHandle(FileHandle* aFileHandle);

  bool
  MaybeFireCallback(StoragesCompleteCallback* aCallback);
};

class BackgroundMutableFileParentBase
  : public PBackgroundMutableFileParent
{
  nsTHashtable<nsPtrHashKey<FileHandle>> mFileHandles;
  nsCString mDirectoryId;
  nsString mFileName;
  FileHandleStorage mStorage;
  bool mInvalidated;
  bool mActorWasAlive;
  bool mActorDestroyed;

protected:
  nsCOMPtr<nsIFile> mFile;

public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundMutableFileParentBase)

  void
  Invalidate();

  FileHandleStorage
  Storage() const
  {
    return mStorage;
  }

  const nsCString&
  DirectoryId() const
  {
    return mDirectoryId;
  }

  const nsString&
  FileName() const
  {
    return mFileName;
  }

  bool
  RegisterFileHandle(FileHandle* aFileHandle);

  void
  UnregisterFileHandle(FileHandle* aFileHandle);

  void
  SetActorAlive();

  bool
  IsActorDestroyed() const
  {
    mozilla::ipc::AssertIsOnBackgroundThread();

    return mActorWasAlive && mActorDestroyed;
  }

  bool
  IsInvalidated() const
  {
    mozilla::ipc::AssertIsOnBackgroundThread();

    return mInvalidated;
  }

  virtual void
  NoteActiveState()
  { }

  virtual void
  NoteInactiveState()
  { }

  virtual mozilla::ipc::PBackgroundParent*
  GetBackgroundParent() const = 0;

  virtual already_AddRefed<nsISupports>
  CreateStream(bool aReadOnly);

  virtual already_AddRefed<BlobImpl>
  CreateBlobImpl()
  {
    return nullptr;
  }

protected:
  BackgroundMutableFileParentBase(FileHandleStorage aStorage,
                                  const nsACString& aDirectoryId,
                                  const nsAString& aFileName,
                                  nsIFile* aFile);

  // Reference counted.
  ~BackgroundMutableFileParentBase();

  // IPDL methods are only called by IPDL.
  virtual void
  ActorDestroy(ActorDestroyReason aWhy) override;

  virtual PBackgroundFileHandleParent*
  AllocPBackgroundFileHandleParent(const FileMode& aMode) override;

  virtual bool
  RecvPBackgroundFileHandleConstructor(PBackgroundFileHandleParent* aActor,
                                       const FileMode& aMode) override;

  virtual bool
  DeallocPBackgroundFileHandleParent(PBackgroundFileHandleParent* aActor)
                                     override;

  virtual bool
  RecvDeleteMe() override;

  virtual bool
  RecvGetFileId(int64_t* aFileId) override;
};

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_filehandle_ActorsParent_h