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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_filereadersync_h__
8 #define mozilla_dom_filereadersync_h__
9 
10 #include "mozilla/dom/WorkerCommon.h"
11 #include "nsISupports.h"
12 
13 class nsIInputStream;
14 
15 namespace mozilla {
16 class ErrorResult;
17 
18 namespace dom {
19 class Blob;
20 class GlobalObject;
21 template <typename>
22 class Optional;
23 
24 class FileReaderSync final {
25   NS_INLINE_DECL_REFCOUNTING(FileReaderSync)
26 
27  private:
28   // Private destructor, to discourage deletion outside of Release():
29   ~FileReaderSync() = default;
30 
31   nsresult ConvertStream(nsIInputStream* aStream, const char* aCharset,
32                          nsAString& aResult);
33 
34   nsresult ConvertAsyncToSyncStream(
35       uint64_t aStreamSize, already_AddRefed<nsIInputStream> aAsyncStream,
36       nsIInputStream** aSyncStream);
37 
38   nsresult SyncRead(nsIInputStream* aStream, char* aBuffer,
39                     uint32_t aBufferSize, uint32_t* aTotalBytesRead);
40 
41  public:
42   static already_AddRefed<FileReaderSync> Constructor(
43       const GlobalObject& aGlobal);
44 
45   bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
46                   JS::MutableHandle<JSObject*> aReflector);
47 
48   void ReadAsArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aScopeObj,
49                          Blob& aBlob, JS::MutableHandle<JSObject*> aRetval,
50                          ErrorResult& aRv);
51   void ReadAsBinaryString(Blob& aBlob, nsAString& aResult, ErrorResult& aRv);
52   void ReadAsText(Blob& aBlob, const Optional<nsAString>& aEncoding,
53                   nsAString& aResult, ErrorResult& aRv);
54   void ReadAsDataURL(Blob& aBlob, nsAString& aResult, ErrorResult& aRv);
55 };
56 
57 }  // namespace dom
58 }  // namespace mozilla
59 
60 #endif  // mozilla_dom_filereadersync_h__
61