Bug 90893 - In FrameLoader, don't apply user agent if one is already set.
Summary: In FrameLoader, don't apply user agent if one is already set.
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Viet-Trung Luu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-10 10:24 PDT by Viet-Trung Luu
Modified: 2012-07-27 01:08 PDT (History)
8 users (show)

See Also:


Attachments
Pretty much the minimal patch that allows me to do what I need.... (1.42 KB, patch)
2012-07-10 10:29 PDT, Viet-Trung Luu
abarth: review-
Details | Formatted Diff | Diff
Added test. Now dependent on https://chromiumcodereview.appspot.com/10735037 . (3.71 KB, patch)
2012-07-10 13:55 PDT, Viet-Trung Luu
abarth: review+
Details | Formatted Diff | Diff
Updated ChangeLog and rolled DEPS. (4.90 KB, patch)
2012-07-10 14:41 PDT, Viet-Trung Luu
abarth: review+
abarth: commit-queue-
Details | Formatted Diff | Diff
variableNameStyleFixes. (4.86 KB, patch)
2012-07-10 15:02 PDT, Viet-Trung Luu
webkit.review.bot: commit-queue-
Details | Formatted Diff | Diff
Archive of layout-test-results from gce-cr-linux-08 (296.04 KB, application/zip)
2012-07-10 17:13 PDT, WebKit Review Bot
no flags Details
Archive of layout-test-results from gce-cr-linux-07 (570.31 KB, application/zip)
2012-07-10 17:17 PDT, WebKit Review Bot
no flags Details
Archive of layout-test-results from gce-cr-linux-05 (346.39 KB, application/zip)
2012-07-10 18:11 PDT, WebKit Review Bot
no flags Details
Archive of layout-test-results from gce-cr-linux-04 (659.14 KB, application/zip)
2012-07-10 18:11 PDT, WebKit Review Bot
no flags Details
New approach: add custom user agent to ResourceRequest, to be handled by the FrameLoaderClientImpl. (10.06 KB, patch)
2012-07-12 16:25 PDT, Viet-Trung Luu
webkit.review.bot: commit-queue-
Details | Formatted Diff | Diff
Updated to ToT. (10.08 KB, patch)
2012-07-12 18:17 PDT, Viet-Trung Luu
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Viet-Trung Luu 2012-07-10 10:24:09 PDT
This would allow things that require custom user agents (e.g., some Pepper plugins -- see https://chromiumcodereview.appspot.com/10701119 ) to set them.
Comment 1 Viet-Trung Luu 2012-07-10 10:29:26 PDT
Created attachment 151482 [details]
Pretty much the minimal patch that allows me to do what I need....
Comment 2 Adam Barth 2012-07-10 10:32:38 PDT
Comment on attachment 151482 [details]
Pretty much the minimal patch that allows me to do what I need....

This patch requires a test.  You might want to write a Chromium unit test that calls the WebKit API in a similar manner to how Pepper does.
Comment 3 Adam Barth 2012-07-10 10:33:09 PDT
http://trac.webkit.org/browser/trunk/Source/WebKit/chromium/tests has a testing framework that might be useful.
Comment 4 Viet-Trung Luu 2012-07-10 13:55:02 PDT
Created attachment 151519 [details]
Added test. Now dependent on https://chromiumcodereview.appspot.com/10735037 .
Comment 5 Adam Barth 2012-07-10 14:01:27 PDT
Comment on attachment 151519 [details]
Added test. Now dependent on https://chromiumcodereview.appspot.com/10735037 .

LGTM.  Thanks for the test.
Comment 6 Viet-Trung Luu 2012-07-10 14:41:18 PDT
Created attachment 151528 [details]
Updated ChangeLog and rolled DEPS.
Comment 7 WebKit Review Bot 2012-07-10 14:45:28 PDT
Attachment 151528 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WebCore/ChangeLog', u'Source/WebCor..." exit_code: 1
Source/WebKit/chromium/tests/AssociatedURLLoaderTest.cpp:621:  user_agent_header_name is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/tests/AssociatedURLLoaderTest.cpp:641:  sent_request is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/tests/AssociatedURLLoaderTest.cpp:642:  default_user_agent_value is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/tests/AssociatedURLLoaderTest.cpp:650:  user_agent_value is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/tests/AssociatedURLLoaderTest.cpp:663:  sent_user_agent_value is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Total errors found: 5 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 8 Adam Barth 2012-07-10 14:46:22 PDT
Comment on attachment 151528 [details]
Updated ChangeLog and rolled DEPS.

Please fix these style issues before landing.  WebKit uses camelCase for variable names.
Comment 9 Viet-Trung Luu 2012-07-10 15:02:19 PDT
Created attachment 151534 [details]
variableNameStyleFixes.
Comment 10 WebKit Review Bot 2012-07-10 17:13:13 PDT
Comment on attachment 151534 [details]
variableNameStyleFixes.

Attachment 151534 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13208148

New failing tests:
http/tests/inspector/extensions-useragent.html
Comment 11 WebKit Review Bot 2012-07-10 17:13:16 PDT
Created attachment 151559 [details]
Archive of layout-test-results from gce-cr-linux-08

The attached test failures were seen while running run-webkit-tests on the chromium-ews.
Bot: gce-cr-linux-08  Port: <class 'webkitpy.common.config.ports.ChromiumXVFBPort'>  Platform: Linux-2.6.39-gcg-201203291735-x86_64-with-Ubuntu-10.04-lucid
Comment 12 WebKit Review Bot 2012-07-10 17:17:50 PDT
Comment on attachment 151528 [details]
Updated ChangeLog and rolled DEPS.

Attachment 151528 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13166603

New failing tests:
http/tests/inspector/extensions-useragent.html
Comment 13 WebKit Review Bot 2012-07-10 17:17:54 PDT
Created attachment 151560 [details]
Archive of layout-test-results from gce-cr-linux-07

The attached test failures were seen while running run-webkit-tests on the chromium-ews.
Bot: gce-cr-linux-07  Port: <class 'webkitpy.common.config.ports.ChromiumXVFBPort'>  Platform: Linux-2.6.39-gcg-201203291735-x86_64-with-Ubuntu-10.04-lucid
Comment 14 WebKit Review Bot 2012-07-10 18:10:56 PDT
Comment on attachment 151534 [details]
variableNameStyleFixes.

Attachment 151534 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13198163

New failing tests:
http/tests/inspector/extensions-useragent.html
Comment 15 WebKit Review Bot 2012-07-10 18:11:00 PDT
Created attachment 151566 [details]
Archive of layout-test-results from gce-cr-linux-05

The attached test failures were seen while running run-webkit-tests on the chromium-ews.
Bot: gce-cr-linux-05  Port: <class 'webkitpy.common.config.ports.ChromiumXVFBPort'>  Platform: Linux-2.6.39-gcg-201203291735-x86_64-with-Ubuntu-10.04-lucid
Comment 16 WebKit Review Bot 2012-07-10 18:11:07 PDT
Comment on attachment 151528 [details]
Updated ChangeLog and rolled DEPS.

Attachment 151528 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13209166

New failing tests:
http/tests/inspector/extensions-useragent.html
Comment 17 WebKit Review Bot 2012-07-10 18:11:13 PDT
Created attachment 151567 [details]
Archive of layout-test-results from gce-cr-linux-04

The attached test failures were seen while running run-webkit-tests on the chromium-ews.
Bot: gce-cr-linux-04  Port: <class 'webkitpy.common.config.ports.ChromiumXVFBPort'>  Platform: Linux-2.6.39-gcg-201203291735-x86_64-with-Ubuntu-10.04-lucid
Comment 18 Alexey Proskuryakov 2012-07-10 23:56:12 PDT
Comment on attachment 151534 [details]
variableNameStyleFixes.

View in context: https://bugs.webkit.org/attachment.cgi?id=151534&action=review

> Source/WebCore/loader/FrameLoader.cpp:2870
>  void FrameLoader::applyUserAgent(ResourceRequest& request)
>  {
> +    if (!request.httpUserAgent().isNull())
> +        return;

This function no longer just applies user agent. You should rename it to not be misleading.
Comment 19 Viet-Trung Luu 2012-07-12 16:22:30 PDT
Ugh. The layout test failure revealed a fatal flaw with my plan: my change resulted in InspectorInstrumentation::applyUserAgentOverride() (done in FrameLoader::userAgent()) not getting a chance for reloads.

Arguably, in the long term, a more coherent way of applying user agents to requests would be desirable. But, in the short term, I'll take another approach (new patch coming up shortly).
Comment 20 Viet-Trung Luu 2012-07-12 16:25:40 PDT
Created attachment 152090 [details]
New approach: add custom user agent to ResourceRequest, to be handled by the FrameLoaderClientImpl.
Comment 21 WebKit Review Bot 2012-07-12 16:29:02 PDT
Please wait for approval from abarth@webkit.org, dglazkov@chromium.org, fishd@chromium.org, jamesr@chromium.org or tkent@chromium.org before submitting, as this patch contains changes to the Chromium public API. See also https://trac.webkit.org/wiki/ChromiumWebKitAPI.
Comment 22 Adam Barth 2012-07-12 16:30:00 PDT
Maybe the embedder should do this itself using ExtraData?
Comment 23 Viet-Trung Luu 2012-07-12 17:10:13 PDT
(In reply to comment #22)
> Maybe the embedder should do this itself using ExtraData?

Assuming you mean:

- Have the owner/user/caller/whatever of the AssociatedURLLoader set an appropriate WebURLRequest::ExtraData.
- Then have the AssociatedURLLoader's WebURLLoaderClient use that to set the user agent (in willSendRequest()).

I agree that that'd be the most natural thing. Unfortunately, it has a fatal flaw (the reason for which I don't fully understand, but Nate knows more about this[*]): the AssociatedURLLoader::ClientAdapter::willSendRequest() (which would call its clients willSendRequest()) doesn't get called for the initial request -- only for redirects.

At least I *think* that's the case (i.e., I think I tried this), but let me double-check (since the code doesn't make this obvious)....


[*] I believe he said that the AssociatedURLLoader doesn't get set up as a client in time. Why, I don't know....
Comment 24 Adam Barth 2012-07-12 17:12:28 PDT
Interesting.  That's probably the core of this bug (although it might be tough to fix it there).
Comment 25 WebKit Review Bot 2012-07-12 17:17:30 PDT
Comment on attachment 152090 [details]
New approach: add custom user agent to ResourceRequest, to be handled by the FrameLoaderClientImpl.

Attachment 152090 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13201870
Comment 26 Viet-Trung Luu 2012-07-12 17:34:07 PDT
(In reply to comment #23)
> At least I *think* that's the case (i.e., I think I tried this), but let me double-check (since the code doesn't make this obvious)....

Definitely the case that AssociatedURLLoader::ClientAdapter::willSendRequest() only gets called for redirects, and not the original request. :(
Comment 27 Viet-Trung Luu 2012-07-12 18:17:18 PDT
Created attachment 152120 [details]
Updated to ToT.
Comment 28 Nate Chapin 2012-07-13 09:53:36 PDT
(In reply to comment #23)
> (In reply to comment #22)
> > Maybe the embedder should do this itself using ExtraData?
> 
> Assuming you mean:
> 
> - Have the owner/user/caller/whatever of the AssociatedURLLoader set an appropriate WebURLRequest::ExtraData.
> - Then have the AssociatedURLLoader's WebURLLoaderClient use that to set the user agent (in willSendRequest()).
> 
> I agree that that'd be the most natural thing. Unfortunately, it has a fatal flaw (the reason for which I don't fully understand, but Nate knows more about this[*]): the AssociatedURLLoader::ClientAdapter::willSendRequest() (which would call its clients willSendRequest()) doesn't get called for the initial request -- only for redirects.
> 
> At least I *think* that's the case (i.e., I think I tried this), but let me double-check (since the code doesn't make this obvious)....
> 
> 
> [*] I believe he said that the AssociatedURLLoader doesn't get set up as a client in time. Why, I don't know....

The reason is the timing of starting a request for a CachedResource. Basically, when DocumentThreadableLoader calls CachedResourceLoader::requestRawResource(), the decision on whether to start a resource load is made synchronously, and the load is actually started synchronously. The CachedResourceClient, therefore, doesn't have the opportunity to call CachedResource::addClient() to add itself until after the request has started and willSendRequest() has been called.

I guess we could change this, and it shouldn't be too hard to re-plumb, but I feel like it might make the code a little less readable. The main downside is that there would need to be a CachedResourceClient* parameter on a whole bunch of functions in CachedResourceLoader, but since only CachedRawResource exposes anything analogous to willSendrequest(), it would only be used in a small subset of cases.
Comment 29 Adam Barth 2012-07-13 10:23:28 PDT
That makes sense.  My main worry is that we'll want to do other things during will send request, but I guess we can solve that problem when it comes up.
Comment 30 Adam Barth 2012-07-13 10:25:46 PDT
Comment on attachment 152120 [details]
Updated to ToT.

This patch is fine with me.  My only question is whether we're applying the custom user agent at the right layer.  It's slightly odd for the state to be in WebCore but to use the state at the WebKit layer.

I'd like to defer to Nate for the final review, if that's ok.
Comment 31 Darin Fisher (:fishd, Google) 2012-07-13 10:53:06 PDT
Comment on attachment 152120 [details]
Updated to ToT.

View in context: https://bugs.webkit.org/attachment.cgi?id=152120&action=review

> Source/Platform/chromium/public/WebURLRequest.h:176
> +    WEBKIT_EXPORT void setCustomUserAgent(const WebString&);

question:

which takes precedence?  WebFrameClient::userAgentOverride or WebURLRequest::setCustomUserAgent?

which should take precedence?
Comment 32 Viet-Trung Luu 2012-07-13 10:58:05 PDT
(In reply to comment #31)
> (From update of attachment 152120 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=152120&action=review
> 
> > Source/Platform/chromium/public/WebURLRequest.h:176
> > +    WEBKIT_EXPORT void setCustomUserAgent(const WebString&);
> 
> question:
> 
> which takes precedence?  WebFrameClient::userAgentOverride or WebURLRequest::setCustomUserAgent?
> 
> which should take precedence?

WebURLRequest::setCustomUserAgent(), since it's more specific (applying to a request, as opposed to a URL).
Comment 33 Darin Fisher (:fishd, Google) 2012-07-13 11:14:02 PDT
Suppose someone wants to force all URLs to load with a given User-Agent.  Do we need to have some other mechanism to implement that?
Comment 34 Darin Fisher (:fishd, Google) 2012-07-13 11:18:54 PDT
(In reply to comment #33)
> Suppose someone wants to force all URLs to load with a given User-Agent.  Do we need to have some other mechanism to implement that?

Anyways, we should at least just document that setCustomUserAgent overrides both WebKit::Platform::userAgent() as well as WebKit::WebFrameClient::userAgentOverride().

What about the WebInspector's override UA capability?  I think setCustomUserAgent overrides that too.  (see InspectorInstrumentation::applyUserAgentOverride.)  Is that desirable too?
Comment 35 Darin Fisher (:fishd, Google) 2012-07-13 11:22:23 PDT
By the way, it occurs to me that you could also implement this patch entirely on the Chromium side.

WebURLRequest has an extraData field that could be extended with a CustomUserAgent field.  The implementation of willSendRequest in render_view_impl.cc could read that field and apply it to the WebURLRequest.

This might be slightly cleaner since the embedder (render_view_impl.cc) implements both userAgentOverride as well as willSendRequest, so if it wants to make code in willSendRequest override code in userAgentOverride, then OK.

The advantage is that we avoid having to make WebKit understand N different ways to override the UA string.
Comment 36 Viet-Trung Luu 2012-07-17 13:05:40 PDT
(In reply to comment #35)
> By the way, it occurs to me that you could also implement this patch entirely on the Chromium side.
> 
> WebURLRequest has an extraData field that could be extended with a CustomUserAgent field.  The implementation of willSendRequest in render_view_impl.cc could read that field and apply it to the WebURLRequest.
> 
> This might be slightly cleaner since the embedder (render_view_impl.cc) implements both userAgentOverride as well as willSendRequest, so if it wants to make code in willSendRequest override code in userAgentOverride, then OK.
> 
> The advantage is that we avoid having to make WebKit understand N different ways to override the UA string.

This is what I did (https://chromiumcodereview.appspot.com/10780019/, landed Chromium r147060).