Resource Load Statistics: Grandfather domains for existing data records
[WebKit-https.git] / Tools / WebKitTestRunner / TestController.h
1 /*
2  * Copyright (C) 2010, 2015-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 #ifndef TestController_h
27 #define TestController_h
28
29 #include "GeolocationProviderMock.h"
30 #include "WebNotificationProvider.h"
31 #include "WorkQueueManager.h"
32 #include <WebKit/WKRetainPtr.h>
33 #include <string>
34 #include <vector>
35 #include <wtf/HashMap.h>
36 #include <wtf/Vector.h>
37 #include <wtf/text/StringHash.h>
38
39 OBJC_CLASS WKWebViewConfiguration;
40
41 namespace WTR {
42
43 class TestInvocation;
44 class OriginSettings;
45 class PlatformWebView;
46 class EventSenderProxy;
47 struct TestCommand;
48 struct TestOptions;
49
50 // FIXME: Rename this TestRunner?
51 class TestController {
52 public:
53     static TestController& singleton();
54
55     static const unsigned viewWidth;
56     static const unsigned viewHeight;
57
58     static const unsigned w3cSVGViewWidth;
59     static const unsigned w3cSVGViewHeight;
60
61     static const double defaultShortTimeout;
62     static const double noTimeout;
63
64     TestController(int argc, const char* argv[]);
65     ~TestController();
66
67     bool verbose() const { return m_verbose; }
68
69     WKStringRef injectedBundlePath() const { return m_injectedBundlePath.get(); }
70     WKStringRef testPluginDirectory() const { return m_testPluginDirectory.get(); }
71
72     PlatformWebView* mainWebView() { return m_mainWebView.get(); }
73     WKContextRef context() { return m_context.get(); }
74
75     EventSenderProxy* eventSenderProxy() { return m_eventSenderProxy.get(); }
76
77     bool shouldUseRemoteLayerTree() const { return m_shouldUseRemoteLayerTree; }
78     
79     // Runs the run loop until `done` is true or the timeout elapses.
80     bool useWaitToDumpWatchdogTimer() { return m_useWaitToDumpWatchdogTimer; }
81     void runUntil(bool& done, double timeoutSeconds);
82     void notifyDone();
83
84     bool shouldShowWebView() const { return m_shouldShowWebView; }
85     bool usingServerMode() const { return m_usingServerMode; }
86     void configureViewForTest(const TestInvocation&);
87     
88     bool beforeUnloadReturnValue() const { return m_beforeUnloadReturnValue; }
89     void setBeforeUnloadReturnValue(bool value) { m_beforeUnloadReturnValue = value; }
90
91     void simulateWebNotificationClick(uint64_t notificationID);
92
93     // Geolocation.
94     void setGeolocationPermission(bool);
95     void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed);
96     void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
97     void handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef);
98     bool isGeolocationProviderActive() const;
99
100     // MediaStream.
101     String saltForOrigin(WKFrameRef, String);
102     void getUserMediaInfoForOrigin(WKFrameRef, WKStringRef originKey, bool&, WKRetainPtr<WKStringRef>&);
103     WKStringRef getUserMediaSaltForOrigin(WKFrameRef, WKStringRef originKey);
104     void setUserMediaPermission(bool);
105     void setUserMediaPersistentPermissionForOrigin(bool, WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
106     void handleUserMediaPermissionRequest(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, WKUserMediaPermissionRequestRef);
107     void handleCheckOfUserMediaPermissionForOrigin(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, const WKUserMediaPermissionCheckRef&);
108     OriginSettings& settingsForOrigin(const String&);
109     unsigned userMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
110     void resetUserMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
111
112     // Policy delegate.
113     void setCustomPolicyDelegate(bool enabled, bool permissive);
114
115     // Page Visibility.
116     void setHidden(bool);
117
118     unsigned imageCountInGeneralPasteboard() const;
119
120     bool resetStateToConsistentValues(const TestOptions&);
121     void resetPreferencesToConsistentValues(const TestOptions&);
122
123     void terminateWebContentProcess();
124     void reattachPageToWebProcess();
125
126     static const char* webProcessName();
127     static const char* networkProcessName();
128     static const char* databaseProcessName();
129
130     WorkQueueManager& workQueueManager() { return m_workQueueManager; }
131
132     void setRejectsProtectionSpaceAndContinueForAuthenticationChallenges(bool value) { m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges = value; }
133     void setHandlesAuthenticationChallenges(bool value) { m_handlesAuthenticationChallenges = value; }
134     void setAuthenticationUsername(String username) { m_authenticationUsername = username; }
135     void setAuthenticationPassword(String password) { m_authenticationPassword = password; }
136     void setAllowsAnySSLCertificate(bool);
137
138     void setBlockAllPlugins(bool shouldBlock) { m_shouldBlockAllPlugins = shouldBlock; }
139
140     void setShouldLogHistoryClientCallbacks(bool shouldLog) { m_shouldLogHistoryClientCallbacks = shouldLog; }
141     void setShouldLogCanAuthenticateAgainstProtectionSpace(bool shouldLog) { m_shouldLogCanAuthenticateAgainstProtectionSpace = shouldLog; }
142
143     bool isCurrentInvocation(TestInvocation* invocation) const { return invocation == m_currentInvocation.get(); }
144
145     void setShouldDecideNavigationPolicyAfterDelay(bool value) { m_shouldDecideNavigationPolicyAfterDelay = value; }
146
147     void setNavigationGesturesEnabled(bool value);
148     void setIgnoresViewportScaleLimits(bool);
149
150     void setShouldDownloadUndisplayableMIMETypes(bool value) { m_shouldDownloadUndisplayableMIMETypes = value; }
151
152     void setStatisticsPrevalentResource(WKStringRef hostName, bool value);
153     bool isStatisticsPrevalentResource(WKStringRef hostName);
154     void setStatisticsHasHadUserInteraction(WKStringRef hostName, bool value);
155     bool isStatisticsHasHadUserInteraction(WKStringRef hostName);
156     void setStatisticsGrandfathered(WKStringRef hostName, bool value);
157     bool isStatisticsGrandfathered(WKStringRef hostName);
158     void setStatisticsSubframeUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
159     void setStatisticsSubresourceUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
160     void setStatisticsSubresourceUniqueRedirectTo(WKStringRef hostName, WKStringRef hostNameRedirectedTo);
161     void setStatisticsTimeToLiveUserInteraction(double seconds);
162     void setStatisticsTimeToLiveCookiePartitionFree(double seconds);
163     void statisticsFireDataModificationHandler();
164     void statisticsFireShouldPartitionCookiesHandler();
165     void statisticsFireShouldPartitionCookiesHandlerForOneDomain(WKStringRef hostName, bool value);
166     void setStatisticsNotifyPagesWhenDataRecordsWereScanned(bool);
167     void setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(bool);
168     void setStatisticsMinimumTimeBetweeenDataRecordsRemoval(double);
169     void setStatisticsGrandfatheringTime(double seconds);
170     void statisticsClearInMemoryAndPersistentStore();
171     void statisticsClearInMemoryAndPersistentStoreModifiedSinceHours(unsigned);
172     void statisticsResetToConsistentState();
173
174     WKArrayRef openPanelFileURLs() const { return m_openPanelFileURLs.get(); }
175     void setOpenPanelFileURLs(WKArrayRef fileURLs) { m_openPanelFileURLs = fileURLs; }
176
177     void terminateNetworkProcess();
178     
179 private:
180     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
181     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
182     void initialize(int argc, const char* argv[]);
183     void createWebViewWithOptions(const TestOptions&);
184     void run();
185
186     void runTestingServerLoop();
187     bool runTest(const char* pathOrURL);
188
189     void platformInitialize();
190     void platformDestroy();
191     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
192     void platformInitializeContext();
193     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
194     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
195     void platformResetPreferencesToConsistentValues();
196     void platformResetStateToConsistentValues();
197 #if PLATFORM(COCOA)
198     void cocoaResetStateToConsistentValues();
199 #endif
200     void platformConfigureViewForTest(const TestInvocation&);
201     void platformWillRunTest(const TestInvocation&);
202     void platformRunUntil(bool& done, double timeout);
203     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
204     WKContextRef platformContext();
205     WKPreferencesRef platformPreferences();
206     void initializeInjectedBundlePath();
207     void initializeTestPluginDirectory();
208
209     void ensureViewSupportsOptionsForTest(const TestInvocation&);
210     TestOptions testOptionsForTest(const TestCommand&) const;
211     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const std::string& pathOrURL) const;
212
213     void updateWebViewSizeForTest(const TestInvocation&);
214     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
215
216     void decidePolicyForGeolocationPermissionRequestIfPossible();
217     void decidePolicyForUserMediaPermissionRequestIfPossible();
218
219     // WKContextInjectedBundleClient
220     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
221     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
222     static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void *clientInfo);
223
224     // WKPageInjectedBundleClient
225     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
226     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
227     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
228     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
229     WKRetainPtr<WKTypeRef> getInjectedBundleInitializationUserData();
230
231     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
232
233     // WKContextClient
234     static void networkProcessDidCrash(WKContextRef, const void*);
235     void networkProcessDidCrash();
236     static void databaseProcessDidCrash(WKContextRef, const void*);
237     void databaseProcessDidCrash();
238
239     // WKPageNavigationClient
240     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
241     void didCommitNavigation(WKPageRef, WKNavigationRef);
242
243     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
244     void didFinishNavigation(WKPageRef, WKNavigationRef);
245
246     
247     // WKContextDownloadClient
248     static void downloadDidStart(WKContextRef, WKDownloadRef, const void*);
249     void downloadDidStart(WKContextRef, WKDownloadRef);
250     static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool* allowOverwrite, const void *clientInfo);
251     WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool*& allowOverwrite);
252     static void downloadDidFinish(WKContextRef, WKDownloadRef, const void*);
253     void downloadDidFinish(WKContextRef, WKDownloadRef);
254     static void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef, const void*);
255     void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef);
256     static void downloadDidCancel(WKContextRef, WKDownloadRef, const void*);
257     void downloadDidCancel(WKContextRef, WKDownloadRef);
258     
259     static void processDidCrash(WKPageRef, const void* clientInfo);
260     void processDidCrash();
261
262     static void didBeginNavigationGesture(WKPageRef, const void*);
263     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
264     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
265     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
266     void didBeginNavigationGesture(WKPageRef);
267     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
268     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
269     void didRemoveNavigationGestureSnapshot(WKPageRef);
270
271     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
272     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
273     
274
275     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
276     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
277
278     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
279
280     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void*);
281     bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef);
282
283     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void*);
284     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
285
286     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
287     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
288
289     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
290     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
291
292     // WKContextHistoryClient
293     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
294     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
295
296     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
297     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
298
299     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
300     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
301
302     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
303     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
304
305     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
306
307     static void runModal(WKPageRef, const void* clientInfo);
308     static void runModal(PlatformWebView*);
309
310     static const char* libraryPathForTesting();
311     static const char* platformLibraryPathForTesting();
312
313     std::unique_ptr<TestInvocation> m_currentInvocation;
314
315     bool m_verbose { false };
316     bool m_printSeparators { false };
317     bool m_usingServerMode { false };
318     bool m_gcBetweenTests { false };
319     bool m_shouldDumpPixelsForAllTests { false };
320     std::vector<std::string> m_paths;
321     std::vector<std::string> m_allowedHosts;
322     WKRetainPtr<WKStringRef> m_injectedBundlePath;
323     WKRetainPtr<WKStringRef> m_testPluginDirectory;
324
325     WebNotificationProvider m_webNotificationProvider;
326
327     std::unique_ptr<PlatformWebView> m_mainWebView;
328     WKRetainPtr<WKContextRef> m_context;
329     WKRetainPtr<WKPageGroupRef> m_pageGroup;
330
331     enum State {
332         Initial,
333         Resetting,
334         RunningTest
335     };
336     State m_state { Initial };
337     bool m_doneResetting { false };
338
339     bool m_useWaitToDumpWatchdogTimer { true };
340     bool m_forceNoTimeout { false };
341
342     bool m_didPrintWebProcessCrashedMessage { false };
343     bool m_shouldExitWhenWebProcessCrashes { true };
344     
345     bool m_beforeUnloadReturnValue { true };
346
347     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
348     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
349     bool m_isGeolocationPermissionSet { false };
350     bool m_isGeolocationPermissionAllowed { false };
351
352     HashMap<String, RefPtr<OriginSettings>> m_cachedUserMediaPermissions;
353
354     typedef Vector<std::pair<String, WKRetainPtr<WKUserMediaPermissionRequestRef>>> PermissionRequestList;
355     PermissionRequestList m_userMediaPermissionRequests;
356
357     bool m_isUserMediaPermissionSet { false };
358     bool m_isUserMediaPermissionAllowed { false };
359
360     bool m_policyDelegateEnabled { false };
361     bool m_policyDelegatePermissive { false };
362     bool m_shouldDownloadUndisplayableMIMETypes { false };
363
364     bool m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges { false };
365     bool m_handlesAuthenticationChallenges { false };
366     String m_authenticationUsername;
367     String m_authenticationPassword;
368
369     bool m_shouldBlockAllPlugins { false };
370
371     bool m_forceComplexText { false };
372     bool m_shouldUseAcceleratedDrawing { false };
373     bool m_shouldUseRemoteLayerTree { false };
374
375     bool m_shouldLogCanAuthenticateAgainstProtectionSpace { false };
376     bool m_shouldLogHistoryClientCallbacks { false };
377     bool m_shouldShowWebView { false };
378     
379     bool m_shouldDecideNavigationPolicyAfterDelay { false };
380
381     WKRetainPtr<WKArrayRef> m_openPanelFileURLs;
382
383     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
384
385     WorkQueueManager m_workQueueManager;
386 };
387
388 struct TestCommand {
389     std::string pathOrURL;
390     std::string absolutePath;
391     bool shouldDumpPixels { false };
392     std::string expectedPixelHash;
393     int timeout { 0 };
394     bool dumpJSConsoleLogInStdErr { false };
395 };
396
397 } // namespace WTR
398
399 #endif // TestController_h