d1c02b9e143b7cba05df04ac1fd697857515fa08
[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 #if WK_API_ENABLED
31
32 #import "APIHistoryClient.h"
33 #import "APINavigationClient.h"
34 #import "PageLoadState.h"
35 #import "ProcessTerminationReason.h"
36 #import "ProcessThrottler.h"
37 #import <wtf/RetainPtr.h>
38 #import <wtf/RunLoop.h>
39 #import <wtf/WeakObjCPtr.h>
40
41 @class WKWebView;
42 @protocol WKHistoryDelegatePrivate;
43 @protocol WKNavigationDelegate;
44
45 namespace API {
46 class Navigation;
47 }
48
49 namespace WebCore {
50 struct SecurityOriginData;
51 }
52
53 namespace WebKit {
54
55 struct WebNavigationDataStore;
56
57 class NavigationState final : private PageLoadState::Observer {
58 public:
59     explicit NavigationState(WKWebView *);
60     ~NavigationState();
61
62     static NavigationState& fromWebPage(WebPageProxy&);
63
64     std::unique_ptr<API::NavigationClient> createNavigationClient();
65     std::unique_ptr<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)
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 didCancelClientRedirect(WebPageProxy&) override;
101         void didFailProvisionalNavigationWithError(WebPageProxy&, WebFrameProxy&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
102         void didFailProvisionalLoadInSubframeWithError(WebPageProxy&, WebFrameProxy&, const WebCore::SecurityOriginData&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
103         void didCommitNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
104         void didFinishDocumentLoad(WebPageProxy&, API::Navigation*, API::Object*) override;
105         void didFinishNavigation(WebPageProxy&, API::Navigation*, API::Object*) override;
106         void didFailNavigationWithError(WebPageProxy&, WebFrameProxy&, API::Navigation*, const WebCore::ResourceError&, API::Object*) override;
107         void didSameDocumentNavigation(WebPageProxy&, API::Navigation*, SameDocumentNavigationType, API::Object*) override;
108
109         void renderingProgressDidChange(WebPageProxy&, WebCore::LayoutMilestones) override;
110
111         void didReceiveAuthenticationChallenge(WebPageProxy&, AuthenticationChallengeProxy&) override;
112         bool processDidTerminate(WebPageProxy&, ProcessTerminationReason) override;
113         void processDidBecomeResponsive(WebPageProxy&) override;
114         void processDidBecomeUnresponsive(WebPageProxy&) override;
115
116         RefPtr<API::Data> webCryptoMasterKey(WebPageProxy&) override;
117
118         RefPtr<API::String> signedPublicKeyAndChallengeString(WebPageProxy&, unsigned keySizeIndex, const RefPtr<API::String>& challengeString, const WebCore::URL&) override;
119
120 #if USE(QUICK_LOOK)
121         void didStartLoadForQuickLookDocumentInMainFrame(const WTF::String& fileName, const WTF::String& uti) override;
122         void didFinishLoadForQuickLookDocumentInMainFrame(const QuickLookDocumentData&) override;
123 #endif
124
125 #if PLATFORM(MAC)
126         void webGLLoadPolicy(WebPageProxy&, const WebCore::URL&, CompletionHandler<void(WebCore::WebGLLoadPolicy)>&&) const final;
127         void resolveWebGLLoadPolicy(WebPageProxy&, const WebCore::URL&, CompletionHandler<void(WebCore::WebGLLoadPolicy)>&&) const final;
128         bool willGoToBackForwardListItem(WebPageProxy&, WebBackForwardListItem&, bool inPageCache, API::Object*) final;
129         bool didFailToInitializePlugIn(WebPageProxy&, API::Dictionary&) final;
130         bool didBlockInsecurePluginVersion(WebPageProxy&, API::Dictionary&) final;
131         void decidePolicyForPluginLoad(WebKit::WebPageProxy&, WebKit::PluginModuleLoadPolicy, API::Dictionary&, CompletionHandler<void(WebKit::PluginModuleLoadPolicy, const WTF::String&)>&&) final;
132         bool didChangeBackForwardList(WebPageProxy&, WebBackForwardListItem*, const Vector<Ref<WebBackForwardListItem>>&) final;
133 #endif
134
135         void contentRuleListNotification(WebPageProxy&, WebCore::URL&&, Vector<String>&&, Vector<String>&&) final;
136         void decidePolicyForNavigationAction(WebPageProxy&, Ref<API::NavigationAction>&&, Ref<WebFramePolicyListenerProxy>&&, API::Object* userData) override;
137         void decidePolicyForNavigationResponse(WebPageProxy&, Ref<API::NavigationResponse>&&, Ref<WebFramePolicyListenerProxy>&&, API::Object* userData) override;
138
139         NavigationState& m_navigationState;
140     };
141     
142     class HistoryClient final : public API::HistoryClient {
143     public:
144         explicit HistoryClient(NavigationState&);
145         ~HistoryClient();
146         
147     private:
148         void didNavigateWithNavigationData(WebPageProxy&, const WebNavigationDataStore&) override;
149         void didPerformClientRedirect(WebPageProxy&, const WTF::String&, const WTF::String&) override;
150         void didPerformServerRedirect(WebPageProxy&, const WTF::String&, const WTF::String&) override;
151         void didUpdateHistoryTitle(WebPageProxy&, const WTF::String&, const WTF::String&) override;
152         
153         NavigationState& m_navigationState;
154     };
155
156     // PageLoadState::Observer
157     void willChangeIsLoading() override;
158     void didChangeIsLoading() override;
159     void willChangeTitle() override;
160     void didChangeTitle() override;
161     void willChangeActiveURL() override;
162     void didChangeActiveURL() override;
163     void willChangeHasOnlySecureContent() override;
164     void didChangeHasOnlySecureContent() override;
165     void willChangeEstimatedProgress() override;
166     void didChangeEstimatedProgress() override;
167     void willChangeCanGoBack() override;
168     void didChangeCanGoBack() override;
169     void willChangeCanGoForward() override;
170     void didChangeCanGoForward() override;
171     void willChangeNetworkRequestsInProgress() override;
172     void didChangeNetworkRequestsInProgress() override;
173     void willChangeCertificateInfo() override;
174     void didChangeCertificateInfo() override;
175     void willChangeWebProcessIsResponsive() override;
176     void didChangeWebProcessIsResponsive() override;
177
178 #if PLATFORM(IOS)
179     void releaseNetworkActivityTokenAfterLoadCompletion() { releaseNetworkActivityToken(NetworkActivityTokenReleaseReason::LoadCompleted); }
180 #endif
181
182     WKWebView *m_webView;
183     WeakObjCPtr<id <WKNavigationDelegate> > m_navigationDelegate;
184
185     struct {
186         bool webViewDecidePolicyForNavigationActionDecisionHandler : 1;
187         bool webViewDecidePolicyForNavigationActionDecisionHandlerWebsitePolicies : 1;
188         bool webViewDecidePolicyForNavigationActionUserInfoDecisionHandlerWebsitePolicies : 1;
189         bool webViewDecidePolicyForNavigationResponseDecisionHandler : 1;
190
191         bool webViewDidStartProvisionalNavigation : 1;
192         bool webViewDidStartProvisionalNavigationUserInfo : 1;
193         bool webViewDidReceiveServerRedirectForProvisionalNavigation : 1;
194         bool webViewDidFailProvisionalNavigationWithError : 1;
195         bool webViewNavigationDidFailProvisionalLoadInSubframeWithError : 1;
196         bool webViewWillPerformClientRedirect : 1;
197         bool webViewDidCancelClientRedirect : 1;
198         bool webViewDidCommitNavigation : 1;
199         bool webViewNavigationDidFinishDocumentLoad : 1;
200         bool webViewDidFinishNavigation : 1;
201         bool webViewDidFailNavigationWithError : 1;
202         bool webViewDidFailNavigationWithErrorUserInfo : 1;
203         bool webViewNavigationDidSameDocumentNavigation : 1;
204
205         bool webViewRenderingProgressDidChange : 1;
206         bool webViewDidReceiveAuthenticationChallengeCompletionHandler : 1;
207         bool webViewWebContentProcessDidTerminate : 1;
208         bool webViewWebContentProcessDidTerminateWithReason : 1;
209         bool webViewWebProcessDidCrash : 1;
210         bool webViewWebProcessDidBecomeResponsive : 1;
211         bool webViewWebProcessDidBecomeUnresponsive : 1;
212         bool webCryptoMasterKeyForWebView : 1;
213         bool webViewDidBeginNavigationGesture : 1;
214         bool webViewWillEndNavigationGestureWithNavigationToBackForwardListItem : 1;
215         bool webViewDidEndNavigationGestureWithNavigationToBackForwardListItem : 1;
216         bool webViewWillSnapshotBackForwardListItem : 1;
217         bool webViewNavigationGestureSnapshotWasRemoved : 1;
218         bool webViewURLContentRuleListIdentifiersNotifications : 1;
219 #if USE(QUICK_LOOK)
220         bool webViewDidStartLoadForQuickLookDocumentInMainFrame : 1;
221         bool webViewDidFinishLoadForQuickLookDocumentInMainFrame : 1;
222         bool webViewDidRequestPasswordForQuickLookDocument : 1;
223 #endif
224
225 #if PLATFORM(MAC)
226         bool webViewWebGLLoadPolicyForURL : 1;
227         bool webViewResolveWebGLLoadPolicyForURL : 1;
228         bool webViewBackForwardListItemAddedRemoved : 1;
229         bool webViewDidFailToInitializePlugInWithInfo : 1;
230         bool webViewDidBlockInsecurePluginVersionWithInfo : 1;
231         bool webViewWillGoToBackForwardListItemInPageCache : 1;
232         bool webViewDecidePolicyForPluginLoadWithCurrentPolicyPluginInfoCompletionHandler : 1;
233 #endif
234     } m_navigationDelegateMethods;
235
236     WeakObjCPtr<id <WKHistoryDelegatePrivate> > m_historyDelegate;
237     struct {
238         bool webViewDidNavigateWithNavigationData : 1;
239         bool webViewDidPerformClientRedirectFromURLToURL : 1;
240         bool webViewDidPerformServerRedirectFromURLToURL : 1;
241         bool webViewDidUpdateHistoryTitleForURL : 1;
242     } m_historyDelegateMethods;
243
244 #if PLATFORM(IOS)
245     ProcessThrottler::BackgroundActivityToken m_activityToken;
246     RunLoop::Timer<NavigationState> m_releaseActivityTimer;
247 #endif
248 };
249
250 } // namespace WebKit
251
252 #endif