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/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, AllContentTypes));
92 file->m_relativePath = relativePath;
93 return file.release();
96 File::File(const String& path, ContentTypeLookupPolicy policy)
97 : Blob(BlobDataHandle::create(createBlobDataForFile(path, policy), -1))
98 , m_hasBackingFile(true)
100 , m_name(blink::Platform::current()->fileUtilities()->baseName(path))
102 , m_snapshotModificationTime(invalidFileTime())
104 ScriptWrappable::init(this);
107 File::File(const String& path, const String& name, ContentTypeLookupPolicy policy)
108 : Blob(BlobDataHandle::create(createBlobDataForFileWithName(path, name, policy), -1))
109 , m_hasBackingFile(true)
113 , m_snapshotModificationTime(invalidFileTime())
115 ScriptWrappable::init(this);
118 File::File(const String& path, const String& name, const String& relativePath, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
119 : Blob(blobDataHandle)
120 , m_hasBackingFile(!path.isEmpty() || !relativePath.isEmpty())
123 , m_snapshotSize(hasSnaphotData ? static_cast<long long>(size) : -1)
124 , m_snapshotModificationTime(hasSnaphotData ? lastModified : invalidFileTime())
125 , m_relativePath(relativePath)
127 ScriptWrappable::init(this);
130 File::File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
131 : Blob(blobDataHandle)
132 , m_hasBackingFile(false)
134 , m_snapshotSize(Blob::size())
135 , m_snapshotModificationTime(modificationTime)
137 ScriptWrappable::init(this);
140 File::File(const String& name, const FileMetadata& metadata)
141 : Blob(BlobDataHandle::create(createBlobDataForFileWithMetadata(name, metadata), metadata.length))
142 , m_hasBackingFile(true)
143 , m_path(metadata.platformPath)
145 , m_snapshotSize(metadata.length)
146 , m_snapshotModificationTime(metadata.modificationTime)
148 ScriptWrappable::init(this);
151 File::File(const KURL& fileSystemURL, const FileMetadata& metadata)
152 : Blob(BlobDataHandle::create(createBlobDataForFileSystemURL(fileSystemURL, metadata), metadata.length))
153 , m_hasBackingFile(true)
154 , m_name(decodeURLEscapeSequences(fileSystemURL.lastPathComponent()))
155 , m_fileSystemURL(fileSystemURL)
156 , m_snapshotSize(metadata.length)
157 , m_snapshotModificationTime(metadata.modificationTime)
159 ScriptWrappable::init(this);
162 double File::lastModifiedDate() 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 unsigned long long File::size() const
176 if (hasValidSnapshotMetadata())
177 return m_snapshotSize;
179 // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to
180 // come up with an exception to throw if file size is not representable.
182 if (!hasBackingFile() || !getFileSize(m_path, size))
184 return static_cast<unsigned long long>(size);
187 PassRefPtrWillBeRawPtr<Blob> File::slice(long long start, long long end, const String& contentType, ExceptionState& exceptionState) const
189 if (hasBeenClosed()) {
190 exceptionState.throwDOMException(InvalidStateError, "File has been closed.");
194 if (!m_hasBackingFile)
195 return Blob::slice(start, end, contentType, exceptionState);
197 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
199 double modificationTime;
200 captureSnapshot(size, modificationTime);
201 clampSliceOffsets(size, start, end);
203 long long length = end - start;
204 OwnPtr<BlobData> blobData = BlobData::create();
205 blobData->setContentType(contentType);
206 if (!m_fileSystemURL.isEmpty()) {
207 blobData->appendFileSystemURL(m_fileSystemURL, start, length, modificationTime);
209 ASSERT(!m_path.isEmpty());
210 blobData->appendFile(m_path, start, length, modificationTime);
212 return Blob::create(BlobDataHandle::create(blobData.release(), length));
215 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
217 if (hasValidSnapshotMetadata()) {
218 snapshotSize = m_snapshotSize;
219 snapshotModificationTime = m_snapshotModificationTime;
223 // 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.
224 // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned.
225 FileMetadata metadata;
226 if (!hasBackingFile() || !getFileMetadata(m_path, metadata)) {
228 snapshotModificationTime = invalidFileTime();
232 snapshotSize = metadata.length;
233 snapshotModificationTime = metadata.modificationTime;
236 void File::close(ExecutionContext* executionContext, ExceptionState& exceptionState)
238 if (hasBeenClosed()) {
239 exceptionState.throwDOMException(InvalidStateError, "Blob has been closed.");
243 // Reset the File to its closed representation, an empty
244 // Blob. The name isn't cleared, as it should still be
246 m_hasBackingFile = false;
248 m_fileSystemURL = KURL();
249 invalidateSnapshotMetadata();
250 m_relativePath = String();
251 Blob::close(executionContext, exceptionState);
254 void File::appendTo(BlobData& blobData) const
256 if (!m_hasBackingFile) {
257 Blob::appendTo(blobData);
261 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
263 double modificationTime;
264 captureSnapshot(size, modificationTime);
265 if (!m_fileSystemURL.isEmpty()) {
266 blobData.appendFileSystemURL(m_fileSystemURL, 0, size, modificationTime);
269 ASSERT(!m_path.isEmpty());
270 blobData.appendFile(m_path, 0, size, modificationTime);
273 } // namespace WebCore