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_File_h
8 #define mozilla_dom_File_h
9 
10 #include "mozilla/dom/Blob.h"
11 #include "mozilla/dom/Date.h"
12 
13 class nsIFile;
14 
15 namespace mozilla {
16 namespace dom {
17 
18 struct ChromeFilePropertyBag;
19 struct FilePropertyBag;
20 class Promise;
21 
22 class File final : public Blob {
23   friend class Blob;
24 
25  public:
26   // Note: BlobImpl must be a File in order to use this method.
27   // Check impl->IsFile().
28   static File* Create(nsISupports* aParent, BlobImpl* aImpl);
29 
30   static already_AddRefed<File> Create(nsISupports* aParent,
31                                        const nsAString& aName,
32                                        const nsAString& aContentType,
33                                        uint64_t aLength,
34                                        int64_t aLastModifiedDate);
35 
36   // The returned File takes ownership of aMemoryBuffer. aMemoryBuffer will be
37   // freed by free so it must be allocated by malloc or something
38   // compatible with it.
39   static already_AddRefed<File> CreateMemoryFile(nsISupports* aParent,
40                                                  void* aMemoryBuffer,
41                                                  uint64_t aLength,
42                                                  const nsAString& aName,
43                                                  const nsAString& aContentType,
44                                                  int64_t aLastModifiedDate);
45 
46   // This method creates a BlobFileImpl for the new File object. This is
47   // thread-safe, cross-process, cross-thread as any other BlobImpl, but, when
48   // GetType() is called, it must dispatch a runnable to the main-thread in
49   // order to use nsIMIMEService.
50   // Would be nice if we try to avoid to use this method outside the
51   // main-thread to avoid extra runnables.
52   static already_AddRefed<File> CreateFromFile(nsISupports* aParent,
53                                                nsIFile* aFile);
54 
55   static already_AddRefed<File> CreateFromFile(nsISupports* aParent,
56                                                nsIFile* aFile,
57                                                const nsAString& aName,
58                                                const nsAString& aContentType);
59 
60   // WebIDL methods
61 
62   virtual JSObject* WrapObject(JSContext* cx,
63                                JS::Handle<JSObject*> aGivenProto) override;
64 
65   // File constructor
66   static already_AddRefed<File> Constructor(const GlobalObject& aGlobal,
67                                             const Sequence<BlobPart>& aData,
68                                             const nsAString& aName,
69                                             const FilePropertyBag& aBag,
70                                             ErrorResult& aRv);
71 
72   // ChromeOnly
73   static already_AddRefed<Promise> CreateFromFileName(
74       const GlobalObject& aGlobal, const nsAString& aFilePath,
75       const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
76       ErrorResult& aRv);
77 
78   // ChromeOnly
79   static already_AddRefed<Promise> CreateFromNsIFile(
80       const GlobalObject& aGlobal, nsIFile* aFile,
81       const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
82       ErrorResult& aRv);
83 
84   void GetName(nsAString& aName) const;
85 
86   int64_t GetLastModified(ErrorResult& aRv);
87 
88   Date GetLastModifiedDate(ErrorResult& aRv);
89 
90   void GetRelativePath(nsAString& aPath) const;
91 
92   void GetMozFullPath(nsAString& aFilename, SystemCallerGuarantee aGuarantee,
93                       ErrorResult& aRv) const;
94 
95   void GetMozFullPathInternal(nsAString& aName, ErrorResult& aRv) const;
96 
97  protected:
HasFileInterface()98   virtual bool HasFileInterface() const override { return true; }
99 
100  private:
101   // File constructor should never be used directly. Use Blob::Create or
102   // File::Create.
103   File(nsISupports* aParent, BlobImpl* aImpl);
104   ~File();
105 };
106 
107 }  // namespace dom
108 }  // namespace mozilla
109 
110 #endif  // mozilla_dom_File_h
111