diff options
22 files changed, 123 insertions, 101 deletions
diff --git a/base/base.gypi b/base/base.gypi index 693f393..a5e819b 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -281,6 +281,8 @@ 'stringize_macros.h', 'stringprintf.cc', 'stringprintf.h', + 'supports_user_data.cc', + 'supports_user_data.h', 'synchronization/cancellation_flag.cc', 'synchronization/cancellation_flag.h', 'synchronization/condition_variable.h', diff --git a/base/supports_user_data.cc b/base/supports_user_data.cc new file mode 100644 index 0000000..2253c86 --- /dev/null +++ b/base/supports_user_data.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2012 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 "base/supports_user_data.h" + +namespace base { + +SupportsUserData::SupportsUserData() { +} + +SupportsUserData::~SupportsUserData() { +} + +SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const { + DataMap::const_iterator found = user_data_.find(key); + if (found != user_data_.end()) + return found->second.get(); + return NULL; +} + +void SupportsUserData::SetUserData(const void* key, Data* data) { + user_data_[key] = linked_ptr<Data>(data); +} + +} // namespace base diff --git a/base/supports_user_data.h b/base/supports_user_data.h new file mode 100644 index 0000000..0296a3e --- /dev/null +++ b/base/supports_user_data.h @@ -0,0 +1,47 @@ +// Copyright (c) 2012 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 BASE_SUPPORTS_USER_DATA_H_ +#define BASE_SUPPORTS_USER_DATA_H_ + +#include <map> + +#include "base/base_export.h" +#include "base/memory/linked_ptr.h" + +namespace base { + +// This is a helper for classes that want to allow users to stash random data by +// key. At destruction all the objects will be destructed. +class BASE_EXPORT SupportsUserData { + public: + SupportsUserData(); + virtual ~SupportsUserData(); + + // Derive from this class and add your own data members to associate extra + // information with this object. Use GetUserData(key) and SetUserData() + class BASE_EXPORT Data { + public: + virtual ~Data() {} + }; + + // The user data allows the clients to associate data with this object. + // Multiple user data values can be stored under different keys. + // This object will TAKE OWNERSHIP of the given data pointer, and will + // delete the object if it is changed or the object is destroyed. + Data* GetUserData(const void* key) const; + void SetUserData(const void* key, Data* data); + + private: + typedef std::map<const void*, linked_ptr<Data> > DataMap; + + // Externally-defined data accessible by key + DataMap user_data_; + + DISALLOW_COPY_AND_ASSIGN(SupportsUserData); +}; + +} // namespace base + +#endif // BASE_SUPPORTS_USER_DATA_H_ diff --git a/chrome/browser/browsing_data_appcache_helper.cc b/chrome/browser/browsing_data_appcache_helper.cc index 106d507..135b015 100644 --- a/chrome/browser/browsing_data_appcache_helper.cc +++ b/chrome/browser/browsing_data_appcache_helper.cc @@ -6,7 +6,6 @@ #include "base/bind.h" #include "base/bind_helpers.h" -#include "chrome/browser/net/chrome_url_request_context.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/url_constants.h" #include "content/public/browser/browser_thread.h" diff --git a/chrome/browser/renderer_host/chrome_url_request_user_data.cc b/chrome/browser/renderer_host/chrome_url_request_user_data.cc index 511cbf1..a1609e0 100644 --- a/chrome/browser/renderer_host/chrome_url_request_user_data.cc +++ b/chrome/browser/renderer_host/chrome_url_request_user_data.cc @@ -4,6 +4,8 @@ #include "chrome/browser/renderer_host/chrome_url_request_user_data.h" +#include "net/url_request/url_request.h" + namespace { const char* const kKeyName = "chrome_url_request_user_data"; diff --git a/chrome/browser/renderer_host/chrome_url_request_user_data.h b/chrome/browser/renderer_host/chrome_url_request_user_data.h index 5acc9bd..1c1071b 100644 --- a/chrome/browser/renderer_host/chrome_url_request_user_data.h +++ b/chrome/browser/renderer_host/chrome_url_request_user_data.h @@ -6,9 +6,13 @@ #define CHROME_BROWSER_RENDERER_HOST_CHROME_URL_REQUEST_USER_DATA_H_ #pragma once -#include "net/url_request/url_request.h" +#include "base/supports_user_data.h" -class ChromeURLRequestUserData : public net::URLRequest::UserData { +namespace net { +class URLRequest; +} + +class ChromeURLRequestUserData : public base::SupportsUserData::Data { public: bool is_prerender() const { return is_prerender_; } void set_is_prerender(bool is_prerender) { is_prerender_ = is_prerender; } diff --git a/content/browser/renderer_host/resource_dispatcher_host_request_info.cc b/content/browser/renderer_host/resource_dispatcher_host_request_info.cc index 74e1a54..60fc30c 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_request_info.cc +++ b/content/browser/renderer_host/resource_dispatcher_host_request_info.cc @@ -7,6 +7,7 @@ #include "content/browser/renderer_host/resource_handler.h" #include "content/browser/ssl/ssl_client_auth_handler.h" #include "content/public/browser/resource_dispatcher_host_login_delegate.h" +#include "net/url_request/url_request.h" #include "webkit/blob/blob_data.h" ResourceDispatcherHostRequestInfo::ResourceDispatcherHostRequestInfo( diff --git a/content/browser/renderer_host/resource_dispatcher_host_request_info.h b/content/browser/renderer_host/resource_dispatcher_host_request_info.h index 2296640..c4a0cb1 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_request_info.h +++ b/content/browser/renderer_host/resource_dispatcher_host_request_info.h @@ -9,13 +9,14 @@ #include <string> #include "base/basictypes.h" +#include "base/memory/ref_counted.h" +#include "base/supports_user_data.h" #include "base/time.h" #include "content/common/content_export.h" #include "content/public/common/page_transition_types.h" #include "content/public/common/process_type.h" #include "content/public/common/referrer.h" #include "net/base/load_states.h" -#include "net/url_request/url_request.h" #include "webkit/glue/resource_type.h" class ResourceDispatcherHost; @@ -28,13 +29,17 @@ class ResourceContext; class ResourceDispatcherHostLoginDelegate; } +namespace net { +class URLRequest; +} + namespace webkit_blob { class BlobData; } // Holds the data ResourceDispatcherHost associates with each request. // Retrieve this data by calling ResourceDispatcherHost::InfoForRequest. -class ResourceDispatcherHostRequestInfo : public net::URLRequest::UserData { +class ResourceDispatcherHostRequestInfo : public base::SupportsUserData::Data { public: // This will take a reference to the handler. CONTENT_EXPORT ResourceDispatcherHostRequestInfo( diff --git a/content/browser/renderer_host/resource_dispatcher_host_unittest.cc b/content/browser/renderer_host/resource_dispatcher_host_unittest.cc index 1a89561..5075014 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_unittest.cc +++ b/content/browser/renderer_host/resource_dispatcher_host_unittest.cc @@ -270,7 +270,7 @@ URLRequestTestDelayedStartJob* URLRequestTestDelayedStartJob::list_head_ = NULL; // Associated with an URLRequest to determine if the URLRequest gets deleted. -class TestUserData : public net::URLRequest::UserData { +class TestUserData : public base::SupportsUserData::Data { public: explicit TestUserData(bool* was_deleted) : was_deleted_(was_deleted) { @@ -291,7 +291,7 @@ class TestResourceDispatcherHostDelegate : defer_start_(false) { } - void set_url_request_user_data(net::URLRequest::UserData* user_data) { + void set_url_request_user_data(base::SupportsUserData::Data* user_data) { user_data_.reset(user_data); } @@ -321,7 +321,7 @@ class TestResourceDispatcherHostDelegate private: bool defer_start_; - scoped_ptr<net::URLRequest::UserData> user_data_; + scoped_ptr<base::SupportsUserData::Data> user_data_; }; class ResourceDispatcherHostTest : public testing::Test, diff --git a/content/browser/renderer_host/resource_queue.cc b/content/browser/renderer_host/resource_queue.cc index ef0ebe3..50ea004 100644 --- a/content/browser/renderer_host/resource_queue.cc +++ b/content/browser/renderer_host/resource_queue.cc @@ -8,6 +8,7 @@ #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/global_request_id.h" +#include "net/url_request/url_request.h" using content::BrowserThread; using content::GlobalRequestID; diff --git a/content/browser/ssl/ssl_host_state.cc b/content/browser/ssl/ssl_host_state.cc index 67f3fd8..e0ceea4 100644 --- a/content/browser/ssl/ssl_host_state.cc +++ b/content/browser/ssl/ssl_host_state.cc @@ -6,25 +6,20 @@ #include "base/logging.h" #include "base/lazy_instance.h" -#include "content/public/browser/notification_details.h" -#include "content/public/browser/notification_source.h" -#include "content/public/browser/notification_types.h" +#include "content/public/browser/browser_context.h" -namespace { -typedef std::map<content::BrowserContext*, SSLHostState*> HostStateMap; -static base::LazyInstance<HostStateMap> g_host_state_map = - LAZY_INSTANCE_INITIALIZER; -} +static const char* kKeyName = "content_ssl_host_state"; -SSLHostState* SSLHostState::GetFor(content::BrowserContext* browser_context) { - if (!g_host_state_map.Get().count(browser_context)) - g_host_state_map.Get()[browser_context] = new SSLHostState(browser_context); - return g_host_state_map.Get()[browser_context]; +SSLHostState* SSLHostState::GetFor(content::BrowserContext* context) { + SSLHostState* rv = static_cast<SSLHostState*>(context->GetUserData(kKeyName)); + if (!rv) { + rv = new SSLHostState(); + context->SetUserData(kKeyName, rv); + } + return rv; } -SSLHostState::SSLHostState(content::BrowserContext* browser_context) { - registrar_.Add(this, content::NOTIFICATION_BROWSER_CONTEXT_DESTRUCTION, - content::Source<content::BrowserContext>(browser_context)); +SSLHostState::SSLHostState() { } SSLHostState::~SSLHostState() { @@ -61,9 +56,3 @@ net::CertPolicy::Judgment SSLHostState::QueryPolicy( return cert_policy_for_host_[host].Check(cert); } - -void SSLHostState::Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - delete this; -} diff --git a/content/browser/ssl/ssl_host_state.h b/content/browser/ssl/ssl_host_state.h index 498046e..b051b05 100644 --- a/content/browser/ssl/ssl_host_state.h +++ b/content/browser/ssl/ssl_host_state.h @@ -12,10 +12,9 @@ #include "base/compiler_specific.h" #include "base/basictypes.h" +#include "base/supports_user_data.h" #include "base/threading/non_thread_safe.h" #include "content/common/content_export.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" #include "googleurl/src/gurl.h" #include "net/base/x509_certificate.h" @@ -32,12 +31,12 @@ class BrowserContext; // controllers. class CONTENT_EXPORT SSLHostState - : public content::NotificationObserver, + : NON_EXPORTED_BASE(base::SupportsUserData::Data), NON_EXPORTED_BASE(public base::NonThreadSafe) { public: static SSLHostState* GetFor(content::BrowserContext* browser_context); - explicit SSLHostState(content::BrowserContext* browser_context); + SSLHostState(); virtual ~SSLHostState(); // Records that a host has run insecure content. @@ -57,10 +56,6 @@ class CONTENT_EXPORT SSLHostState net::X509Certificate* cert, const std::string& host); private: - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; - // A BrokenHostEntry is a pair of (host, process_id) that indicates the host // contains insecure content in that renderer process. typedef std::pair<std::string, int> BrokenHostEntry; @@ -73,8 +68,6 @@ class CONTENT_EXPORT SSLHostState // Certificate policies for each host. std::map<std::string, net::CertPolicy> cert_policy_for_host_; - content::NotificationRegistrar registrar_; - DISALLOW_COPY_AND_ASSIGN(SSLHostState); }; diff --git a/content/browser/ssl/ssl_host_state_unittest.cc b/content/browser/ssl/ssl_host_state_unittest.cc index e057262..08a589f 100644 --- a/content/browser/ssl/ssl_host_state_unittest.cc +++ b/content/browser/ssl/ssl_host_state_unittest.cc @@ -92,7 +92,7 @@ class SSLHostStateTest : public testing::Test { }; TEST_F(SSLHostStateTest, DidHostRunInsecureContent) { - SSLHostState state(NULL); + SSLHostState state; EXPECT_FALSE(state.DidHostRunInsecureContent("www.google.com", 42)); EXPECT_FALSE(state.DidHostRunInsecureContent("www.google.com", 191)); @@ -116,7 +116,7 @@ TEST_F(SSLHostStateTest, QueryPolicy) { net::X509Certificate::CreateFromBytes( reinterpret_cast<const char*>(google_der), sizeof(google_der))); - SSLHostState state(NULL); + SSLHostState state; EXPECT_EQ(state.QueryPolicy(google_cert.get(), "www.google.com"), net::CertPolicy::UNKNOWN); diff --git a/content/content_browser.gypi b/content/content_browser.gypi index b9b9346..1010d5e 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -32,7 +32,6 @@ 'public/browser/browser_child_process_host_delegate.h', 'public/browser/browser_child_process_host_iterator.cc', 'public/browser/browser_child_process_host_iterator.h', - 'public/browser/browser_context.cc', 'public/browser/browser_context.h', 'public/browser/browser_main_parts.h', 'public/browser/browser_main_runner.h', diff --git a/content/public/browser/browser_context.cc b/content/public/browser/browser_context.cc index c3f931e..b8f540f 100644 --- a/content/public/browser/browser_context.cc +++ b/content/public/browser/browser_context.cc @@ -4,20 +4,7 @@ #include "content/public/browser/browser_context.h" -#include "base/logging.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/notification_details.h" -#include "content/public/browser/notification_service.h" -#include "content/public/browser/notification_source.h" -#include "content/public/browser/notification_types.h" - namespace content { -BrowserContext::~BrowserContext() { - NotificationService::current()->Notify( - content::NOTIFICATION_BROWSER_CONTEXT_DESTRUCTION, - content::Source<BrowserContext>(this), - content::NotificationService::NoDetails()); -} } // namespace content diff --git a/content/public/browser/browser_context.h b/content/public/browser/browser_context.h index 6832bec..9618315 100644 --- a/content/public/browser/browser_context.h +++ b/content/public/browser/browser_context.h @@ -7,6 +7,7 @@ #pragma once #include "base/hash_tables.h" +#include "base/supports_user_data.h" #include "content/common/content_export.h" namespace fileapi { @@ -40,9 +41,9 @@ class SpeechInputPreferences; // This class holds the context needed for a browsing session. // It lives on the UI thread. -class CONTENT_EXPORT BrowserContext { +class CONTENT_EXPORT BrowserContext : public base::SupportsUserData { public: - virtual ~BrowserContext(); + virtual ~BrowserContext() {} // Returns the path of the directory where this context's data is stored. virtual FilePath GetPath() = 0; diff --git a/content/public/browser/notification_types.h b/content/public/browser/notification_types.h index b7b45ce..5e4fb1a 100644 --- a/content/public/browser/notification_types.h +++ b/content/public/browser/notification_types.h @@ -414,10 +414,6 @@ enum NotificationType { // of a temporary zoom level change, the details is an empty string. NOTIFICATION_ZOOM_LEVEL_CHANGED, - // Sent when a BrowserContext is being deleted, in case any objects want to do - // related cleanup. The source is the BrowserContext. - NOTIFICATION_BROWSER_CONTEXT_DESTRUCTION, - // Custom notifications used by the embedder should start from here. NOTIFICATION_CONTENT_END, }; diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc index 114efc4..b871d07 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -762,17 +762,6 @@ int64 URLRequest::GetExpectedContentSize() const { return expected_content_size; } -URLRequest::UserData* URLRequest::GetUserData(const void* key) const { - UserDataMap::const_iterator found = user_data_.find(key); - if (found != user_data_.end()) - return found->second.get(); - return NULL; -} - -void URLRequest::SetUserData(const void* key, UserData* data) { - user_data_[key] = linked_ptr<UserData>(data); -} - void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { NetworkDelegate::AuthRequiredResponse rv = NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index 1d2c5a2..918a7e6 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -6,15 +6,14 @@ #define NET_URL_REQUEST_URL_REQUEST_H_ #pragma once -#include <map> #include <string> #include <vector> #include "base/debug/leak_tracker.h" #include "base/logging.h" -#include "base/memory/linked_ptr.h" #include "base/memory/ref_counted.h" #include "base/string16.h" +#include "base/supports_user_data.h" #include "base/time.h" #include "base/threading/non_thread_safe.h" #include "googleurl/src/gurl.h" @@ -99,7 +98,8 @@ typedef std::vector<std::string> ResponseCookies; // // NOTE: All usage of all instances of this class should be on the same thread. // -class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { +class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), + public base::SupportsUserData { public: // Callback function implemented by protocol handlers to create new jobs. // The factory may return NULL to indicate an error, which will cause other @@ -116,14 +116,6 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { #undef HTTP_ATOM }; - // Derive from this class and add your own data members to associate extra - // information with a URLRequest. Use GetUserData(key) and SetUserData() - class UserData { - public: - UserData() {} - virtual ~UserData() {} - }; - // This class handles network interception. Use with // (Un)RegisterRequestInterceptor. class NET_EXPORT Interceptor { @@ -307,14 +299,7 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { // If destroyed after Start() has been called but while IO is pending, // then the request will be effectively canceled and the delegate // will not have any more of its methods called. - ~URLRequest(); - - // The user data allows the clients to associate data with this request. - // Multiple user data values can be stored under different keys. - // This request will TAKE OWNERSHIP of the given data pointer, and will - // delete the object if it is changed or the request is destroyed. - UserData* GetUserData(const void* key) const; - void SetUserData(const void* key, UserData* data); + virtual ~URLRequest(); // Returns true if the scheme can be handled by URLRequest. False otherwise. static bool IsHandledProtocol(const std::string& scheme); @@ -631,8 +616,6 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { private: friend class URLRequestJob; - typedef std::map<const void*, linked_ptr<UserData> > UserDataMap; - // Registers a new protocol handler for the given scheme. If the scheme is // already handled, this will overwrite the given factory. To delete the // protocol factory, use NULL for the factory BUT this WILL NOT put back @@ -741,9 +724,6 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { // whether the job is active. bool is_pending_; - // Externally-defined data accessible by key - UserDataMap user_data_; - // Number of times we're willing to redirect. Used to guard against // infinite redirects. int redirect_limit_; diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 1398664..3cee659 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -3268,9 +3268,9 @@ TEST_F(URLRequestTest, Intercept) { TestDelegate d; TestURLRequest req(GURL("http://test_intercept/foo"), &d); req.set_context(default_context_); - URLRequest::UserData* user_data0 = new URLRequest::UserData(); - URLRequest::UserData* user_data1 = new URLRequest::UserData(); - URLRequest::UserData* user_data2 = new URLRequest::UserData(); + base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); + base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); + base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); req.SetUserData(NULL, user_data0); req.SetUserData(&user_data1, user_data1); req.SetUserData(&user_data2, user_data2); diff --git a/webkit/appcache/appcache_host.cc b/webkit/appcache/appcache_host.cc index ff0cb3e..61ff5be 100644 --- a/webkit/appcache/appcache_host.cc +++ b/webkit/appcache/appcache_host.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "base/string_util.h" #include "base/stringprintf.h" +#include "net/url_request/url_request.h" #include "webkit/appcache/appcache.h" #include "webkit/appcache/appcache_backend_impl.h" #include "webkit/appcache/appcache_policy.h" diff --git a/webkit/appcache/appcache_request_handler.h b/webkit/appcache/appcache_request_handler.h index 6f45358..f7e8c23 100644 --- a/webkit/appcache/appcache_request_handler.h +++ b/webkit/appcache/appcache_request_handler.h @@ -6,7 +6,7 @@ #define WEBKIT_APPCACHE_APPCACHE_REQUEST_HANDLER_H_ #include "base/compiler_specific.h" -#include "net/url_request/url_request.h" +#include "base/supports_user_data.h" #include "webkit/appcache/appcache_entry.h" #include "webkit/appcache/appcache_export.h" #include "webkit/appcache/appcache_host.h" @@ -27,7 +27,7 @@ class AppCacheURLRequestJob; // should use AppCacheHost::CreateRequestHandler to manufacture instances // that can retrieve resources for a particular host. class APPCACHE_EXPORT AppCacheRequestHandler - : NON_EXPORTED_BASE(public net::URLRequest::UserData), + : public base::SupportsUserData::Data, public AppCacheHost::Observer, public AppCacheStorage::Delegate { public: |