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 <wtf/PassRefPtr.h> 31#include <wtf/RefCounted.h> 32#include <wtf/text/WTFString.h> 33 34namespace WebCore { 35 36struct FileMetadata; 37class KURL; 38 39class File : public Blob { 40public: 41 // AllContentTypes should only be used when the full path/name are trusted; otherwise, it could 42 // allow arbitrary pages to determine what applications an user has installed. 43 enum ContentTypeLookupPolicy { 44 WellKnownContentTypes, 45 AllContentTypes, 46 }; 47 48 static PassRefPtr<File> create(const String& path, ContentTypeLookupPolicy policy = WellKnownContentTypes) 49 { 50 return adoptRef(new File(path, policy)); 51 } 52 53 // For deserialization. 54 static PassRefPtr<File> create(const String& path, const KURL& srcURL, const String& type) 55 { 56 return adoptRef(new File(path, srcURL, type)); 57 } 58 59#if ENABLE(DIRECTORY_UPLOAD) 60 static PassRefPtr<File> createWithRelativePath(const String& path, const String& relativePath); 61#endif 62 63#if ENABLE(FILE_SYSTEM) 64 // 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. 65 // 66 // Otherwise calling size(), lastModifiedTime() and slice() will synchronously query the file metadata. 67 static PassRefPtr<File> createForFileSystemFile(const String& name, const FileMetadata& metadata) 68 { 69 return adoptRef(new File(name, metadata)); 70 } 71 72 static PassRefPtr<File> createForFileSystemFile(const KURL& url, const FileMetadata& metadata) 73 { 74 return adoptRef(new File(url, metadata)); 75 } 76 77 KURL fileSystemURL() const { return m_fileSystemURL; } 78#endif 79 80 // 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. 81 static PassRefPtr<File> createWithName(const String& path, const String& name, ContentTypeLookupPolicy policy = WellKnownContentTypes) 82 { 83 if (name.isEmpty()) 84 return adoptRef(new File(path, policy)); 85 return adoptRef(new File(path, name, policy)); 86 } 87 88 virtual unsigned long long size() const; 89 virtual bool isFile() const { return true; } 90 91 const String& path() const { return m_path; } 92 const String& name() const { return m_name; } 93 94 // This returns the current date and time if the file's last modifiecation date is not known (per spec: http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate). 95 double lastModifiedDate() const; 96 97#if ENABLE(DIRECTORY_UPLOAD) 98 // Returns the relative path of this file in the context of a directory selection. 99 const String& webkitRelativePath() const { return m_relativePath; } 100#endif 101 102 // Note that this involves synchronous file operation. Think twice before calling this function. 103 void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const; 104 105private: 106 File(const String& path, ContentTypeLookupPolicy); 107 108 // For deserialization. 109 File(const String& path, const KURL& srcURL, const String& type); 110 File(const String& path, const String& name, ContentTypeLookupPolicy); 111 112# if ENABLE(FILE_SYSTEM) 113 File(const String& name, const FileMetadata&); 114 File(const KURL& fileSystemURL, const FileMetadata&); 115 116 // Returns true if this has a valid snapshot metadata (i.e. m_snapshotSize >= 0). 117 bool hasValidSnapshotMetadata() const { return m_snapshotSize >= 0; } 118#endif 119 120 String m_path; 121 String m_name; 122 123#if ENABLE(FILE_SYSTEM) 124 KURL m_fileSystemURL; 125 126 // 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(). 127 // Otherwise, the snapshot metadata are used directly in those methods. 128 const long long m_snapshotSize; 129 const double m_snapshotModificationTime; 130#endif 131 132#if ENABLE(DIRECTORY_UPLOAD) 133 String m_relativePath; 134#endif 135}; 136 137inline File* toFile(Blob* blob) 138{ 139 ASSERT_WITH_SECURITY_IMPLICATION(!blob || blob->isFile()); 140 return static_cast<File*>(blob); 141} 142 143inline const File* toFile(const Blob* blob) 144{ 145 ASSERT_WITH_SECURITY_IMPLICATION(!blob || blob->isFile()); 146 return static_cast<const File*>(blob); 147} 148 149} // namespace WebCore 150 151#endif // File_h 152