Skip to content

Commit

Permalink
Bug 1072050 - Add pref for setting device identifier in UA string. r=…
Browse files Browse the repository at this point in the history
…khuey, sr=sicking
  • Loading branch information
ethantseng committed Oct 2, 2014
1 parent be4de9e commit 15a9488
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions b2g/app/b2g.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,8 @@ 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);
Expand Down
30 changes: 30 additions & 0 deletions netwerk/protocol/http/nsHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ nsHttpHandler::BuildUserAgent()
mAppVersion.Length() +
mCompatFirefox.Length() +
mCompatDevice.Length() +
mDeviceModelId.Length() +
13);

// Application portion
Expand All @@ -640,6 +641,10 @@ nsHttpHandler::BuildUserAgent()
mUserAgent += mOscpu;
mUserAgent.AppendLiteral("; ");
}
if (!mDeviceModelId.IsEmpty()) {
mUserAgent += mDeviceModelId;
mUserAgent.AppendLiteral("; ");
}
mUserAgent += mMisc;
mUserAgent += ')';

Expand Down Expand Up @@ -703,6 +708,31 @@ nsHttpHandler::InitUserAgentComponents()
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)
Expand Down
1 change: 1 addition & 0 deletions netwerk/protocol/http/nsHttpHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class nsHttpHandler MOZ_FINAL : public nsIHttpProtocolHandler
nsCString mCompatFirefox;
bool mCompatFirefoxEnabled;
nsXPIDLCString mCompatDevice;
nsCString mDeviceModelId;

nsCString mUserAgent;
nsXPIDLCString mUserAgentOverride;
Expand Down

0 comments on commit 15a9488

Please sign in to comment.