Changeset 239384 in webkit


Ignore:
Timestamp:
Dec 19, 2018 12:21:31 PM (5 years ago)
Author:
Megan Gardner
Message:

Allow clients to set the navigator platform
https://bugs.webkit.org/show_bug.cgi?id=192735

Reviewed by Tim Horton.

Source/WebCore:

Expanded TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm.

Lots of piping to allow the setting of a custom navigator platform.

  • loader/DocumentLoader.h:

(WebCore::DocumentLoader::setCustomNavigatorPlatform):
(WebCore::DocumentLoader::customNavigatorPlatform const):

  • loader/FrameLoader.cpp:

(WebCore::FrameLoader::navigatorPlatform const):

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

(WebCore::Navigator::platform const):

  • page/Navigator.h:
  • page/NavigatorBase.cpp:

(WebCore::NavigatorBase::platform const):
(WebCore::NavigatorBase::platform): Deleted.

  • page/NavigatorBase.h:

Source/WebKit:

Add private API to allow clients to override the default navigator
platform. Some websites check for the platform, which
interfears with our request desktop site feature. This should
help more sites function as expected.

  • 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 setCustomNavigatorPlatform:]):
(-[_WKWebsitePolicies customNavigatorPlatform]):

  • UIProcess/Cocoa/NavigationState.mm:

(WebKit::NavigationState::NavigationClient::decidePolicyForNavigationAction):

Source/WebKitLegacy/mac:

Add stubs to fill out the added functionality in FrameLoaderClient.

  • WebCoreSupport/WebFrameLoaderClient.h:
  • WebCoreSupport/WebFrameLoaderClient.mm:

(WebFrameLoaderClient::navigatorPlatform):

Tools:

Add a test for setting a custom navigator platform.

  • TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:

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

