[Cocoa] Add a way for Apple-internal clients to opt into recommended compatibility...
[WebKit-https.git] / Source / WebKit / UIProcess / Cocoa / NavigationState.h
1 /*
2  * Copyright (C) 2014-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 #pragma once
27
28 #import "WKFoundation.h"
29
30 #import "APIHistoryClient.h"
31 #import "APINavigationClient.h"
32 #import "PageLoadState.h"
33 #import "ProcessTerminationReason.h"
34 #import "ProcessThrottler.h"
35 #import <wtf/RetainPtr.h>
36 #import <wtf/RunLoop.h>
37 #import <wtf/UniqueRef.h>
38 #import <wtf/WeakObjCPtr.h>
39
40 @class WKWebView;
41 @protocol WKHistoryDelegatePrivate;
42 @protocol WKNavigationDelegate;
43
44 namespace API {
45 class Navigation;
46 }
47
48 namespace WebCore {
49 struct SecurityOriginData;
50 }
51
52 namespace WebKit {
53
54 struct WebNavigationDataStore;
55
56 class NavigationState final : private PageLoadState::Observer {
57     WTF_MAKE_FAST_ALLOCATED;
58 public:
59     explicit NavigationState(WKWebView *);
60     ~NavigationState();
61
62     static NavigationState& fromWebPage(WebPageProxy&);
63
64     UniqueRef<API::NavigationClient> createNavigationClient();
65     UniqueRef<API::HistoryClient> createHistoryClient();
66
67     RetainPtr<id <WKNavigationDelegate> > navigationDelegate();
68     void setNavigationDelegate(id <WKNavigationDelegate>);
69
70     RetainPtr<id <WKHistoryDelegatePrivate> > historyDelegate();
71     void setHistoryDelegate(id <WKHistoryDelegatePrivate>);
72
73     // Called by the page client.
74     void navigationGestureDidBegin();
75     void navigationGestureWillEnd(bool willNavigate, WebBackForwardListItem&);
76     void navigationGestureDidEnd(bool willNavigate, WebBackForwardListItem&);
77     void willRecordNavigationSnapshot(WebBackForwardListItem&);
78     void navigationGestureSnapshotWasRemoved();
79 #if USE(QUICK_LOOK)
80     void didRequestPasswordForQuickLookDocument();
81 #endif
82
83     void didFirstPaint();
84
85 #if PLATFORM(IOS_FAMILY)
86     enum class NetworkActivityTokenReleaseReason { LoadCompleted, ScreenLocked };
87     void releaseNetworkActivityToken(NetworkActivityTokenReleaseReason);
88 #endif
89
90 private:
91     class NavigationClient final : public API::NavigationClient {
92     public:
93         explicit NavigationClient(NavigationState&);
94         ~NavigationClient();
95
96     private:
97         void didStartProvisionalNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
98         void didReceiveServerRedirectForProvisionalNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
99         void willPerformClientRedirect(WebPageProxy&, const WTF::String&, double) override;
100         void didPerformClientRedirect(WebPageProxy&, const WTF::String&, const WTF::String&) override;
101         void didCancelClientRedirect(WebPageProxy&) override;
102         void didFailProvisionalNavigationWithError(WebPageProxy&, WebFrameProxy&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
103         void didFailProvisionalLoadInSubframeWithError(WebPageProxy&, WebFrameProxy&, const WebCore::SecurityOriginData&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
104         void didCommitNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
105         void didFinishDocumentLoad(WebPageProxy&, API::Navigation*, API::Object*) override;
106         void didFinishNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
107         void didFailNavigationWithError(WebPageProxy&, WebFrameProxy&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
108         void didSameDocumentNavigation(WebPageProxy&, API::Navigation*, SameDocumentNavigationType, API::Object*) override;
109
110         void renderingProgressDidChange(WebPageProxy&, OptionSet<WebCore::LayoutMilestone>) override;
111
112         bool shouldBypassCompatibilityModeSafeguards() const final;
113
114         void didReceiveAuthenticationChallenge(WebPageProxy&, AuthenticationChallengeProxy&) override;
115         bool processDidTerminate(WebPageProxy&, ProcessTerminationReason) override;
116         void processDidBecomeResponsive(WebPageProxy&) override;
117         void processDidBecomeUnresponsive(WebPageProxy&) override;
118
119         RefPtr<API::Data> webCryptoMasterKey(WebPageProxy&) override;
120
121         RefPtr<API::String> signedPublicKeyAndChallengeString(WebPageProxy&, unsigned keySizeIndex, const RefPtr<API::String>& challengeString, const URL&) override;
122
123 #if USE(QUICK_LOOK)
124         void didStartLoadForQuickLookDocumentInMainFrame(const WTF::String& fileName, const WTF::String& uti) override;
125         void didFinishLoadForQuickLookDocumentInMainFrame(const QuickLookDocumentData&) override;
126 #endif
127
128 #if PLATFORM(MAC)
129         void webGLLoadPolicy(WebPageProxy&, const URL&, CompletionHandler<void(WebCore::WebGLLoadPolicy)>&&) const final;
130         void resolveWebGLLoadPolicy(WebPageProxy&, const URL&, CompletionHandler<void(WebCore::WebGLLoadPolicy)>&&) const final;
131         bool willGoToBackForwardListItem(WebPageProxy&, WebBackForwardListItem&, bool inPageCache) final;
132         bool didFailToInitializePlugIn(WebPageProxy&, API::Dictionary&) final;
133         bool didBlockInsecurePluginVersion(WebPageProxy&, API::Dictionary&) final;
134         void decidePolicyForPluginLoad(WebKit::WebPageProxy&, WebKit::PluginModuleLoadPolicy, API::Dictionary&, CompletionHandler<void(WebKit::PluginModuleLoadPolicy, const WTF::String&)>&&) final;
135         bool didChangeBackForwardList(WebPageProxy&, WebBackForwardListItem*, const Vector<Ref<WebBackForwardListItem>>&) final;
136 #endif
137
138         void contentRuleListNotification(WebPageProxy&, URL&&, WebCore::ContentRuleListResults&&) final;
139         void decidePolicyForNavigationAction(WebPageProxy&, Ref<API::NavigationAction>&&, Ref<WebFramePolicyListenerProxy>&&, API::Object* userData) override;
140         void decidePolicyForNavigationResponse(WebPageProxy&, Ref<API::NavigationResponse>&&, Ref<WebFramePolicyListenerProxy>&&, API::Object* userData) override;
141
142         NavigationState& m_navigationState;
143     };
144     
145     class HistoryClient final : public API::HistoryClient {
146     public:
147         explicit HistoryClient(NavigationState&);
148         ~HistoryClient();
149         
150     private:
151         void didNavigateWithNavigationData(WebPageProxy&, const WebNavigationDataStore&) override;
152         void didPerformClientRedirect(WebPageProxy&, const WTF::String&, const WTF::String&) override;
153         void didPerformServerRedirect(WebPageProxy&, const WTF::String&, const WTF::String&) override;
154         void didUpdateHistoryTitle(WebPageProxy&, const WTF::String&, const WTF::String&) override;
155         
156         NavigationState& m_navigationState;
157     };
158
159     // PageLoadState::Observer
160     void willChangeIsLoading() override;
161     void didChangeIsLoading() override;
162     void willChangeTitle() override;
163     void didChangeTitle() override;
164     void willChangeActiveURL() override;
165     void didChangeActiveURL() override;
166     void willChangeHasOnlySecureContent() override;
167     void didChangeHasOnlySecureContent() override;
168     void willChangeEstimatedProgress() override;
169     void didChangeEstimatedProgress() override;
170     void willChangeCanGoBack() override;
171     void didChangeCanGoBack() override;
172     void willChangeCanGoForward() override;
173     void didChangeCanGoForward() override;
174     void willChangeNetworkRequestsInProgress() override;
175     void didChangeNetworkRequestsInProgress() override;
176     void willChangeCertificateInfo() override;
177     void didChangeCertificateInfo() override;
178     void willChangeWebProcessIsResponsive() override;
179     void didChangeWebProcessIsResponsive() override;
180     void didSwapWebProcesses() override;
181
182 #if PLATFORM(IOS_FAMILY)
183     void releaseNetworkActivityTokenAfterLoadCompletion() { releaseNetworkActivityToken(NetworkActivityTokenReleaseReason::LoadCompleted); }
184 #endif
185
186     WKWebView *m_webView;
187     WeakObjCPtr<id <WKNavigationDelegate> > m_navigationDelegate;
188
189     struct {
190         bool webViewDecidePolicyForNavigationActionDecisionHandler : 1;
191         bool webViewDecidePolicyForNavigationActionWithPreferencesDecisionHandler : 1;
192         bool webViewDecidePolicyForNavigationActionDecisionHandlerWebsitePolicies : 1;
193         bool webViewDecidePolicyForNavigationActionUserInfoDecisionHandlerWebsitePolicies : 1;
194         bool webViewDecidePolicyForNavigationResponseDecisionHandler : 1;
195
196         bool webViewDidStartProvisionalNavigation : 1;
197         bool webViewDidStartProvisionalNavigationUserInfo : 1;
198         bool webViewDidReceiveServerRedirectForProvisionalNavigation : 1;
199         bool webViewDidFailProvisionalNavigationWithError : 1;
200         bool webViewNavigationDidFailProvisionalLoadInSubframeWithError : 1;
201         bool webViewWillPerformClientRedirect : 1;
202         bool webViewDidPerformClientRedirect : 1;
203         bool webViewDidCancelClientRedirect : 1;
204         bool webViewDidCommitNavigation : 1;
205         bool webViewNavigationDidFinishDocumentLoad : 1;
206         bool webViewDidFinishNavigation : 1;
207         bool webViewDidFailNavigationWithError : 1;
208         bool webViewDidFailNavigationWithErrorUserInfo : 1;
209         bool webViewNavigationDidSameDocumentNavigation : 1;
210
211         bool webViewRenderingProgressDidChange : 1;
212         bool webViewDidReceiveAuthenticationChallengeCompletionHandler : 1;
213         bool webViewWebContentProcessDidTerminate : 1;
214         bool webViewWebContentProcessDidTerminateWithReason : 1;
215         bool webViewWebProcessDidCrash : 1;
216         bool webViewWebProcessDidBecomeResponsive : 1;
217         bool webViewWebProcessDidBecomeUnresponsive : 1;
218         bool webCryptoMasterKeyForWebView : 1;
219         bool webViewDidBeginNavigationGesture : 1;
220         bool webViewWillEndNavigationGestureWithNavigationToBackForwardListItem : 1;
221         bool webViewDidEndNavigationGestureWithNavigationToBackForwardListItem : 1;
222         bool webViewWillSnapshotBackForwardListItem : 1;
223         bool webViewNavigationGestureSnapshotWasRemoved : 1;
224         bool webViewURLContentRuleListIdentifiersNotifications : 1;
225         bool webViewContentRuleListWithIdentifierPerformedActionForURL : 1;
226 #if USE(QUICK_LOOK)
227         bool webViewDidStartLoadForQuickLookDocumentInMainFrame : 1;
228         bool webViewDidFinishLoadForQuickLookDocumentInMainFrame : 1;
229         bool webViewDidRequestPasswordForQuickLookDocument : 1;
230 #endif
231
232 #if PLATFORM(MAC)
233         bool webViewWebGLLoadPolicyForURL : 1;
234         bool webViewResolveWebGLLoadPolicyForURL : 1;
235         bool webViewBackForwardListItemAddedRemoved : 1;
236         bool webViewDidFailToInitializePlugInWithInfo : 1;
237         bool webViewDidBlockInsecurePluginVersionWithInfo : 1;
238         bool webViewWillGoToBackForwardListItemInPageCache : 1;
239         bool webViewDecidePolicyForPluginLoadWithCurrentPolicyPluginInfoCompletionHandler : 1;
240 #endif
241     } m_navigationDelegateMethods;
242
243     WeakObjCPtr<id <WKHistoryDelegatePrivate> > m_historyDelegate;
244     struct {
245         bool webViewDidNavigateWithNavigationData : 1;
246         bool webViewDidPerformClientRedirectFromURLToURL : 1;
247         bool webViewDidPerformServerRedirectFromURLToURL : 1;
248         bool webViewDidUpdateHistoryTitleForURL : 1;
249     } m_historyDelegateMethods;
250
251 #if PLATFORM(IOS_FAMILY)
252     ProcessThrottler::BackgroundActivityToken m_activityToken;
253     RunLoop::Timer<NavigationState> m_releaseActivityTimer;
254 #endif
255 };
256
257 } // namespace WebKit