Add SPI to allow the client to set the user-agent at main frame level, from the UIProcess
https://bugs.webkit.org/show_bug.cgi?id=192509
<rdar://problem/46500832>

Reviewed by Alex Christensen.

Source/WebCore:

* loader/DocumentLoader.h:
(WebCore::DocumentLoader::setCustomUserAgent):
(WebCore::DocumentLoader::customUserAgent const):
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::userAgent const):

Source/WebKit:

Add SPI to allow the client to set the user-agent at main frame level, from the UIProcess instead of doing
it at resource-level from the injected bundle.

The custom user-agent string can now be set on _WKWebsitePolicies during the
decidePolicyForNavigationAction for the main feame, and will impact this main resource load as well as its
future subresource loads.

* Shared/WebsitePoliciesData.cpp:
(WebKit::WebsitePoliciesData::encode const):
(WebKit::WebsitePoliciesData::decode):
(WebKit::WebsitePoliciesData::applyToDocumentLoader):
* Shared/WebsitePoliciesData.h:
* UIProcess/API/APIWebsitePolicies.cpp:
(API::WebsitePolicies::data):
* UIProcess/API/APIWebsitePolicies.h:
* UIProcess/API/Cocoa/_WKWebsitePolicies.h:
* UIProcess/API/Cocoa/_WKWebsitePolicies.mm:
(-[_WKWebsitePolicies setCustomUserAgent:]):
(-[_WKWebsitePolicies customUserAgent]):
* UIProcess/Cocoa/NavigationState.mm:
(WebKit::NavigationState::NavigationClient::decidePolicyForNavigationAction):

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
(-[DataMappingSchemeHandler addMappingFromURLString:toData:]):
(-[DataMappingSchemeHandler webView:startURLSchemeTask:]):
(-[DataMappingSchemeHandler webView:stopURLSchemeTask:]):
(-[CustomUserAgentDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
(-[CustomUserAgentDelegate webView:didFinishNavigation:]):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@239046 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/loader/FrameLoader.cpp b/Source/WebCore/loader/FrameLoader.cpp
index a857e8f..8336522 100644
--- a/Source/WebCore/loader/FrameLoader.cpp
+++ b/Source/WebCore/loader/FrameLoader.cpp
@@ -2664,6 +2664,12 @@
 
 String FrameLoader::userAgent(const URL& url) const
 {
+    if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) {
+        auto& customUserAgent = documentLoader->customUserAgent();
+        if (!customUserAgent.isEmpty())
+            return customUserAgent;
+    }
+
     return m_client.userAgent(url);
 }