Bug 1072050 - Add pref for setting device identifier in UA string. r=khuey, sr=sicking
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -871,16 +871,18 @@ pref("webgl.can-lose-context-in-foregrou
pref("memory_info_dumper.watch_fifo.enabled", true);
pref("memory_info_dumper.watch_fifo.directory", "/data/local");
// See ua-update.json.in for the packaged UA override list
pref("general.useragent.updates.enabled", true);
pref("general.useragent.updates.url", "https://dynamicua.cdn.mozilla.net/0/%APP_ID%");
pref("general.useragent.updates.interval", 604800); // 1 week
pref("general.useragent.updates.retry", 86400); // 1 day
+// Device ID can be composed of letter, numbers, hyphen ("-") and dot (".")
+pref("general.useragent.device_id", "");
// Make <audio> and <video> talk to the AudioChannelService.
pref("media.useAudioChannelService", true);
pref("b2g.version", @MOZ_B2G_VERSION@);
pref("b2g.osName", @MOZ_B2G_OS_NAME@);
// Disable console buffering to save memory.
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -611,16 +611,17 @@ nsHttpHandler::BuildUserAgent()
mOscpu.Length() +
mMisc.Length() +
mProduct.Length() +
mProductSub.Length() +
mAppName.Length() +
mAppVersion.Length() +
mCompatFirefox.Length() +
mCompatDevice.Length() +
+ mDeviceModelId.Length() +
13);
// Application portion
mUserAgent.Assign(mLegacyAppName);
mUserAgent += '/';
mUserAgent += mLegacyAppVersion;
mUserAgent += ' ';
@@ -635,16 +636,20 @@ nsHttpHandler::BuildUserAgent()
if (!mCompatDevice.IsEmpty()) {
mUserAgent += mCompatDevice;
mUserAgent.AppendLiteral("; ");
}
else if (!mOscpu.IsEmpty()) {
mUserAgent += mOscpu;
mUserAgent.AppendLiteral("; ");
}
+ if (!mDeviceModelId.IsEmpty()) {
+ mUserAgent += mDeviceModelId;
+ mUserAgent.AppendLiteral("; ");
+ }
mUserAgent += mMisc;
mUserAgent += ')';
// Product portion
mUserAgent += ' ';
mUserAgent += mProduct;
mUserAgent += '/';
mUserAgent += mProductSub;
@@ -698,16 +703,41 @@ nsHttpHandler::InitUserAgentComponents()
bool isTablet;
nsresult rv = infoService->GetPropertyAsBool(NS_LITERAL_STRING("tablet"), &isTablet);
if (NS_SUCCEEDED(rv) && isTablet)
mCompatDevice.AssignLiteral("Tablet");
else
mCompatDevice.AssignLiteral("Mobile");
#endif
+#if defined(MOZ_WIDGET_GONK)
+ // Device model identifier should be a simple token, which can be composed
+ // of letters, numbers, hyphen ("-") and dot (".").
+ // Any other characters means the identifier is invalid and ignored.
+ nsCString deviceId;
+ rv = Preferences::GetCString("general.useragent.device_id", &deviceId);
+ if (NS_SUCCEEDED(rv)) {
+ bool valid = true;
+ deviceId.Trim(" ", true, true);
+ for (int i = 0; i < deviceId.Length(); i++) {
+ char c = deviceId.CharAt(i);
+ if (!(isalnum(c) || c == '-' || c == '.')) {
+ valid = false;
+ break;
+ }
+ }
+ if (valid) {
+ mDeviceModelId = deviceId;
+ } else {
+ LOG(("nsHttpHandler: Ignore invalid device ID: [%s]\n",
+ deviceId.get()));
+ }
+ }
+#endif
+
#ifndef MOZ_UA_OS_AGNOSTIC
// Gather OS/CPU.
#if defined(XP_WIN)
OSVERSIONINFO info = { sizeof(OSVERSIONINFO) };
#pragma warning(push)
#pragma warning(disable:4996)
if (GetVersionEx(&info)) {
#pragma warning(pop)
--- a/netwerk/protocol/http/nsHttpHandler.h
+++ b/netwerk/protocol/http/nsHttpHandler.h
@@ -427,16 +427,17 @@ private:
nsCString mMisc;
nsCString mProduct;
nsXPIDLCString mProductSub;
nsXPIDLCString mAppName;
nsXPIDLCString mAppVersion;
nsCString mCompatFirefox;
bool mCompatFirefoxEnabled;
nsXPIDLCString mCompatDevice;
+ nsCString mDeviceModelId;
nsCString mUserAgent;
nsXPIDLCString mUserAgentOverride;
bool mUserAgentIsDirty; // true if mUserAgent should be rebuilt
bool mUseCache;
bool mPromptTempRedirect;