Changeset 246236 in webkit


Ignore:
Timestamp:
Jun 8, 2019 8:55:01 PM (5 years ago)
Author:
Wenson Hsieh
Message:

[iOS] The default application name for user agent when requesting desktop content is still "Mobile/15E148"
https://bugs.webkit.org/show_bug.cgi?id=198693
<rdar://problem/51555371>

Reviewed by Tim Horton.

Source/WebKit:

Fixes the bug by introducing an applicationNameForDesktopUserAgent member to WebPageProxy. This
applicationNameForDesktopUserAgent is set alongside the regular applicationNameForUserAgent; however, in the
case where it is being set via WKWebViewConfiguration, use WKWebViewConfiguration's
_applicationNameForDesktopUserAgent instead. See below for more details.

Tested by adding to the existing API test PreferredContentMode.ApplicationNameForDesktopUserAgent.

  • UIProcess/API/APIWebsitePolicies.cpp:

(API::WebsitePolicies::copy const):

  • UIProcess/API/APIWebsitePolicies.h:

Drive-by fix: rename applicationNameForUserAgentWithModernCompatibility to applicationNameForDesktopUserAgent in
a few places.

  • UIProcess/API/Cocoa/WKBrowsingContextController.mm:

(-[WKBrowsingContextController setApplicationNameForUserAgent:]):

  • UIProcess/API/Cocoa/WKWebView.mm:

(-[WKWebView _initializeWithConfiguration:]):
(-[WKWebView _setApplicationNameForUserAgent:]):

  • UIProcess/API/Cocoa/WKWebViewConfiguration.mm:

Change _applicationNameForUserAgent into an Optional<RetainPtr<NSString>>. A value of WTF::nullopt allows us to
know that the default application name should be used (which is different when requesting desktop content).

(-[WKWebViewConfiguration encodeWithCoder:]):
(-[WKWebViewConfiguration initWithCoder:]):
(-[WKWebViewConfiguration copyWithZone:]):
(-[WKWebViewConfiguration _applicationNameForDesktopUserAgent]):

Add _applicationNameForDesktopUserAgent to the web view configuration. This behaves like
applicationNameForUserAgent, with one exception: in the case where the default applicationNameForUserAgent is
used (i.e. the client has not specified any application name), default to nil as the application name for user
agent, rather than "Mobile/15E148".

(-[WKWebViewConfiguration applicationNameForUserAgent]):
(-[WKWebViewConfiguration setApplicationNameForUserAgent:]):

  • UIProcess/API/Cocoa/WKWebViewConfigurationInternal.h:
  • UIProcess/API/Cocoa/WKWebpagePreferences.mm:

(-[WKWebpagePreferences _applicationNameForUserAgentWithModernCompatibility]):
(-[WKWebpagePreferences _setApplicationNameForUserAgentWithModernCompatibility:]):

  • UIProcess/WebPageProxy.h:

(WebKit::WebPageProxy::setApplicationNameForDesktopUserAgent):
(WebKit::WebPageProxy::applicationNameForDesktopUserAgent const):

  • UIProcess/ios/WebPageProxyIOS.mm:

(WebKit::WebPageProxy::effectiveContentModeAfterAdjustingPolicies):

Tools:

Augment an existing API test to exercise three additional scenarios:

  • The web view configuration's application name is never set; the default should not include

"Mobile/15E148" in desktop mode.

  • The application name is explicitly set to nil; the default application name should still not include

"Mobile/15E148".

  • The application name is explicitly set to the default value, "Mobile/15E148". WKWebView should honor

this when generating the user agent, even in desktop mode.

  • TestWebKitAPI/ios/PreferredContentMode.mm:

