Title: [247851] trunk
Revision
247851
Author
[email protected]
Date
2019-07-25 18:26:24 -0700 (Thu, 25 Jul 2019)

Log Message

WebPageProxy::receivedPolicyDecision should check navigation ID before clear pendingAPIRequest
https://bugs.webkit.org/show_bug.cgi?id=200108
<rdar://problem/53521238>

Reviewed by Chris Dumez.

Source/WebKit:

Assuming there are two loads happening one after another. There is an issue when clients save
the first decisionHandler and then call WKNavigationActionPolicyCancel for it right after the
second decisionHandler received, -[WKWebView URL] could return a null string even though it is
loading the second one.

To solve that, this patch pairs a navigationID with the pendingAPIRequestURL such that
WebPageProxy::receivedPolicyDecision could clear the pendingAPIRequestURL only if
the passed navigation ID matches the current one.

* UIProcess/PageLoadState.cpp:
(WebKit::PageLoadState::reset):
(WebKit::PageLoadState::activeURL):
(WebKit::PageLoadState::estimatedProgress):
(WebKit::PageLoadState::pendingAPIRequestURL const):
(WebKit::PageLoadState::pendingAPIRequest const):
(WebKit::PageLoadState::setPendingAPIRequest):
(WebKit::PageLoadState::clearPendingAPIRequest):
(WebKit::PageLoadState::isLoading):
(WebKit::PageLoadState::setPendingAPIRequestURL): Deleted.
(WebKit::PageLoadState::clearPendingAPIRequestURL): Deleted.
* UIProcess/PageLoadState.h:
(WebKit::PageLoadState::setPendingAPIRequest):
(WebKit::PageLoadState::setPendingAPIRequestURL): Deleted.
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::launchProcessForReload):
(WebKit::WebPageProxy::loadRequestWithNavigationShared):
(WebKit::WebPageProxy::loadFile):
(WebKit::WebPageProxy::loadDataWithNavigationShared):
(WebKit::WebPageProxy::loadAlternateHTML):
(WebKit::WebPageProxy::loadWebArchiveData):
(WebKit::WebPageProxy::reload):
(WebKit::WebPageProxy::goToBackForwardItem):
(WebKit::WebPageProxy::receivedPolicyDecision):
(WebKit::WebPageProxy::continueNavigationInNewProcess):
(WebKit::WebPageProxy::didStartProvisionalLoadForFrameShared):
(WebKit::WebPageProxy::didSameDocumentNavigationForFrame):
(WebKit::WebPageProxy::decidePolicyForNavigationAction):

Tools:

Added an API test.

* TestWebKitAPI/Tests/WebKitCocoa/DecidePolicyForNavigationAction.mm:
(-[DecidePolicyForNavigationActionController webView:decidePolicyForNavigationAction:decisionHandler:]):
(TEST):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (247850 => 247851)


