source: webkit/trunk/Source/WebCore/fileapi/File.cpp

Last change on this file was 291341, checked in by [email protected], 3 years ago

Make MIMETypeRegistry::mimeTypeForPath take a StringView
https://bugs.webkit.org/show_bug.cgi?id=236441
<rdar://problem/89084526>

Reviewed by Chris Dumez.

Source/WebCore:

Passing a StringView is more efficient for some code paths.
Covered by existing tests.

  • editing/cocoa/WebArchiveResourceFromNSAttributedString.mm:

(WebCore::if):

  • fileapi/File.cpp:

(WebCore::File::computeNameAndContentType):

  • loader/cocoa/BundleResourceLoader.mm:

(WebCore::BundleResourceLoader::loadResourceFromBundle):

  • platform/MIMETypeRegistry.cpp:

(WebCore::commonMimeTypesMap):

  • platform/MIMETypeRegistry.h:
  • platform/cocoa/MIMETypeRegistryCocoa.mm:

(WebCore::MIMETypeRegistry::mimeTypeForExtension):

  • platform/win/MIMETypeRegistryWin.cpp:

(WebCore::MIMETypeRegistry::mimeTypeForExtension):

  • platform/xdg/MIMETypeRegistryXdg.cpp:

(WebCore::MIMETypeRegistry::mimeTypeForExtension):

Source/WebKit:

  • UIProcess/API/Cocoa/APIAttachmentCocoa.mm:

(API::mimeTypeInferredFromFileExtension):
(API::Attachment::setFileWrapperAndUpdateContentType):

  • UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:

(-[WKInspectorResourceURLSchemeHandler webView:startURLSchemeTask:]):

  • UIProcess/ios/forms/WKFileUploadPanel.mm:

(-[WKFileUploadPanel presentWithParameters:resultListener:]):

Source/WebKitLegacy/ios:

  • WebCoreSupport/WebMIMETypeRegistry.mm:

(+[WebMIMETypeRegistry mimeTypeForExtension:]):

Tools:

  • TestWebKitAPI/cocoa/TestInspectorURLSchemeHandler.mm:

(-[TestInspectorURLSchemeHandler webView:startURLSchemeTask:]):

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
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#include "config.h"
27#include "File.h"
28
29#include "BlobURL.h"
30#include "MIMETypeRegistry.h"
31#include "ThreadableBlobRegistry.h"
32#include <wtf/DateMath.h>
33#include <wtf/FileSystem.h>
34#include <wtf/IsoMallocInlines.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39WTF_MAKE_ISO_ALLOCATED_IMPL(File);
40
41Ref<File> File::createWithRelativePath(ScriptExecutionContext* context, const String& path, const String& relativePath)
42{
43 auto file = File::create(context, path);
44 file->setRelativePath(relativePath);
45 return file;
46}
47
48Ref<File> File::create(ScriptExecutionContext* context, const String& path, const String& replacementPath, const String& nameOverride)
49{
50 String name;
51 String type;
52 String effectivePath = !replacementPath.isNull() ? replacementPath : path;
53 computeNameAndContentType(effectivePath, nameOverride, name, type);
54
55 auto internalURL = BlobURL::createInternalURL();
56 ThreadableBlobRegistry::registerFileBlobURL(internalURL, path, replacementPath, type);
57
58 auto file = adoptRef(*new File(context, WTFMove(internalURL), WTFMove(type), WTFMove(effectivePath), WTFMove(name)));
59 file->suspendIfNeeded();
60 return file;
61}
62
63File::File(ScriptExecutionContext* context, URL&& url, String&& type, String&& path, String&& name)
64 : Blob(uninitializedContructor, context, WTFMove(url), WTFMove(type))
65 , m_path(WTFMove(path))
66 , m_name(WTFMove(name))
67{
68}
69
70File::File(DeserializationContructor, ScriptExecutionContext* context, const String& path, const URL& url, const String& type, const String& name, const std::optional<int64_t>& lastModified)
71 : Blob(deserializationContructor, context, url, type, { }, path)
72 , m_path(path)
73 , m_name(name)
74 , m_lastModifiedDateOverride(lastModified)
75{
76}
77
78File::File(ScriptExecutionContext& context, Vector<BlobPartVariant>&& blobPartVariants, const String& filename, const PropertyBag& propertyBag)
79 : Blob(context, WTFMove(blobPartVariants), propertyBag)
80 , m_name(filename)
81 , m_lastModifiedDateOverride(propertyBag.lastModified.value_or(WallTime::now().secondsSinceEpoch().milliseconds()))
82{
83}
84
85File::File(ScriptExecutionContext* context, const Blob& blob, const String& name)
86 : Blob(referencingExistingBlobConstructor, context, blob)
87 , m_name(name)
88{
89 ASSERT(!blob.isFile());
90}
91
92File::File(ScriptExecutionContext* context, const File& file, const String& name)
93 : Blob(referencingExistingBlobConstructor, context, file)
94 , m_path(file.path())
95 , m_relativePath(file.relativePath())
96 , m_name(!name.isNull() ? name : file.name())
97 , m_lastModifiedDateOverride(file.m_lastModifiedDateOverride)
98 , m_isDirectory(file.isDirectory())
99{
100}
101
102int64_t File::lastModified() const
103{
104 if (m_lastModifiedDateOverride)
105 return m_lastModifiedDateOverride.value();
106
107 int64_t result;
108
109 // FIXME: This does sync-i/o on the main thread and also recalculates every time the method is called.
110 // The i/o should be performed on a background thread,
111 // and the result should be cached along with an asynchronous monitor for changes to the file.
112 auto modificationTime = FileSystem::fileModificationTime(m_path);
113 if (modificationTime)
114 result = modificationTime->secondsSinceEpoch().millisecondsAs<int64_t>();
115 else
116 result = WallTime::now().secondsSinceEpoch().millisecondsAs<int64_t>();
117
118 return timeClip(result);
119}
120
121void File::computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType)
122{
123#if ENABLE(FILE_REPLACEMENT)
124 if (shouldReplaceFile(path)) {
125 computeNameAndContentTypeForReplacedFile(path, nameOverride, effectiveName, effectiveContentType);
126 return;
127 }
128#endif
129 effectiveName = nameOverride.isEmpty() ? FileSystem::pathFileName(path) : nameOverride;
130 size_t index = effectiveName.reverseFind('.');
131 if (index != notFound) {
132 callOnMainThreadAndWait([&effectiveContentType, &effectiveName, index] {
133 effectiveContentType = MIMETypeRegistry::mimeTypeForExtension(StringView(effectiveName).substring(index + 1)).isolatedCopy();
134 });
135 }
136}
137
138String File::contentTypeForFile(const String& path)
139{
140 String name;
141 String type;
142 computeNameAndContentType(path, String(), name, type);
143
144 return type;
145}
146
147bool File::isDirectory() const
148{
149 if (!m_isDirectory)
150 m_isDirectory = FileSystem::fileTypeFollowingSymlinks(m_path) == FileSystem::FileType::Directory;
151 return *m_isDirectory;
152}
153
154const char* File::activeDOMObjectName() const
155{
156 return "File";
157}
158
159} // namespace WebCore
Note: See TracBrowser for help on using the repository browser.