1/*
2 * Copyright (C) 2013-2015 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 PageLoadState_h
27#define PageLoadState_h
28
29#include "WebCertificateInfo.h"
30#include <wtf/text/WTFString.h>
31
32namespace WebKit {
33
34class WebCertificateInfo;
35class WebPageProxy;
36
37class PageLoadState {
38public:
39 explicit PageLoadState(WebPageProxy&);
40 ~PageLoadState();
41
42 enum class State {
43 Provisional,
44 Committed,
45 Finished
46 };
47
48 class Observer {
49 public:
50 virtual ~Observer() { }
51
52 virtual void willChangeIsLoading() = 0;
53 virtual void didChangeIsLoading() = 0;
54
55 virtual void willChangeTitle() = 0;
56 virtual void didChangeTitle() = 0;
57
58 virtual void willChangeActiveURL() = 0;
59 virtual void didChangeActiveURL() = 0;
60
61 virtual void willChangeHasOnlySecureContent() = 0;
62 virtual void didChangeHasOnlySecureContent() = 0;
63
64 virtual void willChangeEstimatedProgress() = 0;
65 virtual void didChangeEstimatedProgress() = 0;
66
67 virtual void willChangeCanGoBack() = 0;
68 virtual void didChangeCanGoBack() = 0;
69
70 virtual void willChangeCanGoForward() = 0;
71 virtual void didChangeCanGoForward() = 0;
72
73 virtual void willChangeNetworkRequestsInProgress() = 0;
74 virtual void didChangeNetworkRequestsInProgress() = 0;
75
76 virtual void willChangeCertificateInfo() = 0;
77 virtual void didChangeCertificateInfo() = 0;
78
79 virtual void willChangeWebProcessIsResponsive() = 0;
80 virtual void didChangeWebProcessIsResponsive() = 0;
81
82 virtual void didSwapWebProcesses() = 0;
83 };
84
85 class Transaction {
86 WTF_MAKE_NONCOPYABLE(Transaction);
87 public:
88 Transaction(Transaction&&);
89 ~Transaction();
90
91 private:
92 friend class PageLoadState;
93
94 explicit Transaction(PageLoadState&);
95
96 class Token {
97 public:
98 Token(Transaction& transaction)
99#if !ASSERT_DISABLED
100 : m_pageLoadState(*transaction.m_pageLoadState)
101#endif
102 {
103 transaction.m_pageLoadState->m_mayHaveUncommittedChanges = true;
104 }
105
106#if !ASSERT_DISABLED
107 PageLoadState& m_pageLoadState;
108#endif
109 };
110
111 RefPtr<WebPageProxy> m_webPageProxy;
112 PageLoadState* m_pageLoadState;
113 };
114
115 void addObserver(Observer&);
116 void removeObserver(Observer&);
117
118 Transaction transaction() { return Transaction(*this); }
119 void commitChanges();
120
121 void reset(const Transaction::Token&);
122
123 bool isLoading() const;
124 bool isProvisional() const { return m_committedState.state == State::Provisional; }
125 bool isCommitted() const { return m_committedState.state == State::Committed; }
126 bool isFinished() const { return m_committedState.state == State::Finished; }
127
128 bool hasUncommittedLoad() const;
129
130 const String& provisionalURL() const { return m_committedState.provisionalURL; }
131 const String& url() const { return m_committedState.url; }
132 const String& unreachableURL() const { return m_committedState.unreachableURL; }
133
134 String activeURL() const;
135
136 bool hasOnlySecureContent() const;
137
138 double estimatedProgress() const;
139 bool networkRequestsInProgress() const { return m_committedState.networkRequestsInProgress; }
140
141 WebCertificateInfo* certificateInfo() const { return m_committedState.certificateInfo.get(); }
142
143 const String& pendingAPIRequestURL() const;
144 void setPendingAPIRequestURL(const Transaction::Token&, const String&);
145 void clearPendingAPIRequestURL(const Transaction::Token&);
146
147 void didStartProvisionalLoad(const Transaction::Token&, const String& url, const String& unreachableURL);
148 void didExplicitOpen(const Transaction::Token&, const String& url);
149 void didReceiveServerRedirectForProvisionalLoad(const Transaction::Token&, const String& url);
150 void didFailProvisionalLoad(const Transaction::Token&);
151
152 void didCommitLoad(const Transaction::Token&, WebCertificateInfo&, bool hasInsecureContent);
153 void didFinishLoad(const Transaction::Token&);
154 void didFailLoad(const Transaction::Token&);
155
156 void didSameDocumentNavigation(const Transaction::Token&, const String& url);
157
158 void didDisplayOrRunInsecureContent(const Transaction::Token&);
159
160 void setUnreachableURL(const Transaction::Token&, const String&);
161
162 const String& title() const;
163 void setTitle(const Transaction::Token&, const String&);
164
165 bool canGoBack() const;
166 void setCanGoBack(const Transaction::Token&, bool);
167
168 bool canGoForward() const;
169 void setCanGoForward(const Transaction::Token&, bool);
170
171 void didStartProgress(const Transaction::Token&);
172 void didChangeProgress(const Transaction::Token&, double);
173 void didFinishProgress(const Transaction::Token&);
174 void setNetworkRequestsInProgress(const Transaction::Token&, bool);
175
176 void didSwapWebProcesses();
177
178 bool committedHasInsecureContent() const { return m_committedState.hasInsecureContent; }
179
180 // FIXME: We piggy-back off PageLoadState::Observer so that both WKWebView and WKObservablePageState
181 // can listen for changes. Once we get rid of WKObservablePageState these could just be part of API::NavigationClient.
182 void willChangeProcessIsResponsive();
183 void didChangeProcessIsResponsive();
184
185private:
186 void beginTransaction() { ++m_outstandingTransactionCount; }
187 void endTransaction();
188
189 void callObserverCallback(void (Observer::*)());
190
191 Vector<Observer*> m_observers;
192
193 struct Data {
194 Data()
195 : state(State::Finished)
196 , hasInsecureContent(false)
197 , canGoBack(false)
198 , canGoForward(false)
199 , estimatedProgress(0)
200 , networkRequestsInProgress(false)
201 {
202 }
203
204 State state;
205 bool hasInsecureContent;
206
207 String pendingAPIRequestURL;
208
209 String provisionalURL;
210 String url;
211
212 String unreachableURL;
213
214 String title;
215
216 bool canGoBack;
217 bool canGoForward;
218
219 double estimatedProgress;
220 bool networkRequestsInProgress;
221
222 RefPtr<WebCertificateInfo> certificateInfo;
223 };
224
225 static bool isLoading(const Data&);
226 static String activeURL(const Data&);
227 static bool hasOnlySecureContent(const Data&);
228 static double estimatedProgress(const Data&);
229
230 WebPageProxy& m_webPageProxy;
231
232 Data m_committedState;
233 Data m_uncommittedState;
234
235 String m_lastUnreachableURL;
236
237 bool m_mayHaveUncommittedChanges;
238 unsigned m_outstandingTransactionCount;
239};
240
241} // namespace WebKit
242
243#endif // PageLoadState_h
244