[WTF] Remove StaticLock
[WebKit.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 NSSet *QLPreviewGetSupportedMIMETypesSet()
44 {
45     static NSSet *set = [QLPreviewGetSupportedMIMETypes() retain];
46     return set;
47 }
48
49 static Lock qlPreviewConverterDictionaryLock;
50
51 static NSMutableDictionary *QLPreviewConverterDictionary()
52 {
53     static NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
54     return dictionary;
55 }
56
57 static NSMutableDictionary *QLContentDictionary()
58 {
59     static NSMutableDictionary *contentDictionary = [[NSMutableDictionary alloc] init];
60     return contentDictionary;
61 }
62
63 void removeQLPreviewConverterForURL(NSURL *url)
64 {
65     auto locker = holdLock(qlPreviewConverterDictionaryLock);
66     [QLPreviewConverterDictionary() removeObjectForKey:url];
67     [QLContentDictionary() removeObjectForKey:url];
68 }
69
70 static void addQLPreviewConverterWithFileForURL(NSURL *url, id converter, NSString *fileName)
71 {
72     ASSERT(url);
73     ASSERT(converter);
74     auto locker = holdLock(qlPreviewConverterDictionaryLock);
75     [QLPreviewConverterDictionary() setObject:converter forKey:url];
76     [QLContentDictionary() setObject:(fileName ? fileName : @"") forKey:url];
77 }
78
79 RetainPtr<NSURLRequest> registerQLPreviewConverterIfNeeded(NSURL *url, NSString *mimeType, NSData *data)
80 {
81     RetainPtr<NSString> updatedMIMEType = adoptNS(QLTypeCopyBestMimeTypeForURLAndMimeType(url, mimeType));
82
83     if ([QLPreviewGetSupportedMIMETypesSet() containsObject:updatedMIMEType.get()]) {
84         RetainPtr<NSString> uti = adoptNS(QLTypeCopyUTIForURLAndMimeType(url, updatedMIMEType.get()));
85
86         auto converter = std::make_unique<PreviewConverter>(data, uti.get());
87         ResourceRequest previewRequest = converter->previewRequest();
88
89         // We use [request URL] here instead of url since it will be
90         // the URL that the WebDataSource will see during -dealloc.
91         addQLPreviewConverterWithFileForURL(previewRequest.url(), converter->platformConverter(), nil);
92
93         return previewRequest.nsURLRequest(DoNotUpdateHTTPBody);
94     }
95
96     return nil;
97 }
98
99 const char* QLPreviewProtocol()
100 {
101     static const char* const previewProtocol = fastStrDup([QLPreviewScheme UTF8String]);
102     return previewProtocol;
103 }
104
105 bool isQuickLookPreviewURL(const URL& url)
106 {
107     // Use some known protocols as a short-cut to avoid loading the QuickLook framework.
108     if (url.protocolIsInHTTPFamily() || url.isBlankURL() || url.protocolIsBlob() || url.protocolIsData() || SchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol().toString()))
109         return false;
110     return url.protocolIs(QLPreviewProtocol());
111 }
112
113 static NSDictionary *temporaryFileAttributes()
114 {
115     static NSDictionary *attributes = [@{
116         NSFileOwnerAccountName : NSUserName(),
117         NSFilePosixPermissions : [NSNumber numberWithInteger:(WEB_UREAD | WEB_UWRITE)],
118         } retain];
119     return attributes;
120 }
121
122 static NSDictionary *temporaryDirectoryAttributes()
123 {
124     static NSDictionary *attributes = [@{
125         NSFileOwnerAccountName : NSUserName(),
126         NSFilePosixPermissions : [NSNumber numberWithInteger:(WEB_UREAD | WEB_UWRITE | WEB_UEXEC)],
127         NSFileProtectionKey : NSFileProtectionCompleteUnlessOpen,
128         } retain];
129     return attributes;
130 }
131
132 NSString *createTemporaryFileForQuickLook(NSString *fileName)
133 {
134     NSString *downloadDirectory = FileSystem::createTemporaryDirectory(@"QuickLookContent");
135     if (!downloadDirectory)
136         return nil;
137
138     NSFileManager *fileManager = [NSFileManager defaultManager];
139
140     NSError *error;
141     if (![fileManager setAttributes:temporaryDirectoryAttributes() ofItemAtPath:downloadDirectory error:&error]) {
142         LOG_ERROR("Failed to set attribute NSFileProtectionCompleteUnlessOpen on directory %@ with error: %@.", downloadDirectory, error.localizedDescription);
143         return nil;
144     }
145
146     NSString *contentPath = [downloadDirectory stringByAppendingPathComponent:fileName.lastPathComponent];
147     if (![fileManager _web_createFileAtPath:contentPath contents:nil attributes:temporaryFileAttributes()]) {
148         LOG_ERROR("Failed to create QuickLook temporary file at path %@.", contentPath);
149         return nil;
150     }
151
152     return contentPath;
153 }
154
155 } // namespace WebCore
156
157 #endif // USE(QUICK_LOOK)