|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=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_file_fileservice_h__ |
|
8 #define mozilla_dom_file_fileservice_h__ |
|
9 |
|
10 #include "FileCommon.h" |
|
11 |
|
12 #include "nsIObserver.h" |
|
13 |
|
14 #include "nsClassHashtable.h" |
|
15 #include "mozilla/Attributes.h" |
|
16 |
|
17 #include "mozilla/dom/file/FileHelper.h" |
|
18 #include "mozilla/dom/file/LockedFile.h" |
|
19 |
|
20 BEGIN_FILE_NAMESPACE |
|
21 |
|
22 class FileService MOZ_FINAL : public nsIObserver |
|
23 { |
|
24 public: |
|
25 NS_DECL_ISUPPORTS |
|
26 NS_DECL_NSIOBSERVER |
|
27 |
|
28 // Returns a non-owning reference! |
|
29 static FileService* |
|
30 GetOrCreate(); |
|
31 |
|
32 // Returns a non-owning reference! |
|
33 static FileService* |
|
34 Get(); |
|
35 |
|
36 static void |
|
37 Shutdown(); |
|
38 |
|
39 // Returns true if we've begun the shutdown process. |
|
40 static bool |
|
41 IsShuttingDown(); |
|
42 |
|
43 nsresult |
|
44 Enqueue(LockedFile* aLockedFile, FileHelper* aFileHelper); |
|
45 |
|
46 void |
|
47 NotifyLockedFileCompleted(LockedFile* aLockedFile); |
|
48 |
|
49 void |
|
50 WaitForStoragesToComplete(nsTArray<nsCOMPtr<nsIFileStorage> >& aStorages, |
|
51 nsIRunnable* aCallback); |
|
52 |
|
53 void |
|
54 AbortLockedFilesForStorage(nsIFileStorage* aFileStorage); |
|
55 |
|
56 bool |
|
57 HasLockedFilesForStorage(nsIFileStorage* aFileStorage); |
|
58 |
|
59 nsIEventTarget* |
|
60 StreamTransportTarget() |
|
61 { |
|
62 NS_ASSERTION(mStreamTransportTarget, "This should never be null!"); |
|
63 return mStreamTransportTarget; |
|
64 } |
|
65 |
|
66 private: |
|
67 class LockedFileQueue MOZ_FINAL : public FileHelperListener |
|
68 { |
|
69 friend class FileService; |
|
70 |
|
71 public: |
|
72 NS_IMETHOD_(MozExternalRefCountType) |
|
73 AddRef() MOZ_OVERRIDE; |
|
74 |
|
75 NS_IMETHOD_(MozExternalRefCountType) |
|
76 Release() MOZ_OVERRIDE; |
|
77 |
|
78 inline nsresult |
|
79 Enqueue(FileHelper* aFileHelper); |
|
80 |
|
81 virtual void |
|
82 OnFileHelperComplete(FileHelper* aFileHelper) MOZ_OVERRIDE; |
|
83 |
|
84 private: |
|
85 inline |
|
86 LockedFileQueue(LockedFile* aLockedFile); |
|
87 |
|
88 nsresult |
|
89 ProcessQueue(); |
|
90 |
|
91 ThreadSafeAutoRefCnt mRefCnt; |
|
92 NS_DECL_OWNINGTHREAD |
|
93 nsRefPtr<LockedFile> mLockedFile; |
|
94 nsTArray<nsRefPtr<FileHelper> > mQueue; |
|
95 nsRefPtr<FileHelper> mCurrentHelper; |
|
96 }; |
|
97 |
|
98 struct DelayedEnqueueInfo |
|
99 { |
|
100 nsRefPtr<LockedFile> mLockedFile; |
|
101 nsRefPtr<FileHelper> mFileHelper; |
|
102 }; |
|
103 |
|
104 class FileStorageInfo |
|
105 { |
|
106 friend class FileService; |
|
107 |
|
108 public: |
|
109 inline LockedFileQueue* |
|
110 CreateLockedFileQueue(LockedFile* aLockedFile); |
|
111 |
|
112 inline LockedFileQueue* |
|
113 GetLockedFileQueue(LockedFile* aLockedFile); |
|
114 |
|
115 void |
|
116 RemoveLockedFileQueue(LockedFile* aLockedFile); |
|
117 |
|
118 bool |
|
119 HasRunningLockedFiles() |
|
120 { |
|
121 return !mLockedFileQueues.IsEmpty(); |
|
122 } |
|
123 |
|
124 inline bool |
|
125 HasRunningLockedFiles(nsIFileStorage* aFileStorage); |
|
126 |
|
127 inline DelayedEnqueueInfo* |
|
128 CreateDelayedEnqueueInfo(LockedFile* aLockedFile, FileHelper* aFileHelper); |
|
129 |
|
130 inline void |
|
131 CollectRunningAndDelayedLockedFiles( |
|
132 nsIFileStorage* aFileStorage, |
|
133 nsTArray<nsRefPtr<LockedFile> >& aLockedFiles); |
|
134 |
|
135 void |
|
136 LockFileForReading(const nsAString& aFileName) |
|
137 { |
|
138 mFilesReading.PutEntry(aFileName); |
|
139 } |
|
140 |
|
141 void |
|
142 LockFileForWriting(const nsAString& aFileName) |
|
143 { |
|
144 mFilesWriting.PutEntry(aFileName); |
|
145 } |
|
146 |
|
147 bool |
|
148 IsFileLockedForReading(const nsAString& aFileName) |
|
149 { |
|
150 return mFilesReading.Contains(aFileName); |
|
151 } |
|
152 |
|
153 bool |
|
154 IsFileLockedForWriting(const nsAString& aFileName) |
|
155 { |
|
156 return mFilesWriting.Contains(aFileName); |
|
157 } |
|
158 |
|
159 private: |
|
160 FileStorageInfo() |
|
161 { |
|
162 } |
|
163 |
|
164 nsTArray<nsRefPtr<LockedFileQueue> > mLockedFileQueues; |
|
165 nsTArray<DelayedEnqueueInfo> mDelayedEnqueueInfos; |
|
166 nsTHashtable<nsStringHashKey> mFilesReading; |
|
167 nsTHashtable<nsStringHashKey> mFilesWriting; |
|
168 }; |
|
169 |
|
170 struct StoragesCompleteCallback |
|
171 { |
|
172 nsTArray<nsCOMPtr<nsIFileStorage> > mStorages; |
|
173 nsCOMPtr<nsIRunnable> mCallback; |
|
174 }; |
|
175 |
|
176 FileService(); |
|
177 ~FileService(); |
|
178 |
|
179 nsresult |
|
180 Init(); |
|
181 |
|
182 nsresult |
|
183 Cleanup(); |
|
184 |
|
185 bool |
|
186 MaybeFireCallback(StoragesCompleteCallback& aCallback); |
|
187 |
|
188 nsCOMPtr<nsIEventTarget> mStreamTransportTarget; |
|
189 nsClassHashtable<nsCStringHashKey, FileStorageInfo> mFileStorageInfos; |
|
190 nsTArray<StoragesCompleteCallback> mCompleteCallbacks; |
|
191 }; |
|
192 |
|
193 END_FILE_NAMESPACE |
|
194 |
|
195 #endif /* mozilla_dom_file_fileservice_h__ */ |