--- trunk/Source/WebKit/ChangeLog	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Source/WebKit/ChangeLog	2019-07-26 01:26:24 UTC (rev 247851)
@@ -1,3 +1,49 @@
+2019-07-25  Jiewen Tan  <[email protected]>
+
+        WebPageProxy::receivedPolicyDecision should check navigation ID before clear pendingAPIRequest
+        https://bugs.webkit.org/show_bug.cgi?id=200108
+        <rdar://problem/53521238>
+
+        Reviewed by Chris Dumez.
+
+        Assuming there are two loads happening one after another. There is an issue when clients save
+        the first decisionHandler and then call WKNavigationActionPolicyCancel for it right after the
+        second decisionHandler received, -[WKWebView URL] could return a null string even though it is
+        loading the second one.
+
+        To solve that, this patch pairs a navigationID with the pendingAPIRequestURL such that
+        WebPageProxy::receivedPolicyDecision could clear the pendingAPIRequestURL only if
+        the passed navigation ID matches the current one.
+
+        * UIProcess/PageLoadState.cpp:
+        (WebKit::PageLoadState::reset):
+        (WebKit::PageLoadState::activeURL):
+        (WebKit::PageLoadState::estimatedProgress):
+        (WebKit::PageLoadState::pendingAPIRequestURL const):
+        (WebKit::PageLoadState::pendingAPIRequest const):
+        (WebKit::PageLoadState::setPendingAPIRequest):
+        (WebKit::PageLoadState::clearPendingAPIRequest):
+        (WebKit::PageLoadState::isLoading):
+        (WebKit::PageLoadState::setPendingAPIRequestURL): Deleted.
+        (WebKit::PageLoadState::clearPendingAPIRequestURL): Deleted.
+        * UIProcess/PageLoadState.h:
+        (WebKit::PageLoadState::setPendingAPIRequest):
+        (WebKit::PageLoadState::setPendingAPIRequestURL): Deleted.
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::launchProcessForReload):
+        (WebKit::WebPageProxy::loadRequestWithNavigationShared):
+        (WebKit::WebPageProxy::loadFile):
+        (WebKit::WebPageProxy::loadDataWithNavigationShared):
+        (WebKit::WebPageProxy::loadAlternateHTML):
+        (WebKit::WebPageProxy::loadWebArchiveData):
+        (WebKit::WebPageProxy::reload):
+        (WebKit::WebPageProxy::goToBackForwardItem):
+        (WebKit::WebPageProxy::receivedPolicyDecision):
+        (WebKit::WebPageProxy::continueNavigationInNewProcess):
+        (WebKit::WebPageProxy::didStartProvisionalLoadForFrameShared):
+        (WebKit::WebPageProxy::didSameDocumentNavigationForFrame):
+        (WebKit::WebPageProxy::decidePolicyForNavigationAction):
+
 2019-07-25  Commit Queue  <[email protected]>
 
         Unreviewed, rolling out r247806.

Modified: trunk/Source/WebKit/UIProcess/PageLoadState.cpp (247850 => 247851)


--- trunk/Source/WebKit/UIProcess/PageLoadState.cpp	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Source/WebKit/UIProcess/PageLoadState.cpp	2019-07-26 01:26:24 UTC (rev 247851)
@@ -154,7 +154,7 @@
     m_uncommittedState.state = State::Finished;
     m_uncommittedState.hasInsecureContent = false;
 
-    m_uncommittedState.pendingAPIRequestURL = String();
+    m_uncommittedState.pendingAPIRequest = { };
     m_uncommittedState.provisionalURL = String();
     m_uncommittedState.url = ""
 
@@ -182,8 +182,8 @@
     // If there is a currently pending URL, it is the active URL,
     // even when there's no main frame yet, as it might be the
     // first API request.
-    if (!data.pendingAPIRequestURL.isNull())
-        return data.pendingAPIRequestURL;
+    if (!data.pendingAPIRequest.url.isNull())
+        return data.pendingAPIRequest.url;
 
     if (!data.unreachableURL.isEmpty())
         return data.unreachableURL;
