Bug 1262591 - Add ability to add device ID to user agent for Fennec; r=jduell
authorMichael Kaply <mozilla@kaply.com>
Mon, 11 Apr 2016 13:49:53 -0500
changeset 292793 60ea379dd10231fd7970b7244b656ccd0af0b6a6
parent 292792 88a5069742fee445e11923ad865f8a9bf88a25eb
child 292794 5b37f138e5bfdb84fe3460dd115ec225933e0f5f
push id74979
push usercbook@mozilla.com
push dateTue, 12 Apr 2016 11:53:25 +0000
treeherdermozilla-inbound@cb2b8b7bc47f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs1262591
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1262591 - Add ability to add device ID to user agent for Fennec; r=jduell
netwerk/protocol/http/nsHttpHandler.cpp
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -121,16 +121,28 @@ NewURI(const nsACString &aSpec,
     if (NS_FAILED(rv)) {
         return rv;
     }
 
     url.forget(aURI);
     return NS_OK;
 }
 
+static nsCString
+GetDeviceModelId() {
+    nsCOMPtr<nsIPropertyBag2> infoService = do_GetService("@mozilla.org/system-info;1");
+    MOZ_ASSERT(infoService, "Could not find a system info service");
+    nsAutoString androidDevice;
+    nsresult rv = infoService->GetPropertyAsAString(NS_LITERAL_STRING("device"), androidDevice);
+    if (NS_SUCCEEDED(rv)) {
+        return NS_LossyConvertUTF16toASCII(androidDevice);
+    }
+    return EmptyCString();
+}
+
 //-----------------------------------------------------------------------------
 // nsHttpHandler <public>
 //-----------------------------------------------------------------------------
 
 nsHttpHandler *gHttpHandler = nullptr;
 
 nsHttpHandler::nsHttpHandler()
     : mHttpVersion(NS_HTTP_VERSION_1_1)
@@ -774,16 +786,20 @@ nsHttpHandler::InitUserAgentComponents()
         bool isTV;
         rv = infoService->GetPropertyAsBool(NS_LITERAL_STRING("tv"), &isTV);
         if (NS_SUCCEEDED(rv) && isTV) {
             mCompatDevice.AssignLiteral("TV");
         } else {
             mCompatDevice.AssignLiteral("Mobile");
         }
     }
+
+    if (Preferences::GetBool(UA_PREF("use_device"), false)) {
+        mDeviceModelId = mozilla::net::GetDeviceModelId();
+    }
 #endif // ANDROID
 
 #ifdef MOZ_MULET
     {
         // Add the `Mobile` or `Tablet` or `TV` token when running in the b2g
         // desktop simulator via preference.
         nsCString deviceType;
         nsresult rv = Preferences::GetCString("devtools.useragent.device_type", &deviceType);
@@ -952,16 +968,28 @@ nsHttpHandler::PrefsChanged(nsIPrefBranc
 
     // general.useragent.override
     if (PREF_CHANGED(UA_PREF("override"))) {
         prefs->GetCharPref(UA_PREF("override"),
                             getter_Copies(mUserAgentOverride));
         mUserAgentIsDirty = true;
     }
 
+#ifdef ANDROID
+    // general.useragent.use_device
+    if (PREF_CHANGED(UA_PREF("use_device"))) {
+        if (Preferences::GetBool(UA_PREF("use_device"), false)) {
+            mDeviceModelId = mozilla::net::GetDeviceModelId();
+        } else {
+            mDeviceModelId = EmptyCString();
+        }
+        mUserAgentIsDirty = true;
+    }
+#endif
+
     //
     // HTTP options
     //
 
     if (PREF_CHANGED(HTTP_PREF("keep-alive.timeout"))) {
         rv = prefs->GetIntPref(HTTP_PREF("keep-alive.timeout"), &val);
         if (NS_SUCCEEDED(rv))
             mIdleTimeout = PR_SecondsToInterval(clamped(val, 1, 0xffff));