Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / fileapi / File.cpp
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "core/fileapi/File.h"
28
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"
37
38 namespace blink {
39
40 static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy)
41 {
42     String type;
43     int index = name.reverseFind('.');
44     if (index != -1) {
45         if (policy == File::WellKnownContentTypes)
46             type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
47         else {
48             ASSERT(policy == File::AllContentTypes);
49             type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
50         }
51     }
52     return type;
53 }
54
55 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
56 {
57     OwnPtr<BlobData> blobData = BlobData::create();
58     blobData->setContentType(contentType);
59     blobData->appendFile(path);
60     return blobData.release();
61 }
62
63 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy)
64 {
65     return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy));
66 }
67
68 static PassOwnPtr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy)
69 {
70     return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy));
71 }
72
73 static PassOwnPtr<BlobData> createBlobDataForFileWithMetadata(const String& fileSystemName, const FileMetadata& metadata)
74 {
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();
79 }
80
81 static PassOwnPtr<BlobData> createBlobDataForFileSystemURL(const KURL& fileSystemURL, const FileMetadata& metadata)
82 {
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();
87 }
88
89 PassRefPtrWillBeRawPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
90 {
91     RefPtrWillBeRawPtr<File> file = adoptRefWillBeNoop(new File(path, File::AllContentTypes, File::IsUserVisible));
92     file->m_relativePath = relativePath;
93     return file.release();
94 }
95
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)
100     , m_path(path)
101     , m_name(Platform::current()->fileUtilities()->baseName(path))
102     , m_snapshotSize(-1)
103     , m_snapshotModificationTime(invalidFileTime())
104 {
105 }
106
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)
111     , m_path(path)
112     , m_name(name)
113     , m_snapshotSize(-1)
114     , m_snapshotModificationTime(invalidFileTime())
115 {
116 }
117
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)
122     , m_path(path)
123     , m_name(name)
124     , m_snapshotSize(hasSnaphotData ? static_cast<long long>(size) : -1)
125     , m_snapshotModificationTime(hasSnaphotData ? lastModified : invalidFileTime())
126     , m_relativePath(relativePath)
127 {
128 }
129
130 File::File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
131     : Blob(blobDataHandle)
132     , m_hasBackingFile(false)
133     , m_userVisibility(File::IsNotUserVisible)
134     , m_name(name)
135     , m_snapshotSize(Blob::size())
136     , m_snapshotModificationTime(modificationTime)
137 {
138 }
139
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)
145     , m_name(name)
146     , m_snapshotSize(metadata.length)
147     , m_snapshotModificationTime(metadata.modificationTime)
148 {
149 }
150
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)
159 {
160 }
161
162 double File::lastModifiedMS() const
163 {
164     if (hasValidSnapshotMetadata() && isValidFileTime(m_snapshotModificationTime))
165         return m_snapshotModificationTime * msPerSecond;
166
167     time_t modificationTime;
168     if (hasBackingFile() && getFileModificationTime(m_path, modificationTime) && isValidFileTime(modificationTime))
169         return modificationTime * msPerSecond;
170
171     return currentTime() * msPerSecond;
172 }
173
174 long long File::lastModified() const
175 {
176     double modifiedDate = lastModifiedMS();
177
178     // The getter should return the current time when the last modification time isn't known.
179     if (!isValidFileTime(modifiedDate))
180         modifiedDate = currentTimeMS();
181
182     // lastModified returns a number, not a Date instance,
183     // http://dev.w3.org/2006/webapi/FileAPI/#file-attrs
184     return floor(modifiedDate);
185 }
186
187 double File::lastModifiedDate() const
188 {
189     double modifiedDate = lastModifiedMS();
190
191     // The getter should return the current time when the last modification time isn't known.
192     if (!isValidFileTime(modifiedDate))
193         modifiedDate = currentTimeMS();
194
195     // lastModifiedDate returns a Date instance,
196     // http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate
197     return modifiedDate;
198 }
199
200 unsigned long long File::size() const
201 {
202     if (hasValidSnapshotMetadata())
203         return m_snapshotSize;
204
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.
207     long long size;
208     if (!hasBackingFile() || !getFileSize(m_path, size))
209         return 0;
210     return static_cast<unsigned long long>(size);
211 }
212
213 PassRefPtrWillBeRawPtr<Blob> File::slice(long long start, long long end, const String& contentType, ExceptionState& exceptionState) const
214 {
215     if (hasBeenClosed()) {
216         exceptionState.throwDOMException(InvalidStateError, "File has been closed.");
217         return nullptr;
218     }
219
220     if (!m_hasBackingFile)
221         return Blob::slice(start, end, contentType, exceptionState);
222
223     // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
224     long long size;
225     double modificationTime;
226     captureSnapshot(size, modificationTime);
227     clampSliceOffsets(size, start, end);
228
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);
234     } else {
235         ASSERT(!m_path.isEmpty());
236         blobData->appendFile(m_path, start, length, modificationTime);
237     }
238     return Blob::create(BlobDataHandle::create(blobData.release(), length));
239 }
240
241 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
242 {
243     if (hasValidSnapshotMetadata()) {
244         snapshotSize = m_snapshotSize;
245         snapshotModificationTime = m_snapshotModificationTime;
246         return;
247     }
248
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)) {
253         snapshotSize = 0;
254         snapshotModificationTime = invalidFileTime();
255         return;
256     }
257
258     snapshotSize = metadata.length;
259     snapshotModificationTime = metadata.modificationTime;
260 }
261
262 void File::close(ExecutionContext* executionContext, ExceptionState& exceptionState)
263 {
264     if (hasBeenClosed()) {
265         exceptionState.throwDOMException(InvalidStateError, "Blob has been closed.");
266         return;
267     }
268
269     // Reset the File to its closed representation, an empty
270     // Blob. The name isn't cleared, as it should still be
271     // available.
272     m_hasBackingFile = false;
273     m_path = String();
274     m_fileSystemURL = KURL();
275     invalidateSnapshotMetadata();
276     m_relativePath = String();
277     Blob::close(executionContext, exceptionState);
278 }
279
280 void File::appendTo(BlobData& blobData) const
281 {
282     if (!m_hasBackingFile) {
283         Blob::appendTo(blobData);
284         return;
285     }
286
287     // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
288     long long size;
289     double modificationTime;
290     captureSnapshot(size, modificationTime);
291     if (!m_fileSystemURL.isEmpty()) {
292         blobData.appendFileSystemURL(m_fileSystemURL, 0, size, modificationTime);
293         return;
294     }
295     ASSERT(!m_path.isEmpty());
296     blobData.appendFile(m_path, 0, size, modificationTime);
297 }
298
299 } // namespace blink