diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-25 08:31:47 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-25 08:31:47 +0000 |
commit | 0f7066ec8158cd03ddf8b7b6075a54b1da09673f (patch) | |
tree | 8a57df1ffd95eb6910339f757e12390b11d5be4c /chrome/browser/net | |
parent | 24a7f3c1308fd32d8620f5bd412ede8f84da9794 (diff) | |
download | chromium_src-0f7066ec8158cd03ddf8b7b6075a54b1da09673f.zip chromium_src-0f7066ec8158cd03ddf8b7b6075a54b1da09673f.tar.gz chromium_src-0f7066ec8158cd03ddf8b7b6075a54b1da09673f.tar.bz2 |
Reland r42473. Add a delegate to CookieMonster and broadcast notifications about changes to cookies.
This change will allow implementing the experimental cookie extension API, specifically the cookies.onChaned event
BUG=none
TEST=net_unittests
TBR=jochen@
Review URL: http://codereview.chromium.org/1287001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42586 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r-- | chrome/browser/net/chrome_cookie_notification_details.h | 22 | ||||
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.cc | 101 | ||||
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.h | 2 |
3 files changed, 121 insertions, 4 deletions
diff --git a/chrome/browser/net/chrome_cookie_notification_details.h b/chrome/browser/net/chrome_cookie_notification_details.h new file mode 100644 index 0000000..a4ee4a0 --- /dev/null +++ b/chrome/browser/net/chrome_cookie_notification_details.h @@ -0,0 +1,22 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_NET_CHROME_COOKIE_NOTIFICATION_DETAILS_H_ +#define CHROME_BROWSER_NET_CHROME_COOKIE_NOTIFICATION_DETAILS_H_ + +#include "net/base/cookie_monster.h" + +struct ChromeCookieDetails { + public: + ChromeCookieDetails(net::CookieMonster::CookieListPair* cookie_pair_copy, + bool is_removed) + : cookie_pair(cookie_pair_copy), + removed(is_removed) { + } + + net::CookieMonster::CookieListPair* cookie_pair; + bool removed; +}; + +#endif // CHROME_BROWSER_NET_CHROME_COOKIE_NOTIFICATION_DETAILS_H_ diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc index f19bd08..be1e63b 100644 --- a/chrome/browser/net/chrome_url_request_context.cc +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -11,6 +11,7 @@ #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/extensions/user_script_master.h" #include "chrome/browser/io_thread.h" +#include "chrome/browser/net/chrome_cookie_notification_details.h" #include "chrome/browser/net/chrome_net_log.h" #include "chrome/browser/net/sqlite_persistent_cookie_store.h" #include "chrome/browser/net/dns_global.h" @@ -107,6 +108,92 @@ net::ProxyService* CreateProxyService( } // ---------------------------------------------------------------------------- +// CookieMonster::Delegate implementation +// ---------------------------------------------------------------------------- +class ChromeCookieMonsterDelegate : public net::CookieMonster::Delegate { + public: + explicit ChromeCookieMonsterDelegate(Profile* profile) { + CheckCurrentlyOnMainThread(); + profile_getter_ = new ProfileGetter(profile); + } + + // net::CookieMonster::Delegate implementation. + virtual void OnCookieChanged( + const std::string& domain_key, + const net::CookieMonster::CanonicalCookie& cookie, + bool removed) { + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod(this, + &ChromeCookieMonsterDelegate::OnCookieChangedAsyncHelper, + net::CookieMonster::CookieListPair(domain_key, cookie), + removed)); + } + + private: + // This class allows us to safely access the Profile pointer. The Delegate + // itself cannot observe the PROFILE_DESTROYED notification, since it cannot + // guarantee to be deleted on the UI thread and therefore unregister from + // the notifications. All methods of ProfileGetter must be invoked on the UI + // thread. + class ProfileGetter + : public base::RefCountedThreadSafe<ProfileGetter, + ChromeThread::DeleteOnUIThread>, + public NotificationObserver { + public: + explicit ProfileGetter(Profile* profile) : profile_(profile) { + CheckCurrentlyOnMainThread(); + registrar_.Add(this, + NotificationType::PROFILE_DESTROYED, + Source<Profile>(profile_)); + } + + // NotificationObserver implementation. + void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + CheckCurrentlyOnMainThread(); + if (NotificationType::PROFILE_DESTROYED == type) { + Profile* profile = Source<Profile>(source).ptr(); + if (profile_ == profile) + profile_ = NULL; + } + } + + Profile* get() { + CheckCurrentlyOnMainThread(); + return profile_; + } + + private: + friend class ::ChromeThread; + friend class DeleteTask<ProfileGetter>; + + virtual ~ProfileGetter() {} + + NotificationRegistrar registrar_; + + Profile* profile_; + }; + + virtual ~ChromeCookieMonsterDelegate() {} + + void OnCookieChangedAsyncHelper( + net::CookieMonster::CookieListPair cookie_pair, + bool removed) { + if (profile_getter_->get()) { + ChromeCookieDetails cookie_details(&cookie_pair, removed); + NotificationService::current()->Notify( + NotificationType::COOKIE_CHANGED, + Source<Profile>(profile_getter_->get()), + Details<ChromeCookieDetails>(&cookie_details)); + } + } + + scoped_refptr<ProfileGetter> profile_getter_; +}; + +// ---------------------------------------------------------------------------- // Helper factories // ---------------------------------------------------------------------------- @@ -175,7 +262,8 @@ ChromeURLRequestContext* FactoryForOriginal::Create() { if (record_mode || playback_mode) { // Don't use existing cookies and use an in-memory store. - context->set_cookie_store(new net::CookieMonster(NULL)); + context->set_cookie_store(new net::CookieMonster(NULL, + cookie_monster_delegate_)); cache->set_mode( record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); } @@ -190,7 +278,8 @@ ChromeURLRequestContext* FactoryForOriginal::Create() { scoped_refptr<SQLitePersistentCookieStore> cookie_db = new SQLitePersistentCookieStore(cookie_store_path_); - context->set_cookie_store(new net::CookieMonster(cookie_db.get())); + context->set_cookie_store(new net::CookieMonster(cookie_db.get(), + cookie_monster_delegate_)); } context->set_cookie_policy( @@ -232,7 +321,8 @@ ChromeURLRequestContext* FactoryForExtensions::Create() { scoped_refptr<SQLitePersistentCookieStore> cookie_db = new SQLitePersistentCookieStore(cookie_store_path_); - net::CookieMonster* cookie_monster = new net::CookieMonster(cookie_db.get()); + net::CookieMonster* cookie_monster = + new net::CookieMonster(cookie_db.get(), NULL); // Enable cookies for extension URLs only. const char* schemes[] = {chrome::kExtensionScheme}; @@ -282,7 +372,8 @@ ChromeURLRequestContext* FactoryForOffTheRecord::Create() { context->ssl_config_service(), context->http_auth_handler_factory(), 0); - context->set_cookie_store(new net::CookieMonster(NULL)); + context->set_cookie_store(new net::CookieMonster(NULL, + cookie_monster_delegate_)); context->set_cookie_policy( new ChromeCookiePolicy(host_content_settings_map_)); context->set_http_transaction_factory(cache); @@ -865,6 +956,8 @@ ChromeURLRequestContextFactory::ChromeURLRequestContextFactory(Profile* profile) ssl_config_service_ = profile->GetSSLConfigService(); profile_dir_path_ = profile->GetPath(); + + cookie_monster_delegate_ = new ChromeCookieMonsterDelegate(profile); } ChromeURLRequestContextFactory::~ChromeURLRequestContextFactory() { diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h index 7e44d04..d483600 100644 --- a/chrome/browser/net/chrome_url_request_context.h +++ b/chrome/browser/net/chrome_url_request_context.h @@ -7,6 +7,7 @@ #include "base/file_path.h" #include "base/linked_ptr.h" +#include "net/base/cookie_monster.h" #include "net/base/cookie_policy.h" #include "chrome/browser/appcache/chrome_appcache_service.h" #include "chrome/browser/host_content_settings_map.h" @@ -384,6 +385,7 @@ class ChromeURLRequestContextFactory { scoped_refptr<Blacklist> privacy_blacklist_; scoped_refptr<net::TransportSecurityState> transport_security_state_; scoped_refptr<net::SSLConfigService> ssl_config_service_; + scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate_; FilePath profile_dir_path_; |