summaryrefslogtreecommitdiffstats
path: root/webkit/glue/webkit_glue.cc
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-24 20:48:29 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-24 20:48:29 +0000
commitf5db58da8a081fe91b25d7a373987c97d397f378 (patch)
tree52a8f3bea7d1ffecbc45d652e6caea2fc8dac4b3 /webkit/glue/webkit_glue.cc
parent05992b2a814db10d48e4deaeb9fea873e37bd329 (diff)
downloadchromium_src-f5db58da8a081fe91b25d7a373987c97d397f378.zip
chromium_src-f5db58da8a081fe91b25d7a373987c97d397f378.tar.gz
chromium_src-f5db58da8a081fe91b25d7a373987c97d397f378.tar.bz2
Add a command-line switch to change the user agent.BUG=757345
Review URL: http://codereview.chromium.org/4059 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2571 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/webkit_glue.cc')
-rw-r--r--webkit/glue/webkit_glue.cc115
1 files changed, 66 insertions, 49 deletions
diff --git a/webkit/glue/webkit_glue.cc b/webkit/glue/webkit_glue.cc
index 904834b..9e40a85 100644
--- a/webkit/glue/webkit_glue.cc
+++ b/webkit/glue/webkit_glue.cc
@@ -339,74 +339,91 @@ std::string GetWebKitVersion() {
return StringPrintf("%d.%d", WEBKIT_VERSION_MAJOR, WEBKIT_VERSION_MINOR);
}
-const std::string& GetDefaultUserAgent() {
+namespace {
+
+std::string* user_agent = NULL;
+bool user_agent_requested = false;
+
+void SetUserAgentToDefault() {
+ static std::string default_user_agent;
#if defined(OS_WIN) || defined(OS_MACOSX)
- static std::string user_agent;
- static bool generated_user_agent;
- if (!generated_user_agent) {
- int32 os_major_version = 0;
- int32 os_minor_version = 0;
- int32 os_bugfix_version = 0;
+ int32 os_major_version = 0;
+ int32 os_minor_version = 0;
+ int32 os_bugfix_version = 0;
#if defined(OS_WIN)
- OSVERSIONINFO info = {0};
- info.dwOSVersionInfoSize = sizeof(info);
- GetVersionEx(&info);
- os_major_version = info.dwMajorVersion;
- os_minor_version = info.dwMinorVersion;
+ OSVERSIONINFO info = {0};
+ info.dwOSVersionInfoSize = sizeof(info);
+ GetVersionEx(&info);
+ os_major_version = info.dwMajorVersion;
+ os_minor_version = info.dwMinorVersion;
#elif defined(OS_MACOSX)
- Gestalt(gestaltSystemVersionMajor,
- reinterpret_cast<SInt32*>(&os_major_version));
- Gestalt(gestaltSystemVersionMinor,
- reinterpret_cast<SInt32*>(&os_minor_version));
- Gestalt(gestaltSystemVersionBugFix,
- reinterpret_cast<SInt32*>(&os_bugfix_version));
+ Gestalt(gestaltSystemVersionMajor,
+ reinterpret_cast<SInt32*>(&os_major_version));
+ Gestalt(gestaltSystemVersionMinor,
+ reinterpret_cast<SInt32*>(&os_minor_version));
+ Gestalt(gestaltSystemVersionBugFix,
+ reinterpret_cast<SInt32*>(&os_bugfix_version));
#endif
- // Get the product name and version, and replace Safari's Version/X string
- // with it. This is done to expose our product name in a manner that is
- // maximally compatible with Safari, we hope!!
- std::string product;
+ // Get the product name and version, and replace Safari's Version/X string
+ // with it. This is done to expose our product name in a manner that is
+ // maximally compatible with Safari, we hope!!
+ std::string product;
- FileVersionInfo* version_info =
- FileVersionInfo::CreateFileVersionInfoForCurrentModule();
- if (version_info)
- product = "Chrome/" + WideToASCII(version_info->product_version());
+ FileVersionInfo* version_info =
+ FileVersionInfo::CreateFileVersionInfoForCurrentModule();
+ if (version_info)
+ product = "Chrome/" + WideToASCII(version_info->product_version());
- if (product.empty())
- product = "Version/3.1";
+ if (product.empty())
+ product = "Version/3.1";
- // Derived from Safari's UA string.
- StringAppendF(
- &user_agent,
+ // Derived from Safari's UA string.
+ StringAppendF(
+ &default_user_agent,
#if defined(OS_WIN)
- "Mozilla/5.0 (Windows; U; Windows NT %d.%d; en-US) AppleWebKit/%d.%d"
+ "Mozilla/5.0 (Windows; U; Windows NT %d.%d; en-US) AppleWebKit/%d.%d"
#elif defined(OS_MACOSX)
- "Mozilla/5.0 (Macintosh; U; Intel Mac OS X %d_%d_%d; en-US) AppleWebKit/%d.%d"
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X %d_%d_%d; en-US) "
+ "AppleWebKit/%d.%d"
#endif
- " (KHTML, like Gecko) %s Safari/%d.%d",
- os_major_version,
- os_minor_version,
+ " (KHTML, like Gecko) %s Safari/%d.%d",
+ os_major_version,
+ os_minor_version,
#if defined(OS_MACOSX)
- os_bugfix_version,
+ os_bugfix_version,
#endif
- WEBKIT_VERSION_MAJOR,
- WEBKIT_VERSION_MINOR,
- product.c_str(),
- WEBKIT_VERSION_MAJOR,
- WEBKIT_VERSION_MINOR
- );
-
- generated_user_agent = true;
- }
-
- return user_agent;
+ WEBKIT_VERSION_MAJOR,
+ WEBKIT_VERSION_MINOR,
+ product.c_str(),
+ WEBKIT_VERSION_MAJOR,
+ WEBKIT_VERSION_MINOR
+ );
#else
// TODO(port): we need something like FileVersionInfo for our UA string.
NOTIMPLEMENTED();
- return EmptyString();
#endif
+ user_agent = &default_user_agent;
}
+};
+
+void SetUserAgent(const std::string& new_user_agent) {
+ DCHECK(!user_agent_requested) << "Setting the user agent after someone has "
+ "already requested it can result in unexpected behavior.";
+ static std::string overridden_user_agent;
+ overridden_user_agent = new_user_agent; // If you combine this with the
+ // previous line, the function only
+ // works the first time.
+ user_agent = &overridden_user_agent;
+}
+
+const std::string& GetUserAgent() {
+ if (!user_agent)
+ SetUserAgentToDefault();
+ user_agent_requested = true;
+ return *user_agent;
+}
void NotifyJSOutOfMemory(WebCore::Frame* frame) {
if (!frame)