• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fileapi/Blob.h"
30 #include "platform/heap/Handle.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/text/WTFString.h"
33 
34 namespace WebCore {
35 
36 class ExceptionState;
37 class ExecutionContext;
38 struct FileMetadata;
39 class KURL;
40 
41 class File FINAL : public Blob {
42 public:
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,
47         AllContentTypes,
48     };
49 
50     static PassRefPtrWillBeRawPtr<File> create(const String& path, ContentTypeLookupPolicy policy = WellKnownContentTypes)
51     {
52         return adoptRefWillBeNoop(new File(path, policy));
53     }
54 
create(const String & name,double modificationTime,PassRefPtr<BlobDataHandle> blobDataHandle)55     static PassRefPtrWillBeRawPtr<File> create(const String& name, double modificationTime, PassRefPtr<BlobDataHandle> blobDataHandle)
56     {
57         return adoptRefWillBeNoop(new File(name, modificationTime, blobDataHandle));
58     }
59 
60     // For deserialization.
create(const String & path,const String & name,const String & relativePath,bool hasSnaphotData,uint64_t size,double lastModified,PassRefPtr<BlobDataHandle> blobDataHandle)61     static PassRefPtrWillBeRawPtr<File> create(const String& path, const String& name, const String& relativePath, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
62     {
63         return adoptRefWillBeNoop(new File(path, name, relativePath, hasSnaphotData, size, lastModified, blobDataHandle));
64     }
create(const String & path,const String & name,uint64_t size,double lastModified,PassRefPtr<BlobDataHandle> blobDataHandle)65     static PassRefPtrWillBeRawPtr<File> create(const String& path, const String& name, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle> blobDataHandle)
66     {
67         return adoptRefWillBeNoop(new File(path, name, String(), true, size, lastModified, blobDataHandle));
68     }
69 
70     static PassRefPtrWillBeRawPtr<File> createWithRelativePath(const String& path, const String& relativePath);
71 
72     // 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.
73     //
74     // Otherwise calling size(), lastModifiedTime() and slice() will synchronously query the file metadata.
createForFileSystemFile(const String & name,const FileMetadata & metadata)75     static PassRefPtrWillBeRawPtr<File> createForFileSystemFile(const String& name, const FileMetadata& metadata)
76     {
77         return adoptRefWillBeNoop(new File(name, metadata));
78     }
79 
createForFileSystemFile(const KURL & url,const FileMetadata & metadata)80     static PassRefPtrWillBeRawPtr<File> createForFileSystemFile(const KURL& url, const FileMetadata& metadata)
81     {
82         return adoptRefWillBeNoop(new File(url, metadata));
83     }
84 
fileSystemURL()85     KURL fileSystemURL() const { ASSERT(hasValidFileSystemURL()); return m_fileSystemURL; }
86 
87     // 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.
88     static PassRefPtrWillBeRawPtr<File> createWithName(const String& path, const String& name, ContentTypeLookupPolicy policy = WellKnownContentTypes)
89     {
90         if (name.isEmpty())
91             return adoptRefWillBeNoop(new File(path, policy));
92         return adoptRefWillBeNoop(new File(path, name, policy));
93     }
94 
95     virtual unsigned long long size() const OVERRIDE;
96     virtual PassRefPtrWillBeRawPtr<Blob> slice(long long start, long long end, const String& contentType, ExceptionState&) const OVERRIDE;
97     virtual void close(ExecutionContext*, ExceptionState&) OVERRIDE;
98 
isFile()99     virtual bool isFile() const OVERRIDE { return true; }
hasBackingFile()100     virtual bool hasBackingFile() const OVERRIDE { return m_hasBackingFile; }
101 
102     virtual void appendTo(BlobData&) const OVERRIDE;
103 
path()104     const String& path() const { ASSERT(hasValidFilePath()); return m_path; }
name()105     const String name() const { return m_name; }
106 
107     // This returns the current date and time if the file's last modification date is not known (per spec: http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate).
108     double lastModifiedDate() const;
109 
110     // Returns the relative path of this file in the context of a directory selection.
webkitRelativePath()111     const String& webkitRelativePath() const { return m_relativePath; }
112 
113     // Note that this involves synchronous file operation. Think twice before calling this function.
114     void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const;
115 
116     // Returns true if this has a valid snapshot metadata (i.e. m_snapshotSize >= 0).
hasValidSnapshotMetadata()117     bool hasValidSnapshotMetadata() const { return m_snapshotSize >= 0; }
118 
119 private:
120     File(const String& path, ContentTypeLookupPolicy);
121     File(const String& path, const String& name, ContentTypeLookupPolicy);
122     File(const String& path, const String& name, const String& relativePath, bool hasSnaphotData, uint64_t size, double lastModified, PassRefPtr<BlobDataHandle>);
123     File(const String& name, double modificationTime, PassRefPtr<BlobDataHandle>);
124     File(const String& name, const FileMetadata&);
125     File(const KURL& fileSystemURL, const FileMetadata&);
126 
invalidateSnapshotMetadata()127     void invalidateSnapshotMetadata() { m_snapshotSize = -1; }
128 
129 #ifndef NDEBUG
hasValidFileSystemURL()130     bool hasValidFileSystemURL() const { return hasBackingFile(); }
131     // Instances not backed by a file must have an empty path set.
hasValidFilePath()132     bool hasValidFilePath() const { return hasBackingFile() || m_path.isEmpty(); }
133 #endif
134 
135     bool m_hasBackingFile;
136     String m_path;
137     String m_name;
138 
139     KURL m_fileSystemURL;
140 
141     // 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().
142     // Otherwise, the snapshot metadata are used directly in those methods.
143     long long m_snapshotSize;
144     const double m_snapshotModificationTime;
145 
146     String m_relativePath;
147 };
148 
149 DEFINE_TYPE_CASTS(File, Blob, blob, blob->isFile(), blob.isFile());
150 
151 } // namespace WebCore
152 
153 #endif // File_h
154