(TestWebKitAPI::setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate):
(TestWebKitAPI::setUpWebViewForPreferredContentModeTesting):
(TestWebKitAPI::TEST):

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r246235 r246236  
     12019-06-08  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] The default application name for user agent when requesting desktop content is still "Mobile/15E148"
     4        https://bugs.webkit.org/show_bug.cgi?id=198693
     5        <rdar://problem/51555371>
     6
     7        Reviewed by Tim Horton.
     8
     9        Fixes the bug by introducing an applicationNameForDesktopUserAgent member to WebPageProxy. This
     10        applicationNameForDesktopUserAgent is set alongside the regular applicationNameForUserAgent; however, in the
     11        case where it is being set via WKWebViewConfiguration, use WKWebViewConfiguration's
     12        _applicationNameForDesktopUserAgent instead. See below for more details.
     13
     14        Tested by adding to the existing API test PreferredContentMode.ApplicationNameForDesktopUserAgent.
     15
     16        * UIProcess/API/APIWebsitePolicies.cpp:
     17        (API::WebsitePolicies::copy const):
     18        * UIProcess/API/APIWebsitePolicies.h:
     19
     20        Drive-by fix: rename applicationNameForUserAgentWithModernCompatibility to applicationNameForDesktopUserAgent in
     21        a few places.
     22
     23        * UIProcess/API/Cocoa/WKBrowsingContextController.mm:
     24        (-[WKBrowsingContextController setApplicationNameForUserAgent:]):
     25        * UIProcess/API/Cocoa/WKWebView.mm:
     26        (-[WKWebView _initializeWithConfiguration:]):
     27        (-[WKWebView _setApplicationNameForUserAgent:]):
     28        * UIProcess/API/Cocoa/WKWebViewConfiguration.mm:
     29
     30        Change _applicationNameForUserAgent into an Optional<RetainPtr<NSString>>. A value of WTF::nullopt allows us to
     31        know that the default application name should be used (which is different when requesting desktop content).
     32
     33        (-[WKWebViewConfiguration encodeWithCoder:]):
     34        (-[WKWebViewConfiguration initWithCoder:]):
     35        (-[WKWebViewConfiguration copyWithZone:]):
     36        (-[WKWebViewConfiguration _applicationNameForDesktopUserAgent]):
     37
     38        Add _applicationNameForDesktopUserAgent to the web view configuration. This behaves like
     39        applicationNameForUserAgent, with one exception: in the case where the default applicationNameForUserAgent is
     40        used (i.e. the client has not specified any application name), default to `nil` as the application name for user
     41        agent, rather than "Mobile/15E148".
     42
     43        (-[WKWebViewConfiguration applicationNameForUserAgent]):
     44        (-[WKWebViewConfiguration setApplicationNameForUserAgent:]):
     45        * UIProcess/API/Cocoa/WKWebViewConfigurationInternal.h:
     46        * UIProcess/API/Cocoa/WKWebpagePreferences.mm:
     47        (-[WKWebpagePreferences _applicationNameForUserAgentWithModernCompatibility]):
     48        (-[WKWebpagePreferences _setApplicationNameForUserAgentWithModernCompatibility:]):
     49        * UIProcess/WebPageProxy.h:
     50        (WebKit::WebPageProxy::setApplicationNameForDesktopUserAgent):
     51        (WebKit::WebPageProxy::applicationNameForDesktopUserAgent const):
     52        * UIProcess/ios/WebPageProxyIOS.mm:
     53        (WebKit::WebPageProxy::effectiveContentModeAfterAdjustingPolicies):
     54
    1552019-06-08  Andy Estes  <aestes@apple.com>
    256
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp

    r246118 r246236  
    7676    policies->setCustomHeaderFields(WTFMove(customHeaderFields));
    7777    policies->setAllowSiteSpecificQuirksToOverrideContentMode(m_allowSiteSpecificQuirksToOverrideContentMode);
    78     policies->setApplicationNameForUserAgentWithModernCompatibility(m_applicationNameForUserAgentWithModernCompatibility);
     78    policies->setApplicationNameForDesktopUserAgent(m_applicationNameForDesktopUserAgent);
    7979    return policies;
    8080}
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h

    r246118 r246236  
    112112    void setAllowSiteSpecificQuirksToOverrideContentMode(bool value) { m_allowSiteSpecificQuirksToOverrideContentMode = value; }
    113113
    114     WTF::String applicationNameForUserAgentWithModernCompatibility() const { return m_applicationNameForUserAgentWithModernCompatibility; }
    115     void setApplicationNameForUserAgentWithModernCompatibility(const WTF::String& applicationName) { m_applicationNameForUserAgentWithModernCompatibility = applicationName; }
     114    WTF::String applicationNameForDesktopUserAgent() const { return m_applicationNameForDesktopUserAgent; }
     115    void setApplicationNameForDesktopUserAgent(const WTF::String& applicationName) { m_applicationNameForDesktopUserAgent = applicationName; }
    116116
    117117private:
     
    137137    WebKit::WebsiteLegacyOverflowScrollingTouchPolicy m_legacyOverflowScrollingTouchPolicy { WebKit::WebsiteLegacyOverflowScrollingTouchPolicy::Default };
    138138    bool m_allowSiteSpecificQuirksToOverrideContentMode { false };
    139     WTF::String m_applicationNameForUserAgentWithModernCompatibility;
     139    WTF::String m_applicationNameForDesktopUserAgent;
    140140};
    141141
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKBrowsingContextController.mm

    r242339 r246236  
    200200- (void)setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
    201201{
     202    _page->setApplicationNameForDesktopUserAgent(applicationNameForUserAgent);
    202203    _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
    203204}
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm

    r246230 r246236  
    749749        _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
    750750
     751    _page->setApplicationNameForDesktopUserAgent(configuration._applicationNameForDesktopUserAgent);
     752
    751753    _navigationState = std::make_unique<WebKit::NavigationState>(self);
    752754    _page->setNavigationClient(_navigationState->createNavigationClient());
     
    47834785{
    47844786    _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
     4787    _page->setApplicationNameForDesktopUserAgent(applicationNameForUserAgent);
    47854788}
    47864789
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewConfiguration.mm

    r245491 r246236  
    123123    WeakObjCPtr<WKWebView> _alternateWebViewForNavigationGestures;
    124124    RetainPtr<NSString> _groupIdentifier;
    125     LazyInitialized<RetainPtr<NSString>> _applicationNameForUserAgent;
     125    Optional<RetainPtr<NSString>> _applicationNameForUserAgent;
    126126    NSTimeInterval _incrementalRenderingSuppressionTimeout;
    127127    BOOL _respectsImageOrientation;
     
    287287
    288288    [coder encodeBool:self.suppressesIncrementalRendering forKey:@"suppressesIncrementalRendering"];
    289     [coder encodeObject:self.applicationNameForUserAgent forKey:@"applicationNameForUserAgent"];
     289
     290    if (_applicationNameForUserAgent.hasValue())
     291        [coder encodeObject:self.applicationNameForUserAgent forKey:@"applicationNameForUserAgent"];
     292
    290293    [coder encodeBool:self.allowsAirPlayForMediaPlayback forKey:@"allowsAirPlayForMediaPlayback"];
    291294
     
    321324
    322325    self.suppressesIncrementalRendering = [coder decodeBoolForKey:@"suppressesIncrementalRendering"];
    323     self.applicationNameForUserAgent = decodeObjectOfClassForKeyFromCoder([NSString class], @"applicationNameForUserAgent", coder);
     326
     327    if ([coder containsValueForKey:@"applicationNameForUserAgent"])
     328        self.applicationNameForUserAgent = decodeObjectOfClassForKeyFromCoder([NSString class], @"applicationNameForUserAgent", coder);
     329
    324330    self.allowsAirPlayForMediaPlayback = [coder decodeBoolForKey:@"allowsAirPlayForMediaPlayback"];
    325331
     
    367373
    368374    configuration->_suppressesIncrementalRendering = self->_suppressesIncrementalRendering;
    369     configuration.applicationNameForUserAgent = self.applicationNameForUserAgent;
     375    configuration->_applicationNameForUserAgent = self->_applicationNameForUserAgent;
    370376
    371377    configuration->_respectsImageOrientation = self->_respectsImageOrientation;
     
    496502}
    497503
     504- (NSString *)_applicationNameForDesktopUserAgent
     505{
     506    return _applicationNameForUserAgent.valueOr(nil).get();
     507}
     508
    498509- (NSString *)applicationNameForUserAgent
    499510{
    500     return _applicationNameForUserAgent.get([] { return defaultApplicationNameForUserAgent(); });
     511    return _applicationNameForUserAgent.valueOr(defaultApplicationNameForUserAgent()).get();
    501512}
    502513
    503514- (void)setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
    504515{
    505     _applicationNameForUserAgent.set(adoptNS([applicationNameForUserAgent copy]));
     516    _applicationNameForUserAgent.emplace(adoptNS(applicationNameForUserAgent.copy));
    506517}
    507518
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewConfigurationInternal.h

    r242339 r246236  
    3636@property (nonatomic, setter=_setContentProviderRegistry:) WKWebViewContentProviderRegistry *_contentProviderRegistry;
    3737#endif
     38@property (nonatomic, readonly) NSString *_applicationNameForDesktopUserAgent;
    3839
    3940- (Ref<API::PageConfiguration>)copyPageConfiguration;
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferences.mm

    r246118 r246236  
    319319- (NSString *)_applicationNameForUserAgentWithModernCompatibility
    320320{
    321     return _websitePolicies->applicationNameForUserAgentWithModernCompatibility();
     321    return _websitePolicies->applicationNameForDesktopUserAgent();
    322322}
    323323
    324324- (void)_setApplicationNameForUserAgentWithModernCompatibility:(NSString *)applicationName
    325325{
    326     _websitePolicies->setApplicationNameForUserAgentWithModernCompatibility(applicationName);
     326    _websitePolicies->setApplicationNameForDesktopUserAgent(applicationName);
    327327}
    328328
  • trunk/Source/WebKit/UIProcess/WebPageProxy.h

    r246235 r246236  
    856856    void setApplicationNameForUserAgent(const String&);
    857857    const String& applicationNameForUserAgent() const { return m_applicationNameForUserAgent; }
     858    void setApplicationNameForDesktopUserAgent(const String& applicationName) { m_applicationNameForDesktopUserAgent = applicationName; }
     859    const String& applicationNameForDesktopUserAgent() const { return m_applicationNameForDesktopUserAgent; }
    858860    void setCustomUserAgent(const String&);
    859861    const String& customUserAgent() const { return m_customUserAgent; }
     
    21462148    String m_userAgent;
    21472149    String m_applicationNameForUserAgent;
     2150    String m_applicationNameForDesktopUserAgent;
    21482151    String m_customUserAgent;
    21492152    String m_customTextEncodingName;
  • trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm

    r246213 r246236  
    13831383
    13841384    if (policies.customUserAgent().isEmpty() && customUserAgent().isEmpty()) {
    1385         auto applicationName = policies.applicationNameForUserAgentWithModernCompatibility();
     1385        auto applicationName = policies.applicationNameForDesktopUserAgent();
    13861386        if (applicationName.isEmpty())
    1387             applicationName = applicationNameForUserAgent();
     1387            applicationName = applicationNameForDesktopUserAgent();
    13881388        policies.setCustomUserAgent(standardUserAgentWithApplicationName(applicationName, UserAgentType::Desktop));
    13891389    }
  • trunk/Tools/ChangeLog

    r246233 r246236  
     12019-06-08  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] The default application name for user agent when requesting desktop content is still "Mobile/15E148"
     4        https://bugs.webkit.org/show_bug.cgi?id=198693
     5        <rdar://problem/51555371>
     6
     7        Reviewed by Tim Horton.
     8
     9        Augment an existing API test to exercise three additional scenarios:
     10            -   The web view configuration's application name is never set; the default should not include
     11                "Mobile/15E148" in desktop mode.
     12            -   The application name is explicitly set to nil; the default application name should still not include
     13                "Mobile/15E148".
     14            -   The application name is explicitly set to the default value, "Mobile/15E148". WKWebView should honor
     15                this when generating the user agent, even in desktop mode.
     16
     17        * TestWebKitAPI/ios/PreferredContentMode.mm:
     18        (TestWebKitAPI::setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate):
     19        (TestWebKitAPI::setUpWebViewForPreferredContentModeTesting):
     20        (TestWebKitAPI::TEST):
     21
    1222019-06-08  Zalan Bujtas  <zalan@apple.com>
    223
  • trunk/Tools/TestWebKitAPI/ios/PreferredContentMode.mm

    r246118 r246236  
    186186
    187187template <typename ViewClass>
    188 RetainPtr<ViewClass> setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate(Optional<WKContentMode> defaultContentMode = { }, const String& applicationNameForUserAgent = "TestWebKitAPI", CGSize size = CGSizeMake(1024, 768))
     188RetainPtr<ViewClass> setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate(Optional<WKContentMode> defaultContentMode = { }, Optional<String> applicationNameForUserAgent = { "TestWebKitAPI" }, CGSize size = CGSizeMake(1024, 768))
    189189{
    190190    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
    191191    if (defaultContentMode)
    192192        [configuration setDefaultWebpagePreferences:[WKWebpagePreferences preferencesWithContentMode:defaultContentMode.value()]];
    193     if (!!applicationNameForUserAgent)
    194         [configuration setApplicationNameForUserAgent:applicationNameForUserAgent];
     193    if (applicationNameForUserAgent)
     194        [configuration setApplicationNameForUserAgent:applicationNameForUserAgent->isNull() ? nil : (NSString *)applicationNameForUserAgent.value()];
    195195    auto webView = adoptNS([[ViewClass alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height) configuration:configuration.get()]);
    196196    EXPECT_TRUE([webView isKindOfClass:WKWebView.class]);
     
    199199
    200200template <typename ViewClass>
    201 std::pair<RetainPtr<ViewClass>, RetainPtr<ContentModeNavigationDelegate>> setUpWebViewForPreferredContentModeTesting(Optional<WKContentMode> defaultContentMode = { }, const String& applicationNameForUserAgent = "TestWebKitAPI", CGSize size = CGSizeMake(1024, 768))
     201std::pair<RetainPtr<ViewClass>, RetainPtr<ContentModeNavigationDelegate>> setUpWebViewForPreferredContentModeTesting(Optional<WKContentMode> defaultContentMode = { }, Optional<String> applicationNameForUserAgent = { "TestWebKitAPI" }, CGSize size = CGSizeMake(1024, 768))
    202202{
    203203    auto webView = setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate<ViewClass>(defaultContentMode, applicationNameForUserAgent, size);
     
    248248    IPadUserInterfaceSwizzler iPadUserInterface;
    249249
    250     auto webView = setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate<TestWKWebView>(WKContentModeDesktop, "TestWebKitAPI");
     250    auto webView = setUpWebViewForPreferredContentModeTestingWithoutNavigationDelegate<TestWKWebView>(WKContentModeDesktop);
    251251    [webView loadHTMLString:@"<body>Hello world</body>" baseURL:nil];
    252252
     
    403403    RetainPtr<TestWKWebView> webView;
    404404    RetainPtr<ContentModeNavigationDelegate> delegate;
    405     std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>({ }, "TestWebKitAPI", CGSizeZero);
     405    std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>({ }, { "TestWebKitAPI" }, CGSizeZero);
    406406    [webView loadTestPageNamed:@"simple" andExpectEffectiveContentMode:WKContentModeDesktop withPolicyDecisionHandler:nil];
    407407    [[webView navigatorUserAgent] shouldContainStrings:@"Mozilla/5.0 (Macintosh", @"TestWebKitAPI", nil];
     
    429429    [[webView navigatorUserAgent] shouldContainStrings:@"Mozilla/5.0 (Macintosh", @"DesktopBrowser", nil];
    430430    EXPECT_WK_STREQ("MacIntel", [webView navigatorPlatform]);
     431
     432    {
     433        // Don't attempt to change the application name, but still opt into desktop-class browsing;
     434        // the application name should not default to "Mobile/15E148".
     435        std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>(WKContentModeDesktop, { });
     436        [webView loadTestPageNamed:@"simple" withPolicyDecisionHandler:nil];
     437        NSString *userAgent = [webView navigatorUserAgent];
     438        EXPECT_FALSE([userAgent containsString:@"Mobile"]);
     439        EXPECT_TRUE([userAgent containsString:@"Macintosh"]);
     440    }
     441    {
     442        // Don't attempt to change the application name, but this time, opt into mobile content. The application
     443        // name should default to "Mobile/15E148".
     444        std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>(WKContentModeMobile, { });
     445        [webView loadTestPageNamed:@"simple" withPolicyDecisionHandler:nil];
     446        NSString *userAgent = [webView navigatorUserAgent];
     447        EXPECT_TRUE([userAgent containsString:@"Mobile"]);
     448        EXPECT_FALSE([userAgent containsString:@"Macintosh"]);
     449    }
     450    {
     451        // Manually set the application name for user agent to the default value, Mobile/15E148.
     452        std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>(WKContentModeDesktop, { "Mobile/15E148" });
     453        [webView loadTestPageNamed:@"simple" withPolicyDecisionHandler:nil];
     454        NSString *userAgent = [webView navigatorUserAgent];
     455        EXPECT_TRUE([userAgent containsString:@"Mobile"]);
     456        EXPECT_TRUE([userAgent containsString:@"Macintosh"]);
     457    }
     458    {
     459        // Manually set the application name for user agent to nil.
     460        std::tie(webView, delegate) = setUpWebViewForPreferredContentModeTesting<WKWebView>(WKContentModeDesktop, {{ }});
     461        [webView loadTestPageNamed:@"simple" withPolicyDecisionHandler:nil];
     462        NSString *userAgent = [webView navigatorUserAgent];
     463        EXPECT_FALSE([userAgent containsString:@"Mobile"]);
     464        EXPECT_TRUE([userAgent containsString:@"Macintosh"]);
     465    }
    431466}
    432467
Note: See TracChangeset for help on using the changeset viewer.