Last Comment Bug 814379 - Firefox 17 + User agent switcher. The JS "navigator.userAgent" property is broken (it never changes)
: Firefox 17 + User agent switcher. The JS "navigator.userAgent" property is br...
Status: RESOLVED FIXED
: regression
Product: Core
Classification: Components
Component: DOM (show other bugs)
: 17 Branch
: All All
: -- normal with 8 votes (vote)
: mozilla23
Assigned To: Mitchell Mebane
:
: Andrew Overholt [:overholt]
Mentors:
: 787798 (view as bug list)
Depends on:
Blocks: 800157 861470
  Show dependency treegraph
 
Reported: 2012-11-22 04:12 PST by Paulo
Modified: 2016-01-13 10:38 PST (History)
14 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
patch (1.43 KB, patch)
2013-05-09 06:33 PDT, Mitchell Mebane
dao+bmo: review+
Details | Diff | Splinter Review
patch r2: fixed spacing (1.45 KB, patch)
2013-05-09 10:12 PDT, Mitchell Mebane
dao+bmo: review+
Details | Diff | Splinter Review

Description Paulo 2012-11-22 04:12:38 PST
User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.14 Safari/537.17

Steps to reproduce:

After an upgrade to Firefox 17.0 (on Ubuntu)
I used the extension "User agent switcher" to change the user agent.


Actual results:

- The user agent reported to the webserver has changed.
- The JS "navigator.userAgent" property was NOT changed.


Expected results:

Both the user agent reported to the webserver and the JS "navigator.userAgent" property should have changed to the new user agent.

I tried with other extensions and none of them can change the  JS "navigator.userAgent" property

In Firefox 16 this worked without problems
Comment 1 Matthias Versen [:Matti] 2012-11-22 09:52:25 PST

*** This bug has been marked as a duplicate of bug 787798 ***
Comment 2 Matthias Versen [:Matti] 2012-11-23 08:48:00 PST
>In Firefox 16 this worked without problems
In that case it's not a dupe, sorry
Comment 3 Julien Carnec 2013-02-01 01:42:51 PST
Same for me.
In the web app I am working on, we provide an extension that changes the "general.useragent.override" preference to set it to various mobile devices user agent strings.
When doing so, we get the same results:
- The user agent reported to the webserver has changed.
- The JS "navigator.userAgent" property was NOT changed.
It worked fine with FF16 and started to fail with FF17.
It still fails with FF18.
Comment 4 Julien Carnec 2013-02-06 05:59:27 PST
I just saw that this bug was only targeting the x86_64 linux platform.
On my side, I also get it on Windows.
Comment 5 basa.nl 2013-02-24 05:47:22 PST
This happens also on MacOSX.
After restarting the browser, the correct value is returned by (both) navigator.userAgent (and the HTTP header).
Comment 6 muzuiget 2013-04-05 02:03:05 PDT
After modify preference "general.useragent.override"(some extension just provide a GUI to modify this). The http request header send to server is changed, but not the javascript object "navigator.userAgent", latter need to restart Firefox.

Firefox 20 on Window/Linux both have this problem. 

But inserting, when enable "general.useragent.site_specific_overrides", then add "general.useragent.override.google.com", both http header and "navigator.userAgent" changed immediately, no need to restart.
Comment 7 Matt Brubeck (:mbrubeck) 2013-04-08 13:24:59 PDT
Possibly a regression from bug 800157.
Comment 8 Brad Lassey [:blassey] (use needinfo?) 2013-04-08 13:27:19 PDT
qawanted to confirm, bug 800157 sounds like it already fixed this
Comment 9 Mitchell Mebane 2013-05-06 08:31:04 PDT
Some testing results

* 17.0b1 - working immediately after setting pref
* 17.0b2 - working only after restart
* 19.0.2 - working only after restart
* 20.0.1 - working only after restart
* 21.0b6 - working only after restart
* 22.0a2 20130503 - working immediately after setting pref

Tested by setting general.useragent.override, then opening the Web Console and typing navigator.useragent.

I guess User Agent Switcher will be broken until FF 22, because it resets general.useragent.override to default on restart.
Comment 10 Mitchell Mebane 2013-05-06 09:41:58 PDT
Actually, I can't reproduce the Aurora 22 results.  Maybe I accidentally restarted the browser earlier during testing.

* 22.0a2 20130503 and 20130506  - working only after restart
Comment 11 Mitchell Mebane 2013-05-09 06:33:52 PDT
Created attachment 747404 [details] [diff] [review]
patch

I managed to get Firefox building, and did some bisecting.  Looks like this is the breaking change:

http://hg.mozilla.org/mozilla-central/rev/9f28c28e988f

Looks like when navigator.userAgent is requested, and there is no site-specific override, the global UA is used.  However, this is stored in a const field, so it is never update.

I'm not familiar with Mercurial or the Firefox codebase, but the issue seemed simple enough, so I took a stab at a patch.  It fixes the issue for me.  Hopefully the formatting, etc. is at least halfway right.  :)
Comment 12 Josh Matthews [:jdm] (on vacation until Dec 5) 2013-05-09 06:55:56 PDT
Comment on attachment 747404 [details] [diff] [review]
patch

Thanks Mitchell!
Comment 13 Dão Gottwald [:dao] 2013-05-09 07:06:57 PDT
Comment on attachment 747404 [details] [diff] [review]
patch

>-const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
>-                     .getService(Ci.nsIHttpProtocolHandler)
>-                     .userAgent;
>+const HTTP_PROTO_HANDLER = Cc["@mozilla.org/network/protocol;1?name=http"]
>+                     .getService(Ci.nsIHttpProtocolHandler);

The last line should be indented with another eight spaces. r=me with that fixed. Thanks!
Comment 14 Mitchell Mebane 2013-05-09 10:12:03 PDT
Created attachment 747497 [details] [diff] [review]
patch r2: fixed spacing

Added 8 more spaces.

Is there a style guide for indentation when splitting over multiple lines?  I looked around a bit but didn't see anything.
Comment 15 Dão Gottwald [:dao] 2013-05-10 03:09:05 PDT
(In reply to Mitchell Mebane from comment #14)
> Created attachment 747497 [details] [diff] [review]
> patch r2: fixed spacing
> 
> Added 8 more spaces.
> 
> Is there a style guide for indentation when splitting over multiple lines?

Not that I know of...
Comment 16 Ryan VanderMeulen [:RyanVM] 2013-05-10 05:43:18 PDT
https://hg.mozilla.org/integration/mozilla-inbound/rev/da4ef4dc056b
Comment 17 David Baron :dbaron: ⌚️UTC-10 2013-05-11 22:53:06 PDT
https://hg.mozilla.org/mozilla-central/rev/da4ef4dc056b
Comment 18 Pete 2013-06-13 12:50:33 PDT
While waiting for a fix... as a hack/workaround... I used an HTTPObserver to set a site specific override for every domain visited by the user (and used an event listener to clear up the resulting mess after the page is unloaded).

Its not nice, but it works.

observerService.addObserver(myHTTPObserver, "http-on-modify-request", false);

And in the observer code;

var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
prefsGUAP.setCharPref("override." + httpChannel.URI.host, myUserAgent );

Later in the page unload event listener;

prefsGUAP.deleteBranch("override.");

Looking forward to FF23, nice work Mitchell.
Comment 19 Dão Gottwald [:dao] 2016-01-13 10:38:11 PST
*** Bug 787798 has been marked as a duplicate of this bug. ***

Note You need to log in before you can comment on or make changes to this bug.