94d6264dc838c6a3f171a6e3ac5c2f189e132cfa
[WebKit-https.git] / Source / WebKit / UIProcess / ios / WKSystemPreviewView.mm
1 /*
2  * Copyright (C) 2018 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 "WKSystemPreviewView.h"
28
29 #if USE(SYSTEM_PREVIEW)
30
31 #import "APIFindClient.h"
32 #import "APIUIClient.h"
33 #import "WKWebViewInternal.h"
34 #import "WebPageProxy.h"
35 #import <MobileCoreServices/MobileCoreServices.h>
36 #import <WebCore/FloatRect.h>
37 #import <WebCore/LocalizedStrings.h>
38 #import <pal/spi/cg/CoreGraphicsSPI.h>
39 #import <pal/spi/ios/SystemPreviewSPI.h>
40 #import <wtf/RetainPtr.h>
41 #import <wtf/SoftLinking.h>
42 #import <wtf/Vector.h>
43
44 #if USE(APPLE_INTERNAL_SDK)
45 #import <WebKitAdditions/SystemPreviewTypes.cpp>
46 #endif
47
48 SOFT_LINK_FRAMEWORK(QuickLook);
49 SOFT_LINK_CLASS(QuickLook, QLItem);
50
51 SOFT_LINK_PRIVATE_FRAMEWORK(AssetViewer);
52 SOFT_LINK_CLASS(AssetViewer, ASVThumbnailView);
53
54 @interface WKSystemPreviewView () <ASVThumbnailViewDelegate>
55 @end
56
57 @implementation WKSystemPreviewView {
58     RetainPtr<NSItemProvider> _itemProvider;
59     RetainPtr<NSData> _data;
60     RetainPtr<NSString> _suggestedFilename;
61     RetainPtr<NSString> _mimeType;
62     RetainPtr<QLItem> _item;
63     RetainPtr<ASVThumbnailView> _thumbnailView;
64     WKWebView *_webView;
65 }
66
67 - (instancetype)web_initWithFrame:(CGRect)frame webView:(WKWebView *)webView mimeType:(NSString *)mimeType
68 {
69     if (!(self = [super initWithFrame:frame]))
70         return nil;
71
72     UIColor *backgroundColor = [UIColor colorWithRed:(38. / 255) green:(38. / 255) blue:(38. / 255) alpha:1];
73     self.backgroundColor = backgroundColor;
74
75     _webView = webView;
76     _mimeType = mimeType;
77
78     UIScrollView *scrollView = webView.scrollView;
79     [scrollView setMinimumZoomScale:1];
80     [scrollView setMaximumZoomScale:1];
81     [scrollView setBackgroundColor:backgroundColor];
82
83     return self;
84 }
85
86 - (void)web_setContentProviderData:(NSData *)data suggestedFilename:(NSString *)filename
87 {
88     RefPtr<WebKit::WebPageProxy> page = _webView->_page;
89     UIViewController *presentingViewController = page->uiClient().presentingViewController();
90
91     if (!presentingViewController)
92         return;
93
94     _suggestedFilename = adoptNS([filename copy]);
95     _data = adoptNS([data copy]);
96
97     NSString *contentType;
98 #if USE(APPLE_INTERNAL_SDK)
99     contentType = WebKit::getUTIForMIMEType(_mimeType.get());
100 #else
101     NSString *extension = [[_suggestedFilename.get() pathExtension] lowercaseString];
102     RetainPtr<CFStringRef> contentTypeCF = adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, nil));
103     contentType = (NSString *)contentTypeCF.get();
104 #endif
105
106     _item = adoptNS([allocQLItemInstance() initWithDataProvider:self contentType:contentType previewTitle:_suggestedFilename.get()]);
107     [_item setUseLoadingTimeout:NO];
108
109     _thumbnailView = adoptNS([allocASVThumbnailViewInstance() init]);
110     [_thumbnailView setDelegate:self];
111     [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
112
113     [self setAutoresizesSubviews:YES];
114     [self setClipsToBounds:YES];
115     [self addSubview:_thumbnailView.get()];
116     [self _layoutThumbnailView];
117
118     auto screenBounds = UIScreen.mainScreen.bounds;
119     CGFloat maxDimension = CGFloatMin(screenBounds.size.width, screenBounds.size.height);
120     [_thumbnailView setMaxThumbnailSize:CGSizeMake(maxDimension, maxDimension)];
121
122     [_thumbnailView setThumbnailItem:_item.get()];
123 }
124
125 - (void)_layoutThumbnailView
126 {
127     if (_thumbnailView) {
128         UIEdgeInsets safeAreaInsets = _webView._computedUnobscuredSafeAreaInset;
129         UIEdgeInsets obscuredAreaInsets = _webView._computedObscuredInset;
130
131         CGRect layoutFrame = UIEdgeInsetsInsetRect(self.frame, safeAreaInsets);
132
133         layoutFrame.size.width -= obscuredAreaInsets.left + obscuredAreaInsets.right;
134         layoutFrame.size.height -= obscuredAreaInsets.top + obscuredAreaInsets.bottom;
135
136         [_thumbnailView setFrame:layoutFrame];
137         [_thumbnailView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
138     }
139 }
140
141 #pragma mark ASVThumbnailViewDelegate
142
143 - (void)thumbnailView:(ASVThumbnailView *)thumbnailView wantsToPresentPreviewController:(QLPreviewController *)previewController forItem:(QLItem *)item
144 {
145     RefPtr<WebKit::WebPageProxy> page = _webView->_page;
146     UIViewController *presentingViewController = page->uiClient().presentingViewController();
147     [presentingViewController presentViewController:previewController animated:YES completion:nil];
148 }
149
150 #pragma mark WKWebViewContentProvider protocol
151
152 - (UIView *)web_contentView
153 {
154     return self;
155 }
156
157 - (void)web_setMinimumSize:(CGSize)size
158 {
159 }
160
161 - (void)web_setOverlaidAccessoryViewsInset:(CGSize)inset
162 {
163 }
164
165 - (void)web_computedContentInsetDidChange
166 {
167     [self _layoutThumbnailView];
168 }
169
170 - (void)web_setFixedOverlayView:(UIView *)fixedOverlayView
171 {
172 }
173
174 - (void)web_didSameDocumentNavigation:(WKSameDocumentNavigationType)navigationType
175 {
176 }
177
178 #pragma mark Find-in-Page
179
180 - (void)web_countStringMatches:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
181 {
182     RefPtr<WebKit::WebPageProxy> page = _webView->_page;
183     page->findClient().didCountStringMatches(page.get(), string, 0);
184 }
185
186 - (void)web_findString:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
187 {
188     RefPtr<WebKit::WebPageProxy> page = _webView->_page;
189     page->findClient().didFailToFindString(page.get(), string);
190 }
191
192 - (void)web_hideFindUI
193 {
194 }
195
196 #pragma mark QLPreviewItemDataProvider
197
198 - (NSData *)provideDataForItem:(QLItem *)item
199 {
200     return _data.get();
201 }
202
203 @end
204
205 #endif