WebKit Bugzilla
Attachment 360088 Details for
Bug 193762
: Need a mechanism to override navigator.userAgent
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Adds a mechanism
bug-193762-20190125000019.patch (text/plain), 16.14 KB, created by
Ryosuke Niwa
on 2019-01-25 00:00:20 PST
(
hide
)
Description:
Adds a mechanism
Filename:
MIME Type:
Creator:
Ryosuke Niwa
Created:
2019-01-25 00:00:20 PST
Size:
16.14 KB
patch
obsolete
>Index: Source/WebCore/ChangeLog >=================================================================== >--- Source/WebCore/ChangeLog (revision 240470) >+++ Source/WebCore/ChangeLog (working copy) >@@ -1,3 +1,22 @@ >+2019-01-25 Ryosuke Niwa <rniwa@webkit.org> >+ >+ Need a mechanism to override navigator.userAgent >+ https://bugs.webkit.org/show_bug.cgi?id=193762 >+ <rdar://problem/47504939> >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ 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): >+ > 2019-01-24 Zalan Bujtas <zalan@apple.com> > > [LFC][BFC][MarginCollapsing] Refactor MarginCollapse::updateCollapsedMarginAfter >Index: Source/WebCore/loader/DocumentLoader.h >=================================================================== >--- Source/WebCore/loader/DocumentLoader.h (revision 240470) >+++ Source/WebCore/loader/DocumentLoader.h (working copy) >@@ -270,7 +270,10 @@ public: > > void setCustomUserAgent(const String& customUserAgent) { m_customUserAgent = customUserAgent; } > const String& customUserAgent() const { return m_customUserAgent; } >- >+ >+ void setCustomJavaScriptUserAgent(const String& customJavaScriptUserAgent) { m_customJavaScriptUserAgent = customJavaScriptUserAgent; } >+ const String& customJavaScriptUserAgent() const { return m_customJavaScriptUserAgent; } >+ > void setCustomNavigatorPlatform(const String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; } > const String& customNavigatorPlatform() const { return m_customNavigatorPlatform; } > >@@ -543,6 +546,7 @@ private: > HashMap<String, Vector<std::pair<String, uint32_t>>> m_pendingContentExtensionDisplayNoneSelectors; > #endif > String m_customUserAgent; >+ String m_customJavaScriptUserAgent; > String m_customNavigatorPlatform; > bool m_userContentExtensionsEnabled { true }; > bool m_deviceOrientationEventEnabled { true }; >Index: Source/WebCore/loader/FrameLoader.cpp >=================================================================== >--- Source/WebCore/loader/FrameLoader.cpp (revision 240470) >+++ Source/WebCore/loader/FrameLoader.cpp (working copy) >@@ -2705,6 +2705,20 @@ String FrameLoader::userAgent(const URL& > > return m_client.userAgent(url); > } >+ >+String FrameLoader::userAgentForJavaScript(const URL& url) const >+{ >+ if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) { >+ auto& customJavaScriptUserAgent = documentLoader->customJavaScriptUserAgent(); >+ if (!customJavaScriptUserAgent.isEmpty()) >+ return customJavaScriptUserAgent; >+ auto& customUserAgent = documentLoader->customUserAgent(); >+ if (!customUserAgent.isEmpty()) >+ return customUserAgent; >+ } >+ >+ return m_client.userAgent(url); >+} > > String FrameLoader::navigatorPlatform() const > { >Index: Source/WebCore/loader/FrameLoader.h >=================================================================== >--- Source/WebCore/loader/FrameLoader.h (revision 240470) >+++ Source/WebCore/loader/FrameLoader.h (working copy) >@@ -234,6 +234,7 @@ public: > > void dispatchOnloadEvents(); > String userAgent(const URL&) const; >+ String userAgentForJavaScript(const URL&) const; > String navigatorPlatform() const; > > void dispatchDidClearWindowObjectInWorld(DOMWrapperWorld&); >Index: Source/WebCore/page/Navigator.cpp >=================================================================== >--- Source/WebCore/page/Navigator.cpp (revision 240470) >+++ Source/WebCore/page/Navigator.cpp (working copy) >@@ -92,7 +92,7 @@ const String& Navigator::userAgent() con > if (RuntimeEnabledFeatures::sharedFeatures().webAPIStatisticsEnabled()) > ResourceLoadObserver::shared().logNavigatorAPIAccessed(*frame->document(), ResourceLoadStatistics::NavigatorAPI::UserAgent); > if (m_userAgent.isNull()) >- m_userAgent = frame->loader().userAgent(frame->document()->url()); >+ m_userAgent = frame->loader().userAgentForJavaScript(frame->document()->url()); > return m_userAgent; > } > >Index: Source/WebKit/ChangeLog >=================================================================== >--- Source/WebKit/ChangeLog (revision 240470) >+++ Source/WebKit/ChangeLog (working copy) >@@ -1,3 +1,27 @@ >+2019-01-25 Ryosuke Niwa <rniwa@webkit.org> >+ >+ Need a mechanism to override navigator.userAgent >+ https://bugs.webkit.org/show_bug.cgi?id=193762 >+ <rdar://problem/47504939> >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ 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. >+ >+ * 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]): >+ > 2019-01-24 Ryosuke Niwa <rniwa@webkit.org> > > iOS: Split keyboard should not shrink visualViewport.height >Index: Source/WebKit/Shared/WebsitePoliciesData.cpp >=================================================================== >--- Source/WebKit/Shared/WebsitePoliciesData.cpp (revision 240470) >+++ Source/WebKit/Shared/WebsitePoliciesData.cpp (working copy) >@@ -44,6 +44,7 @@ void WebsitePoliciesData::encode(IPC::En > encoder << popUpPolicy; > encoder << websiteDataStoreParameters; > encoder << customUserAgent; >+ encoder << customJavaScriptUserAgent; > encoder << customNavigatorPlatform; > } > >@@ -89,6 +90,11 @@ Optional<WebsitePoliciesData> WebsitePol > if (!customUserAgent) > return WTF::nullopt; > >+ Optional<String> customJavaScriptUserAgent; >+ decoder >> customJavaScriptUserAgent; >+ if (!customJavaScriptUserAgent) >+ return WTF::nullopt; >+ > Optional<String> customNavigatorPlatform; > decoder >> customNavigatorPlatform; > if (!customNavigatorPlatform) >@@ -103,6 +109,7 @@ Optional<WebsitePoliciesData> WebsitePol > WTFMove(*popUpPolicy), > WTFMove(*websiteDataStoreParameters), > WTFMove(*customUserAgent), >+ WTFMove(*customJavaScriptUserAgent), > WTFMove(*customNavigatorPlatform), > } }; > } >@@ -111,6 +118,7 @@ void WebsitePoliciesData::applyToDocumen > { > documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields)); > documentLoader.setCustomUserAgent(websitePolicies.customUserAgent); >+ documentLoader.setCustomJavaScriptUserAgent(websitePolicies.customJavaScriptUserAgent); > documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform); > documentLoader.setDeviceOrientationEventEnabled(websitePolicies.deviceOrientationEventEnabled); > >Index: Source/WebKit/Shared/WebsitePoliciesData.h >=================================================================== >--- Source/WebKit/Shared/WebsitePoliciesData.h (revision 240470) >+++ Source/WebKit/Shared/WebsitePoliciesData.h (working copy) >@@ -54,8 +54,9 @@ struct WebsitePoliciesData { > WebsitePopUpPolicy popUpPolicy { WebsitePopUpPolicy::Default }; > Optional<WebsiteDataStoreParameters> websiteDataStoreParameters; > String customUserAgent; >+ String customJavaScriptUserAgent; > String customNavigatorPlatform; >- >+ > void encode(IPC::Encoder&) const; > static Optional<WebsitePoliciesData> decode(IPC::Decoder&); > }; >Index: Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp >=================================================================== >--- Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp (revision 240470) >+++ Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp (working copy) >@@ -56,7 +56,8 @@ WebKit::WebsitePoliciesData WebsitePolic > Optional<WebKit::WebsiteDataStoreParameters> parameters; > if (m_websiteDataStore) > parameters = m_websiteDataStore->websiteDataStore().parameters(); >- return { contentBlockersEnabled(), deviceOrientationEventEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customNavigatorPlatform }; >+ return { contentBlockersEnabled(), deviceOrientationEventEnabled(), allowedAutoplayQuirks(), autoplayPolicy(), >+ customHeaderFields(), popUpPolicy(), WTFMove(parameters), m_customUserAgent, m_customJavaScriptUserAgent, m_customNavigatorPlatform }; > } > > } >Index: Source/WebKit/UIProcess/API/APIWebsitePolicies.h >=================================================================== >--- Source/WebKit/UIProcess/API/APIWebsitePolicies.h (revision 240470) >+++ Source/WebKit/UIProcess/API/APIWebsitePolicies.h (working copy) >@@ -74,7 +74,10 @@ public: > > void setCustomUserAgent(const WTF::String& customUserAgent) { m_customUserAgent = customUserAgent; } > const WTF::String& customUserAgent() const { return m_customUserAgent; } >- >+ >+ void setCustomJavaScriptUserAgent(const WTF::String& customJavaScriptUserAgent) { m_customJavaScriptUserAgent = customJavaScriptUserAgent; } >+ const WTF::String& customJavaScriptUserAgent() const { return m_customJavaScriptUserAgent; } >+ > void setCustomNavigatorPlatform(const WTF::String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; } > const WTF::String& customNavigatorPlatform() const { return m_customNavigatorPlatform; } > >@@ -89,6 +92,7 @@ private: > WebKit::WebsitePopUpPolicy m_popUpPolicy { WebKit::WebsitePopUpPolicy::Default }; > RefPtr<WebsiteDataStore> m_websiteDataStore; > WTF::String m_customUserAgent; >+ WTF::String m_customJavaScriptUserAgent; > WTF::String m_customNavigatorPlatform; > }; > >Index: Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h >=================================================================== >--- Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h (revision 240470) >+++ Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h (working copy) >@@ -59,6 +59,7 @@ WK_CLASS_AVAILABLE(macosx(10.12.3), ios( > @property (nonatomic) _WKWebsitePopUpPolicy popUpPolicy WK_API_AVAILABLE(macosx(10.14), ios(12.0)); > @property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore WK_API_AVAILABLE(macosx(10.13.4), ios(11.3)); > @property (nonatomic, copy) NSString *customUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA)); >+@property (nonatomic, copy) NSString *customJavaScriptUserAgent WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA)); > @property (nonatomic, copy) NSString *customNavigatorPlatform WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA)); > @property (nonatomic) BOOL deviceOrientationEventEnabled WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA)); > >Index: Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm >=================================================================== >--- Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm (revision 240470) >+++ Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.mm (working copy) >@@ -209,6 +209,16 @@ - (NSString *)customUserAgent > return _websitePolicies->customUserAgent(); > } > >+- (void)setCustomJavaScriptUserAgent:(NSString *)customUserAgent >+{ >+ _websitePolicies->setCustomJavaScriptUserAgent(customUserAgent); >+} >+ >+- (NSString *)customJavaScriptUserAgent >+{ >+ return _websitePolicies->customJavaScriptUserAgent(); >+} >+ > - (void)setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform > { > _websitePolicies->setCustomNavigatorPlatform(customNavigatorPlatform); >Index: Tools/ChangeLog >=================================================================== >--- Tools/ChangeLog (revision 240470) >+++ Tools/ChangeLog (working copy) >@@ -1,3 +1,17 @@ >+2019-01-25 Ryosuke Niwa <rniwa@webkit.org> >+ >+ Need a mechanism to override navigator.userAgent >+ https://bugs.webkit.org/show_bug.cgi?id=193762 >+ <rdar://problem/47504939> >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ Added a test for _WKWebsitePolicies.customJavaScriptUserAgent. >+ >+ * TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm: >+ (-[CustomJavaScriptUserAgentDelegate _webView:decidePolicyForNavigationAction:userInfo:decisionHandler:]): >+ (-[CustomJavaScriptUserAgentDelegate webView:didFinishNavigation:]): >+ > 2019-01-24 Ryan Haddad <ryanhaddad@apple.com> > > Update macOS JSC bot configurations >Index: Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm >=================================================================== >--- Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm (revision 240470) >+++ Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm (working copy) >@@ -1046,6 +1046,9 @@ onload = () => { > fetch("test://www.webkit.org/fetchResource.html"); > }, 0); > } >+onmessage = (event) => { >+ window.subframeUserAgent = event.data; >+} > </script> > )TESTRESOURCE"; > >@@ -1058,6 +1061,7 @@ onload = () => { > setTimeout(() => { > fetch("test://www.apple.com/fetchResource.html"); > }, 0); >+ top.postMessage(navigator.userAgent, '*'); > } > </script> > )TESTRESOURCE"; >@@ -1100,6 +1104,61 @@ TEST(WebKit, WebsitePoliciesCustomUserAg > loadCount = 0; > } > >+@interface CustomJavaScriptUserAgentDelegate : NSObject <WKNavigationDelegate> { >+} >+@end >+ >+@implementation CustomJavaScriptUserAgentDelegate >+ >+- (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction userInfo:(id <NSSecureCoding>)userInfo decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler >+{ >+ _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease]; >+ if (navigationAction.targetFrame.mainFrame) { >+ [websitePolicies setCustomJavaScriptUserAgent:@"Foo Custom JavaScript UserAgent"]; >+ [websitePolicies setCustomUserAgent:@"Foo Custom Request UserAgent"]; >+ } >+ >+ decisionHandler(WKNavigationActionPolicyAllow, websitePolicies); >+} >+ >+- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation >+{ >+ finishedNavigation = true; >+} >+ >+@end >+ >+TEST(WebKit, WebsitePoliciesCustomJavaScriptUserAgent) >+{ >+ auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]); >+ >+ auto schemeHandler = adoptNS([[DataMappingSchemeHandler alloc] init]); >+ [schemeHandler addMappingFromURLString:@"test://www.webkit.org/main.html" toData:customUserAgentMainFrameTestBytes]; >+ [schemeHandler addMappingFromURLString:@"test://www.apple.com/subframe.html" toData:customUserAgentSubFrameTestBytes]; >+ [schemeHandler setTaskHandler:[](id <WKURLSchemeTask> task) { >+ EXPECT_STREQ("Foo Custom Request UserAgent", [[task.request valueForHTTPHeaderField:@"User-Agent"] UTF8String]); >+ }]; >+ [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"]; >+ >+ auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); >+ >+ auto delegate = adoptNS([[CustomJavaScriptUserAgentDelegate alloc] init]); >+ [webView setNavigationDelegate:delegate.get()]; >+ >+ loadCount = 0; >+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test://www.webkit.org/main.html"]]; >+ [webView loadRequest:request]; >+ >+ TestWebKitAPI::Util::run(&finishedNavigation); >+ finishedNavigation = false; >+ >+ while (loadCount != 9U) >+ TestWebKitAPI::Util::spinRunLoop(); >+ >+ EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"navigator.userAgent"] UTF8String]); >+ EXPECT_STREQ("Foo Custom JavaScript UserAgent", [[webView stringByEvaluatingJavaScript:@"subframeUserAgent"] UTF8String]); >+} >+ > @interface CustomNavigatorPlatformDelegate : NSObject <WKNavigationDelegate> { > } > @end
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 193762
:
359993
|
360088
|
360217
|
360223