Line data Source code
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 : #include "File.h"
8 : #include "FileBlobImpl.h"
9 : #include "MemoryBlobImpl.h"
10 : #include "MultipartBlobImpl.h"
11 : #include "mozilla/dom/BlobBinding.h"
12 : #include "mozilla/dom/FileBinding.h"
13 : #include "mozilla/dom/FileCreatorHelper.h"
14 : #include "mozilla/dom/FileSystemUtils.h"
15 : #include "mozilla/dom/Promise.h"
16 : #include "nsXULAppAPI.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 0 : File::File(nsISupports* aParent, BlobImpl* aImpl)
22 0 : : Blob(aParent, aImpl)
23 : {
24 0 : MOZ_ASSERT(aImpl->IsFile());
25 0 : }
26 :
27 0 : File::~File()
28 0 : {}
29 :
30 : /* static */ File*
31 0 : File::Create(nsISupports* aParent, BlobImpl* aImpl)
32 : {
33 0 : MOZ_ASSERT(aImpl);
34 0 : MOZ_ASSERT(aImpl->IsFile());
35 :
36 0 : return new File(aParent, aImpl);
37 : }
38 :
39 : /* static */ already_AddRefed<File>
40 0 : File::Create(nsISupports* aParent, const nsAString& aName,
41 : const nsAString& aContentType, uint64_t aLength,
42 : int64_t aLastModifiedDate)
43 : {
44 : RefPtr<File> file = new File(aParent,
45 0 : new BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate));
46 0 : return file.forget();
47 : }
48 :
49 : /* static */ already_AddRefed<File>
50 0 : File::CreateMemoryFile(nsISupports* aParent, void* aMemoryBuffer,
51 : uint64_t aLength, const nsAString& aName,
52 : const nsAString& aContentType,
53 : int64_t aLastModifiedDate)
54 : {
55 : RefPtr<File> file = new File(aParent,
56 0 : new MemoryBlobImpl(aMemoryBuffer, aLength, aName,
57 0 : aContentType, aLastModifiedDate));
58 0 : return file.forget();
59 : }
60 :
61 : /* static */ already_AddRefed<File>
62 0 : File::CreateFromFile(nsISupports* aParent, nsIFile* aFile)
63 : {
64 0 : MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
65 0 : RefPtr<File> file = new File(aParent, new FileBlobImpl(aFile));
66 0 : return file.forget();
67 : }
68 :
69 : /* static */ already_AddRefed<File>
70 0 : File::CreateFromFile(nsISupports* aParent, nsIFile* aFile,
71 : const nsAString& aName, const nsAString& aContentType)
72 : {
73 0 : MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
74 : RefPtr<File> file = new File(aParent,
75 0 : new FileBlobImpl(aFile, aName, aContentType));
76 0 : return file.forget();
77 : }
78 :
79 : JSObject*
80 0 : File::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
81 : {
82 0 : return FileBinding::Wrap(aCx, this, aGivenProto);
83 : }
84 :
85 : void
86 0 : File::GetName(nsAString& aFileName) const
87 : {
88 0 : mImpl->GetName(aFileName);
89 0 : }
90 :
91 : void
92 0 : File::GetRelativePath(nsAString& aPath) const
93 : {
94 0 : aPath.Truncate();
95 :
96 0 : nsAutoString path;
97 0 : mImpl->GetDOMPath(path);
98 :
99 : // WebkitRelativePath doesn't start with '/'
100 0 : if (!path.IsEmpty()) {
101 0 : MOZ_ASSERT(path[0] == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
102 0 : aPath.Assign(Substring(path, 1));
103 : }
104 0 : }
105 :
106 : Date
107 0 : File::GetLastModifiedDate(ErrorResult& aRv)
108 : {
109 0 : int64_t value = GetLastModified(aRv);
110 0 : if (aRv.Failed()) {
111 0 : return Date();
112 : }
113 :
114 0 : return Date(JS::TimeClip(value));
115 : }
116 :
117 : int64_t
118 0 : File::GetLastModified(ErrorResult& aRv)
119 : {
120 0 : return mImpl->GetLastModified(aRv);
121 : }
122 :
123 : void
124 0 : File::GetMozFullPath(nsAString& aFilename, SystemCallerGuarantee aGuarantee,
125 : ErrorResult& aRv) const
126 : {
127 0 : mImpl->GetMozFullPath(aFilename, aGuarantee, aRv);
128 0 : }
129 :
130 : void
131 0 : File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
132 : {
133 0 : mImpl->GetMozFullPathInternal(aFileName, aRv);
134 0 : }
135 :
136 : /* static */ already_AddRefed<File>
137 0 : File::Constructor(const GlobalObject& aGlobal,
138 : const Sequence<BlobPart>& aData,
139 : const nsAString& aName,
140 : const FilePropertyBag& aBag,
141 : ErrorResult& aRv)
142 : {
143 : // Normalizing the filename
144 0 : nsString name(aName);
145 0 : name.ReplaceChar('/', ':');
146 :
147 0 : RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(name);
148 :
149 0 : nsAutoString type(aBag.mType);
150 0 : MakeValidBlobType(type);
151 0 : impl->InitializeBlob(aData, type, false, aRv);
152 0 : if (aRv.Failed()) {
153 0 : return nullptr;
154 : }
155 0 : MOZ_ASSERT(impl->IsFile());
156 :
157 0 : if (aBag.mLastModified.WasPassed()) {
158 0 : impl->SetLastModified(aBag.mLastModified.Value());
159 : }
160 :
161 0 : RefPtr<File> file = new File(aGlobal.GetAsSupports(), impl);
162 0 : return file.forget();
163 : }
164 :
165 : /* static */ already_AddRefed<Promise>
166 0 : File::CreateFromNsIFile(const GlobalObject& aGlobal,
167 : nsIFile* aData,
168 : const ChromeFilePropertyBag& aBag,
169 : SystemCallerGuarantee aGuarantee,
170 : ErrorResult& aRv)
171 : {
172 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
173 :
174 : RefPtr<Promise> promise =
175 0 : FileCreatorHelper::CreateFile(global, aData, aBag, true, aRv);
176 0 : return promise.forget();
177 : }
178 :
179 : /* static */ already_AddRefed<Promise>
180 0 : File::CreateFromFileName(const GlobalObject& aGlobal,
181 : const nsAString& aPath,
182 : const ChromeFilePropertyBag& aBag,
183 : SystemCallerGuarantee aGuarantee,
184 : ErrorResult& aRv)
185 : {
186 0 : nsCOMPtr<nsIFile> file;
187 0 : aRv = NS_NewLocalFile(aPath, false, getter_AddRefs(file));
188 0 : if (NS_WARN_IF(aRv.Failed())) {
189 0 : return nullptr;
190 : }
191 :
192 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
193 :
194 : RefPtr<Promise> promise =
195 0 : FileCreatorHelper::CreateFile(global, file, aBag, false, aRv);
196 0 : return promise.forget();
197 : }
198 :
199 : } // namespace dom
200 : } // namespace mozilla
|