diff options
-rw-r--r-- | chrome/browser/browser_about_handler.cc | 2 | ||||
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.cc | 13 | ||||
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.h | 2 | ||||
-rw-r--r-- | net/url_request/url_request_context.h | 24 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.cc | 6 | ||||
-rw-r--r-- | net/url_request/url_request_inet_job.cc | 4 | ||||
-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 | 41 | ||||
-rw-r--r-- | webkit/glue/webkit_glue.h | 7 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_request_context.cc | 6 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_request_context.h | 2 |
12 files changed, 72 insertions, 40 deletions
diff --git a/chrome/browser/browser_about_handler.cc b/chrome/browser/browser_about_handler.cc index 0445fc4..8c01f75 100644 --- a/chrome/browser/browser_about_handler.cc +++ b/chrome/browser/browser_about_handler.cc @@ -274,7 +274,7 @@ std::string BrowserAboutHandler::AboutVersion() { l10n_util::GetString(IDS_ABOUT_VERSION_UNOFFICIAL)); } localized_strings.SetString(L"useragent", - UTF8ToWide(webkit_glue::GetUserAgent())); + UTF8ToWide(webkit_glue::GetUserAgent(GURL()))); static const StringPiece version_html( ResourceBundle::GetSharedInstance().GetRawDataResource( diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc index f11b710..5cd4688 100644 --- a/chrome/browser/net/chrome_url_request_context.cc +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -94,8 +94,6 @@ ChromeURLRequestContext* ChromeURLRequestContext::CreateOffTheRecord( ChromeURLRequestContext::ChromeURLRequestContext(Profile* profile) : prefs_(profile->GetPrefs()), is_off_the_record_(profile->IsOffTheRecord()) { - user_agent_ = webkit_glue::GetUserAgent(); - // Set up Accept-Language and Accept-Charset header values accept_language_ = net::HttpUtil::GenerateAcceptLanguageHeader( WideToASCII(prefs_->GetString(prefs::kAcceptLanguages))); @@ -183,14 +181,21 @@ FilePath ChromeURLRequestContext::GetPathForExtension(const std::string& id) { } } -void ChromeURLRequestContext::OnAcceptLanguageChange(std::string accept_language) { +const std::string& ChromeURLRequestContext::GetUserAgent( + const GURL& url) const { + return webkit_glue::GetUserAgent(url); +} + +void ChromeURLRequestContext::OnAcceptLanguageChange( + std::string accept_language) { DCHECK(MessageLoop::current() == ChromeThread::GetMessageLoop(ChromeThread::IO)); accept_language_ = net::HttpUtil::GenerateAcceptLanguageHeader(accept_language); } -void ChromeURLRequestContext::OnCookiePolicyChange(net::CookiePolicy::Type type) { +void ChromeURLRequestContext::OnCookiePolicyChange( + net::CookiePolicy::Type type) { DCHECK(MessageLoop::current() == ChromeThread::GetMessageLoop(ChromeThread::IO)); cookie_policy_.SetType(type); diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h index 9ddbd47..915322e 100644 --- a/chrome/browser/net/chrome_url_request_context.h +++ b/chrome/browser/net/chrome_url_request_context.h @@ -44,6 +44,8 @@ class ChromeURLRequestContext : public URLRequestContext, return user_script_dir_path_; } + virtual const std::string& GetUserAgent(const GURL& url) const; + private: // Private constructor, use the static factory methods instead. This is // expected to be called on the UI thread. diff --git a/net/url_request/url_request_context.h b/net/url_request/url_request_context.h index 5941df8..f439482 100644 --- a/net/url_request/url_request_context.h +++ b/net/url_request/url_request_context.h @@ -10,11 +10,9 @@ #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ -#include <string> - -#include "base/basictypes.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" +#include "base/string_util.h" #include "net/base/cookie_policy.h" #include "net/ftp/ftp_auth_cache.h" #include "net/http/http_transaction_factory.h" @@ -53,20 +51,24 @@ class URLRequestContext : // Gets the FTP authentication cache for this context. net::FtpAuthCache* ftp_auth_cache() { return &ftp_auth_cache_; } - // Gets the UA string to use for this context. - const std::string& user_agent() const { return user_agent_; } - // Gets the value of 'Accept-Charset' header field. const std::string& accept_charset() const { return accept_charset_; } // Gets the value of 'Accept-Language' header field. const std::string& accept_language() const { return accept_language_; } - // Do not call this directly. TODO(darin): extending from RefCounted* should - // not require a public destructor! - virtual ~URLRequestContext() {} + // Gets the UA string to use for the given URL. Pass an invalid URL (such as + // GURL()) to get the default UA string. Subclasses should override this + // method to provide a UA string. + virtual const std::string& GetUserAgent(const GURL& url) const { + return EmptyString(); + } protected: + friend class base::RefCountedThreadSafe<URLRequestContext>; + + virtual ~URLRequestContext() {} + // The following members are expected to be initialized and owned by // subclasses. net::ProxyService* proxy_service_; @@ -74,13 +76,11 @@ class URLRequestContext : net::CookieMonster* cookie_store_; net::CookiePolicy cookie_policy_; net::FtpAuthCache ftp_auth_cache_; - std::string user_agent_; std::string accept_language_; std::string accept_charset_; private: - DISALLOW_EVIL_CONSTRUCTORS(URLRequestContext); + DISALLOW_COPY_AND_ASSIGN(URLRequestContext); }; #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ - diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc index 7728f6c..197a2d6 100644 --- a/net/url_request/url_request_http_job.cc +++ b/net/url_request/url_request_http_job.cc @@ -96,8 +96,10 @@ void URLRequestHttpJob::Start() { request_info_.method = request_->method(); request_info_.load_flags = request_->load_flags(); - if (request_->context()) - request_info_.user_agent = request_->context()->user_agent(); + if (request_->context()) { + request_info_.user_agent = + request_->context()->GetUserAgent(request_->url()); + } AddExtraHeaders(); diff --git a/net/url_request/url_request_inet_job.cc b/net/url_request/url_request_inet_job.cc index 2d45526..3c81c2e 100644 --- a/net/url_request/url_request_inet_job.cc +++ b/net/url_request/url_request_inet_job.cc @@ -83,8 +83,8 @@ URLRequestInetJob::URLRequestInetJob(URLRequest* request) // TODO(darin): we should re-create the internet if the UA string changes, // but we have to be careful about existing users of this internet. if (!the_internet_) { - InitializeTheInternet( - request->context() ? request->context()->user_agent() : std::string()); + InitializeTheInternet(request->context() ? + request->context()->GetUserAgent(GURL()) : std::string()); } #ifndef NDEBUG DCHECK(MessageLoop::current() == my_message_loop_) << diff --git a/webkit/glue/plugins/plugin_host.cc b/webkit/glue/plugins/plugin_host.cc index 108537b..c12ee5c 100644 --- a/webkit/glue/plugins/plugin_host.cc +++ b/webkit/glue/plugins/plugin_host.cc @@ -575,7 +575,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::GetUserAgent().c_str(); + return webkit_glue::GetUserAgent(GURL()).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 2bdb9d8..cd4aca0 100644 --- a/webkit/glue/webframeloaderclient_impl.cc +++ b/webkit/glue/webframeloaderclient_impl.cc @@ -1264,7 +1264,8 @@ void WebFrameLoaderClient::setTitle(const String& title, const KURL& url) { } String WebFrameLoaderClient::userAgent(const KURL& url) { - return webkit_glue::StdStringToString(webkit_glue::GetUserAgent()); + return webkit_glue::StdStringToString( + webkit_glue::GetUserAgent(webkit_glue::KURLToGURL(url))); } void WebFrameLoaderClient::savePlatformDataToCachedPage(WebCore::CachedPage*) { diff --git a/webkit/glue/webkit_glue.cc b/webkit/glue/webkit_glue.cc index 276ba47..ebcb3e8 100644 --- a/webkit/glue/webkit_glue.cc +++ b/webkit/glue/webkit_glue.cc @@ -316,11 +316,11 @@ std::string GetWebKitVersion() { namespace { -std::string* user_agent = NULL; +const std::string* user_agent = NULL; bool user_agent_requested = false; +bool user_agent_is_overridden = false; -void SetUserAgentToDefault() { - static std::string default_user_agent; +void BuildUserAgent(bool mimic_safari, std::string* result) { #if defined(OS_WIN) || defined(OS_MACOSX) int32 os_major_version = 0; int32 os_minor_version = 0; @@ -345,17 +345,19 @@ void SetUserAgentToDefault() { // maximally compatible with Safari, we hope!! std::string product; - scoped_ptr<FileVersionInfo> version_info( - FileVersionInfo::CreateFileVersionInfoForCurrentModule()); - if (version_info.get()) - product = "Chrome/" + WideToASCII(version_info->product_version()); + if (!mimic_safari) { + scoped_ptr<FileVersionInfo> version_info( + FileVersionInfo::CreateFileVersionInfoForCurrentModule()); + if (version_info.get()) + product = "Chrome/" + WideToASCII(version_info->product_version()); + } if (product.empty()) - product = "Version/3.1"; + product = "Version/3.2.1"; // Derived from Safari's UA string. StringAppendF( - &default_user_agent, + result, #if defined(OS_WIN) "Mozilla/5.0 (Windows; U; Windows NT %d.%d; en-US) AppleWebKit/%d.%d" #elif defined(OS_MACOSX) @@ -380,17 +382,22 @@ void SetUserAgentToDefault() { // Windows. Some solution for embedding the Chrome version number needs to be // found here. StringAppendF( - &default_user_agent, + result, "Mozilla/5.0 (Linux; U; en-US) AppleWebKit/525.13 " "(KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13"); #else // TODO(port): we need something like FileVersionInfo for our UA string. NOTIMPLEMENTED(); #endif +} + +void SetUserAgentToDefault() { + static std::string default_user_agent; + BuildUserAgent(false, &default_user_agent); user_agent = &default_user_agent; } -}; +} // namespace void SetUserAgent(const std::string& new_user_agent) { DCHECK(!user_agent_requested) << "Setting the user agent after someone has " @@ -399,13 +406,23 @@ void SetUserAgent(const std::string& new_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_is_overridden = true; user_agent = &overridden_user_agent; } -const std::string& GetUserAgent() { +const std::string& GetUserAgent(const GURL& url) { if (!user_agent) SetUserAgentToDefault(); user_agent_requested = true; + if (!user_agent_is_overridden) { + static std::string mimic_safari_user_agent; + // For hotmail, we need to spoof as Safari (bug 4111). + if (MatchPattern(url.host(), "*.mail.live.com")) { + if (mimic_safari_user_agent.empty()) + BuildUserAgent(true, &mimic_safari_user_agent); + return mimic_safari_user_agent; + } + } return *user_agent; } diff --git a/webkit/glue/webkit_glue.h b/webkit/glue/webkit_glue.h index 50cd2c4..a3c4d2c 100644 --- a/webkit/glue/webkit_glue.h +++ b/webkit/glue/webkit_glue.h @@ -105,9 +105,10 @@ std::string GetWebKitVersion(); // 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(); +// Returns the user agent to use for the given URL, 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(const GURL& url); // 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/tools/test_shell/test_shell_request_context.cc b/webkit/tools/test_shell/test_shell_request_context.cc index 8f8202d..2597f77 100644 --- a/webkit/tools/test_shell/test_shell_request_context.cc +++ b/webkit/tools/test_shell/test_shell_request_context.cc @@ -25,8 +25,6 @@ void TestShellRequestContext::Init( bool no_proxy) { cookie_store_ = new net::CookieMonster(); - user_agent_ = webkit_glue::GetUserAgent(); - // hard-code A-L and A-C for test shells accept_language_ = "en-us,en"; accept_charset_ = "iso-8859-1,*,utf-8"; @@ -51,3 +49,7 @@ TestShellRequestContext::~TestShellRequestContext() { delete proxy_service_; } +const std::string& TestShellRequestContext::GetUserAgent( + const GURL& url) const { + return webkit_glue::GetUserAgent(url); +} diff --git a/webkit/tools/test_shell/test_shell_request_context.h b/webkit/tools/test_shell/test_shell_request_context.h index 45a899d..daf3ab2 100644 --- a/webkit/tools/test_shell/test_shell_request_context.h +++ b/webkit/tools/test_shell/test_shell_request_context.h @@ -22,6 +22,8 @@ class TestShellRequestContext : public URLRequestContext { ~TestShellRequestContext(); + virtual const std::string& GetUserAgent(const GURL& url) const; + private: void Init(const std::wstring& cache_path, net::HttpCache::Mode cache_mode, bool no_proxy); |