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"
19 #include "nsIInputStream.h"
20 #include "nsIRunnable.h"
30 class FileRequestBase
;
34 class MutableFileBase
;
37 * This class provides a base for FileHandle implementations.
44 NORMAL
= 0, // Sequential
57 friend class FileHelper
;
58 friend class FileService
;
59 friend class FinishHelper
;
60 friend class MetadataHelper
;
62 ReadyState mReadyState
;
64 RequestMode mRequestMode
;
66 uint32_t mPendingRequests
;
68 nsTArray
<nsCOMPtr
<nsISupports
>> mParallelStreams
;
69 nsCOMPtr
<nsISupports
> mStream
;
75 NS_IMETHOD_(MozExternalRefCountType
)
78 NS_IMETHOD_(MozExternalRefCountType
)
82 CreateParallelStream(nsISupports
** aStream
);
85 GetOrCreateStream(nsISupports
** aStream
);
102 virtual MutableFileBase
*
103 MutableFile() const = 0;
106 OpenInputStream(bool aWholeFile
, uint64_t aStart
, uint64_t aLength
,
107 nsIInputStream
** aResult
);
109 // Shared WebIDL (IndexedDB FileHandle and FileSystem FileHandle)
113 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
121 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
129 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
131 if (mLocation
== UINT64_MAX
) {
132 return Nullable
<uint64_t>();
135 return Nullable
<uint64_t>(mLocation
);
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
;
147 mLocation
= aLocation
.Value();
151 already_AddRefed
<FileRequestBase
>
152 Read(uint64_t aSize
, bool aHasEncoding
, const nsAString
& aEncoding
,
155 already_AddRefed
<FileRequestBase
>
156 Truncate(const Optional
<uint64_t>& aSize
, ErrorResult
& aRv
);
158 already_AddRefed
<FileRequestBase
>
159 Flush(ErrorResult
& aRv
);
162 Abort(ErrorResult
& aRv
);
165 FileHandleBase(FileMode aMode
,
166 RequestMode aRequestMode
);
176 OnReturnToEventLoop();
179 OnCompleteOrAbort(bool aAborted
) = 0;
182 CheckState(ErrorResult
& aRv
);
185 CheckStateAndArgumentsForRead(uint64_t aSize
, ErrorResult
& aRv
);
188 CheckStateForWrite(ErrorResult
& aRv
);
193 virtual already_AddRefed
<FileRequestBase
>
194 GenerateFileRequest() = 0;
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
)) {
207 // Additional state checking for write
208 if (!aAppend
&& mLocation
== UINT64_MAX
) {
209 aRv
.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR
);
214 nsCOMPtr
<nsIInputStream
> stream
= GetInputStream(aValue
, &length
, aRv
);
223 // Do nothing if the window is closed
224 if (!CheckWindow()) {
228 return WriteInternal(stream
, length
, aAppend
, aRv
);
231 already_AddRefed
<FileRequestBase
>
232 WriteInternal(nsIInputStream
* aInputStream
, uint64_t aInputLength
,
233 bool aAppend
, ErrorResult
& aRv
);
238 static already_AddRefed
<nsIInputStream
>
239 GetInputStream(const ArrayBuffer
& aValue
, uint64_t* aInputLength
,
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
,
250 class FinishHelper MOZ_FINAL
: public nsIRunnable
252 friend class FileHandleBase
;
255 NS_DECL_THREADSAFE_ISUPPORTS
259 FinishHelper(FileHandleBase
* aFileHandle
);
263 nsRefPtr
<FileHandleBase
> mFileHandle
;
264 nsTArray
<nsCOMPtr
<nsISupports
>> mParallelStreams
;
265 nsCOMPtr
<nsISupports
> mStream
;
271 } // namespace mozilla
273 #endif // mozilla_dom_FileHandle_h