diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-24 16:06:01 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-24 16:06:01 +0000 |
commit | e5e9dd07968a2811196b262e739633e373837acc (patch) | |
tree | e21719077227acef1a165aea1406c08b036ae6be /chrome | |
parent | c0591e5a622fd3e573ebf03e95d1ab2d7d3c265f (diff) | |
download | chromium_src-e5e9dd07968a2811196b262e739633e373837acc.zip chromium_src-e5e9dd07968a2811196b262e739633e373837acc.tar.gz chromium_src-e5e9dd07968a2811196b262e739633e373837acc.tar.bz2 |
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.onChanged event
BUG=38398
TEST=net_unittests
Review URL: http://codereview.chromium.org/1023004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42473 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-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 | ||||
-rw-r--r-- | chrome/browser/sync/glue/http_bridge.cc | 2 | ||||
-rwxr-xr-x | chrome/chrome_browser.gypi | 1 | ||||
-rw-r--r-- | chrome/common/notification_type.h | 6 | ||||
-rw-r--r-- | chrome/test/testing_profile.cc | 2 |
7 files changed, 130 insertions, 6 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_; diff --git a/chrome/browser/sync/glue/http_bridge.cc b/chrome/browser/sync/glue/http_bridge.cc index 37ae729..d22e780 100644 --- a/chrome/browser/sync/glue/http_bridge.cc +++ b/chrome/browser/sync/glue/http_bridge.cc @@ -66,7 +66,7 @@ HttpBridge::RequestContext::RequestContext(URLRequestContext* baseline_context) : baseline_context_(baseline_context) { // Create empty, in-memory cookie store. - cookie_store_ = new net::CookieMonster(NULL); + cookie_store_ = new net::CookieMonster(NULL, NULL); // We don't use a cache for bridged loads, but we do want to share proxy info. host_resolver_ = baseline_context->host_resolver(); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index a0596e6..5de47ed 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1518,6 +1518,7 @@ 'browser/nacl_host/nacl_process_host.h', 'browser/net/browser_url_util.cc', 'browser/net/browser_url_util.h', + 'browser/net/chrome_cookie_notification_details.h', 'browser/net/chrome_cookie_policy.cc', 'browser/net/chrome_cookie_policy.h', 'browser/net/chrome_net_log.cc', diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index 026df5c..53a2430 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -896,6 +896,12 @@ class NotificationType { // The sync service is finished the configuration process. SYNC_CONFIGURE_DONE, + // Cookies ----------------------------------------------------------------- + + // Sent when a cookie changes. The source is a Profile object, the details + // are a ChromeCookieDetails object. + COOKIE_CHANGED, + #if defined(OS_CHROMEOS) // Sent when a chromium os user logs in. LOGIN_USER_CHANGED, diff --git a/chrome/test/testing_profile.cc b/chrome/test/testing_profile.cc index f9d4924..bafda74 100644 --- a/chrome/test/testing_profile.cc +++ b/chrome/test/testing_profile.cc @@ -89,7 +89,7 @@ class BookmarkLoadObserver : public BookmarkModelObserver { class TestURLRequestContext : public URLRequestContext { public: TestURLRequestContext() { - cookie_store_ = new net::CookieMonster(NULL); + cookie_store_ = new net::CookieMonster(NULL, NULL); } }; |