2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/fileapi/File.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "platform/FileMetadata.h"
32 #include "platform/MIMETypeRegistry.h"
33 #include "public/platform/Platform.h"
34 #include "public/platform/WebFileUtilities.h"
35 #include "wtf/CurrentTime.h"
36 #include "wtf/DateMath.h"
40 static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy)
43 int index = name.reverseFind('.');
45 if (policy == File::WellKnownContentTypes)
46 type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
48 ASSERT(policy == File::AllContentTypes);
49 type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
55 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
57 OwnPtr<BlobData> blobData = BlobData::create();
58 blobData->setContentType(contentType);
59 blobData->appendFile(path);
60 return blobData.release();
63 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy)
65 return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy));
68 static PassOwnPtr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy)
70 return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy));
73 static PassOwnPtr<BlobData> createBlobDataForFileWithMetadata(const String& fileSystemName, const FileMetadata& metadata)
75 OwnPtr<BlobData> blobData = BlobData::create();
76 blobData->setContentType(getContentTypeFromFileName(fileSystemName, File::WellKnownContentTypes));
77 blobData->appendFile(metadata.platformPath, 0, metadata.length, metadata.modificationTime);
78 return blobData.release();
81 static PassOwnPtr<BlobData> createBlobDataForFileSystemURL(const KURL& fileSystemURL, const FileMetadata& metadata)
83 OwnPtr<BlobData> blobData = BlobData::create();
84 blobData->setContentType(getContentTypeFromFileName(fileSystemURL.path(), File::WellKnownContentTypes));
85 blobData->appendFileSystemURL(fileSystemURL, 0, metadata.length, metadata.modificationTime);
86 return blobData.release();
89 PassRefPtrWillBeRawPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
91 RefPtrWillBeRawPtr<File> file = adoptRefWillBeNoop(new File(path, File::AllContentTypes, File::IsUserVisible));
92 file->m_relativePath = relativePath;
93 return file.release();
96 File::File(const String& path, ContentTypeLookupPolicy policy, UserVisibility userVisibility)
97 : Blob(BlobDataHandle::create(createBlobDataForFile(path, policy), -1))
98 , m_hasBackingFile(true)
99 , m_userVisibility(userVisibility)
101 , m_name(Platform::current()->fileUtilities()->baseName(path))
103 , m_snapshotModificationTime(invalidFileTime())
107 File::File(const String& path, const String& name, ContentTypeLookupPolicy policy, UserVisibility userVisibility)
108 : Blob(BlobDataHandle::create(createBlobDataForFileWithName(path, name, policy), -1))
109 , m_hasBackingFile(true)
110 , m_userVisibility(userVisibility)
114 , m_snapshotModificationTime(invalidFileTime())
118 File::File(const String& path, const String& name, const String& relativePath, UserVisibility userVisibility, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
119 : Blob(blobDataHandle)
120 , m_hasBackingFile(!path.isEmpty() || !relativePath.isEmpty())
121 , m_userVisibility(userVisibility)
124 , m_snapshotSize(hasSnaphotData ? static_cast<long long>(size) : -1)
125 , m_snapshotModificationTime(hasSnaphotData ? lastModified : invalidFileTime())
126 , m_relativePath(relativePath)
130 File::File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
131 : Blob(blobDataHandle)
132 , m_hasBackingFile(false)
133 , m_userVisibility(File::IsNotUserVisible)
135 , m_snapshotSize(Blob::size())
136 , m_snapshotModificationTime(modificationTime)
140 File::File(const String& name, const FileMetadata& metadata, UserVisibility userVisibility)
141 : Blob(BlobDataHandle::create(createBlobDataForFileWithMetadata(name, metadata), metadata.length))
142 , m_hasBackingFile(true)
143 , m_userVisibility(userVisibility)
144 , m_path(metadata.platformPath)
146 , m_snapshotSize(metadata.length)
147 , m_snapshotModificationTime(metadata.modificationTime)
151 File::File(const KURL& fileSystemURL, const FileMetadata& metadata)
152 : Blob(BlobDataHandle::create(createBlobDataForFileSystemURL(fileSystemURL, metadata), metadata.length))
153 , m_hasBackingFile(true)
154 , m_userVisibility(File::IsNotUserVisible)
155 , m_name(decodeURLEscapeSequences(fileSystemURL.lastPathComponent()))
156 , m_fileSystemURL(fileSystemURL)
157 , m_snapshotSize(metadata.length)
158 , m_snapshotModificationTime(metadata.modificationTime)
162 double File::lastModifiedMS() const
164 if (hasValidSnapshotMetadata() && isValidFileTime(m_snapshotModificationTime))
165 return m_snapshotModificationTime * msPerSecond;
167 time_t modificationTime;
168 if (hasBackingFile() && getFileModificationTime(m_path, modificationTime) && isValidFileTime(modificationTime))
169 return modificationTime * msPerSecond;
171 return currentTime() * msPerSecond;
174 long long File::lastModified() const
176 double modifiedDate = lastModifiedMS();
178 // The getter should return the current time when the last modification time isn't known.
179 if (!isValidFileTime(modifiedDate))
180 modifiedDate = currentTimeMS();
182 // lastModified returns a number, not a Date instance,
183 // http://dev.w3.org/2006/webapi/FileAPI/#file-attrs
184 return floor(modifiedDate);
187 double File::lastModifiedDate() const
189 double modifiedDate = lastModifiedMS();
191 // The getter should return the current time when the last modification time isn't known.
192 if (!isValidFileTime(modifiedDate))
193 modifiedDate = currentTimeMS();
195 // lastModifiedDate returns a Date instance,
196 // http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate
200 unsigned long long File::size() const
202 if (hasValidSnapshotMetadata())
203 return m_snapshotSize;
205 // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to
206 // come up with an exception to throw if file size is not representable.
208 if (!hasBackingFile() || !getFileSize(m_path, size))
210 return static_cast<unsigned long long>(size);
213 PassRefPtrWillBeRawPtr<Blob> File::slice(long long start, long long end, const String& contentType, ExceptionState& exceptionState) const
215 if (hasBeenClosed()) {
216 exceptionState.throwDOMException(InvalidStateError, "File has been closed.");
220 if (!m_hasBackingFile)
221 return Blob::slice(start, end, contentType, exceptionState);
223 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
225 double modificationTime;
226 captureSnapshot(size, modificationTime);
227 clampSliceOffsets(size, start, end);
229 long long length = end - start;
230 OwnPtr<BlobData> blobData = BlobData::create();
231 blobData->setContentType(contentType);
232 if (!m_fileSystemURL.isEmpty()) {
233 blobData->appendFileSystemURL(m_fileSystemURL, start, length, modificationTime);
235 ASSERT(!m_path.isEmpty());
236 blobData->appendFile(m_path, start, length, modificationTime);
238 return Blob::create(BlobDataHandle::create(blobData.release(), length));
241 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
243 if (hasValidSnapshotMetadata()) {
244 snapshotSize = m_snapshotSize;
245 snapshotModificationTime = m_snapshotModificationTime;
249 // Obtains a snapshot of the file by capturing its current size and modification time. This is used when we slice a file for the first time.
250 // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned.
251 FileMetadata metadata;
252 if (!hasBackingFile() || !getFileMetadata(m_path, metadata)) {
254 snapshotModificationTime = invalidFileTime();
258 snapshotSize = metadata.length;
259 snapshotModificationTime = metadata.modificationTime;
262 void File::close(ExecutionContext* executionContext, ExceptionState& exceptionState)
264 if (hasBeenClosed()) {
265 exceptionState.throwDOMException(InvalidStateError, "Blob has been closed.");
269 // Reset the File to its closed representation, an empty
270 // Blob. The name isn't cleared, as it should still be
272 m_hasBackingFile = false;
274 m_fileSystemURL = KURL();
275 invalidateSnapshotMetadata();
276 m_relativePath = String();
277 Blob::close(executionContext, exceptionState);
280 void File::appendTo(BlobData& blobData) const
282 if (!m_hasBackingFile) {
283 Blob::appendTo(blobData);
287 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
289 double modificationTime;
290 captureSnapshot(size, modificationTime);
291 if (!m_fileSystemURL.isEmpty()) {
292 blobData.appendFileSystemURL(m_fileSystemURL, 0, size, modificationTime);
295 ASSERT(!m_path.isEmpty());
296 blobData.appendFile(m_path, 0, size, modificationTime);