initial import
[vuplus_webkit] / Source / WebCore / 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 "File.h"
28
29 #include "FileSystem.h"
30 #include "MIMETypeRegistry.h"
31 #include <wtf/CurrentTime.h>
32 #include <wtf/text/WTFString.h>
33
34 namespace WebCore {
35
36 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
37 {
38     OwnPtr<BlobData> blobData = BlobData::create();
39     blobData->setContentType(contentType);
40     blobData->appendFile(path);
41     return blobData.release();
42 }
43
44 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path)
45 {
46     String type;
47     int index = path.reverseFind('.');
48     if (index != -1)
49         type = MIMETypeRegistry::getMIMETypeForExtension(path.substring(index + 1));
50     return createBlobDataForFileWithType(path, type);
51 }
52
53 #if ENABLE(FILE_SYSTEM)
54 static PassOwnPtr<BlobData> createBlobDataForFileSystemFile(const String& path, const String& fileSystemName)
55 {
56     String type;
57     int index = fileSystemName.reverseFind('.');
58     if (index != -1)
59         type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(fileSystemName.substring(index + 1));
60     return createBlobDataForFileWithType(path, type);
61 }
62 #endif
63
64 #if ENABLE(DIRECTORY_UPLOAD)
65 PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
66 {
67     RefPtr<File> file = adoptRef(new File(path));
68     file->m_relativePath = relativePath;
69     return file.release();
70 }
71 #endif
72
73 File::File(const String& path)
74     : Blob(createBlobDataForFile(path), -1)
75     , m_path(path)
76     , m_name(pathGetFileName(path))
77 {
78 }
79
80 File::File(const String& path, const KURL& url, const String& type)
81     : Blob(url, type, -1)
82     , m_path(path)
83 {
84     m_name = pathGetFileName(path);
85     // FIXME: File object serialization/deserialization does not include
86     // newer file object data members: m_name and m_relativePath.
87     // See SerializedScriptValue.cpp for js and v8.
88 }
89
90 #if ENABLE(FILE_SYSTEM)
91 File::File(const String& path, const String& name)
92     : Blob(createBlobDataForFileSystemFile(path, name), -1)
93     , m_path(path)
94     , m_name(name)
95 {
96 }
97 #endif
98
99 double File::lastModifiedDate() const
100 {
101     time_t modificationTime;
102     if (!getFileModificationTime(m_path, modificationTime))
103         return 0;
104
105     // Needs to return epoch time in milliseconds for Date.
106     return modificationTime * 1000.0;
107 }
108
109 unsigned long long File::size() const
110 {
111     // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to
112     // come up with an exception to throw if file size is not representable.
113     long long size;
114     if (!getFileSize(m_path, size))
115         return 0;
116     return static_cast<unsigned long long>(size);
117 }
118
119 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
120 {
121     // 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.
122     // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned.
123     // FIXME: Combine getFileSize and getFileModificationTime into one file system call.
124     time_t modificationTime;
125     if (!getFileSize(m_path, snapshotSize) || !getFileModificationTime(m_path, modificationTime)) {
126         snapshotSize = 0;
127         snapshotModificationTime = 0;
128     } else
129         snapshotModificationTime = modificationTime;
130 }
131
132 } // namespace WebCore