| 1 | /* |
| 2 | * Copyright (C) 2008-2019 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 | #pragma once |
| 27 | |
| 28 | #include "Blob.h" |
| 29 | #include <wtf/IsoMalloc.h> |
| 30 | #include <wtf/Optional.h> |
| 31 | #include <wtf/Ref.h> |
| 32 | #include <wtf/TypeCasts.h> |
| 33 | #include <wtf/text/WTFString.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | class File final : public Blob { |
| 38 | WTF_MAKE_ISO_ALLOCATED_EXPORT(File, WEBCORE_EXPORT); |
| 39 | public: |
| 40 | struct PropertyBag : BlobPropertyBag { |
| 41 | Optional<int64_t> lastModified; |
| 42 | }; |
| 43 | |
| 44 | static Ref<File> create(const String& path) |
| 45 | { |
| 46 | return adoptRef(*new File(path)); |
| 47 | } |
| 48 | |
| 49 | // Create a File using the 'new File' constructor. |
| 50 | static Ref<File> create(Vector<BlobPartVariant>&& blobPartVariants, const String& filename, const PropertyBag& propertyBag) |
| 51 | { |
| 52 | return adoptRef(*new File(WTFMove(blobPartVariants), filename, propertyBag)); |
| 53 | } |
| 54 | |
| 55 | static Ref<File> deserialize(const String& path, const URL& srcURL, const String& type, const String& name, const Optional<int64_t>& lastModified = WTF::nullopt) |
| 56 | { |
| 57 | return adoptRef(*new File(deserializationContructor, path, srcURL, type, name, lastModified)); |
| 58 | } |
| 59 | |
| 60 | // 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. |
| 61 | static Ref<File> createWithName(const String& path, const String& nameOverride) |
| 62 | { |
| 63 | if (nameOverride.isEmpty()) |
| 64 | return adoptRef(*new File(path)); |
| 65 | return adoptRef(*new File(path, nameOverride)); |
| 66 | } |
| 67 | |
| 68 | static Ref<File> create(const Blob& blob, const String& name) |
| 69 | { |
| 70 | return adoptRef(*new File(blob, name)); |
| 71 | } |
| 72 | |
| 73 | static Ref<File> create(const File& file, const String& name) |
| 74 | { |
| 75 | return adoptRef(*new File(file, name)); |
| 76 | } |
| 77 | |
| 78 | static Ref<File> createWithRelativePath(const String& path, const String& relativePath); |
| 79 | |
| 80 | bool isFile() const override { return true; } |
| 81 | |
| 82 | const String& path() const { return m_path; } |
| 83 | const String& relativePath() const { return m_relativePath; } |
| 84 | void setRelativePath(const String& relativePath) { m_relativePath = relativePath; } |
| 85 | const String& name() const { return m_name; } |
| 86 | WEBCORE_EXPORT int64_t lastModified() const; // Number of milliseconds since Epoch. |
| 87 | const Optional<int64_t>& lastModifiedOverride() const { return m_lastModifiedDateOverride; } // Number of milliseconds since Epoch. |
| 88 | |
| 89 | static String contentTypeForFile(const String& path); |
| 90 | |
| 91 | #if ENABLE(FILE_REPLACEMENT) |
| 92 | static bool shouldReplaceFile(const String& path); |
| 93 | #endif |
| 94 | |
| 95 | bool isDirectory() const; |
| 96 | |
| 97 | private: |
| 98 | WEBCORE_EXPORT explicit File(const String& path); |
| 99 | File(const String& path, const String& nameOverride); |
| 100 | File(Vector<BlobPartVariant>&& blobPartVariants, const String& filename, const PropertyBag&); |
| 101 | File(const Blob&, const String& name); |
| 102 | File(const File&, const String& name); |
| 103 | |
| 104 | File(DeserializationContructor, const String& path, const URL& srcURL, const String& type, const String& name, const Optional<int64_t>& lastModified); |
| 105 | |
| 106 | static void computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType); |
| 107 | #if ENABLE(FILE_REPLACEMENT) |
| 108 | static void computeNameAndContentTypeForReplacedFile(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType); |
| 109 | #endif |
| 110 | |
| 111 | String m_path; |
| 112 | String m_relativePath; |
| 113 | String m_name; |
| 114 | |
| 115 | Optional<int64_t> m_lastModifiedDateOverride; |
| 116 | mutable Optional<bool> m_isDirectory; |
| 117 | }; |
| 118 | |
| 119 | } // namespace WebCore |
| 120 | |
| 121 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::File) |
| 122 | static bool isType(const WebCore::Blob& blob) { return blob.isFile(); } |
| 123 | SPECIALIZE_TYPE_TRAITS_END() |
| 124 | |