@@ -223,7 +223,7 @@
 
 double PageLoadState::estimatedProgress(const Data& data)
 {
-    if (!data.pendingAPIRequestURL.isNull())
+    if (!data.pendingAPIRequest.url.isNull())
         return initialProgressValue;
 
     return data.estimatedProgress;
@@ -236,25 +236,30 @@
 
 const String& PageLoadState::pendingAPIRequestURL() const
 {
-    return m_committedState.pendingAPIRequestURL;
+    return m_committedState.pendingAPIRequest.url;
 }
 
+auto PageLoadState::pendingAPIRequest() const -> const PendingAPIRequest&
+{
+    return m_committedState.pendingAPIRequest;
+}
+
 const URL& PageLoadState::resourceDirectoryURL() const
 {
     return m_committedState.resourceDirectoryURL;
 }
 
-void PageLoadState::setPendingAPIRequestURL(const Transaction::Token& token, const String& pendingAPIRequestURL, const URL& resourceDirectoryURL)
+void PageLoadState::setPendingAPIRequest(const Transaction::Token& token, PendingAPIRequest&& pendingAPIRequest, const URL& resourceDirectoryURL)
 {
     ASSERT_UNUSED(token, &token.m_pageLoadState == this);
-    m_uncommittedState.pendingAPIRequestURL = pendingAPIRequestURL;
+    m_uncommittedState.pendingAPIRequest = WTFMove(pendingAPIRequest);
     m_uncommittedState.resourceDirectoryURL = resourceDirectoryURL;
 }
 
-void PageLoadState::clearPendingAPIRequestURL(const Transaction::Token& token)
+void PageLoadState::clearPendingAPIRequest(const Transaction::Token& token)
 {
     ASSERT_UNUSED(token, &token.m_pageLoadState == this);
-    m_uncommittedState.pendingAPIRequestURL = String();
+    m_uncommittedState.pendingAPIRequest = { };
 }
 
 void PageLoadState::didExplicitOpen(const Transaction::Token& token, const String& url)
@@ -410,7 +415,7 @@
 
 bool PageLoadState::isLoading(const Data& data)
 {
-    if (!data.pendingAPIRequestURL.isNull())
+    if (!data.pendingAPIRequest.url.isNull())
         return true;
 
     switch (data.state) {

Modified: trunk/Source/WebKit/UIProcess/PageLoadState.h (247850 => 247851)


--- trunk/Source/WebKit/UIProcess/PageLoadState.h	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Source/WebKit/UIProcess/PageLoadState.h	2019-07-26 01:26:24 UTC (rev 247851)
@@ -113,6 +113,11 @@
         PageLoadState* m_pageLoadState;
     };
 
+    struct PendingAPIRequest {
+        uint64_t navigationID { 0 };
+        String url;
+    };
+
     void addObserver(Observer&);
     void removeObserver(Observer&);
 
@@ -144,8 +149,9 @@
     const URL& resourceDirectoryURL() const;
 
     const String& pendingAPIRequestURL() const;
-    void setPendingAPIRequestURL(const Transaction::Token&, const String& pendingAPIRequestURL, const URL& resourceDirectoryPath = { });
-    void clearPendingAPIRequestURL(const Transaction::Token&);
+    const PendingAPIRequest& pendingAPIRequest() const;
+    void setPendingAPIRequest(const Transaction::Token&, PendingAPIRequest&& pendingAPIRequest, const URL& resourceDirectoryPath = { });
+    void clearPendingAPIRequest(const Transaction::Token&);
 
     void didStartProvisionalLoad(const Transaction::Token&, const String& url, const String& unreachableURL);
     void didExplicitOpen(const Transaction::Token&, const String& url);
@@ -207,7 +213,7 @@
         State state;
         bool hasInsecureContent;
 
-        String pendingAPIRequestURL;
+        PendingAPIRequest pendingAPIRequest;
 
         String provisionalURL;
         String url;

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (247850 => 247851)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2019-07-26 01:26:24 UTC (rev 247851)
@@ -888,6 +888,12 @@
 
     auto navigation = m_navigationState->createReloadNavigation();
 
+    String url = ""
+    if (!url.isEmpty()) {
+        auto transaction = m_pageLoadState.transaction();
+        m_pageLoadState.setPendingAPIRequest(transaction, { navigation->navigationID(), url });
+    }
+
     // We allow stale content when reloading a WebProcess that's been killed or crashed.
     m_process->send(Messages::WebPage::GoToBackForwardItem(navigation->navigationID(), m_backForwardList->currentItem()->itemID(), FrameLoadType::IndexedBackForward, ShouldTreatAsContinuingLoad::No, WTF::nullopt), m_pageID);
     m_process->responsivenessTimer().start();
@@ -1115,7 +1121,7 @@
 
     auto url = ""
     if (shouldTreatAsContinuingLoad != ShouldTreatAsContinuingLoad::Yes)
-        m_pageLoadState.setPendingAPIRequestURL(transaction, url);
+        m_pageLoadState.setPendingAPIRequest(transaction, { navigation.navigationID(), url });
 
     LoadParameters loadParameters;
     loadParameters.navigationID = navigation.navigationID();
@@ -1168,7 +1174,7 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    m_pageLoadState.setPendingAPIRequestURL(transaction, fileURLString, resourceDirectoryURL);
+    m_pageLoadState.setPendingAPIRequest(transaction, { navigation->navigationID(), fileURLString }, resourceDirectoryURL);
 
     String resourceDirectoryPath = resourceDirectoryURL.fileSystemPath();
 
@@ -1212,7 +1218,7 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    m_pageLoadState.setPendingAPIRequestURL(transaction, !baseURL.isEmpty() ? baseURL : WTF::blankURL().string());
+    m_pageLoadState.setPendingAPIRequest(transaction, { navigation.navigationID(), !baseURL.isEmpty() ? baseURL : WTF::blankURL().string() });
 
     LoadParameters loadParameters;
     loadParameters.navigationID = navigation.navigationID();
@@ -1251,7 +1257,7 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    m_pageLoadState.setPendingAPIRequestURL(transaction, unreachableURL);
+    m_pageLoadState.setPendingAPIRequest(transaction, { 0, unreachableURL });
     m_pageLoadState.setUnreachableURL(transaction, unreachableURL);
 
     if (m_mainFrame)
@@ -1287,7 +1293,7 @@
         launchProcess({ });
 
     auto transaction = m_pageLoadState.transaction();
-    m_pageLoadState.setPendingAPIRequestURL(transaction, WTF::blankURL().string());
+    m_pageLoadState.setPendingAPIRequest(transaction, { 0, WTF::blankURL().string() });
 
     LoadParameters loadParameters;
     loadParameters.navigationID = 0;
@@ -1345,9 +1351,6 @@
 
     String url = ""
     if (!url.isEmpty()) {
-        auto transaction = m_pageLoadState.transaction();
-        m_pageLoadState.setPendingAPIRequestURL(transaction, url);
-
         // We may not have an extension yet if back/forward list was reinstated after a WebProcess crash or a browser relaunch
         maybeInitializeSandboxExtensionHandle(m_process, URL(URL(), url), currentResourceDirectoryURL(), sandboxExtensionHandle);
     }
@@ -1357,6 +1360,11 @@
     
     auto navigation = m_navigationState->createReloadNavigation();
 
+    if (!url.isEmpty()) {
+        auto transaction = m_pageLoadState.transaction();
+        m_pageLoadState.setPendingAPIRequest(transaction, { navigation->navigationID(), url });
+    }
+
     // Store decision to reload without content blockers on the navigation so that we can later set the corresponding
     // WebsitePolicies flag in WebPageProxy::receivedNavigationPolicyDecision().
     if (options.contains(WebCore::ReloadOption::DisableContentBlockers))
@@ -1424,14 +1432,13 @@
     if (!hasRunningProcess())
         return launchProcessWithItem(item);
 
-    auto transaction = m_pageLoadState.transaction();
-
-    m_pageLoadState.setPendingAPIRequestURL(transaction, item.url());
-
     RefPtr<API::Navigation> navigation;
     if (!m_backForwardList->currentItem()->itemIsInSameDocument(item))
         navigation = m_navigationState->createBackForwardNavigation(item, m_backForwardList->currentItem(), frameLoadType);
 
+    auto transaction = m_pageLoadState.transaction();
+    m_pageLoadState.setPendingAPIRequest(transaction, { navigation ? navigation->navigationID() : 0, item.url() });
+
     m_process->send(Messages::WebPage::GoToBackForwardItem(navigation ? navigation->navigationID() : 0, item.itemID(), frameLoadType, ShouldTreatAsContinuingLoad::No, WTF::nullopt), m_pageID);
     m_process->responsivenessTimer().start();
 
@@ -2877,8 +2884,8 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    if (action == PolicyAction::Ignore && willContinueLoadInNewProcess == WillContinueLoadInNewProcess::No)
-        m_pageLoadState.clearPendingAPIRequestURL(transaction);
+    if (action == PolicyAction::Ignore && willContinueLoadInNewProcess == WillContinueLoadInNewProcess::No && navigation && navigation->navigationID() == m_pageLoadState.pendingAPIRequest().navigationID)
+        m_pageLoadState.clearPendingAPIRequest(transaction);
 
     DownloadID downloadID = { };
     if (action == PolicyAction::Download) {
@@ -2956,7 +2963,7 @@
         LOG(Loading, "WebPageProxy %p continueNavigationInNewProcess to back item URL %s", this, item->url().utf8().data());
 
         auto transaction = m_pageLoadState.transaction();
-        m_pageLoadState.setPendingAPIRequestURL(transaction, item->url());
+        m_pageLoadState.setPendingAPIRequest(transaction, { navigation.navigationID(), item->url() });
 
         m_provisionalPage->goToBackForwardItem(navigation, *item, WTFMove(websitePolicies));
         return;
@@ -3976,7 +3983,7 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    m_pageLoadState.clearPendingAPIRequestURL(transaction);
+    m_pageLoadState.clearPendingAPIRequest(transaction);
 
     if (frame->isMainFrame()) {
         process->didStartProvisionalLoadForMainFrame(url);
@@ -4441,7 +4448,7 @@
             automationSession->navigationOccurredForFrame(*frame);
     }
 
-    m_pageLoadState.clearPendingAPIRequestURL(transaction);
+    m_pageLoadState.clearPendingAPIRequest(transaction);
     frame->didSameDocumentNavigation(url);
 
     m_pageLoadState.commitChanges();
@@ -4623,9 +4630,9 @@
 
     auto transaction = m_pageLoadState.transaction();
 
-    bool fromAPI = request.url() == m_pageLoadState.pendingAPIRequestURL();
+    bool fromAPI = navigationID == m_pageLoadState.pendingAPIRequest().navigationID;
     if (!fromAPI)
-        m_pageLoadState.clearPendingAPIRequestURL(transaction);
+        m_pageLoadState.clearPendingAPIRequest(transaction);
 
     if (!checkURLReceivedFromCurrentOrPreviousWebProcess(process, request.url())) {
         RELEASE_LOG_ERROR_IF_ALLOWED(Process, "Ignoring request to load this main resource because it is outside the sandbox");
@@ -4684,7 +4691,7 @@
 
     auto listener = makeRef(frame.setUpPolicyListenerProxy([this, protectedThis = makeRef(*this), frame = makeRef(frame), sender = WTFMove(sender), navigation] (PolicyAction policyAction, API::WebsitePolicies* policies, ProcessSwapRequestedByClient processSwapRequestedByClient, RefPtr<SafeBrowsingWarning>&& safeBrowsingWarning) mutable {
 
-        auto completionHandler = [this, protectedThis = protectedThis.copyRef(), frame = frame.copyRef(), sender = WTFMove(sender), navigation = WTFMove(navigation), processSwapRequestedByClient, policies = makeRefPtr(policies)] (PolicyAction policyAction) mutable {
+        auto completionHandler = [this, protectedThis = protectedThis.copyRef(), frame = frame.copyRef(), sender = WTFMove(sender), navigation, processSwapRequestedByClient, policies = makeRefPtr(policies)] (PolicyAction policyAction) mutable {
             if (frame->isMainFrame()) {
                 if (!policies) {
                     if (auto* defaultPolicies = m_configuration->defaultWebsitePolicies())
@@ -4704,7 +4711,7 @@
         if (safeBrowsingWarning) {
             if (frame->isMainFrame() && safeBrowsingWarning->url().isValid()) {
                 auto transaction = m_pageLoadState.transaction();
-                m_pageLoadState.setPendingAPIRequestURL(transaction, safeBrowsingWarning->url());
+                m_pageLoadState.setPendingAPIRequest(transaction, { navigation->navigationID(), safeBrowsingWarning->url() });
                 m_pageLoadState.commitChanges();
             }
 

Modified: trunk/Tools/ChangeLog (247850 => 247851)


--- trunk/Tools/ChangeLog	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Tools/ChangeLog	2019-07-26 01:26:24 UTC (rev 247851)
@@ -1,3 +1,17 @@
+2019-07-24  Jiewen Tan  <[email protected]>
+
+        WebPageProxy::receivedPolicyDecision should check navigation ID before clear pendingAPIRequest
+        https://bugs.webkit.org/show_bug.cgi?id=200108
+        <rdar://problem/53521238>
+
+        Reviewed by Chris Dumez.
+
+        Added an API test.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/DecidePolicyForNavigationAction.mm:
+        (-[DecidePolicyForNavigationActionController webView:decidePolicyForNavigationAction:decisionHandler:]):
+        (TEST):
+
 2019-07-25  Jonathan Bedard  <[email protected]>
 
         results.webkit.org: Color should be the worst result, count should include all results

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DecidePolicyForNavigationAction.mm (247850 => 247851)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DecidePolicyForNavigationAction.mm	2019-07-26 01:03:40 UTC (rev 247850)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DecidePolicyForNavigationAction.mm	2019-07-26 01:26:24 UTC (rev 247851)
@@ -32,15 +32,18 @@
 #import <WebKit/WKNavigationActionPrivate.h>
 #import <WebKit/WKProcessPoolPrivate.h>
 #import <WebKit/_WKProcessPoolConfiguration.h>
+#import <wtf/BlockPtr.h>
 #import <wtf/RetainPtr.h>
 #import <wtf/mac/AppKitCompatibilityDeclarations.h>
 
 static bool shouldCancelNavigation;
+static bool shouldDelayDecision;
 static bool createdWebView;
 static bool decidedPolicy;
 static bool finishedNavigation;
 static RetainPtr<WKNavigationAction> action;
 static RetainPtr<WKWebView> newWebView;
+static BlockPtr<void(WKNavigationActionPolicy)> delayedDecision;
 
 static NSString *firstURL = @"data:text/html,First";
 static NSString *secondURL = @"data:text/html,Second";
@@ -62,6 +65,14 @@
         return;
     }
 
+    if (shouldDelayDecision) {
+        if (delayedDecision)
+            delayedDecision(WKNavigationActionPolicyCancel);
+        delayedDecision = makeBlockPtr(decisionHandler);
+        decidedPolicy = true;
+        return;
+    }
+
     decisionHandler(webView == newWebView.get() ? WKNavigationActionPolicyCancel : WKNavigationActionPolicyAllow);
 
     action = ""
@@ -593,6 +604,25 @@
     action = ""
 }
 
+TEST(WebKit, DelayDecidePolicyForNavigationAction)
+{
+    shouldDelayDecision = true;
+
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+    auto controller = adoptNS([[DecidePolicyForNavigationActionController alloc] init]);
+    [webView setNavigationDelegate:controller.get()];
+
+    RetainPtr<NSURL> testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
+    [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]];
+    TestWebKitAPI::Util::run(&decidedPolicy);
+    [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]];
+    TestWebKitAPI::Util::sleep(0.5); // Wait until the pending api request gets clear.
+    EXPECT_TRUE([[webView URL] isEqual:testURL.get()]);
+
+    shouldDelayDecision = false;
+    delayedDecision(WKNavigationActionPolicyCancel);
+}
+
 static size_t calls;
 static bool done;
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to