blob: d53447a5bbfdd2b86c725ce4810db493b78d7ccf [file] [log] [blame]
weinig@apple.com50bca612008-06-20 21:43:11 +00001/*
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
achristensen@apple.com760090a2016-08-22 15:51:58 +000026#pragma once
weinig@apple.com50bca612008-06-20 21:43:11 +000027
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000028#include "Blob.h"
beidson@apple.com07faaf92016-04-25 17:31:29 +000029#include <wtf/Optional.h>
akling@apple.comad2beb52014-12-25 07:50:20 +000030#include <wtf/Ref.h>
cdumez@apple.com2a73a5e2014-10-02 04:10:35 +000031#include <wtf/TypeCasts.h>
weinig@apple.com3f5ab022012-09-06 17:36:48 +000032#include <wtf/text/WTFString.h>
weinig@apple.com50bca612008-06-20 21:43:11 +000033
34namespace WebCore {
35
darin@apple.com5ffbb5c2013-09-27 16:39:41 +000036class URL;
jianli@chromium.org42e36c72010-08-31 04:45:21 +000037
ap@apple.com701f9862014-05-06 00:42:52 +000038class File final : public Blob {
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000039public:
akling@apple.comad2beb52014-12-25 07:50:20 +000040 static Ref<File> create(const String& path)
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000041 {
akling@apple.comad2beb52014-12-25 07:50:20 +000042 return adoptRef(*new File(path));
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000043 }
weinig@apple.com50bca612008-06-20 21:43:11 +000044
beidson@apple.com07faaf92016-04-25 17:31:29 +000045 // Create a File using the 'new File' constructor.
46 static Ref<File> create(Vector<BlobPart> blobParts, const String& filename, const String& contentType, int64_t lastModified)
47 {
48 return adoptRef(*new File(WTFMove(blobParts), filename, contentType, lastModified));
49 }
50
akling@apple.comad2beb52014-12-25 07:50:20 +000051 static Ref<File> deserialize(const String& path, const URL& srcURL, const String& type, const String& name)
jianli@chromium.org51ceb752010-08-11 00:03:19 +000052 {
akling@apple.comad2beb52014-12-25 07:50:20 +000053 return adoptRef(*new File(deserializationContructor, path, srcURL, type, name));
jianli@chromium.org51ceb752010-08-11 00:03:19 +000054 }
55
adamk@chromium.orgaee872b2011-05-23 20:47:06 +000056 // 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.
akling@apple.comad2beb52014-12-25 07:50:20 +000057 static Ref<File> createWithName(const String& path, const String& nameOverride)
johnnyg@google.com4b0a4552010-07-15 20:36:40 +000058 {
ap@apple.comc559d642014-05-09 04:37:25 +000059 if (nameOverride.isEmpty())
akling@apple.comad2beb52014-12-25 07:50:20 +000060 return adoptRef(*new File(path));
61 return adoptRef(*new File(path, nameOverride));
johnnyg@google.com4b0a4552010-07-15 20:36:40 +000062 }
johnnyg@google.com4b0a4552010-07-15 20:36:40 +000063
darin@apple.com11ff47c2016-03-04 16:47:55 +000064 bool isFile() const override { return true; }
jianli@chromium.orgd8be2d12010-04-16 00:50:18 +000065
jianli@chromium.org42e36c72010-08-31 04:45:21 +000066 const String& path() const { return m_path; }
67 const String& name() const { return m_name; }
achristensen@apple.com760090a2016-08-22 15:51:58 +000068 WEBCORE_EXPORT double lastModified() const;
kinuko@chromium.orga492ad02012-05-30 09:08:43 +000069
ap@apple.comc559d642014-05-09 04:37:25 +000070 static String contentTypeForFile(const String& path);
71
72#if ENABLE(FILE_REPLACEMENT)
73 static bool shouldReplaceFile(const String& path);
74#endif
ap@apple.com1fddc772014-04-25 00:48:38 +000075
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000076private:
weinig@apple.come798c252015-02-25 01:49:59 +000077 WEBCORE_EXPORT explicit File(const String& path);
ap@apple.comc559d642014-05-09 04:37:25 +000078 File(const String& path, const String& nameOverride);
beidson@apple.com07faaf92016-04-25 17:31:29 +000079 File(Vector<BlobPart>&& blobParts, const String& filename, const String& contentType, int64_t lastModified);
adamk@chromium.orgaee872b2011-05-23 20:47:06 +000080
ap@apple.com85125572014-05-09 21:24:01 +000081 File(DeserializationContructor, const String& path, const URL& srcURL, const String& type, const String& name);
jianli@chromium.org51ceb752010-08-11 00:03:19 +000082
ap@apple.comc559d642014-05-09 04:37:25 +000083 static void computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType);
84#if ENABLE(FILE_REPLACEMENT)
85 static void computeNameAndContentTypeForReplacedFile(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType);
86#endif
87
jianli@chromium.org42e36c72010-08-31 04:45:21 +000088 String m_path;
89 String m_name;
beidson@apple.com07faaf92016-04-25 17:31:29 +000090
utatane.tea@gmail.com43926962016-11-27 06:08:16 +000091 std::optional<int64_t> m_overrideLastModifiedDate;
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000092};
weinig@apple.com50bca612008-06-20 21:43:11 +000093
94} // namespace WebCore
95
cdumez@apple.com2a73a5e2014-10-02 04:10:35 +000096SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::File)
97 static bool isType(const WebCore::Blob& blob) { return blob.isFile(); }
98SPECIALIZE_TYPE_TRAITS_END()