| 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 | |
| 32 | namespace WebKit { |
| 33 | |
| 34 | class WebCertificateInfo; |
| 35 | class WebPageProxy; |
| 36 | |
| 37 | class PageLoadState { |
| 38 | public: |
| 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 | const String& provisionalURL() const { return m_committedState.provisionalURL; } |
| 129 | const String& url() const { return m_committedState.url; } |
| 130 | const String& unreachableURL() const { return m_committedState.unreachableURL; } |
| 131 | |
| 132 | String activeURL() const; |
| 133 | |
| 134 | bool hasOnlySecureContent() const; |
| 135 | |
| 136 | double estimatedProgress() const; |
| 137 | bool networkRequestsInProgress() const { return m_committedState.networkRequestsInProgress; } |
| 138 | |
| 139 | WebCertificateInfo* certificateInfo() const { return m_committedState.certificateInfo.get(); } |
| 140 | |
| 141 | const String& pendingAPIRequestURL() const; |
| 142 | void setPendingAPIRequestURL(const Transaction::Token&, const String&); |
| 143 | void clearPendingAPIRequestURL(const Transaction::Token&); |
| 144 | |
| 145 | void didStartProvisionalLoad(const Transaction::Token&, const String& url, const String& unreachableURL); |
| 146 | void didExplicitOpen(const Transaction::Token&, const String& url); |
| 147 | void didReceiveServerRedirectForProvisionalLoad(const Transaction::Token&, const String& url); |
| 148 | void didFailProvisionalLoad(const Transaction::Token&); |
| 149 | |
| 150 | void didCommitLoad(const Transaction::Token&, WebCertificateInfo&, bool hasInsecureContent); |
| 151 | void didFinishLoad(const Transaction::Token&); |
| 152 | void didFailLoad(const Transaction::Token&); |
| 153 | |
| 154 | void didSameDocumentNavigation(const Transaction::Token&, const String& url); |
| 155 | |
| 156 | void didDisplayOrRunInsecureContent(const Transaction::Token&); |
| 157 | |
| 158 | void setUnreachableURL(const Transaction::Token&, const String&); |
| 159 | |
| 160 | const String& title() const; |
| 161 | void setTitle(const Transaction::Token&, const String&); |
| 162 | |
| 163 | bool canGoBack() const; |
| 164 | void setCanGoBack(const Transaction::Token&, bool); |
| 165 | |
| 166 | bool canGoForward() const; |
| 167 | void setCanGoForward(const Transaction::Token&, bool); |
| 168 | |
| 169 | void didStartProgress(const Transaction::Token&); |
| 170 | void didChangeProgress(const Transaction::Token&, double); |
| 171 | void didFinishProgress(const Transaction::Token&); |
| 172 | void setNetworkRequestsInProgress(const Transaction::Token&, bool); |
| 173 | |
| 174 | void didSwapWebProcesses(); |
| 175 | |
| 176 | bool committedHasInsecureContent() const { return m_committedState.hasInsecureContent; } |
| 177 | |
| 178 | // FIXME: We piggy-back off PageLoadState::Observer so that both WKWebView and WKObservablePageState |
| 179 | // can listen for changes. Once we get rid of WKObservablePageState these could just be part of API::NavigationClient. |
| 180 | void willChangeProcessIsResponsive(); |
| 181 | void didChangeProcessIsResponsive(); |
| 182 | |
| 183 | private: |
| 184 | void beginTransaction() { ++m_outstandingTransactionCount; } |
| 185 | void endTransaction(); |
| 186 | |
| 187 | void callObserverCallback(void (Observer::*)()); |
| 188 | |
| 189 | Vector<Observer*> m_observers; |
| 190 | |
| 191 | struct Data { |
| 192 | Data() |
| 193 | : state(State::Finished) |
| 194 | , hasInsecureContent(false) |
| 195 | , canGoBack(false) |
| 196 | , canGoForward(false) |
| 197 | , estimatedProgress(0) |
| 198 | , networkRequestsInProgress(false) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | State state; |
| 203 | bool hasInsecureContent; |
| 204 | |
| 205 | String pendingAPIRequestURL; |
| 206 | |
| 207 | String provisionalURL; |
| 208 | String url; |
| 209 | |
| 210 | String unreachableURL; |
| 211 | |
| 212 | String title; |
| 213 | |
| 214 | bool canGoBack; |
| 215 | bool canGoForward; |
| 216 | |
| 217 | double estimatedProgress; |
| 218 | bool networkRequestsInProgress; |
| 219 | |
| 220 | RefPtr<WebCertificateInfo> certificateInfo; |
| 221 | }; |
| 222 | |
| 223 | static bool isLoading(const Data&); |
| 224 | static String activeURL(const Data&); |
| 225 | static bool hasOnlySecureContent(const Data&); |
| 226 | static double estimatedProgress(const Data&); |
| 227 | |
| 228 | WebPageProxy& m_webPageProxy; |
| 229 | |
| 230 | Data m_committedState; |
| 231 | Data m_uncommittedState; |
| 232 | |
| 233 | String m_lastUnreachableURL; |
| 234 | |
| 235 | bool m_mayHaveUncommittedChanges; |
| 236 | unsigned m_outstandingTransactionCount; |
| 237 | }; |
| 238 | |
| 239 | } // namespace WebKit |
| 240 | |
| 241 | #endif // PageLoadState_h |
| 242 | |