Add WKNavigation/WKNavigationAction related SPI.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Jul 2018 17:33:28 +0000 (17:33 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Jul 2018 17:33:28 +0000 (17:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=187826

Reviewed by Chris Dumez.

Source/WebKit:

* UIProcess/API/APINavigationAction.h:

* UIProcess/API/C/mac/WKPagePrivateMac.h:
* UIProcess/API/C/mac/WKPagePrivateMac.mm:
(WKPageLoadURLRequestReturningNavigation):

* UIProcess/API/Cocoa/WKNavigationAction.mm:
(-[WKNavigationAction _mainFrameNavigation]):
* UIProcess/API/Cocoa/WKNavigationActionPrivate.h:

* UIProcess/API/glib/WebKitUIClient.cpp:

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::decidePolicyForNavigationAction):
(WebKit::WebPageProxy::decidePolicyForNewWindowAction):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/Navigation.mm:
(-[NavigationActionHasNavigationDelegate webView:didFinishNavigation:]):
(-[NavigationActionHasNavigationDelegate webView:decidePolicyForNavigationAction:decisionHandler:]):
(TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@234052 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/APINavigationAction.h
Source/WebKit/UIProcess/API/C/mac/WKPagePrivateMac.h
Source/WebKit/UIProcess/API/C/mac/WKPagePrivateMac.mm
Source/WebKit/UIProcess/API/Cocoa/WKNavigationAction.mm
Source/WebKit/UIProcess/API/Cocoa/WKNavigationActionPrivate.h
Source/WebKit/UIProcess/API/glib/WebKitUIClient.cpp
Source/WebKit/UIProcess/WebPageProxy.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/Navigation.mm

index 4bc8137..7a0f591 100644 (file)
@@ -1,3 +1,26 @@
+2018-07-20  Brady Eidson  <beidson@apple.com>
+
+        Add WKNavigation/WKNavigationAction related SPI.
+        https://bugs.webkit.org/show_bug.cgi?id=187826
+
+        Reviewed by Chris Dumez.
+
+        * UIProcess/API/APINavigationAction.h:
+
+        * UIProcess/API/C/mac/WKPagePrivateMac.h:
+        * UIProcess/API/C/mac/WKPagePrivateMac.mm:
+        (WKPageLoadURLRequestReturningNavigation):
+
+        * UIProcess/API/Cocoa/WKNavigationAction.mm:
+        (-[WKNavigationAction _mainFrameNavigation]):
+        * UIProcess/API/Cocoa/WKNavigationActionPrivate.h:
+
+        * UIProcess/API/glib/WebKitUIClient.cpp:
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::decidePolicyForNavigationAction):
+        (WebKit::WebPageProxy::decidePolicyForNewWindowAction):
+
 2018-07-20  Philippe Normand  <pnormand@igalia.com>
 
         [GTK][WPE] enable-media-capabilities websetting
index 6dad89b..eecbff4 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "APIFrameInfo.h"
+#include "APINavigation.h"
 #include "APIObject.h"
 #include "APIUserInitiatedAction.h"
 #include "NavigationActionData.h"
@@ -36,9 +37,9 @@ namespace API {
 
 class NavigationAction final : public ObjectImpl<Object::Type::NavigationAction> {
 public:
-    static Ref<NavigationAction> create(WebKit::NavigationActionData&& navigationActionData, API::FrameInfo* sourceFrame, API::FrameInfo* targetFrame, std::optional<WTF::String> targetFrameName, WebCore::ResourceRequest&& request, const WebCore::URL& originalURL, bool shouldOpenAppLinks, RefPtr<UserInitiatedAction>&& userInitiatedAction)
+    template<typename... Args> static Ref<NavigationAction> create(Args&&... args)
     {
-        return adoptRef(*new NavigationAction(WTFMove(navigationActionData), sourceFrame, targetFrame, targetFrameName, WTFMove(request), originalURL, shouldOpenAppLinks, WTFMove(userInitiatedAction)));
+        return adoptRef(*new NavigationAction(std::forward<Args>(args)...));
     }
 
     FrameInfo* sourceFrame() const { return m_sourceFrame.get(); }
@@ -62,8 +63,10 @@ public:
     bool isProcessingUserGesture() const { return m_userInitiatedAction; }
     UserInitiatedAction* userInitiatedAction() const { return m_userInitiatedAction.get(); }
 
+    Navigation* mainFrameNavigation() const { return m_mainFrameNavigation.get(); }
+
 private:
-NavigationAction(WebKit::NavigationActionData&& navigationActionData, API::FrameInfo* sourceFrame, API::FrameInfo* targetFrame, std::optional<WTF::String> targetFrameName, WebCore::ResourceRequest&& request, const WebCore::URL& originalURL, bool shouldOpenAppLinks, RefPtr<UserInitiatedAction>&& userInitiatedAction)
+    NavigationAction(WebKit::NavigationActionData&& navigationActionData, API::FrameInfo* sourceFrame, API::FrameInfo* targetFrame, std::optional<WTF::String> targetFrameName, WebCore::ResourceRequest&& request, const WebCore::URL& originalURL, bool shouldOpenAppLinks, RefPtr<UserInitiatedAction>&& userInitiatedAction, API::Navigation* mainFrameNavigation)
         : m_sourceFrame(sourceFrame)
         , m_targetFrame(targetFrame)
         , m_targetFrameName(targetFrameName)
@@ -72,6 +75,12 @@ NavigationAction(WebKit::NavigationActionData&& navigationActionData, API::Frame
         , m_shouldOpenAppLinks(shouldOpenAppLinks)
         , m_userInitiatedAction(WTFMove(userInitiatedAction))
         , m_navigationActionData(WTFMove(navigationActionData))
+        , m_mainFrameNavigation(mainFrameNavigation)
+    {
+    }
+
+    NavigationAction(WebKit::NavigationActionData&& navigationActionData, API::FrameInfo* sourceFrame, API::FrameInfo* targetFrame, std::optional<WTF::String> targetFrameName, WebCore::ResourceRequest&& request, const WebCore::URL& originalURL, bool shouldOpenAppLinks, RefPtr<UserInitiatedAction>&& userInitiatedAction)
+        : NavigationAction(WTFMove(navigationActionData), sourceFrame, targetFrame, targetFrameName, WTFMove(request), originalURL, shouldOpenAppLinks, WTFMove(userInitiatedAction), nullptr)
     {
     }
 
@@ -87,6 +96,7 @@ NavigationAction(WebKit::NavigationActionData&& navigationActionData, API::Frame
     RefPtr<UserInitiatedAction> m_userInitiatedAction;
 
     WebKit::NavigationActionData m_navigationActionData;
+    RefPtr<Navigation> m_mainFrameNavigation;
 };
 
 } // namespace API
index caab73b..d7c9555 100644 (file)
@@ -59,8 +59,13 @@ WK_EXPORT _WKRemoteObjectRegistry *WKPageGetObjectRegistry(WKPageRef page);
 WK_EXPORT void WKPageSetFullscreenDelegate(WKPageRef page, id <_WKFullscreenDelegate>);
 WK_EXPORT id <_WKFullscreenDelegate> WKPageGetFullscreenDelegate(WKPageRef page);
 
+#if !TARGET_OS_IPHONE && (defined(__clang__) && defined(__APPLE__) && !defined(__i386__))
+@class WKNavigation;
+WK_EXPORT WKNavigation *WKPageLoadURLRequestReturningNavigation(WKPageRef page, WKURLRequestRef request);
 #endif
 
+#endif // __OBJC__
+
 WK_EXPORT bool WKPageIsURLKnownHSTSHost(WKPageRef page, WKURLRef url);
 
 #if !TARGET_OS_IPHONE
index c5c4a69..2eb36b5 100644 (file)
@@ -30,6 +30,7 @@
 #import "PageLoadStateObserver.h"
 #import "WKAPICast.h"
 #import "WKNSURLExtras.h"
+#import "WKNavigationInternal.h"
 #import "WKViewInternal.h"
 #import "WebPageGroup.h"
 #import "WebPageProxy.h"
@@ -138,6 +139,19 @@ bool WKPageIsURLKnownHSTSHost(WKPageRef page, WKURLRef url)
     return webPageProxy->process().processPool().isURLKnownHSTSHost(toImpl(url)->string(), privateBrowsingEnabled);
 }
 
+#if !TARGET_OS_IPHONE && (defined(__clang__) && defined(__APPLE__) && !defined(__i386__))
+WKNavigation *WKPageLoadURLRequestReturningNavigation(WKPageRef pageRef, WKURLRequestRef urlRequestRef)
+{
+    auto resourceRequest = toImpl(urlRequestRef)->resourceRequest();
+    auto navigation = toImpl(pageRef)->loadRequest(WTFMove(resourceRequest));
+
+    if (!navigation)
+        return nil;
+
+    return [API::wrapper(*navigation.leakRef()) autorelease];
+}
+#endif
+
 #if PLATFORM(MAC)
 bool WKPageIsPlayingVideoInEnhancedFullscreen(WKPageRef pageRef)
 {
index 4f8a4dd..3f8ff69 100644 (file)
@@ -30,6 +30,7 @@
 
 #import "NavigationActionData.h"
 #import "WKFrameInfoInternal.h"
+#import "WKNavigationInternal.h"
 #import "_WKUserInitiatedActionInternal.h"
 #import <WebCore/FloatPoint.h>
 #import <wtf/RetainPtr.h>
@@ -236,6 +237,14 @@ static NSInteger toNSButtonNumber(WebKit::WebMouseEvent::Button mouseButton)
     return _navigationAction->isRedirect();
 }
 
+- (WKNavigation *)_mainFrameNavigation
+{
+    if (auto* navigation = _navigationAction->mainFrameNavigation())
+        return wrapper(*navigation);
+    
+    return nil;
+}
+
 @end
 
 #endif
index 472a7b3..5df7ae0 100644 (file)
@@ -27,6 +27,7 @@
 
 #if WK_API_ENABLED
 
+@class WKNavigation;
 @class _WKUserInitiatedAction;
 
 #if TARGET_OS_IPHONE
@@ -55,6 +56,7 @@ typedef NS_ENUM(NSInteger, WKSyntheticClickType) {
 #endif
 
 @property (nonatomic, readonly) BOOL _isRedirect WK_API_AVAILABLE(macosx(10.13), ios(11.0));
+@property (nonatomic, readonly) WKNavigation *_mainFrameNavigation WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
 
 @end
 
index 42bfa9f..f5dab1e 100644 (file)
@@ -50,7 +50,7 @@ private:
     void createNewPage(WebPageProxy& page, Ref<API::FrameInfo>&& frameInfo, WebCore::ResourceRequest&& resourceRequest, WebCore::WindowFeatures&& windowFeatures, NavigationActionData&& navigationActionData, CompletionHandler<void(RefPtr<WebPageProxy>&&)>&& completionHandler) final
     {
         auto userInitiatedActivity = page.process().userInitiatedActivity(navigationActionData.userGestureTokenIdentifier);
-        WebKitNavigationAction navigationAction(API::NavigationAction::create(WTFMove(navigationActionData), frameInfo.ptr(), nullptr, std::nullopt, WTFMove(resourceRequest), { }, false, WTFMove(userInitiatedActivity)));
+        WebKitNavigationAction navigationAction(API::NavigationAction::create(WTFMove(navigationActionData), frameInfo.ptr(), nullptr, std::nullopt, WTFMove(resourceRequest), WebCore::URL { }, false, WTFMove(userInitiatedActivity)));
         completionHandler(webkitWebViewCreateNewPage(m_webView, windowFeatures, &navigationAction));
     }
 
index e00f6ed..cbad2d6 100644 (file)
@@ -4022,6 +4022,8 @@ void WebPageProxy::decidePolicyForNavigationAction(uint64_t frameID, const Secur
     navigation->setOpener(navigationActionData.opener);
 
     auto listener = makeRef(frame->setUpPolicyListenerProxy(listenerID, PolicyListenerType::NavigationAction));
+
+    API::Navigation* mainFrameNavigation = frame->isMainFrame() ? navigation.get() : nullptr;
     listener->setNavigation(navigation.releaseNonNull());
 
 #if ENABLE(CONTENT_FILTERING)
@@ -4047,7 +4049,7 @@ void WebPageProxy::decidePolicyForNavigationAction(uint64_t frameID, const Secur
         auto userInitiatedActivity = m_process->userInitiatedActivity(navigationActionData.userGestureTokenIdentifier);
         bool shouldOpenAppLinks = !m_shouldSuppressAppLinksInNextNavigationPolicyDecision && destinationFrameInfo->isMainFrame() && !hostsAreEqual(URL(ParsedURLString, m_mainFrame->url()), request.url()) && navigationActionData.navigationType != WebCore::NavigationType::BackForward;
 
-        auto navigationAction = API::NavigationAction::create(WTFMove(navigationActionData), sourceFrameInfo.get(), destinationFrameInfo.ptr(), std::nullopt, WTFMove(request), originalRequest.url(), shouldOpenAppLinks, WTFMove(userInitiatedActivity));
+        auto navigationAction = API::NavigationAction::create(WTFMove(navigationActionData), sourceFrameInfo.get(), destinationFrameInfo.ptr(), std::nullopt, WTFMove(request), originalRequest.url(), shouldOpenAppLinks, WTFMove(userInitiatedActivity), mainFrameNavigation);
 
         m_navigationClient->decidePolicyForNavigationAction(*this, WTFMove(navigationAction), WTFMove(listener), m_process->transformHandlesToObjects(userData.object()).get());
     } else
@@ -4085,7 +4087,7 @@ void WebPageProxy::decidePolicyForNewWindowAction(uint64_t frameID, const Securi
 
         auto userInitiatedActivity = m_process->userInitiatedActivity(navigationActionData.userGestureTokenIdentifier);
         bool shouldOpenAppLinks = !hostsAreEqual(URL(ParsedURLString, m_mainFrame->url()), request.url());
-        auto navigationAction = API::NavigationAction::create(WTFMove(navigationActionData), sourceFrameInfo.get(), nullptr, frameName, WTFMove(request), { }, shouldOpenAppLinks, WTFMove(userInitiatedActivity));
+        auto navigationAction = API::NavigationAction::create(WTFMove(navigationActionData), sourceFrameInfo.get(), nullptr, frameName, WTFMove(request), URL { }, shouldOpenAppLinks, WTFMove(userInitiatedActivity));
 
         m_navigationClient->decidePolicyForNavigationAction(*this, navigationAction.get(), WTFMove(listener), m_process->transformHandlesToObjects(userData.object()).get());
 
index 480817f..75e1ed9 100644 (file)
@@ -1,3 +1,15 @@
+2018-07-20  Brady Eidson  <beidson@apple.com>
+
+        Add WKNavigation/WKNavigationAction related SPI.
+        https://bugs.webkit.org/show_bug.cgi?id=187826
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/Navigation.mm:
+        (-[NavigationActionHasNavigationDelegate webView:didFinishNavigation:]):
+        (-[NavigationActionHasNavigationDelegate webView:decidePolicyForNavigationAction:decisionHandler:]):
+        (TEST):
+
 2018-07-20  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r233943, r233988, r234004, and
index df77d96..cf3d37b 100644 (file)
@@ -26,6 +26,7 @@
 #include "config.h"
 
 #import <WebKit/WKBackForwardListPrivate.h>
+#import <WebKit/WKNavigationActionPrivate.h>
 #import <WebKit/WKNavigationDelegatePrivate.h>
 #import <WebKit/WKNavigationPrivate.h>
 #import <WebKit/WKWebView.h>
@@ -217,6 +218,42 @@ TEST(WKNavigation, DecidePolicyForPageCacheNavigation)
     ASSERT_TRUE([delegate decidedPolicyForBackForwardNavigation]);
 }
 
+@interface NavigationActionHasNavigationDelegate : NSObject <WKNavigationDelegate> {
+@public
+    WKNavigation *navigation;
+}
+@end
+
+@implementation NavigationActionHasNavigationDelegate
+
+- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
+{
+    isDone = true;
+}
+
+- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
+{
+    EXPECT_TRUE(!!navigationAction._mainFrameNavigation);
+    EXPECT_EQ(navigationAction._mainFrameNavigation, navigation);
+    decisionHandler(WKNavigationActionPolicyAllow);
+}
+
+@end
+
+TEST(WKNavigation, NavigationActionHasNavigation)
+{
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+
+    RetainPtr<NavigationActionHasNavigationDelegate> delegate = adoptNS([[NavigationActionHasNavigationDelegate alloc] init]);
+    [webView setNavigationDelegate:delegate.get()];
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"data:text/html,1"]];
+
+    isDone = false;
+    delegate->navigation = [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&isDone);
+}
+
 @interface ClientRedirectNavigationDelegate : NSObject<WKNavigationDelegatePrivate>
 @end