diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-24 20:48:29 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-24 20:48:29 +0000 |
commit | f5db58da8a081fe91b25d7a373987c97d397f378 (patch) | |
tree | 52a8f3bea7d1ffecbc45d652e6caea2fc8dac4b3 | |
parent | 05992b2a814db10d48e4deaeb9fea873e37bd329 (diff) | |
download | chromium_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
-rw-r--r-- | chrome/browser/browser_about_handler.cc | 2 | ||||
-rw-r--r-- | chrome/browser/browser_init.cc | 6 | ||||
-rw-r--r-- | chrome/browser/plugin_process_host.cc | 1 | ||||
-rw-r--r-- | chrome/browser/profile.cc | 2 | ||||
-rw-r--r-- | chrome/browser/render_process_host.cc | 1 | ||||
-rw-r--r-- | chrome/browser/web_contents.cc | 4 | ||||
-rw-r--r-- | chrome/common/child_process.cc | 12 | ||||
-rw-r--r-- | chrome/common/chrome_switches.cc | 3 | ||||
-rw-r--r-- | chrome/common/chrome_switches.h | 1 | ||||
-rw-r--r-- | chrome/common/render_messages.h | 50 | ||||
-rw-r--r-- | webkit/glue/plugins/plugin_host.cc | 2 | ||||
-rw-r--r-- | webkit/glue/webframeloaderclient_impl.cc | 3 | ||||
-rw-r--r-- | webkit/glue/webkit_glue.cc | 115 | ||||
-rw-r--r-- | webkit/glue/webkit_glue.h | 10 | ||||
-rw-r--r-- | webkit/glue/webpreferences.h | 2 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell.cc | 1 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_request_context.cc | 2 |
17 files changed, 127 insertions, 90 deletions
diff --git a/chrome/browser/browser_about_handler.cc b/chrome/browser/browser_about_handler.cc index f338dfa..4b8ac12 100644 --- a/chrome/browser/browser_about_handler.cc +++ b/chrome/browser/browser_about_handler.cc @@ -263,7 +263,7 @@ std::string BrowserAboutHandler::AboutVersion() { l10n_util::GetString(IDS_ABOUT_VERSION_UNOFFICIAL)); } localized_strings.SetString(L"useragent", - UTF8ToWide(webkit_glue::GetDefaultUserAgent())); + UTF8ToWide(webkit_glue::GetUserAgent())); static const StringPiece version_html( ResourceBundle::GetSharedInstance().GetRawDataResource( diff --git a/chrome/browser/browser_init.cc b/chrome/browser/browser_init.cc index e7437ce..859b94b 100644 --- a/chrome/browser/browser_init.cc +++ b/chrome/browser/browser_init.cc @@ -39,6 +39,7 @@ #include "chrome/common/win_util.h" #include "net/base/cookie_monster.h" #include "net/base/net_util.h" +#include "webkit/glue/webkit_glue.h" #include "chromium_strings.h" #include "generated_resources.h" @@ -407,6 +408,11 @@ bool BrowserInit::LaunchWithProfile::Launch(Profile* profile, if (parsed_command_line.HasSwitch(switches::kEnableFileCookies)) net::CookieMonster::EnableFileScheme(); + if (parsed_command_line.HasSwitch(switches::kUserAgent)) { + webkit_glue::SetUserAgent(WideToUTF8( + parsed_command_line.GetSwitchValue(switches::kUserAgent))); + } + #ifndef NDEBUG if (parsed_command_line.HasSwitch(switches::kApp)) { NOTREACHED(); diff --git a/chrome/browser/plugin_process_host.cc b/chrome/browser/plugin_process_host.cc index 58bdc53..b3db5f1 100644 --- a/chrome/browser/plugin_process_host.cc +++ b/chrome/browser/plugin_process_host.cc @@ -343,6 +343,7 @@ bool PluginProcessHost::Init(const std::wstring& dll, switches::kNoSandbox, switches::kSafePlugins, switches::kTestSandbox, + switches::kUserAgent, switches::kDisableBreakpad, switches::kFullMemoryCrashReport, switches::kEnableLogging, diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc index 9ed5496..ad2ddb1 100644 --- a/chrome/browser/profile.cc +++ b/chrome/browser/profile.cc @@ -101,7 +101,7 @@ class ProfileImpl::RequestContext : public URLRequestContext, cookie_store_ = NULL; // setup user agent - user_agent_ = webkit_glue::GetDefaultUserAgent(); + user_agent_ = webkit_glue::GetUserAgent(); // set up Accept-Language and Accept-Charset header values // TODO(jungshik) : This may slow down http requests. Perhaps, // we have to come up with a better way to set up these values. diff --git a/chrome/browser/render_process_host.cc b/chrome/browser/render_process_host.cc index 34eba76..a1244ed 100644 --- a/chrome/browser/render_process_host.cc +++ b/chrome/browser/render_process_host.cc @@ -249,6 +249,7 @@ bool RenderProcessHost::Init() { switches::kTestSandbox, switches::kInProcessPlugins, switches::kDomAutomationController, + switches::kUserAgent, switches::kJavaScriptFlags, switches::kRecordMode, switches::kPlaybackMode, diff --git a/chrome/browser/web_contents.cc b/chrome/browser/web_contents.cc index b00d63f..35c55ad 100644 --- a/chrome/browser/web_contents.cc +++ b/chrome/browser/web_contents.cc @@ -1896,10 +1896,6 @@ WebPreferences WebContents::GetWebkitPrefs() { WebPreferences web_prefs; PrefService* prefs = profile()->GetPrefs(); - // TODO(darin): Support overriding this value from prefs, which also - // involves modifying our URLRequestContext. - web_prefs.user_agent = webkit_glue::GetDefaultUserAgent(); - web_prefs.fixed_font_family = prefs->GetString(prefs::kWebKitFixedFontFamily); web_prefs.serif_font_family = diff --git a/chrome/common/child_process.cc b/chrome/common/child_process.cc index 50f8b88..979418f 100644 --- a/chrome/common/child_process.cc +++ b/chrome/common/child_process.cc @@ -3,10 +3,15 @@ // found in the LICENSE file. #include <windows.h> + #include "chrome/common/child_process.h" #include "base/atomic_ref_count.h" #include "base/basictypes.h" +#include "base/command_line.h" +#include "base/string_util.h" +#include "chrome/common/chrome_switches.h" +#include "webkit/glue/webkit_glue.h" ChildProcess* ChildProcess::child_process_; MessageLoop* ChildProcess::main_thread_loop_; @@ -72,6 +77,13 @@ bool ChildProcess::GlobalInit(const std::wstring &channel_name, shutdown_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); child_process_ = factory->Create(channel_name); + + CommandLine command_line; + if (command_line.HasSwitch(switches::kUserAgent)) { + webkit_glue::SetUserAgent(WideToUTF8( + command_line.GetSwitchValue(switches::kUserAgent))); + } + return true; } diff --git a/chrome/common/chrome_switches.cc b/chrome/common/chrome_switches.cc index 7dec663..742eee1 100644 --- a/chrome/common/chrome_switches.cc +++ b/chrome/common/chrome_switches.cc @@ -128,6 +128,9 @@ const wchar_t kDomAutomationController[] = L"dom-automation"; // Tells the plugin process the path of the plugin to load const wchar_t kPluginPath[] = L"plugin-path"; +// A string used to override the default user agent with a custom one. +const wchar_t kUserAgent[] = L"user-agent"; + // Specifies the flags passed to JS engine const wchar_t kJavaScriptFlags[] = L"js-flags"; diff --git a/chrome/common/chrome_switches.h b/chrome/common/chrome_switches.h index 241170e8..9df559b 100644 --- a/chrome/common/chrome_switches.h +++ b/chrome/common/chrome_switches.h @@ -43,6 +43,7 @@ extern const wchar_t kApp[]; extern const wchar_t kAppUploadFile[]; extern const wchar_t kDomAutomationController[]; extern const wchar_t kPluginPath[]; +extern const wchar_t kUserAgent[]; extern const wchar_t kJavaScriptFlags[]; extern const wchar_t kGeoID[]; extern const wchar_t kLang[]; diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 7deb5dc..95268a0 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -1443,35 +1443,33 @@ struct ParamTraits<WebPreferences> { WriteParam(m, p.java_enabled); WriteParam(m, p.user_style_sheet_enabled); WriteParam(m, p.user_style_sheet_location); - WriteParam(m, p.user_agent); } static bool Read(const Message* m, void** iter, param_type* p) { return - ReadParam(m, iter, &p->standard_font_family) && - ReadParam(m, iter, &p->fixed_font_family) && - ReadParam(m, iter, &p->serif_font_family) && - ReadParam(m, iter, &p->sans_serif_font_family) && - ReadParam(m, iter, &p->cursive_font_family) && - ReadParam(m, iter, &p->fantasy_font_family) && - ReadParam(m, iter, &p->default_font_size) && - ReadParam(m, iter, &p->default_fixed_font_size) && - ReadParam(m, iter, &p->minimum_font_size) && - ReadParam(m, iter, &p->minimum_logical_font_size) && - ReadParam(m, iter, &p->default_encoding) && - ReadParam(m, iter, &p->javascript_enabled) && - ReadParam(m, iter, &p->javascript_can_open_windows_automatically) && - ReadParam(m, iter, &p->loads_images_automatically) && - ReadParam(m, iter, &p->plugins_enabled) && - ReadParam(m, iter, &p->dom_paste_enabled) && - ReadParam(m, iter, &p->developer_extras_enabled) && - ReadParam(m, iter, &p->shrinks_standalone_images_to_fit) && - ReadParam(m, iter, &p->uses_universal_detector) && - ReadParam(m, iter, &p->text_areas_are_resizable) && - ReadParam(m, iter, &p->dashboard_compatibility_mode) && - ReadParam(m, iter, &p->java_enabled) && - ReadParam(m, iter, &p->user_style_sheet_enabled) && - ReadParam(m, iter, &p->user_style_sheet_location) && - ReadParam(m, iter, &p->user_agent); + ReadParam(m, iter, &p->standard_font_family) && + ReadParam(m, iter, &p->fixed_font_family) && + ReadParam(m, iter, &p->serif_font_family) && + ReadParam(m, iter, &p->sans_serif_font_family) && + ReadParam(m, iter, &p->cursive_font_family) && + ReadParam(m, iter, &p->fantasy_font_family) && + ReadParam(m, iter, &p->default_font_size) && + ReadParam(m, iter, &p->default_fixed_font_size) && + ReadParam(m, iter, &p->minimum_font_size) && + ReadParam(m, iter, &p->minimum_logical_font_size) && + ReadParam(m, iter, &p->default_encoding) && + ReadParam(m, iter, &p->javascript_enabled) && + ReadParam(m, iter, &p->javascript_can_open_windows_automatically) && + ReadParam(m, iter, &p->loads_images_automatically) && + ReadParam(m, iter, &p->plugins_enabled) && + ReadParam(m, iter, &p->dom_paste_enabled) && + ReadParam(m, iter, &p->developer_extras_enabled) && + ReadParam(m, iter, &p->shrinks_standalone_images_to_fit) && + ReadParam(m, iter, &p->uses_universal_detector) && + ReadParam(m, iter, &p->text_areas_are_resizable) && + ReadParam(m, iter, &p->dashboard_compatibility_mode) && + ReadParam(m, iter, &p->java_enabled) && + ReadParam(m, iter, &p->user_style_sheet_enabled) && + ReadParam(m, iter, &p->user_style_sheet_location); } static void Log(const param_type& p, std::wstring* l) { l->append(L"<WebPreferences>"); diff --git a/webkit/glue/plugins/plugin_host.cc b/webkit/glue/plugins/plugin_host.cc index 273219d..7993048 100644 --- a/webkit/glue/plugins/plugin_host.cc +++ b/webkit/glue/plugins/plugin_host.cc @@ -631,7 +631,7 @@ const char* NPN_UserAgent(NPP id) { if (id) { scoped_refptr<NPAPI::PluginInstance> plugin = FindInstance(id); if (plugin.get() && !plugin->use_mozilla_user_agent()) - return webkit_glue::GetDefaultUserAgent().c_str(); + return webkit_glue::GetUserAgent().c_str(); } static const char *UA = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061103 Firefox/2.0a1"; diff --git a/webkit/glue/webframeloaderclient_impl.cc b/webkit/glue/webframeloaderclient_impl.cc index 3250144..7d3011c 100644 --- a/webkit/glue/webframeloaderclient_impl.cc +++ b/webkit/glue/webframeloaderclient_impl.cc @@ -1257,8 +1257,7 @@ void WebFrameLoaderClient::setTitle(const String& title, const KURL& url) { } String WebFrameLoaderClient::userAgent(const KURL& url) { - return webkit_glue::StdStringToString( - webframe_->webview_impl()->GetPreferences().user_agent); + return webkit_glue::StdStringToString(webkit_glue::GetUserAgent()); } void WebFrameLoaderClient::savePlatformDataToCachedPage(WebCore::CachedPage*) { 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) diff --git a/webkit/glue/webkit_glue.h b/webkit/glue/webkit_glue.h index 411ad20..78c953a 100644 --- a/webkit/glue/webkit_glue.h +++ b/webkit/glue/webkit_glue.h @@ -88,8 +88,14 @@ void ResetBeforeTestRun(WebView* view); // Returns the WebKit version (major.minor). std::string GetWebKitVersion(); -// Returns the user agent. -const std::string& GetDefaultUserAgent(); +// Called to override the default user agent with a custom one. Call this +// before anyone actually asks for the user agent in order to prevent +// inconsistent behavior. +void SetUserAgent(const std::string& new_user_agent); + +// Returns the user agent, which is usually the default user agent but may be +// overriden by a call to SetUserAgent() (which should be done at startup). +const std::string& GetUserAgent(); // Creates serialized state for the specified URL. This is a variant of // HistoryItemToString (in glue_serialize) that is used during session restore diff --git a/webkit/glue/webpreferences.h b/webkit/glue/webpreferences.h index 524e38e..4a08fa0 100644 --- a/webkit/glue/webpreferences.h +++ b/webkit/glue/webpreferences.h @@ -44,8 +44,6 @@ struct WebPreferences { bool user_style_sheet_enabled; GURL user_style_sheet_location; - std::string user_agent; - // We try to keep the default values the same as the default values in // chrome, except for the cases where it would require lots of extra work for // the embedder to use the same default value. diff --git a/webkit/tools/test_shell/test_shell.cc b/webkit/tools/test_shell/test_shell.cc index 283e730..f11d21a 100644 --- a/webkit/tools/test_shell/test_shell.cc +++ b/webkit/tools/test_shell/test_shell.cc @@ -247,7 +247,6 @@ void TestShell::ResetWebPreferences() { web_prefs_->shrinks_standalone_images_to_fit = false; web_prefs_->uses_universal_detector = false; web_prefs_->text_areas_are_resizable = false; - web_prefs_->user_agent = webkit_glue::GetDefaultUserAgent(); web_prefs_->dashboard_compatibility_mode = false; web_prefs_->java_enabled = true; web_prefs_->allow_scripts_to_close_windows = false; diff --git a/webkit/tools/test_shell/test_shell_request_context.cc b/webkit/tools/test_shell/test_shell_request_context.cc index 2fdf164..9767b83 100644 --- a/webkit/tools/test_shell/test_shell_request_context.cc +++ b/webkit/tools/test_shell/test_shell_request_context.cc @@ -22,7 +22,7 @@ void TestShellRequestContext::Init( net::HttpCache::Mode cache_mode) { cookie_store_ = new net::CookieMonster(); - user_agent_ = webkit_glue::GetDefaultUserAgent(); + user_agent_ = webkit_glue::GetUserAgent(); // hard-code A-L and A-C for test shells accept_language_ = "en-us,en"; |