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.
29 #include "core/fileapi/Blob.h"
30 #include "platform/heap/Handle.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/text/WTFString.h"
37 class ExecutionContext;
41 class File FINAL : public Blob {
43 // AllContentTypes should only be used when the full path/name are trusted; otherwise, it could
44 // allow arbitrary pages to determine what applications an user has installed.
45 enum ContentTypeLookupPolicy {
46 WellKnownContentTypes,
50 // The user should not be able to browse to some files, such as the ones
51 // generated by the Filesystem API.
52 enum UserVisibility { IsUserVisible, IsNotUserVisible };
54 static PassRefPtrWillBeRawPtr<File> create(const String& path, ContentTypeLookupPolicy policy = WellKnownContentTypes)
56 return adoptRefWillBeNoop(new File(path, policy, File::IsUserVisible));
59 static PassRefPtrWillBeRawPtr<File> create(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
61 return adoptRefWillBeNoop(new File(name, modificationTime, blobDataHandle));
64 // For deserialization.
65 static PassRefPtrWillBeRawPtr<File> createFromSerialization(const String& path, const String& name, const String& relativePath, UserVisibility userVisibility, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
67 return adoptRefWillBeNoop(new File(path, name, relativePath, userVisibility, hasSnaphotData, size, lastModified, blobDataHandle));
69 static PassRefPtrWillBeRawPtr<File> createFromIndexedSerialization(const String& path, const String& name, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
71 return adoptRefWillBeNoop(new File(path, name, String(), IsNotUserVisible, true, size, lastModified, blobDataHandle));
74 static PassRefPtrWillBeRawPtr<File> createWithRelativePath(const String& path, const String& relativePath);
76 // 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.
78 // Otherwise calling size(), lastModifiedTime() and slice() will synchronously query the file metadata.
79 static PassRefPtrWillBeRawPtr<File> createForFileSystemFile(const String& name, const FileMetadata& metadata)
81 return adoptRefWillBeNoop(new File(name, metadata));
84 static PassRefPtrWillBeRawPtr<File> createForFileSystemFile(const KURL& url, const FileMetadata& metadata)
86 return adoptRefWillBeNoop(new File(url, metadata));
89 KURL fileSystemURL() const { ASSERT(hasValidFileSystemURL()); return m_fileSystemURL; }
91 // 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.
92 static PassRefPtrWillBeRawPtr<File> createForUserProvidedFile(const String& path, const String& displayName)
94 if (displayName.isEmpty())
95 return adoptRefWillBeNoop(new File(path, File::AllContentTypes, File::IsUserVisible));
96 return adoptRefWillBeNoop(new File(path, displayName, File::AllContentTypes, File::IsUserVisible));
99 static PassRefPtrWillBeRawPtr<File> createForFileSystemFile(const String& path, const String& name, ContentTypeLookupPolicy policy = WellKnownContentTypes)
102 return adoptRefWillBeNoop(new File(path, policy, File::IsNotUserVisible));
103 return adoptRefWillBeNoop(new File(path, name, policy, File::IsNotUserVisible));
106 virtual unsigned long long size() const OVERRIDE;
107 virtual PassRefPtrWillBeRawPtr<Blob> slice(long long start, long long end, const String& contentType, ExceptionState&) const OVERRIDE;
108 virtual void close(ExecutionContext*, ExceptionState&) OVERRIDE;
110 virtual bool isFile() const OVERRIDE { return true; }
111 virtual bool hasBackingFile() const OVERRIDE { return m_hasBackingFile; }
113 virtual void appendTo(BlobData&) const OVERRIDE;
115 const String& path() const { ASSERT(hasValidFilePath()); return m_path; }
116 const String name() const { return m_name; }
118 // Getter for the lastModified IDL attribute,
119 // http://dev.w3.org/2006/webapi/FileAPI/#file-attrs
120 long long lastModified() const;
122 // Getter for the lastModifiedDate IDL attribute,
123 // http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate
124 double lastModifiedDate() const;
126 UserVisibility userVisibility() const { return m_userVisibility; }
128 // Returns the relative path of this file in the context of a directory selection.
129 const String& webkitRelativePath() const { return m_relativePath; }
131 // Note that this involves synchronous file operation. Think twice before calling this function.
132 void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const;
134 // Returns true if this has a valid snapshot metadata (i.e. m_snapshotSize >= 0).
135 bool hasValidSnapshotMetadata() const { return m_snapshotSize >= 0; }
138 File(const String& path, ContentTypeLookupPolicy, UserVisibility);
139 File(const String& path, const String& name, ContentTypeLookupPolicy, UserVisibility);
140 File(const String& path, const String& name, const String& relativePath, UserVisibility, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle>);
141 File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle>);
142 File(const String& name, const FileMetadata&);
143 File(const KURL& fileSystemURL, const FileMetadata&);
145 void invalidateSnapshotMetadata() { m_snapshotSize = -1; }
147 // Returns File's last modified time (in MS since Epoch.)
148 // If the modification time isn't known, the current time is returned.
149 double lastModifiedMS() const;
152 bool hasValidFileSystemURL() const { return hasBackingFile(); }
153 // Instances not backed by a file must have an empty path set.
154 bool hasValidFilePath() const { return hasBackingFile() || m_path.isEmpty(); }
157 bool m_hasBackingFile;
158 UserVisibility m_userVisibility;
162 KURL m_fileSystemURL;
164 // 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 slice().
165 // Otherwise, the snapshot metadata are used directly in those methods.
166 long long m_snapshotSize;
167 const double m_snapshotModificationTime;
169 String m_relativePath;
172 DEFINE_TYPE_CASTS(File, Blob, blob, blob->isFile(), blob.isFile());