Changeset 240541 in webkit


Ignore:
Timestamp:
Jan 25, 2019 10:50:58 PM (5 years ago)
Author:
rniwa@webkit.org
Message:

Need a mechanism to override navigator.userAgent
https://bugs.webkit.org/show_bug.cgi?id=193762
<rdar://problem/47504939>

Reviewed by Brent Fulgham.

Source/WebCore:

Added the ability to specify user agent string just for navigator.userAgent via DocumentLoader.

  • loader/DocumentLoader.h:

(WebCore::DocumentLoader::setCustomJavaScriptUserAgent):
(WebCore::DocumentLoader::customJavaScriptUserAgent const):

  • loader/FrameLoader.cpp:

(WebCore::FrameLoader::userAgentForJavaScript const):

  • loader/FrameLoader.h:
  • page/Navigator.cpp:

(WebCore::Navigator::userAgent const):

Source/WebKit:

This patch adds a new _WKWebsitePolicies SPI to specify the user agent string returned by
navigator.userAgent without affecting the user agent string used to send network requests.

Tests: WebKit.WebsitePoliciesCustomJavaScriptUserAgent

WebKit.WebsitePoliciesCustomUserAgents

  • 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 setCustomJavaScriptUserAgent:]):
(-[_WKWebsitePolicies customJavaScriptUserAgent]):

Tools:

Added test cases for _WKWebsitePolicies.customJavaScriptUserAgent.

  • TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:

(-[CustomJavaScriptUserAgentDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
(-[CustomJavaScriptUserAgentDelegate webView:didFinishNavigation:]):

Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r240540 r240541  
     12019-01-25  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Need a mechanism to override navigator.userAgent
     4        https://bugs.webkit.org/show_bug.cgi?id=193762
     5        <rdar://problem/47504939>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        Added the ability to specify user agent string just for navigator.userAgent via DocumentLoader.
     10
     11        * loader/DocumentLoader.h:
     12        (WebCore::DocumentLoader::setCustomJavaScriptUserAgent):
     13        (WebCore::DocumentLoader::customJavaScriptUserAgent const):
     14        * loader/FrameLoader.cpp:
     15        (WebCore::FrameLoader::userAgentForJavaScript const):
     16        * loader/FrameLoader.h:
     17        * page/Navigator.cpp:
     18        (WebCore::Navigator::userAgent const):
     19
    1202019-01-25  Devin Rousso  <drousso@apple.com>
    221
  • trunk/Source/WebCore/loader/DocumentLoader.h

    r239830 r240541  
    271271    void setCustomUserAgent(const String& customUserAgent) { m_customUserAgent = customUserAgent; }
    272272    const String& customUserAgent() const { return m_customUserAgent; }
    273        
     273
     274    void setCustomJavaScriptUserAgent(const String& customJavaScriptUserAgent) { m_customJavaScriptUserAgent = customJavaScriptUserAgent; }
     275    const String& customJavaScriptUserAgent() const { return m_customJavaScriptUserAgent; }
     276
    274277    void setCustomNavigatorPlatform(const String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; }
    275278    const String& customNavigatorPlatform() const { return m_customNavigatorPlatform; }
     
    544547#endif
    545548    String m_customUserAgent;
     549    String m_customJavaScriptUserAgent;
    546550    String m_customNavigatorPlatform;
    547551    bool m_userContentExtensionsEnabled { true };
  • trunk/Source/WebCore/loader/FrameLoader.cpp

    r240046 r240541  
    27062706    return m_client.userAgent(url);
    27072707}
     2708
     2709String FrameLoader::userAgentForJavaScript(const URL& url) const
     2710{
     2711    if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) {
     2712        auto& customJavaScriptUserAgent = documentLoader->customJavaScriptUserAgent();
     2713        if (!customJavaScriptUserAgent.isEmpty())
     2714            return customJavaScriptUserAgent;
     2715        auto& customUserAgent = documentLoader->customUserAgent();
     2716        if (!customUserAgent.isEmpty())
     2717            return customUserAgent;
     2718    }
     2719
     2720    return m_client.userAgent(url);
     2721}
    27082722   
    27092723String FrameLoader::navigatorPlatform() const
  • trunk/Source/WebCore/loader/FrameLoader.h

    r239427 r240541  
    235235    void dispatchOnloadEvents();
    236236    String userAgent(const URL&) const;
     237    String userAgentForJavaScript(const URL&) const;
    237238    String navigatorPlatform() const;
    238239
  • trunk/Source/WebCore/page/Navigator.cpp

    r240014 r240541  
    9393        ResourceLoadObserver::shared().logNavigatorAPIAccessed(*frame->document(), ResourceLoadStatistics::NavigatorAPI::UserAgent);
    9494    if (m_userAgent.isNull())
    95         m_userAgent = frame->loader().userAgent(frame->document()->url());
     95        m_userAgent = frame->loader().userAgentForJavaScript(frame->document()->url());
    9696    return m_userAgent;
    9797}
  • trunk/Source/WebKit/ChangeLog

    r240540 r240541  
     12019-01-25  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Need a mechanism to override navigator.userAgent
     4        https://bugs.webkit.org/show_bug.cgi?id=193762
     5        <rdar://problem/47504939>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        This patch adds a new _WKWebsitePolicies SPI to specify the user agent string returned by
     10        navigator.userAgent without affecting the user agent string used to send network requests.
     11
     12        Tests: WebKit.WebsitePoliciesCustomJavaScriptUserAgent
     13               WebKit.WebsitePoliciesCustomUserAgents
     14
     15        * Shared/WebsitePoliciesData.cpp:
     16        (WebKit::WebsitePoliciesData::encode const):
     17        (WebKit::WebsitePoliciesData::decode):
     18        (WebKit::WebsitePoliciesData::applyToDocumentLoader):
     19        * Shared/WebsitePoliciesData.h:
     20        * UIProcess/API/APIWebsitePolicies.cpp:
     21        (API::WebsitePolicies::data):
     22        * UIProcess/API/APIWebsitePolicies.h:
     23        * UIProcess/API/Cocoa/_WKWebsitePolicies.h:
     24        * UIProcess/API/Cocoa/_WKWebsitePolicies.mm:
     25        (-[_WKWebsitePolicies setCustomJavaScriptUserAgent:]):
     26        (-[_WKWebsitePolicies customJavaScriptUserAgent]):
     27
    1282019-01-25  Devin Rousso  <drousso@apple.com>
    229
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp

    r239830 r240541  
    4545    encoder << websiteDataStoreParameters;
    4646    encoder << customUserAgent;
     47    encoder << customJavaScriptUserAgent;
    4748    encoder << customNavigatorPlatform;
    4849}
     
    9091        return WTF::nullopt;
    9192
     93    Optional<String> customJavaScriptUserAgent;
     94    decoder >> customJavaScriptUserAgent;
     95    if (!customJavaScriptUserAgent)
     96        return WTF::nullopt;
     97
    9298    Optional<String> customNavigatorPlatform;
    9399    decoder >> customNavigatorPlatform;
     
    104110        WTFMove(*websiteDataStoreParameters),
    105111        WTFMove(*customUserAgent),
     112        WTFMove(*customJavaScriptUserAgent),
    106113        WTFMove(*customNavigatorPlatform),
    107114    } };
     
    112119    documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
    113120    documentLoader.setCustomUserAgent(websitePolicies.customUserAgent);
     121    documentLoader.setCustomJavaScriptUserAgent(websitePolicies.customJavaScriptUserAgent);
    114122    documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform);
    115123    documentLoader.setDeviceOrientationEventEnabled(websitePolicies.deviceOrientationEventEnabled);
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.h

    r239639 r240541  
    5555    Optional<WebsiteDataStoreParameters> websiteDataStoreParameters;
    5656    String customUserAgent;
     57    String customJavaScriptUserAgent;
    5758    String customNavigatorPlatform;
    58    
     59
    5960    void encode(IPC::Encoder&) const;
    6061    static Optional<WebsitePoliciesData> decode(IPC::Decoder&);
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp

    r239639 r240541  
    5757    if (m_websiteDataStore)
    5858        parameters = m_websiteDataStore->websiteDataStore().parameters();
    59     return { contentBlockersEnabled(), deviceOrientationEventEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customNavigatorPlatform };
     59    return { contentBlockersEnabled(), deviceOrientationEventEnabled(), allowedAutoplayQuirks(), autoplayPolicy(),
     60        customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customJavaScriptUserAgent, m_customNavigatorPlatform };
    6061}
    6162
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h

    r239639 r240541  
    7575    void setCustomUserAgent(const WTF::String& customUserAgent) { m_customUserAgent = customUserAgent; }
    7676    const WTF::String& customUserAgent() const { return m_customUserAgent; }
    77    
     77
     78    void setCustomJavaScriptUserAgent(const WTF::String& customJavaScriptUserAgent) { m_customJavaScriptUserAgent = customJavaScriptUserAgent; }
     79    const WTF::String& customJavaScriptUserAgent() const { return m_customJavaScriptUserAgent; }
     80
    7881    void setCustomNavigatorPlatform(const WTF::String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; }
    7982    const WTF::String& customNavigatorPlatform() const { return m_customNavigatorPlatform; }
     
    9093    RefPtr<WebsiteDataStore> m_websiteDataStore;
    9194    WTF::String m_customUserAgent;
     95    WTF::String m_customJavaScriptUserAgent;
    9296    WTF::String m_customNavigatorPlatform;
    9397};
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h

    r239830 r240541  
    6060@property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore WK_API_AVAILABLE(macosx(10.13.4), ios(11.3));
    6161@property (nonatomic, copy) NSString *customUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
     62@property (nonatomic, copy) NSString *customJavaScriptUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
    6263@property (nonatomic, copy) NSString *customNavigatorPlatform WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
    6364@property (nonatomic) BOOL deviceOrientationEventEnabled WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm

    r239830 r240541  
    210210}
    211211
     212- (void)setCustomJavaScriptUserAgent:(NSString *)customUserAgent
     213{
     214    _websitePolicies->setCustomJavaScriptUserAgent(customUserAgent);
     215}
     216
     217- (NSString *)customJavaScriptUserAgent
     218{
     219    return _websitePolicies->customJavaScriptUserAgent();
     220}
     221
    212222- (void)setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform
    213223{
  • trunk/Tools/ChangeLog

    r240533 r240541  
     12019-01-25  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Need a mechanism to override navigator.userAgent
     4        https://bugs.webkit.org/show_bug.cgi?id=193762
     5        <rdar://problem/47504939>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        Added test cases for _WKWebsitePolicies.customJavaScriptUserAgent.
     10
     11        * TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
     12        (-[CustomJavaScriptUserAgentDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
     13        (-[CustomJavaScriptUserAgentDelegate webView:didFinishNavigation:]):
     14
    1152019-01-25  Dean Jackson  <dino@apple.com>
    216
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm

    r239830 r240541  
    10471047    }, 0);
    10481048}
     1049onmessage = (event) => {
     1050    window.subframeUserAgent = event.data;
     1051}
    10491052</script>
    10501053)TESTRESOURCE";
     
    10591062        fetch("test://www.apple.com/fetchResource.html");
    10601063    }, 0);
     1064    top.postMessage(navigator.userAgent, '*');
    10611065}
    10621066</script>
     
    10991103    EXPECT_EQ(1U, loadCount);
    11001104    loadCount = 0;
     1105}
     1106
     1107@interface CustomJavaScriptUserAgentDelegate : NSObject <WKNavigationDelegate>
     1108@property (nonatomic) BOOL setCustomUserAgent;
     1109@end
     1110
     1111@implementation CustomJavaScriptUserAgentDelegate
     1112
     1113- (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction userInfo:(id <NSSecureCoding>)userInfo decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
     1114{
     1115    _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
     1116    if (navigationAction.targetFrame.mainFrame) {
     1117        [websitePolicies setCustomJavaScriptUserAgent:@"Foo Custom JavaScript UserAgent"];
     1118        if (_setCustomUserAgent)
     1119            [websitePolicies setCustomUserAgent:@"Foo Custom Request UserAgent"];
     1120    }
     1121
     1122    decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
     1123}
     1124
     1125- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
     1126{
     1127    finishedNavigation = true;
     1128}
     1129
     1130@end
     1131
     1132TEST(WebKit, WebsitePoliciesCustomJavaScriptUserAgent)
     1133{
     1134    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     1135
     1136    auto schemeHandler = adoptNS([[DataMappingSchemeHandler alloc] init]);
     1137    [schemeHandler addMappingFromURLString:@"test://www.webkit.org/main.html" toData:customUserAgentMainFrameTestBytes];
     1138    [schemeHandler addMappingFromURLString:@"test://www.apple.com/subframe.html" toData:customUserAgentSubFrameTestBytes];
     1139    [schemeHandler setTaskHandler:[](id <WKURLSchemeTask> task) {
     1140        auto* userAgentString = [task.request valueForHTTPHeaderField:@"User-Agent"];
     1141        EXPECT_TRUE([userAgentString hasPrefix:@"Mozilla/5.0 (Macintosh;"]);
     1142        EXPECT_TRUE([userAgentString hasSuffix:@"(KHTML, like Gecko)"]);
     1143    }];
     1144    [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
     1145
     1146    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     1147
     1148    auto delegate = adoptNS([[CustomJavaScriptUserAgentDelegate alloc] init]);
     1149    [webView setNavigationDelegate:delegate.get()];
     1150
     1151    loadCount = 0;
     1152    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test://www.webkit.org/main.html"]];
     1153    [webView loadRequest:request];
     1154
     1155    TestWebKitAPI::Util::run(&finishedNavigation);
     1156    finishedNavigation = false;
     1157
     1158    while (loadCount != 9U)
     1159        TestWebKitAPI::Util::spinRunLoop();
     1160
     1161    EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"navigator.userAgent"] UTF8String]);
     1162    EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"subframeUserAgent"] UTF8String]);
     1163}
     1164
     1165TEST(WebKit, WebsitePoliciesCustomUserAgents)
     1166{
     1167    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     1168
     1169    auto schemeHandler = adoptNS([[DataMappingSchemeHandler alloc] init]);
     1170    [schemeHandler addMappingFromURLString:@"test://www.webkit.org/main.html" toData:customUserAgentMainFrameTestBytes];
     1171    [schemeHandler addMappingFromURLString:@"test://www.apple.com/subframe.html" toData:customUserAgentSubFrameTestBytes];
     1172    [schemeHandler setTaskHandler:[](id <WKURLSchemeTask> task) {
     1173        EXPECT_STREQ("Foo Custom Request UserAgent", [[task.request valueForHTTPHeaderField:@"User-Agent"] UTF8String]);
     1174    }];
     1175    [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
     1176
     1177    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     1178
     1179    auto delegate = adoptNS([[CustomJavaScriptUserAgentDelegate alloc] init]);
     1180    delegate.get().setCustomUserAgent = YES;
     1181    [webView setNavigationDelegate:delegate.get()];
     1182
     1183    loadCount = 0;
     1184    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test://www.webkit.org/main.html"]];
     1185    [webView loadRequest:request];
     1186
     1187    TestWebKitAPI::Util::run(&finishedNavigation);
     1188    finishedNavigation = false;
     1189
     1190    while (loadCount != 9U)
     1191        TestWebKitAPI::Util::spinRunLoop();
     1192
     1193    EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"navigator.userAgent"] UTF8String]);
     1194    EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"subframeUserAgent"] UTF8String]);
    11011195}
    11021196
Note: See TracChangeset for help on using the changeset viewer.