Location:
trunk
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r239381 r239384  
     12018-12-19  Megan Gardner  <megan_gardner@apple.com>
     2
     3        Allow clients to set the navigator platform
     4        https://bugs.webkit.org/show_bug.cgi?id=192735
     5
     6        Reviewed by Tim Horton.
     7
     8        Expanded TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm.
     9
     10        Lots of piping to allow the setting of a custom navigator platform.
     11
     12        * loader/DocumentLoader.h:
     13        (WebCore::DocumentLoader::setCustomNavigatorPlatform):
     14        (WebCore::DocumentLoader::customNavigatorPlatform const):
     15        * loader/FrameLoader.cpp:
     16        (WebCore::FrameLoader::navigatorPlatform const):
     17        * loader/FrameLoader.h:
     18        * page/Navigator.cpp:
     19        (WebCore::Navigator::platform const):
     20        * page/Navigator.h:
     21        * page/NavigatorBase.cpp:
     22        (WebCore::NavigatorBase::platform const):
     23        (WebCore::NavigatorBase::platform): Deleted.
     24        * page/NavigatorBase.h:
     25
    1262018-12-19  Ryan Haddad  <ryanhaddad@apple.com>
    227
  • trunk/Source/WebCore/loader/DocumentLoader.h

    r239182 r239384  
    267267    void setCustomUserAgent(const String& customUserAgent) { m_customUserAgent = customUserAgent; }
    268268    const String& customUserAgent() const { return m_customUserAgent; }
     269       
     270    void setCustomNavigatorPlatform(const String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; }
     271    const String& customNavigatorPlatform() const { return m_customNavigatorPlatform; }
    269272
    270273    OptionSet<AutoplayQuirk> allowedAutoplayQuirks() const { return m_allowedAutoplayQuirks; }
     
    537540#endif
    538541    String m_customUserAgent;
     542    String m_customNavigatorPlatform;
    539543    bool m_userContentExtensionsEnabled { true };
    540544    AutoplayPolicy m_autoplayPolicy { AutoplayPolicy::Default };
  • trunk/Source/WebCore/loader/FrameLoader.cpp

    r239182 r239384  
    26782678    return m_client.userAgent(url);
    26792679}
     2680   
     2681String FrameLoader::navigatorPlatform() const
     2682{
     2683    if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) {
     2684        auto& customNavigatorPlatform = documentLoader->customNavigatorPlatform();
     2685        if (!customNavigatorPlatform.isEmpty())
     2686            return customNavigatorPlatform;
     2687    }
     2688    return String();
     2689}
    26802690
    26812691void FrameLoader::dispatchOnloadEvents()
  • trunk/Source/WebCore/loader/FrameLoader.h

    r238819 r239384  
    235235    void dispatchOnloadEvents();
    236236    String userAgent(const URL&) const;
     237    String navigatorPlatform() const;
    237238
    238239    void dispatchDidClearWindowObjectInWorld(DOMWrapperWorld&);
  • trunk/Source/WebCore/page/Navigator.cpp

    r237266 r239384  
    9696    return m_userAgent;
    9797}
     98   
     99const String& Navigator::platform() const
     100{
     101    auto* frame = this->frame();
     102    if (!frame || !frame->page())
     103        return m_platform;
     104
     105    if (m_platform.isNull())
     106        m_platform = frame->loader().navigatorPlatform();
     107   
     108    if (m_platform.isNull())
     109        m_platform = NavigatorBase::platform();
     110    return m_platform;
     111}
    98112
    99113void Navigator::userAgentChanged()
  • trunk/Source/WebCore/page/Navigator.h

    r237266 r239384  
    4343    bool javaEnabled() const;
    4444    const String& userAgent() const final;
     45    const String& platform() const final;
    4546    void userAgentChanged();
    4647    bool onLine() const final;
     
    5960    mutable RefPtr<DOMMimeTypeArray> m_mimeTypes;
    6061    mutable String m_userAgent;
     62    mutable String m_platform;
    6163};
    6264
  • trunk/Source/WebCore/page/NavigatorBase.cpp

    r238538 r239384  
    101101}
    102102
    103 String NavigatorBase::platform()
     103const String& NavigatorBase::platform() const
    104104{
     105    static NeverDestroyed<String> defaultPlatform = WEBCORE_NAVIGATOR_PLATFORM;
    105106#if OS(LINUX)
    106107    if (!String(WEBCORE_NAVIGATOR_PLATFORM).isEmpty())
    107         return WEBCORE_NAVIGATOR_PLATFORM;
     108        return defaultPlatform;
    108109    struct utsname osname;
    109110    static NeverDestroyed<String> platformName(uname(&osname) >= 0 ? String(osname.sysname) + " "_str + String(osname.machine) : emptyString());
    110111    return platformName;
    111112#else
    112     return WEBCORE_NAVIGATOR_PLATFORM;
     113    return defaultPlatform;
    113114#endif
    114115}
  • trunk/Source/WebCore/page/NavigatorBase.h

    r237185 r239384  
    4444    String appVersion() const;
    4545    virtual const String& userAgent() const = 0;
    46     static String platform();
     46    virtual const String& platform() const;
    4747
    4848    static String appCodeName();
  • trunk/Source/WebKit/ChangeLog

    r239380 r239384  
     12018-12-19  Megan Gardner  <megan_gardner@apple.com>
     2
     3        Allow clients to set the navigator platform
     4        https://bugs.webkit.org/show_bug.cgi?id=192735
     5
     6        Reviewed by Tim Horton.
     7
     8        Add private API to allow clients to override the default navigator
     9        platform. Some websites check for the platform, which
     10        interfears with our request desktop site feature. This should
     11        help more sites function as expected.
     12
     13        * Shared/WebsitePoliciesData.cpp:
     14        (WebKit::WebsitePoliciesData::encode const):
     15        (WebKit::WebsitePoliciesData::decode):
     16        (WebKit::WebsitePoliciesData::applyToDocumentLoader):
     17        * Shared/WebsitePoliciesData.h:
     18        * UIProcess/API/APIWebsitePolicies.cpp:
     19        (API::WebsitePolicies::data):
     20        * UIProcess/API/APIWebsitePolicies.h:
     21        * UIProcess/API/Cocoa/_WKWebsitePolicies.h:
     22        * UIProcess/API/Cocoa/_WKWebsitePolicies.mm:
     23        (-[_WKWebsitePolicies setCustomNavigatorPlatform:]):
     24        (-[_WKWebsitePolicies customNavigatorPlatform]):
     25        * UIProcess/Cocoa/NavigationState.mm:
     26        (WebKit::NavigationState::NavigationClient::decidePolicyForNavigationAction):
     27
    1282018-12-19  Truitt Savell  <tsavell@apple.com>
    229
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp

    r239046 r239384  
    4444    encoder << websiteDataStoreParameters;
    4545    encoder << customUserAgent;
     46    encoder << customNavigatorPlatform;
    4647}
    4748
     
    8283    if (!customUserAgent)
    8384        return std::nullopt;
     85
     86    std::optional<String> customNavigatorPlatform;
     87    decoder >> customNavigatorPlatform;
     88    if (!customNavigatorPlatform)
     89        return std::nullopt;
    8490   
    8591    return { {
     
    9197        WTFMove(*websiteDataStoreParameters),
    9298        WTFMove(*customUserAgent),
     99        WTFMove(*customNavigatorPlatform),
    93100    } };
    94101}
     
    98105    documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
    99106    documentLoader.setCustomUserAgent(websitePolicies.customUserAgent);
     107    documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform);
    100108   
    101109    // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.h

    r239046 r239384  
    5454    std::optional<WebsiteDataStoreParameters> websiteDataStoreParameters;
    5555    String customUserAgent;
     56    String customNavigatorPlatform;
    5657   
    5758    void encode(IPC::Encoder&) const;
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp

    r239046 r239384  
    5757    if (m_websiteDataStore)
    5858        parameters = m_websiteDataStore->websiteDataStore().parameters();
    59     return { contentBlockersEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent };
     59    return { contentBlockersEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customNavigatorPlatform };
    6060}
    6161
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h

    r239046 r239384  
    7272    void setCustomUserAgent(const WTF::String& customUserAgent) { m_customUserAgent = customUserAgent; }
    7373    const WTF::String& customUserAgent() const { return m_customUserAgent; }
     74   
     75    void setCustomNavigatorPlatform(const WTF::String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; }
     76    const WTF::String& customNavigatorPlatform() const { return m_customNavigatorPlatform; }
    7477
    7578private:
     
    8386    RefPtr<WebsiteDataStore> m_websiteDataStore;
    8487    WTF::String m_customUserAgent;
     88    WTF::String m_customNavigatorPlatform;
    8589};
    8690
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h

    r239046 r239384  
    5959@property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore WK_API_AVAILABLE(macosx(10.13.4), ios(11.3));
    6060@property (nonatomic, copy) NSString *customUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
     61@property (nonatomic, copy) NSString *customNavigatorPlatform WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
    6162
    6263@end
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm

    r239046 r239384  
    194194}
    195195
     196- (void)setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform
     197{
     198    _websitePolicies->setCustomNavigatorPlatform(customNavigatorPlatform);
     199}
     200
     201- (NSString *)customNavigatorPlatform
     202{
     203    return _websitePolicies->customNavigatorPlatform();
     204}
     205
    196206- (NSString *)description
    197207{
  • trunk/Source/WebKit/UIProcess/Cocoa/NavigationState.mm

    r239078 r239384  
    550550            if (!apiWebsitePolicies->customUserAgent().isNull() && subframeNavigation)
    551551                [NSException raise:NSInvalidArgumentException format:@"_WKWebsitePolicies.customUserAgent must be nil for subframe navigations."];
     552            if (!apiWebsitePolicies->customNavigatorPlatform().isNull() && subframeNavigation)
     553                [NSException raise:NSInvalidArgumentException format:@"_WKWebsitePolicies.customNavigatorPlatform must be nil for subframe navigations."];
    552554        }
    553555
  • trunk/Source/WebKitLegacy/mac/ChangeLog

    r239313 r239384  
     12018-12-19  Megan Gardner  <megan_gardner@apple.com>
     2
     3        Allow clients to set the navigator platform
     4        https://bugs.webkit.org/show_bug.cgi?id=192735
     5
     6        Reviewed by Tim Horton.
     7
     8        Add stubs to fill out the added functionality in FrameLoaderClient.
     9
     10        * WebCoreSupport/WebFrameLoaderClient.h:
     11        * WebCoreSupport/WebFrameLoaderClient.mm:
     12        (WebFrameLoaderClient::navigatorPlatform):
     13
    1142018-12-17  Ryosuke Niwa  <rniwa@webkit.org>
    215
  • trunk/Tools/ChangeLog

    r239380 r239384  
     12018-12-19  Megan Gardner  <megan_gardner@apple.com>
     2
     3        Allow clients to set the navigator platform
     4        https://bugs.webkit.org/show_bug.cgi?id=192735
     5
     6        Reviewed by Tim Horton.
     7
     8        Add a test for setting a custom navigator platform.
     9
     10        * TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
     11        (-[CustomNavigatorPlatformDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]):
     12        (-[CustomNavigatorPlatformDelegate webView:didFinishNavigation:]):
     13
    1142018-12-19  Truitt Savell  <tsavell@apple.com>
    215
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm

    r239210 r239384  
    984984}
    985985
     986@interface CustomNavigatorPlatformDelegate : NSObject <WKNavigationDelegate> {
     987}
     988@end
     989
     990@implementation CustomNavigatorPlatformDelegate
     991
     992- (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction userInfo:(id <NSSecureCoding>)userInfo decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
     993{
     994    _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
     995    if (navigationAction.targetFrame.mainFrame)
     996        [websitePolicies setCustomNavigatorPlatform:@"Test Custom Platform"];
     997
     998    decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
     999}
     1000
     1001- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
     1002{
     1003    finishedNavigation = true;
     1004}
     1005
     1006@end
     1007
     1008TEST(WebKit, WebsitePoliciesCustomNavigatorPlatform)
     1009{
     1010    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     1011   
     1012    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     1013
     1014    auto delegate = adoptNS([[CustomNavigatorPlatformDelegate alloc] init]);
     1015    [webView setNavigationDelegate:delegate.get()];
     1016   
     1017    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"data:text/html,hello"]];
     1018    [webView loadRequest:request];
     1019   
     1020    TestWebKitAPI::Util::run(&finishedNavigation);
     1021    finishedNavigation = false;
     1022
     1023    EXPECT_STREQ("Test Custom Platform", [[webView stringByEvaluatingJavaScript:@"navigator.platform"] UTF8String]);
     1024}
     1025
     1026
    9861027@interface PopUpPoliciesDelegate : NSObject <WKNavigationDelegate, WKUIDelegatePrivate>
    9871028@property (nonatomic, copy) _WKWebsitePopUpPolicy(^popUpPolicyForURL)(NSURL *);
Note: See TracChangeset for help on using the changeset viewer.