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/text/WTFString.h>
32
33namespace WebCore {
34
35class URL;
36
37class File final : public Blob {
38public:
39    static PassRefPtr<File> create(const String& path)
40    {
41        return adoptRef(new File(path));
42    }
43
44    static PassRefPtr<File> deserialize(const String& path, const URL& srcURL, const String& type, const String& name)
45    {
46        return adoptRef(new File(deserializationContructor, path, srcURL, type, name));
47    }
48
49    // 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.
50    static PassRefPtr<File> createWithName(const String& path, const String& nameOverride)
51    {
52        if (nameOverride.isEmpty())
53            return adoptRef(new File(path));
54        return adoptRef(new File(path, nameOverride));
55    }
56
57    virtual bool isFile() const override { return true; }
58
59    const String& path() const { return m_path; }
60    const String& name() const { return m_name; }
61
62    // 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).
63    double lastModifiedDate() const;
64
65    static String contentTypeForFile(const String& path);
66
67#if ENABLE(FILE_REPLACEMENT)
68    static bool shouldReplaceFile(const String& path);
69#endif
70
71private:
72    explicit File(const String& path);
73    File(const String& path, const String& nameOverride);
74
75    File(DeserializationContructor, const String& path, const URL& srcURL, const String& type, const String& name);
76
77    static void computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType);
78#if ENABLE(FILE_REPLACEMENT)
79    static void computeNameAndContentTypeForReplacedFile(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType);
80#endif
81
82    String m_path;
83    String m_name;
84};
85
86inline File* toFile(Blob* blob)
87{
88    ASSERT_WITH_SECURITY_IMPLICATION(!blob || blob->isFile());
89    return static_cast<File*>(blob);
90}
91
92inline const File* toFile(const Blob* blob)
93{
94    ASSERT_WITH_SECURITY_IMPLICATION(!blob || blob->isFile());
95    return static_cast<const File*>(blob);
96}
97
98} // namespace WebCore
99
100#endif // File_h
101