diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-23 22:34:50 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-23 22:34:50 +0000 |
commit | 6ab9b20222cb8ac94dea662c3d65a59839817dd4 (patch) | |
tree | 7797501d237c55b031569418caac62eb1b94ea97 /chrome/browser/net | |
parent | be4576e292be2d77ffc45ef4df8245a0059e3d5f (diff) | |
download | chromium_src-6ab9b20222cb8ac94dea662c3d65a59839817dd4.zip chromium_src-6ab9b20222cb8ac94dea662c3d65a59839817dd4.tar.gz chromium_src-6ab9b20222cb8ac94dea662c3d65a59839817dd4.tar.bz2 |
Refactor the two URLRequestContext subclasses in profile.cc into a new shared
ChromeRequestContext class. This will allow us to put browser-specific request
context here rather than in URLRequestContext and eliminates a lot of
duplicate code.
I looked at having two different classes using either inheritance (as proposed
by the existing TODO) or composition, but it seems like there isn't enough
difference between these two classes to justify it.
Removed is_off_the_record() because it wasn't being used anywhere and did a
few other minor code cleanup things.
Review URL: http://codereview.chromium.org/16408
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7448 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.cc | 171 | ||||
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.h | 58 |
2 files changed, 229 insertions, 0 deletions
diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc new file mode 100644 index 0000000..d5ce7fb --- /dev/null +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -0,0 +1,171 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/net/chrome_url_request_context.h" + +#include "base/command_line.h" +#include "base/string_util.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/profile.h" +#include "chrome/common/chrome_constants.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/pref_names.h" +#include "net/http/http_cache.h" +#include "net/proxy/proxy_service.h" +#include "webkit/glue/webkit_glue.h" + +// Sets up proxy info if it was specified, otherwise returns NULL. The +// returned pointer MUST be deleted by the caller if non-NULL. +static net::ProxyInfo* CreateProxyInfo() { + net::ProxyInfo* proxy_info = NULL; + + CommandLine command_line; + if (command_line.HasSwitch(switches::kProxyServer)) { + proxy_info = new net::ProxyInfo(); + const std::wstring& proxy_server = + command_line.GetSwitchValue(switches::kProxyServer); + proxy_info->UseNamedProxy(WideToASCII(proxy_server)); + } + + return proxy_info; +} + +// static +ChromeURLRequestContext* ChromeURLRequestContext::CreateOriginal( + Profile* profile, const std::wstring& cookie_store_path, + const std::wstring& disk_cache_path) { + DCHECK(!profile->IsOffTheRecord()); + ChromeURLRequestContext* context = new ChromeURLRequestContext(profile); + + scoped_ptr<net::ProxyInfo> proxy_info(CreateProxyInfo()); + context->proxy_service_ = net::ProxyService::Create(proxy_info.get()); + + net::HttpCache* cache = + new net::HttpCache(context->proxy_service_, disk_cache_path, 0); + + CommandLine command_line; + bool record_mode = chrome::kRecordModeEnabled && + command_line.HasSwitch(switches::kRecordMode); + bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode); + + if (record_mode || playback_mode) { + // Don't use existing cookies and use an in-memory store. + context->cookie_store_ = new net::CookieMonster(); + cache->set_mode( + record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); + } + context->http_transaction_factory_ = cache; + + // setup cookie store + if (!context->cookie_store_) { + DCHECK(!cookie_store_path.empty()); + context->cookie_db_.reset(new SQLitePersistentCookieStore( + cookie_store_path, g_browser_process->db_thread()->message_loop())); + context->cookie_store_ = new net::CookieMonster(context->cookie_db_.get()); + } + + return context; +} + +// static +ChromeURLRequestContext* ChromeURLRequestContext::CreateOffTheRecord( + Profile* profile) { + DCHECK(profile->IsOffTheRecord());
+ ChromeURLRequestContext* context = new ChromeURLRequestContext(profile);
+
+ // Share the same proxy service as the original profile. This proxy
+ // service's lifespan is dependent on the lifespan of the original profile,
+ // which we reference (see above).
+ context->proxy_service_ =
+ profile->GetOriginalProfile()->GetRequestContext()->proxy_service();
+
+ context->http_transaction_factory_ = + new net::HttpCache(context->proxy_service_, 0); + context->cookie_store_ = new net::CookieMonster; + + return context; +} + +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 + // TODO(jungshik) : This may slow down http requests. Perhaps, + // we have to come up with a better way to set up these values. + accept_language_ = WideToASCII(prefs_->GetString(prefs::kAcceptLanguages)); + accept_charset_ = WideToASCII(prefs_->GetString(prefs::kDefaultCharset)); + accept_charset_ += ",*,utf-8"; + + cookie_policy_.SetType(net::CookiePolicy::FromInt( + prefs_->GetInteger(prefs::kCookieBehavior))); + + prefs_->AddPrefObserver(prefs::kAcceptLanguages, this); + prefs_->AddPrefObserver(prefs::kCookieBehavior, this); +} + +// NotificationObserver implementation. +void ChromeURLRequestContext::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (NOTIFY_PREF_CHANGED == type) { + std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); + PrefService* prefs = Source<PrefService>(source).ptr(); + DCHECK(pref_name_in && prefs); + if (*pref_name_in == prefs::kAcceptLanguages) { + std::string accept_language = + WideToASCII(prefs->GetString(prefs::kAcceptLanguages)); + g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, + NewRunnableMethod(this, + &ChromeURLRequestContext::OnAcceptLanguageChange, + accept_language)); + } else if (*pref_name_in == prefs::kCookieBehavior) { + net::CookiePolicy::Type type = net::CookiePolicy::FromInt( + prefs_->GetInteger(prefs::kCookieBehavior)); + g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, + NewRunnableMethod(this, + &ChromeURLRequestContext::OnCookiePolicyChange, + type)); + } + } else { + NOTREACHED(); + } +} + +void ChromeURLRequestContext::CleanupOnUIThread() { + // Unregister for pref notifications. + prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this); + prefs_->RemovePrefObserver(prefs::kCookieBehavior, this); + prefs_ = NULL; +} + +void ChromeURLRequestContext::OnAcceptLanguageChange(std::string accept_language) { + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::IO)); + accept_language_ = accept_language; +} + +void ChromeURLRequestContext::OnCookiePolicyChange(net::CookiePolicy::Type type) { + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::IO)); + cookie_policy_.SetType(type); +} + +ChromeURLRequestContext::~ChromeURLRequestContext() { + DCHECK(NULL == prefs_); + + NotificationService::current()->Notify(NOTIFY_URL_REQUEST_CONTEXT_RELEASED, + Source<URLRequestContext>(this), + NotificationService::NoDetails()); + + delete cookie_store_; + delete http_transaction_factory_; + + // Do not delete the proxy service in the case of OTR, as it is owned by the + // original URLRequestContext. + if (!is_off_the_record_) + delete proxy_service_; +} diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h new file mode 100644 index 0000000..ab62e60 --- /dev/null +++ b/chrome/browser/net/chrome_url_request_context.h @@ -0,0 +1,58 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/net/cookie_monster_sqlite.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/pref_service.h" +#include "net/url_request/url_request_context.h" + +class Profile; + +// A URLRequestContext subclass used by the browser. This can be used to store +// extra information about requests, beyond what is supported by the base +// URLRequestContext class. +// +// All methods are expected to be called on the IO thread except the +// constructor and factories (CreateOriginal, CreateOffTheRecord), which are +// expected to be called on the UI thread. +class ChromeURLRequestContext : public URLRequestContext, + public NotificationObserver { + public: + // Create an instance for use with an 'original' (non-OTR) profile. This is + // expected to get called on the UI thread. + static ChromeURLRequestContext* CreateOriginal( + Profile* profile, const std::wstring& cookie_store_path, + const std::wstring& disk_cache_path); + + // Create an instance for use with an OTR profile. This is expected to get + // called on the UI thread. + static ChromeURLRequestContext* CreateOffTheRecord(Profile* profile); + + // Clean up UI thread resources. This is expected to get called on the UI + // thread before the instance is deleted on the IO thread. + void CleanupOnUIThread(); + + private: + // Private constructor, use the static factory methods instead. This is + // expected to be called on the UI thread. + ChromeURLRequestContext(Profile* profile); + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // Callback for when the accept language changes. + void OnAcceptLanguageChange(std::string accept_language); + + // Callback for when the cookie policy changes. + void OnCookiePolicyChange(net::CookiePolicy::Type type); + + // Destructor. + virtual ~ChromeURLRequestContext(); + + scoped_ptr<SQLitePersistentCookieStore> cookie_db_; + PrefService* prefs_; + bool is_off_the_record_; +}; |