[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.24
[framework/web/webkit-efl.git] / Source / WebCore / fileapi / File.h
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 #ifndef File_h
27 #define File_h
28
29 #include "Blob.h"
30 #include "PlatformString.h"
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/RefCounted.h>
33
34 namespace WebCore {
35
36 struct FileMetadata;
37 class KURL;
38
39 class File : public Blob {
40 public:
41     static PassRefPtr<File> create(const String& path)
42     {
43         return adoptRef(new File(path));
44     }
45
46     // For deserialization.
47     static PassRefPtr<File> create(const String& path, const KURL& srcURL, const String& type)
48     {
49         return adoptRef(new File(path, srcURL, type));
50     }
51
52 #if ENABLE(DIRECTORY_UPLOAD)
53     static PassRefPtr<File> createWithRelativePath(const String& path, const String& relativePath);
54 #endif
55
56 #if ENABLE(FILE_SYSTEM)
57     // If filesystem files live in the remote filesystem, the port might pass the valid metadata (whose length field is non-negative) and cache in the File object.
58     //
59     // Otherwise calling size(), lastModifiedTime() and webkitSlice() will synchronously query the file metadata.
60     static PassRefPtr<File> createForFileSystemFile(const String& name, const FileMetadata& metadata)
61     {
62         return adoptRef(new File(name, metadata));
63     }
64 #endif
65
66     // Create a file with a name exposed to the author (via File.name and associated DOM properties) that differs from the one provided in the path.
67     static PassRefPtr<File> createWithName(const String& path, const String& name)
68     {
69         if (name.isEmpty())
70             return adoptRef(new File(path));
71         return adoptRef(new File(path, name));
72     }
73
74     virtual unsigned long long size() const;
75     virtual bool isFile() const { return true; }
76
77     const String& path() const { return m_path; }
78     const String& name() const { return m_name; }
79
80     // This may return zero if getFileModificationTime() platform call has failed or zero snapshot lastModifiedTime is given at construction time.
81     double lastModifiedDate() const;
82
83     // For binding. We want to return null Date if we get the value 0 Date (which is used to indicate the information is unavailable).
84     double lastModifiedDateForBinding() const;
85
86 #if ENABLE(DIRECTORY_UPLOAD)
87     // Returns the relative path of this file in the context of a directory selection.
88     const String& webkitRelativePath() const { return m_relativePath; }
89 #endif
90
91     // Note that this involves synchronous file operation. Think twice before calling this function.
92     void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const;
93
94 private:
95     File(const String& path);
96
97     // For deserialization.
98     File(const String& path, const KURL& srcURL, const String& type);
99     File(const String& path, const String& name);
100
101 # if ENABLE(FILE_SYSTEM)
102     File(const String& name, const FileMetadata&);
103
104     // Returns true if this has a valid snapshot metadata (i.e. m_snapshotSize >= 0).
105     bool hasValidSnapshotMetadata() const { return m_snapshotSize >= 0; }
106 #endif
107
108     String m_path;
109     String m_name;
110
111 #if ENABLE(FILE_SYSTEM)
112     // If m_snapshotSize is negative (initialized to -1 by default), the snapshot metadata is invalid and we retrieve the latest metadata synchronously in size(), lastModifiedTime() and webkitSlice().
113     // Otherwise, the snapshot metadata are used directly in those methods.
114     const long long m_snapshotSize;
115     const double m_snapshotModificationTime;
116 #endif
117
118 #if ENABLE(DIRECTORY_UPLOAD)
119     String m_relativePath;
120 #endif
121 };
122
123 inline File* toFile(Blob* blob)
124 {
125     ASSERT(!blob || blob->isFile());
126     return static_cast<File*>(blob);
127 }
128
129 inline const File* toFile(const Blob* blob)
130 {
131     ASSERT(!blob || blob->isFile());
132     return static_cast<const File*>(blob);
133 }
134
135 } // namespace WebCore
136
137 #endif // File_h