Bumping manifests a=b2g-bump
[gecko.git] / dom / filehandle / FileHandle.h
blob30228293610eac3109a37f8eeba354d5b4cf5ae5
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/. */
7 #ifndef mozilla_dom_FileHandle_h
8 #define mozilla_dom_FileHandle_h
10 #include "MainThreadUtils.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/dom/FileModeBinding.h"
14 #include "mozilla/dom/Nullable.h"
15 #include "mozilla/dom/TypedArray.h"
16 #include "mozilla/ErrorResult.h"
17 #include "nsAutoPtr.h"
18 #include "nsCOMPtr.h"
19 #include "nsIInputStream.h"
20 #include "nsIRunnable.h"
21 #include "nsTArray.h"
23 class nsAString;
24 class nsIDOMBlob;
26 namespace mozilla {
27 namespace dom {
29 class FileHelper;
30 class FileRequestBase;
31 class FileService;
32 class FinishHelper;
33 class MetadataHelper;
34 class MutableFileBase;
36 /**
37 * This class provides a base for FileHandle implementations.
39 class FileHandleBase
41 public:
42 enum RequestMode
44 NORMAL = 0, // Sequential
45 PARALLEL
48 enum ReadyState
50 INITIAL = 0,
51 LOADING,
52 FINISHING,
53 DONE
56 private:
57 friend class FileHelper;
58 friend class FileService;
59 friend class FinishHelper;
60 friend class MetadataHelper;
62 ReadyState mReadyState;
63 FileMode mMode;
64 RequestMode mRequestMode;
65 uint64_t mLocation;
66 uint32_t mPendingRequests;
68 nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
69 nsCOMPtr<nsISupports> mStream;
71 bool mAborted;
72 bool mCreating;
74 public:
75 NS_IMETHOD_(MozExternalRefCountType)
76 AddRef() = 0;
78 NS_IMETHOD_(MozExternalRefCountType)
79 Release() = 0;
81 nsresult
82 CreateParallelStream(nsISupports** aStream);
84 nsresult
85 GetOrCreateStream(nsISupports** aStream);
87 bool
88 IsOpen() const;
90 bool
91 IsAborted() const
93 return mAborted;
96 void
97 SetCreating()
99 mCreating = true;
102 virtual MutableFileBase*
103 MutableFile() const = 0;
105 nsresult
106 OpenInputStream(bool aWholeFile, uint64_t aStart, uint64_t aLength,
107 nsIInputStream** aResult);
109 // Shared WebIDL (IndexedDB FileHandle and FileSystem FileHandle)
110 FileMode
111 Mode() const
113 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
115 return mMode;
118 bool
119 Active() const
121 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
123 return IsOpen();
126 Nullable<uint64_t>
127 GetLocation() const
129 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
131 if (mLocation == UINT64_MAX) {
132 return Nullable<uint64_t>();
135 return Nullable<uint64_t>(mLocation);
138 void
139 SetLocation(const Nullable<uint64_t>& aLocation)
141 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
143 // Null means the end-of-file.
144 if (aLocation.IsNull()) {
145 mLocation = UINT64_MAX;
146 } else {
147 mLocation = aLocation.Value();
151 already_AddRefed<FileRequestBase>
152 Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
153 ErrorResult& aRv);
155 already_AddRefed<FileRequestBase>
156 Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
158 already_AddRefed<FileRequestBase>
159 Flush(ErrorResult& aRv);
161 void
162 Abort(ErrorResult& aRv);
164 protected:
165 FileHandleBase(FileMode aMode,
166 RequestMode aRequestMode);
167 ~FileHandleBase();
169 void
170 OnNewRequest();
172 void
173 OnRequestFinished();
175 void
176 OnReturnToEventLoop();
178 virtual nsresult
179 OnCompleteOrAbort(bool aAborted) = 0;
181 bool
182 CheckState(ErrorResult& aRv);
184 bool
185 CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
187 bool
188 CheckStateForWrite(ErrorResult& aRv);
190 virtual bool
191 CheckWindow() = 0;
193 virtual already_AddRefed<FileRequestBase>
194 GenerateFileRequest() = 0;
196 template<class T>
197 already_AddRefed<FileRequestBase>
198 WriteOrAppend(const T& aValue, bool aAppend, ErrorResult& aRv)
200 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
202 // State checking for write
203 if (!CheckStateForWrite(aRv)) {
204 return nullptr;
207 // Additional state checking for write
208 if (!aAppend && mLocation == UINT64_MAX) {
209 aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
210 return nullptr;
213 uint64_t length;
214 nsCOMPtr<nsIInputStream> stream = GetInputStream(aValue, &length, aRv);
215 if (aRv.Failed()) {
216 return nullptr;
219 if (!length) {
220 return nullptr;
223 // Do nothing if the window is closed
224 if (!CheckWindow()) {
225 return nullptr;
228 return WriteInternal(stream, length, aAppend, aRv);
231 already_AddRefed<FileRequestBase>
232 WriteInternal(nsIInputStream* aInputStream, uint64_t aInputLength,
233 bool aAppend, ErrorResult& aRv);
235 nsresult
236 Finish();
238 static already_AddRefed<nsIInputStream>
239 GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength,
240 ErrorResult& aRv);
242 static already_AddRefed<nsIInputStream>
243 GetInputStream(nsIDOMBlob* aValue, uint64_t* aInputLength, ErrorResult& aRv);
245 static already_AddRefed<nsIInputStream>
246 GetInputStream(const nsAString& aValue, uint64_t* aInputLength,
247 ErrorResult& aRv);
250 class FinishHelper MOZ_FINAL : public nsIRunnable
252 friend class FileHandleBase;
254 public:
255 NS_DECL_THREADSAFE_ISUPPORTS
256 NS_DECL_NSIRUNNABLE
258 private:
259 FinishHelper(FileHandleBase* aFileHandle);
260 ~FinishHelper()
263 nsRefPtr<FileHandleBase> mFileHandle;
264 nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
265 nsCOMPtr<nsISupports> mStream;
267 bool mAborted;
270 } // namespace dom
271 } // namespace mozilla
273 #endif // mozilla_dom_FileHandle_h