Add a FrameLoaderClient willInjectUserScriptForFrame callback
[WebKit-https.git] / Tools / WebKitTestRunner / cocoa / TestControllerCocoa.mm
1 /*
2  * Copyright (C) 2015-2016 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 "TestController.h"
28
29 #import "CrashReporterInfo.h"
30 #import "PlatformWebView.h"
31 #import "StringFunctions.h"
32 #import "TestInvocation.h"
33 #import "TestRunnerWKWebView.h"
34 #import <Foundation/Foundation.h>
35 #import <WebKit/WKContextConfigurationRef.h>
36 #import <WebKit/WKCookieManager.h>
37 #import <WebKit/WKPreferencesRefPrivate.h>
38 #import <WebKit/WKProcessPoolPrivate.h>
39 #import <WebKit/WKStringCF.h>
40 #import <WebKit/WKUserContentControllerPrivate.h>
41 #import <WebKit/WKWebView.h>
42 #import <WebKit/WKWebViewConfiguration.h>
43 #import <WebKit/WKWebViewConfigurationPrivate.h>
44 #import <WebKit/WKWebViewPrivate.h>
45 #import <WebKit/WKWebsiteDataRecordPrivate.h>
46 #import <WebKit/WKWebsiteDataStorePrivate.h>
47 #import <WebKit/WKWebsiteDataStoreRef.h>
48 #import <WebKit/_WKApplicationManifest.h>
49 #import <WebKit/_WKUserContentExtensionStore.h>
50 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
51 #import <wtf/MainThread.h>
52
53 namespace WTR {
54
55 static WKWebViewConfiguration *globalWebViewConfiguration;
56
57 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
58 {
59 #if WK_API_ENABLED
60     [globalWebViewConfiguration release];
61     globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
62
63     globalWebViewConfiguration.processPool = (__bridge WKProcessPool *)context;
64     globalWebViewConfiguration.websiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context);
65     globalWebViewConfiguration._allowUniversalAccessFromFileURLs = YES;
66     globalWebViewConfiguration._applePayEnabled = YES;
67
68 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || PLATFORM(IOS)
69     WKCookieManagerSetCookieStoragePartitioningEnabled(WKContextGetCookieManager(context), true);
70     WKCookieManagerSetStorageAccessAPIEnabled(WKContextGetCookieManager(context), true);
71 #endif
72
73     WKWebsiteDataStore* poolWebsiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore((__bridge WKContextRef)globalWebViewConfiguration.processPool);
74     [poolWebsiteDataStore _setCacheStoragePerOriginQuota: 400 * 1024];
75     if (libraryPath) {
76         String cacheStorageDirectory = String(libraryPath) + '/' + "CacheStorage";
77         [poolWebsiteDataStore _setCacheStorageDirectory: cacheStorageDirectory];
78
79         String serviceWorkerRegistrationDirectory = String(libraryPath) + '/' + "ServiceWorkers";
80         [poolWebsiteDataStore _setServiceWorkerRegistrationDirectory: serviceWorkerRegistrationDirectory];
81     }
82
83     [globalWebViewConfiguration.websiteDataStore _setResourceLoadStatisticsEnabled:YES];
84     [globalWebViewConfiguration.websiteDataStore _resourceLoadStatisticsSetShouldSubmitTelemetry:NO];
85
86 #if PLATFORM(IOS)
87     globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
88     globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
89     globalWebViewConfiguration._invisibleAutoplayNotPermitted = NO;
90     globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
91     globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
92 #endif
93     globalWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
94 #endif
95
96 #if USE(SYSTEM_PREVIEW)
97     globalWebViewConfiguration._systemPreviewEnabled = YES;
98 #endif
99 }
100
101 void TestController::cocoaPlatformInitialize()
102 {
103     const char* dumpRenderTreeTemp = libraryPathForTesting();
104     if (!dumpRenderTreeTemp)
105         return;
106
107     String resourceLoadStatisticsFolder = String(dumpRenderTreeTemp) + '/' + "ResourceLoadStatistics";
108     [[NSFileManager defaultManager] createDirectoryAtPath:resourceLoadStatisticsFolder withIntermediateDirectories:YES attributes:nil error: nil];
109     String fullBrowsingSessionResourceLog = resourceLoadStatisticsFolder + '/' + "full_browsing_session_resourceLog.plist";
110     NSDictionary *resourceLogPlist = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1], @"version", nil];
111     if (![resourceLogPlist writeToFile:fullBrowsingSessionResourceLog atomically:YES])
112         WTFCrash();
113     [resourceLogPlist release];
114 }
115
116 WKContextRef TestController::platformContext()
117 {
118 #if WK_API_ENABLED
119     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
120 #else
121     return nullptr;
122 #endif
123 }
124
125 WKPreferencesRef TestController::platformPreferences()
126 {
127 #if WK_API_ENABLED
128     return (__bridge WKPreferencesRef)globalWebViewConfiguration.preferences;
129 #else
130     return nullptr;
131 #endif
132 }
133
134 void TestController::platformAddTestOptions(TestOptions& options) const
135 {
136     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnNavigation"])
137         options.enableProcessSwapOnNavigation = true;
138     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnWindowOpen"])
139         options.enableProcessSwapOnWindowOpen = true;
140 }
141
142 void TestController::platformCreateWebView(WKPageConfigurationRef, const TestOptions& options)
143 {
144 #if WK_API_ENABLED
145     RetainPtr<WKWebViewConfiguration> copiedConfiguration = adoptNS([globalWebViewConfiguration copy]);
146
147 #if PLATFORM(IOS)
148     if (options.useDataDetection)
149         [copiedConfiguration setDataDetectorTypes:WKDataDetectorTypeAll];
150     if (options.ignoresViewportScaleLimits)
151         [copiedConfiguration setIgnoresViewportScaleLimits:YES];
152     if (options.useCharacterSelectionGranularity)
153         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
154     if (options.useCharacterSelectionGranularity)
155         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
156 #endif
157
158     if (options.enableAttachmentElement)
159         [copiedConfiguration _setAttachmentElementEnabled: YES];
160
161     if (options.enableColorFilter)
162         [copiedConfiguration _setColorFilterEnabled: YES];
163
164     if (options.applicationManifest.length()) {
165         auto manifestPath = [NSString stringWithUTF8String:options.applicationManifest.c_str()];
166         NSString *text = [NSString stringWithContentsOfFile:manifestPath usedEncoding:nullptr error:nullptr];
167         [copiedConfiguration _setApplicationManifest:[_WKApplicationManifest applicationManifestFromJSON:text manifestURL:nil documentURL:nil]];
168     }
169
170     m_mainWebView = std::make_unique<PlatformWebView>(copiedConfiguration.get(), options);
171 #else
172     m_mainWebView = std::make_unique<PlatformWebView>(globalWebViewConfiguration, options);
173 #endif
174 }
175
176 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions& options)
177 {
178 #if WK_API_ENABLED
179     WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
180     newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
181     return new PlatformWebView(newConfiguration, options);
182 #else
183     return nullptr;
184 #endif
185 }
186
187 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
188 {
189 #if WK_API_ENABLED
190     initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
191     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
192 #else
193     return nullptr;
194 #endif
195 }
196
197 void TestController::platformRunUntil(bool& done, double timeout)
198 {
199     NSDate *endDate = (timeout > 0) ? [NSDate dateWithTimeIntervalSinceNow:timeout] : [NSDate distantFuture];
200
201     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
202         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
203 }
204
205 void TestController::cocoaResetStateToConsistentValues()
206 {
207 #if WK_API_ENABLED
208     __block bool doneRemoving = false;
209     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
210         doneRemoving = true;
211     }];
212     platformRunUntil(doneRemoving, noTimeout);
213     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
214
215     if (PlatformWebView* webView = mainWebView())
216         [webView->platformView().configuration.userContentController _removeAllUserContentFilters];
217 #endif
218 }
219
220 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
221 {
222     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
223 }
224
225 static NSString * const WebArchivePboardType = @"Apple Web Archive pasteboard type";
226 static NSString * const WebSubresourcesKey = @"WebSubresources";
227 static NSString * const WebSubframeArchivesKey = @"WebResourceMIMEType like 'image*'";
228
229 unsigned TestController::imageCountInGeneralPasteboard() const
230 {
231 #if PLATFORM(MAC)
232     NSData *data = [[NSPasteboard generalPasteboard] dataForType:WebArchivePboardType];
233 #elif PLATFORM(IOS)
234     NSData *data = [[UIPasteboard generalPasteboard] valueForPasteboardType:WebArchivePboardType];
235 #endif
236     if (!data)
237         return 0;
238     
239     NSError *error = nil;
240     id webArchive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
241     if (error) {
242         NSLog(@"Encountered error while serializing Web Archive pasteboard data: %@", error);
243         return 0;
244     }
245     
246     NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:WebSubresourcesKey]];
247     NSPredicate *predicate = [NSPredicate predicateWithFormat:WebSubframeArchivesKey];
248     NSArray *imagesArray = [subItems filteredArrayUsingPredicate:predicate];
249     
250     if (!imagesArray)
251         return 0;
252     
253     return imagesArray.count;
254 }
255
256 void TestController::removeAllSessionCredentials()
257 {
258 #if WK_API_ENABLED
259     auto types = adoptNS([[NSSet alloc] initWithObjects:_WKWebsiteDataTypeCredentials, nil]);
260     [globalWebViewConfiguration.websiteDataStore removeDataOfTypes:types.get() modifiedSince:[NSDate distantPast] completionHandler:^() {
261         m_currentInvocation->didRemoveAllSessionCredentials();
262     }];
263 #endif
264 }
265
266 void TestController::getAllStorageAccessEntries()
267 {
268 #if WK_API_ENABLED
269     auto* parentView = mainWebView();
270     if (!parentView)
271         return;
272
273     [globalWebViewConfiguration.websiteDataStore _getAllStorageAccessEntriesFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
274         Vector<String> domains;
275         domains.reserveInitialCapacity(nsDomains.count);
276         for (NSString *domain : nsDomains)
277             domains.uncheckedAppend(domain);
278         m_currentInvocation->didReceiveAllStorageAccessEntries(domains);
279     }];
280 #endif
281 }
282
283 void TestController::injectUserScript(WKStringRef script)
284 {
285 #if WK_API_ENABLED
286     auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
287
288     [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
289 #endif
290 }
291
292 } // namespace WTR