Shrink various loading-related enums to shrink CachedResource
[WebKit-https.git] / Source / WebCore / platform / ios / QuickLook.mm
1 /*
2  * Copyright (C) 2009-2017 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "QuickLook.h"
28
29 #if USE(QUICK_LOOK)
30
31 #import "FileSystem.h"
32 #import "PreviewConverter.h"
33 #import "ResourceRequest.h"
34 #import "SchemeRegistry.h"
35 #import <pal/spi/cocoa/NSFileManagerSPI.h>
36 #import <wtf/Lock.h>
37 #import <wtf/NeverDestroyed.h>
38
39 #import "QuickLookSoftLink.h"
40
41 namespace WebCore {
42
43 const char* QLPreviewProtocol = "x-apple-ql-id";
44
45 NSSet *QLPreviewGetSupportedMIMETypesSet()
46 {
47     static NSSet *set = [QLPreviewGetSupportedMIMETypes() retain];
48     return set;
49 }
50
51 static Lock qlPreviewConverterDictionaryLock;
52
53 static NSMutableDictionary *QLPreviewConverterDictionary()
54 {
55     static NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
56     return dictionary;
57 }
58
59 static NSMutableDictionary *QLContentDictionary()
60 {
61     static NSMutableDictionary *contentDictionary = [[NSMutableDictionary alloc] init];
62     return contentDictionary;
63 }
64
65 void removeQLPreviewConverterForURL(NSURL *url)
66 {
67     auto locker = holdLock(qlPreviewConverterDictionaryLock);
68     [QLPreviewConverterDictionary() removeObjectForKey:url];
69     [QLContentDictionary() removeObjectForKey:url];
70 }
71
72 static void addQLPreviewConverterWithFileForURL(NSURL *url, id converter, NSString *fileName)
73 {
74     ASSERT(url);
75     ASSERT(converter);
76     auto locker = holdLock(qlPreviewConverterDictionaryLock);
77     [QLPreviewConverterDictionary() setObject:converter forKey:url];
78     [QLContentDictionary() setObject:(fileName ? fileName : @"") forKey:url];
79 }
80
81 RetainPtr<NSURLRequest> registerQLPreviewConverterIfNeeded(NSURL *url, NSString *mimeType, NSData *data)
82 {
83     RetainPtr<NSString> updatedMIMEType = adoptNS(QLTypeCopyBestMimeTypeForURLAndMimeType(url, mimeType));
84
85     if ([QLPreviewGetSupportedMIMETypesSet() containsObject:updatedMIMEType.get()]) {
86         RetainPtr<NSString> uti = adoptNS(QLTypeCopyUTIForURLAndMimeType(url, updatedMIMEType.get()));
87
88         auto converter = std::make_unique<PreviewConverter>(data, uti.get());
89         ResourceRequest previewRequest = converter->previewRequest();
90
91         // We use [request URL] here instead of url since it will be
92         // the URL that the WebDataSource will see during -dealloc.
93         addQLPreviewConverterWithFileForURL(previewRequest.url(), converter->platformConverter(), nil);
94
95         return previewRequest.nsURLRequest(HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody);
96     }
97
98     return nil;
99 }
100
101 bool isQuickLookPreviewURL(const URL& url)
102 {
103     return url.protocolIs(QLPreviewProtocol);
104 }
105
106 static NSDictionary *temporaryFileAttributes()
107 {
108     static NSDictionary *attributes = [@{
109         NSFileOwnerAccountName : NSUserName(),
110         NSFilePosixPermissions : [NSNumber numberWithInteger:(WEB_UREAD | WEB_UWRITE)],
111         } retain];
112     return attributes;
113 }
114
115 static NSDictionary *temporaryDirectoryAttributes()
116 {
117     static NSDictionary *attributes = [@{
118         NSFileOwnerAccountName : NSUserName(),
119         NSFilePosixPermissions : [NSNumber numberWithInteger:(WEB_UREAD | WEB_UWRITE | WEB_UEXEC)],
120         NSFileProtectionKey : NSFileProtectionCompleteUnlessOpen,
121         } retain];
122     return attributes;
123 }
124
125 NSString *createTemporaryFileForQuickLook(NSString *fileName)
126 {
127     NSString *downloadDirectory = FileSystem::createTemporaryDirectory(@"QuickLookContent");
128     if (!downloadDirectory)
129         return nil;
130
131     NSFileManager *fileManager = [NSFileManager defaultManager];
132
133     NSError *error;
134     if (![fileManager setAttributes:temporaryDirectoryAttributes() ofItemAtPath:downloadDirectory error:&error]) {
135         LOG_ERROR("Failed to set attribute NSFileProtectionCompleteUnlessOpen on directory %@ with error: %@.", downloadDirectory, error.localizedDescription);
136         return nil;
137     }
138
139     NSString *contentPath = [downloadDirectory stringByAppendingPathComponent:fileName.lastPathComponent];
140     if (![fileManager _web_createFileAtPath:contentPath contents:nil attributes:temporaryFileAttributes()]) {
141         LOG_ERROR("Failed to create QuickLook temporary file at path %@.", contentPath);
142         return nil;
143     }
144
145     return contentPath;
146 }
147
148 } // namespace WebCore
149
150 #endif // USE(QUICK_LOOK)