summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-10 00:29:37 +0000
committertony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-10 00:29:37 +0000
commit4b50cb5974b9465055113a92494ec68fe0a33ea8 (patch)
tree98609f2d31a9749abd013a0191e613c54a98db2e
parentb3a5bb10518d4710e0434934450cdfb0b9ca5784 (diff)
downloadchromium_src-4b50cb5974b9465055113a92494ec68fe0a33ea8.zip
chromium_src-4b50cb5974b9465055113a92494ec68fe0a33ea8.tar.gz
chromium_src-4b50cb5974b9465055113a92494ec68fe0a33ea8.tar.bz2
Extends NetworkDelegate to avoid use of static_cast<> when handling RegisterProtocolHandler URLs.
Currently we use a static_cast<> to convert a URLRequestContext into a ChromeURLRequestContext in a ProtocolFactory. However, ProtocolFactory is global and not all URLRequestContexts are instances of ChromeURLRequestContext, so this will always crash in certain circumstances (such as the one described in the TEST field). This change solves the problem by providing a MaybeCreateURLRequestJob() method in NetworkDelegate and then having the ProtocolHandlerRegistry::Factory() call it instead of casting. BUG=74063 TEST=Requesting a URL that has a registerProtocolHandler() from chrome://net-internals/#tests doesn't crash. Review URL: http://codereview.chromium.org/6592060 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77559 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/custom_handlers/protocol_handler_registry.cc28
-rw-r--r--chrome/browser/custom_handlers/protocol_handler_registry.h9
-rw-r--r--chrome/browser/io_thread.cc2
-rw-r--r--chrome/browser/net/chrome_network_delegate.cc15
-rw-r--r--chrome/browser/net/chrome_network_delegate.h7
-rw-r--r--chrome/browser/net/chrome_url_request_context.h9
-rw-r--r--chrome/browser/profiles/off_the_record_profile_io_data.cc3
-rw-r--r--chrome/browser/profiles/profile_impl_io_data.cc3
-rw-r--r--chrome/browser/profiles/profile_io_data.cc3
-rw-r--r--chrome/browser/profiles/profile_io_data.h1
-rw-r--r--net/base/network_delegate.cc6
-rw-r--r--net/base/network_delegate.h13
-rw-r--r--net/url_request/url_request.h3
-rw-r--r--net/url_request/url_request_test_util.cc5
-rw-r--r--net/url_request/url_request_test_util.h2
15 files changed, 73 insertions, 36 deletions
diff --git a/chrome/browser/custom_handlers/protocol_handler_registry.cc b/chrome/browser/custom_handlers/protocol_handler_registry.cc
index 20ce34b..d273cdc 100644
--- a/chrome/browser/custom_handlers/protocol_handler_registry.cc
+++ b/chrome/browser/custom_handlers/protocol_handler_registry.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/common/pref_names.h"
#include "content/browser/child_process_security_policy.h"
+#include "net/base/network_delegate.h"
#include "net/url_request/url_request_redirect_job.h"
@@ -79,29 +80,28 @@ bool ProtocolHandlerRegistry::IsAlreadyRegistered(
return currentHandler && *currentHandler == *handler;
}
-net::URLRequestJob* ProtocolHandlerRegistry::CreateJob(
- net::URLRequest* request,
- const std::string& scheme) const {
- ProtocolHandler* handler = GetHandlerFor(scheme);
+net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request,
+ const std::string& scheme) {
+ return request->context()->network_delegate()->MaybeCreateURLRequestJob(
+ request);
+}
+
+
+net::URLRequestJob* ProtocolHandlerRegistry::MaybeCreateJob(
+ net::URLRequest* request) const {
+ ProtocolHandler* handler = GetHandlerFor(request->url().scheme());
if (!handler) {
return NULL;
}
- GURL translatedUrl(handler->TranslateUrl(request->url()));
+ GURL translated_url(handler->TranslateUrl(request->url()));
- if (!translatedUrl.is_valid()) {
+ if (!translated_url.is_valid()) {
return NULL;
}
- return new net::URLRequestRedirectJob(request, translatedUrl);
-}
-
-net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request,
- const std::string& scheme) {
- ChromeURLRequestContext* context =
- static_cast<ChromeURLRequestContext*>(request->context());
- return context->protocol_handler_registry()->CreateJob(request, scheme);
+ return new net::URLRequestRedirectJob(request, translated_url);
}
ProtocolHandlerRegistry::~ProtocolHandlerRegistry() {}
diff --git a/chrome/browser/custom_handlers/protocol_handler_registry.h b/chrome/browser/custom_handlers/protocol_handler_registry.h
index 882e1d5..cf21a1b 100644
--- a/chrome/browser/custom_handlers/protocol_handler_registry.h
+++ b/chrome/browser/custom_handlers/protocol_handler_registry.h
@@ -60,6 +60,10 @@ class ProtocolHandlerRegistry
// Registers the preferences that we store registered protocol handlers in.
static void RegisterPrefs(PrefService* prefService);
+ // Creates a URL request job for the given request if there is a matching
+ // protocol handler, returns NULL otherwise.
+ net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const;
+
private:
typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap;
@@ -73,11 +77,6 @@ class ProtocolHandlerRegistry
// Registers a new protocol handler.
void RegisterProtocolHandler(ProtocolHandler* handler);
- // Creates a URL request job for the given request if there is a matching
- // protocol handler, returns NULL otherwise.
- net::URLRequestJob* CreateJob(net::URLRequest* request,
- const std::string& scheme) const;
-
// Registers a new protocol handler from a JSON dictionary.
void RegisterHandlerFromValue(const DictionaryValue* value);
diff --git a/chrome/browser/io_thread.cc b/chrome/browser/io_thread.cc
index 4324b60..1978a0c 100644
--- a/chrome/browser/io_thread.cc
+++ b/chrome/browser/io_thread.cc
@@ -336,7 +336,7 @@ void IOThread::Init() {
globals_->extension_event_router_forwarder =
extension_event_router_forwarder_;
globals_->system_network_delegate.reset(new ChromeNetworkDelegate(
- extension_event_router_forwarder_, Profile::kInvalidProfileId));
+ extension_event_router_forwarder_, Profile::kInvalidProfileId, NULL));
globals_->host_resolver.reset(
CreateGlobalHostResolver(net_log_));
globals_->cert_verifier.reset(new net::CertVerifier);
diff --git a/chrome/browser/net/chrome_network_delegate.cc b/chrome/browser/net/chrome_network_delegate.cc
index 8fa34b0..68e5138 100644
--- a/chrome/browser/net/chrome_network_delegate.cc
+++ b/chrome/browser/net/chrome_network_delegate.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/net/chrome_network_delegate.h"
#include "base/logging.h"
+#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/extensions/extension_event_router_forwarder.h"
#include "chrome/browser/extensions/extension_proxy_api.h"
#include "chrome/browser/extensions/extension_webrequest_api.h"
@@ -34,9 +35,12 @@ void ForwardProxyErrors(net::URLRequest* request,
ChromeNetworkDelegate::ChromeNetworkDelegate(
ExtensionEventRouterForwarder* event_router,
- ProfileId profile_id)
+ ProfileId profile_id,
+ ProtocolHandlerRegistry* protocol_handler_registry)
: event_router_(event_router),
- profile_id_(profile_id) {
+ profile_id_(profile_id),
+ protocol_handler_registry_(protocol_handler_registry) {
+ DCHECK(event_router);
}
ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
@@ -60,3 +64,10 @@ void ChromeNetworkDelegate::OnReadCompleted(net::URLRequest* request,
int bytes_read) {
ForwardProxyErrors(request, event_router_.get(), profile_id_);
}
+
+net::URLRequestJob* ChromeNetworkDelegate::OnMaybeCreateURLRequestJob(
+ net::URLRequest* request) {
+ if (!protocol_handler_registry_)
+ return NULL;
+ return protocol_handler_registry_->MaybeCreateJob(request);
+}
diff --git a/chrome/browser/net/chrome_network_delegate.h b/chrome/browser/net/chrome_network_delegate.h
index 2f2f0ea..a6002ff 100644
--- a/chrome/browser/net/chrome_network_delegate.h
+++ b/chrome/browser/net/chrome_network_delegate.h
@@ -12,6 +12,7 @@
#include "net/base/network_delegate.h"
class ExtensionEventRouterForwarder;
+class ProtocolHandlerRegistry;
// ChromeNetworkDelegate is the central point from within the chrome code to
// add hooks into the network stack.
@@ -21,7 +22,8 @@ class ChromeNetworkDelegate : public net::NetworkDelegate {
// profiles, otherwise, they will only be sent to the specified profile.
explicit ChromeNetworkDelegate(
ExtensionEventRouterForwarder* event_router,
- ProfileId profile_id);
+ ProfileId profile_id,
+ ProtocolHandlerRegistry* protocol_handler_registry);
virtual ~ChromeNetworkDelegate();
private:
@@ -31,9 +33,12 @@ class ChromeNetworkDelegate : public net::NetworkDelegate {
virtual void OnSendHttpRequest(net::HttpRequestHeaders* headers);
virtual void OnResponseStarted(net::URLRequest* request);
virtual void OnReadCompleted(net::URLRequest* request, int bytes_read);
+ virtual net::URLRequestJob* OnMaybeCreateURLRequestJob(
+ net::URLRequest* request);
scoped_refptr<ExtensionEventRouterForwarder> event_router_;
const ProfileId profile_id_;
+ scoped_refptr<ProtocolHandlerRegistry> protocol_handler_registry_;
DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
};
diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h
index 7634077..1f1b09c 100644
--- a/chrome/browser/net/chrome_url_request_context.h
+++ b/chrome/browser/net/chrome_url_request_context.h
@@ -11,7 +11,6 @@
#include "base/file_path.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
-#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/extensions/extension_info_map.h"
#include "chrome/browser/extensions/extension_webrequest_api.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
@@ -94,10 +93,6 @@ class ChromeURLRequestContext : public net::URLRequestContext {
return prerender_manager_.get();
}
- const ProtocolHandlerRegistry* protocol_handler_registry() {
- return protocol_handler_registry_.get();
- }
-
ChromeURLDataManagerBackend* GetChromeURLDataManagerBackend();
// Setters to simplify initializing from factory objects.
@@ -134,9 +129,6 @@ class ChromeURLRequestContext : public net::URLRequestContext {
void set_prerender_manager(prerender::PrerenderManager* prerender_manager) {
prerender_manager_ = prerender_manager;
}
- void set_protocol_handler_registry(ProtocolHandlerRegistry* registry) {
- protocol_handler_registry_ = registry;
- }
// Callback for when the accept language changes.
void OnAcceptLanguageChange(const std::string& accept_language);
@@ -163,7 +155,6 @@ class ChromeURLRequestContext : public net::URLRequestContext {
scoped_refptr<ExtensionInfoMap> extension_info_map_;
scoped_refptr<prerender::PrerenderManager> prerender_manager_;
scoped_ptr<ChromeURLDataManagerBackend> chrome_url_data_manager_backend_;
- scoped_refptr<ProtocolHandlerRegistry> protocol_handler_registry_;
bool is_off_the_record_;
diff --git a/chrome/browser/profiles/off_the_record_profile_io_data.cc b/chrome/browser/profiles/off_the_record_profile_io_data.cc
index 663bbe1..2be3631 100644
--- a/chrome/browser/profiles/off_the_record_profile_io_data.cc
+++ b/chrome/browser/profiles/off_the_record_profile_io_data.cc
@@ -104,7 +104,8 @@ void OffTheRecordProfileIOData::LazyInitializeInternal() const {
network_delegate_.reset(new ChromeNetworkDelegate(
io_thread_globals->extension_event_router_forwarder.get(),
- profile_params.profile_id));
+ profile_params.profile_id,
+ profile_params.protocol_handler_registry));
main_request_context_->set_network_delegate(network_delegate_.get());
main_request_context_->set_host_resolver(
diff --git a/chrome/browser/profiles/profile_impl_io_data.cc b/chrome/browser/profiles/profile_impl_io_data.cc
index 01d8101..faffcfb 100644
--- a/chrome/browser/profiles/profile_impl_io_data.cc
+++ b/chrome/browser/profiles/profile_impl_io_data.cc
@@ -144,7 +144,8 @@ void ProfileImplIOData::LazyInitializeInternal() const {
network_delegate_.reset(new ChromeNetworkDelegate(
io_thread_globals->extension_event_router_forwarder.get(),
- profile_params.profile_id));
+ profile_params.profile_id,
+ profile_params.protocol_handler_registry));
main_request_context_->set_network_delegate(network_delegate_.get());
media_request_context_->set_network_delegate(network_delegate_.get());
diff --git a/chrome/browser/profiles/profile_io_data.cc b/chrome/browser/profiles/profile_io_data.cc
index 3d0b7b8..ddf493a 100644
--- a/chrome/browser/profiles/profile_io_data.cc
+++ b/chrome/browser/profiles/profile_io_data.cc
@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/browser_process.h"
+#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/extensions/user_script_master.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/net/chrome_cookie_notification_details.h"
@@ -256,8 +257,6 @@ void ProfileIOData::ApplyProfileParamsToContext(
context->set_file_system_context(profile_params.file_system_context);
context->set_extension_info_map(profile_params.extension_info_map);
context->set_prerender_manager(profile_params.prerender_manager);
- context->set_protocol_handler_registry(
- profile_params.protocol_handler_registry);
}
// static
diff --git a/chrome/browser/profiles/profile_io_data.h b/chrome/browser/profiles/profile_io_data.h
index 63ed3ac..705ca3a 100644
--- a/chrome/browser/profiles/profile_io_data.h
+++ b/chrome/browser/profiles/profile_io_data.h
@@ -37,6 +37,7 @@ class TransportSecurityState;
namespace prerender {
class PrerenderManager;
}; // namespace prerender
+class ProtocolHandlerRegistry;
namespace webkit_database {
class DatabaseTracker;
} // webkit_database
diff --git a/net/base/network_delegate.cc b/net/base/network_delegate.cc
index b7ecd44..17da7a8 100644
--- a/net/base/network_delegate.cc
+++ b/net/base/network_delegate.cc
@@ -34,4 +34,10 @@ void NetworkDelegate::NotifyReadCompleted(URLRequest* request, int bytes_read) {
OnReadCompleted(request, bytes_read);
}
+URLRequestJob* NetworkDelegate::MaybeCreateURLRequestJob(URLRequest* request) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(request);
+ return OnMaybeCreateURLRequestJob(request);
+}
+
} // namespace net
diff --git a/net/base/network_delegate.h b/net/base/network_delegate.h
index 7ffa1c3..284728b4 100644
--- a/net/base/network_delegate.h
+++ b/net/base/network_delegate.h
@@ -23,6 +23,7 @@ namespace net {
// are ok.
class HttpRequestHeaders;
class URLRequest;
+class URLRequestJob;
class NetworkDelegate : public base::NonThreadSafe {
public:
@@ -37,6 +38,14 @@ class NetworkDelegate : public base::NonThreadSafe {
void NotifyResponseStarted(URLRequest* request);
void NotifyReadCompleted(URLRequest* request, int bytes_read);
+ // Returns a URLRequestJob that will be used to handle the request if
+ // non-null.
+ // TODO(koz): Currently this is called inside registered ProtocolFactories,
+ // so that we can perform Delegate-dependent request handling from the static
+ // factories, but ultimately it should be called directly from
+ // URLRequestJobManager::CreateJob() as a general override mechanism.
+ URLRequestJob* MaybeCreateURLRequestJob(URLRequest* request);
+
private:
// This is the interface for subclasses of NetworkDelegate to implement. This
// member functions will be called by the respective public notification
@@ -55,6 +64,10 @@ class NetworkDelegate : public base::NonThreadSafe {
// This corresponds to URLRequestDelegate::OnReadCompleted.
virtual void OnReadCompleted(URLRequest* request, int bytes_read) = 0;
+
+ // Called before a request is sent and before a URLRequestJob is created to
+ // handle the request.
+ virtual URLRequestJob* OnMaybeCreateURLRequestJob(URLRequest* request) = 0;
};
} // namespace net
diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h
index b98282a..2c58db1 100644
--- a/net/url_request/url_request.h
+++ b/net/url_request/url_request.h
@@ -261,6 +261,9 @@ class URLRequest : public base::NonThreadSafe {
// Returns true if the url can be handled by URLRequest. False otherwise.
// The function returns true for invalid urls because URLRequest knows how
// to handle those.
+ // NOTE: This will also return true for URLs that are handled by
+ // ProtocolFactories that only work for requests that are scoped to a
+ // Profile.
static bool IsHandledURL(const GURL& url);
// Allow access to file:// on ChromeOS for tests.
diff --git a/net/url_request/url_request_test_util.cc b/net/url_request/url_request_test_util.cc
index 8fd7cab..d13ee06 100644
--- a/net/url_request/url_request_test_util.cc
+++ b/net/url_request/url_request_test_util.cc
@@ -319,3 +319,8 @@ void TestNetworkDelegate::OnReadCompleted(net::URLRequest* request,
last_os_error_ = request->status().os_error();
}
}
+
+net::URLRequestJob* TestNetworkDelegate::OnMaybeCreateURLRequestJob(
+ net::URLRequest* request) {
+ return NULL;
+}
diff --git a/net/url_request/url_request_test_util.h b/net/url_request/url_request_test_util.h
index d166f9e..6dcd39d 100644
--- a/net/url_request/url_request_test_util.h
+++ b/net/url_request/url_request_test_util.h
@@ -206,6 +206,8 @@ class TestNetworkDelegate : public net::NetworkDelegate {
virtual void OnSendHttpRequest(net::HttpRequestHeaders* headers);
virtual void OnResponseStarted(net::URLRequest* request);
virtual void OnReadCompleted(net::URLRequest* request, int bytes_read);
+ virtual net::URLRequestJob* OnMaybeCreateURLRequestJob(
+ net::URLRequest* request);
int last_os_error_;
int error_count_;