Upstream version 7.36.149.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/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 WebCore {
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, AllContentTypes));
92     file->m_relativePath = relativePath;
93     return file.release();
94 }
95
96 File::File(const String& path, ContentTypeLookupPolicy policy)
97     : Blob(BlobDataHandle::create(createBlobDataForFile(path, policy), -1))
98     , m_hasBackingFile(true)
99     , m_path(path)
100     , m_name(blink::Platform::current()->fileUtilities()->baseName(path))
101     , m_snapshotSize(-1)
102     , m_snapshotModificationTime(invalidFileTime())
103 {
104     ScriptWrappable::init(this);
105 }
106
107 File::File(const String& path, const String& name, ContentTypeLookupPolicy policy)
108     : Blob(BlobDataHandle::create(createBlobDataForFileWithName(path, name, policy), -1))
109     , m_hasBackingFile(true)
110     , m_path(path)
111     , m_name(name)
112     , m_snapshotSize(-1)
113     , m_snapshotModificationTime(invalidFileTime())
114 {
115     ScriptWrappable::init(this);
116 }
117
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())
121     , m_path(path)
122     , m_name(name)
123     , m_snapshotSize(hasSnaphotData ? static_cast<long long>(size) : -1)
124     , m_snapshotModificationTime(hasSnaphotData ? lastModified : invalidFileTime())
125     , m_relativePath(relativePath)
126 {
127     ScriptWrappable::init(this);
128 }
129
130 File::File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
131     : Blob(blobDataHandle)
132     , m_hasBackingFile(false)
133     , m_name(name)
134     , m_snapshotSize(Blob::size())
135     , m_snapshotModificationTime(modificationTime)
136 {
137     ScriptWrappable::init(this);
138 }
139
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)
144     , m_name(name)
145     , m_snapshotSize(metadata.length)
146     , m_snapshotModificationTime(metadata.modificationTime)
147 {
148     ScriptWrappable::init(this);
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_name(decodeURLEscapeSequences(fileSystemURL.lastPathComponent()))
155     , m_fileSystemURL(fileSystemURL)
156     , m_snapshotSize(metadata.length)
157     , m_snapshotModificationTime(metadata.modificationTime)
158 {
159     ScriptWrappable::init(this);
160 }
161
162 double File::lastModifiedDate() 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 unsigned long long File::size() const
175 {
176     if (hasValidSnapshotMetadata())
177         return m_snapshotSize;
178
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.
181     long long size;
182     if (!hasBackingFile() || !getFileSize(m_path, size))
183         return 0;
184     return static_cast<unsigned long long>(size);
185 }
186
187 PassRefPtrWillBeRawPtr<Blob> File::slice(long long start, long long end, const String& contentType, ExceptionState& exceptionState) const
188 {
189     if (hasBeenClosed()) {
190         exceptionState.throwDOMException(InvalidStateError, "File has been closed.");
191         return nullptr;
192     }
193
194     if (!m_hasBackingFile)
195         return Blob::slice(start, end, contentType, exceptionState);
196
197     // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
198     long long size;
199     double modificationTime;
200     captureSnapshot(size, modificationTime);
201     clampSliceOffsets(size, start, end);
202
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);
208     } else {
209         ASSERT(!m_path.isEmpty());
210         blobData->appendFile(m_path, start, length, modificationTime);
211     }
212     return Blob::create(BlobDataHandle::create(blobData.release(), length));
213 }
214
215 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
216 {
217     if (hasValidSnapshotMetadata()) {
218         snapshotSize = m_snapshotSize;
219         snapshotModificationTime = m_snapshotModificationTime;
220         return;
221     }
222
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)) {
227         snapshotSize = 0;
228         snapshotModificationTime = invalidFileTime();
229         return;
230     }
231
232     snapshotSize = metadata.length;
233     snapshotModificationTime = metadata.modificationTime;
234 }
235
236 void File::close(ExecutionContext* executionContext, ExceptionState& exceptionState)
237 {
238     if (hasBeenClosed()) {
239         exceptionState.throwDOMException(InvalidStateError, "Blob has been closed.");
240         return;
241     }
242
243     // Reset the File to its closed representation, an empty
244     // Blob. The name isn't cleared, as it should still be
245     // available.
246     m_hasBackingFile = false;
247     m_path = String();
248     m_fileSystemURL = KURL();
249     invalidateSnapshotMetadata();
250     m_relativePath = String();
251     Blob::close(executionContext, exceptionState);
252 }
253
254 void File::appendTo(BlobData& blobData) const
255 {
256     if (!m_hasBackingFile) {
257         Blob::appendTo(blobData);
258         return;
259     }
260
261     // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
262     long long size;
263     double modificationTime;
264     captureSnapshot(size, modificationTime);
265     if (!m_fileSystemURL.isEmpty()) {
266         blobData.appendFileSystemURL(m_fileSystemURL, 0, size, modificationTime);
267         return;
268     }
269     ASSERT(!m_path.isEmpty());
270     blobData.appendFile(m_path, 0, size, modificationTime);
271 }
272
273 } // namespace WebCore