Changeset 240541 in webkit
- Timestamp:
- Jan 25, 2019 10:50:58 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r240540 r240541 1 2019-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 1 20 2019-01-25 Devin Rousso <drousso@apple.com> 2 21 -
trunk/Source/WebCore/loader/DocumentLoader.h
r239830 r240541 271 271 void setCustomUserAgent(const String& customUserAgent) { m_customUserAgent = customUserAgent; } 272 272 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 274 277 void setCustomNavigatorPlatform(const String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; } 275 278 const String& customNavigatorPlatform() const { return m_customNavigatorPlatform; } … … 544 547 #endif 545 548 String m_customUserAgent; 549 String m_customJavaScriptUserAgent; 546 550 String m_customNavigatorPlatform; 547 551 bool m_userContentExtensionsEnabled { true }; -
trunk/Source/WebCore/loader/FrameLoader.cpp
r240046 r240541 2706 2706 return m_client.userAgent(url); 2707 2707 } 2708 2709 String 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 } 2708 2722 2709 2723 String FrameLoader::navigatorPlatform() const -
trunk/Source/WebCore/loader/FrameLoader.h
r239427 r240541 235 235 void dispatchOnloadEvents(); 236 236 String userAgent(const URL&) const; 237 String userAgentForJavaScript(const URL&) const; 237 238 String navigatorPlatform() const; 238 239 -
trunk/Source/WebCore/page/Navigator.cpp
r240014 r240541 93 93 ResourceLoadObserver::shared().logNavigatorAPIAccessed(*frame->document(), ResourceLoadStatistics::NavigatorAPI::UserAgent); 94 94 if (m_userAgent.isNull()) 95 m_userAgent = frame->loader().userAgent (frame->document()->url());95 m_userAgent = frame->loader().userAgentForJavaScript(frame->document()->url()); 96 96 return m_userAgent; 97 97 } -
trunk/Source/WebKit/ChangeLog
r240540 r240541 1 2019-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 1 28 2019-01-25 Devin Rousso <drousso@apple.com> 2 29 -
trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp
r239830 r240541 45 45 encoder << websiteDataStoreParameters; 46 46 encoder << customUserAgent; 47 encoder << customJavaScriptUserAgent; 47 48 encoder << customNavigatorPlatform; 48 49 } … … 90 91 return WTF::nullopt; 91 92 93 Optional<String> customJavaScriptUserAgent; 94 decoder >> customJavaScriptUserAgent; 95 if (!customJavaScriptUserAgent) 96 return WTF::nullopt; 97 92 98 Optional<String> customNavigatorPlatform; 93 99 decoder >> customNavigatorPlatform; … … 104 110 WTFMove(*websiteDataStoreParameters), 105 111 WTFMove(*customUserAgent), 112 WTFMove(*customJavaScriptUserAgent), 106 113 WTFMove(*customNavigatorPlatform), 107 114 } }; … … 112 119 documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields)); 113 120 documentLoader.setCustomUserAgent(websitePolicies.customUserAgent); 121 documentLoader.setCustomJavaScriptUserAgent(websitePolicies.customJavaScriptUserAgent); 114 122 documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform); 115 123 documentLoader.setDeviceOrientationEventEnabled(websitePolicies.deviceOrientationEventEnabled); -
trunk/Source/WebKit/Shared/WebsitePoliciesData.h
r239639 r240541 55 55 Optional<WebsiteDataStoreParameters> websiteDataStoreParameters; 56 56 String customUserAgent; 57 String customJavaScriptUserAgent; 57 58 String customNavigatorPlatform; 58 59 59 60 void encode(IPC::Encoder&) const; 60 61 static Optional<WebsitePoliciesData> decode(IPC::Decoder&); -
trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp
r239639 r240541 57 57 if (m_websiteDataStore) 58 58 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 }; 60 61 } 61 62 -
trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h
r239639 r240541 75 75 void setCustomUserAgent(const WTF::String& customUserAgent) { m_customUserAgent = customUserAgent; } 76 76 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 78 81 void setCustomNavigatorPlatform(const WTF::String& customNavigatorPlatform) { m_customNavigatorPlatform = customNavigatorPlatform; } 79 82 const WTF::String& customNavigatorPlatform() const { return m_customNavigatorPlatform; } … … 90 93 RefPtr<WebsiteDataStore> m_websiteDataStore; 91 94 WTF::String m_customUserAgent; 95 WTF::String m_customJavaScriptUserAgent; 92 96 WTF::String m_customNavigatorPlatform; 93 97 }; -
trunk/Source/WebKit/UIProcess/API/Cocoa/_WKWebsitePolicies.h
r239830 r240541 60 60 @property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore WK_API_AVAILABLE(macosx(10.13.4), ios(11.3)); 61 61 @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)); 62 63 @property (nonatomic, copy) NSString *customNavigatorPlatform WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA)); 63 64 @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 210 210 } 211 211 212 - (void)setCustomJavaScriptUserAgent:(NSString *)customUserAgent 213 { 214 _websitePolicies->setCustomJavaScriptUserAgent(customUserAgent); 215 } 216 217 - (NSString *)customJavaScriptUserAgent 218 { 219 return _websitePolicies->customJavaScriptUserAgent(); 220 } 221 212 222 - (void)setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform 213 223 { -
trunk/Tools/ChangeLog
r240533 r240541 1 2019-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 1 15 2019-01-25 Dean Jackson <dino@apple.com> 2 16 -
trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm
r239830 r240541 1047 1047 }, 0); 1048 1048 } 1049 onmessage = (event) => { 1050 window.subframeUserAgent = event.data; 1051 } 1049 1052 </script> 1050 1053 )TESTRESOURCE"; … … 1059 1062 fetch("test://www.apple.com/fetchResource.html"); 1060 1063 }, 0); 1064 top.postMessage(navigator.userAgent, '*'); 1061 1065 } 1062 1066 </script> … … 1099 1103 EXPECT_EQ(1U, loadCount); 1100 1104 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 1132 TEST(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 1165 TEST(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]); 1101 1195 } 1102 1196
Note: See TracChangeset
for help on using the changeset viewer.