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