Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry.cc

Issue 11669012: Convert ProtocolHandlerRegistry::Interceptor to a net::URLRequestJobFactory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address mmenke's comments Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" 12 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h"
15 #include "chrome/browser/net/chrome_url_request_context.h" 13 #include "chrome/browser/net/chrome_url_request_context.h"
16 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile_io_data.h" 15 #include "chrome/browser/profiles/profile_io_data.h"
18 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
19 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/custom_handlers/protocol_handler.h" 18 #include "chrome/common/custom_handlers/protocol_handler.h"
21 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/child_process_security_policy.h" 20 #include "content/public/browser/child_process_security_policy.h"
24 #include "content/public/browser/notification_service.h"
25 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
26 #include "net/base/network_delegate.h" 22 #include "net/base/network_delegate.h"
27 #include "net/url_request/url_request.h"
28 #include "net/url_request/url_request_job.h"
29 #include "net/url_request/url_request_redirect_job.h" 23 #include "net/url_request/url_request_redirect_job.h"
30 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
31 25
32 using content::BrowserThread; 26 using content::BrowserThread;
33 using content::ChildProcessSecurityPolicy; 27 using content::ChildProcessSecurityPolicy;
34 28
35 namespace { 29 namespace {
36 30
37 const ProtocolHandler& LookupHandler( 31 const ProtocolHandler& LookupHandler(
38 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map, 32 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 142
149 GURL translated_url(handler.TranslateUrl(request->url())); 143 GURL translated_url(handler.TranslateUrl(request->url()));
150 if (!translated_url.is_valid()) 144 if (!translated_url.is_valid())
151 return NULL; 145 return NULL;
152 146
153 return new net::URLRequestRedirectJob( 147 return new net::URLRequestRedirectJob(
154 request, network_delegate, translated_url, 148 request, network_delegate, translated_url,
155 net::URLRequestRedirectJob::REDIRECT_302_FOUND); 149 net::URLRequestRedirectJob::REDIRECT_302_FOUND);
156 } 150 }
157 151
158 // URLInterceptor ------------------------------------------------------------ 152 // JobInterceptorFactory -------------------------------------------------------
159 153
160 // Instances of this class are produced for ownership by the IO 154 // Instances of JobFactory are produced for ownership by the IO
161 // thread where it handler URL requests. We should never hold 155 // thread where it handler URL requests. We should never hold
162 // any pointers on this class, only produce them in response to 156 // any pointers on this class, only produce them in response to
163 // requests via |ProtocolHandlerRegistry::CreateURLInterceptor|. 157 // requests via |ProtocolHandlerRegistry::CreateURLRequestJobFactory|.
164 class ProtocolHandlerRegistry::URLInterceptor 158 ProtocolHandlerRegistry::JobInterceptorFactory::JobInterceptorFactory(
165 : public net::URLRequestJobFactory::Interceptor { 159 Core* core) : core_(core) {
166 public:
167 explicit URLInterceptor(Core* core);
168 virtual ~URLInterceptor();
169
170 virtual net::URLRequestJob* MaybeIntercept(
171 net::URLRequest* request,
172 net::NetworkDelegate* network_delegate) const OVERRIDE;
173
174 virtual bool WillHandleProtocol(const std::string& protocol) const OVERRIDE;
175
176 virtual net::URLRequestJob* MaybeInterceptRedirect(
177 const GURL& url,
178 net::URLRequest* request,
179 net::NetworkDelegate* network_delegate) const OVERRIDE {
180 return NULL;
181 }
182
183 virtual net::URLRequestJob* MaybeInterceptResponse(
184 net::URLRequest* request,
185 net::NetworkDelegate* network_delegate) const OVERRIDE {
186 return NULL;
187 }
188
189 private:
190 scoped_refptr<Core> core_;
191 DISALLOW_COPY_AND_ASSIGN(URLInterceptor);
192 };
193
194 ProtocolHandlerRegistry::URLInterceptor::URLInterceptor(Core* core)
195 : core_(core) {
196 DCHECK(core_); 160 DCHECK(core_);
161 DetachFromThread();
197 } 162 }
198 163
199 ProtocolHandlerRegistry::URLInterceptor::~URLInterceptor() { 164 ProtocolHandlerRegistry::JobInterceptorFactory::~JobInterceptorFactory() {
200 } 165 }
201 166
202 net::URLRequestJob* ProtocolHandlerRegistry::URLInterceptor::MaybeIntercept( 167 void ProtocolHandlerRegistry::JobInterceptorFactory::Chain(
203 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { 168 scoped_ptr<net::URLRequestJobFactory> job_factory) {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 169 job_factory_ = job_factory.Pass();
205
206 return core_->MaybeCreateJob(request, network_delegate);
207 } 170 }
208 171
209 bool ProtocolHandlerRegistry::URLInterceptor::WillHandleProtocol( 172 bool ProtocolHandlerRegistry::JobInterceptorFactory::SetProtocolHandler(
210 const std::string& protocol) const { 173 const std::string& scheme, ProtocolHandler* protocol_handler) {
174 return job_factory_->SetProtocolHandler(scheme, protocol_handler);
175 }
176
177 void ProtocolHandlerRegistry::JobInterceptorFactory::AddInterceptor(
178 Interceptor* interceptor) {
179 return job_factory_->AddInterceptor(interceptor);
180 }
181
182 net::URLRequestJob*
183 ProtocolHandlerRegistry::JobInterceptorFactory::MaybeCreateJobWithInterceptor(
184 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
185 return job_factory_->MaybeCreateJobWithInterceptor(request, network_delegate);
186 }
187
188 net::URLRequestJob*
189 ProtocolHandlerRegistry::JobInterceptorFactory::
190 MaybeCreateJobWithProtocolHandler(
191 const std::string& scheme,
192 net::URLRequest* request,
193 net::NetworkDelegate* network_delegate) const {
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
195 net::URLRequestJob* job = core_->MaybeCreateJob(request, network_delegate);
196 if (job)
197 return job;
198 return job_factory_->MaybeCreateJobWithProtocolHandler(
199 scheme, request, network_delegate);
200 }
212 201
213 return core_->IsHandledProtocol(protocol); 202 net::URLRequestJob*
203 ProtocolHandlerRegistry::JobInterceptorFactory::MaybeInterceptRedirect(
204 const GURL& location,
205 net::URLRequest* request,
206 net::NetworkDelegate* network_delegate) const {
207 return job_factory_->MaybeInterceptRedirect(
208 location, request, network_delegate);
209 }
210
211 net::URLRequestJob*
212 ProtocolHandlerRegistry::JobInterceptorFactory::MaybeInterceptResponse(
213 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
214 return job_factory_->MaybeInterceptResponse(request, network_delegate);
215 }
216
217 bool ProtocolHandlerRegistry::JobInterceptorFactory::IsHandledProtocol(
218 const std::string& scheme) const {
219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
220 return core_->IsHandledProtocol(scheme) ||
221 job_factory_->IsHandledProtocol(scheme);
222 }
223
224 bool ProtocolHandlerRegistry::JobInterceptorFactory::IsHandledURL(
225 const GURL& url) const {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
227 return (url.is_valid() && core_->IsHandledProtocol(url.scheme())) ||
228 job_factory_->IsHandledURL(url);
214 } 229 }
215 230
216 // DefaultClientObserver ------------------------------------------------------ 231 // DefaultClientObserver ------------------------------------------------------
217 232
218 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver( 233 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver(
219 ProtocolHandlerRegistry* registry) 234 ProtocolHandlerRegistry* registry)
220 : worker_(NULL), 235 : worker_(NULL),
221 registry_(registry) { 236 registry_(registry) {
222 DCHECK(registry_); 237 DCHECK(registry_);
223 } 238 }
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 ignored_protocol_handlers_.push_back(handler); 883 ignored_protocol_handlers_.push_back(handler);
869 } 884 }
870 885
871 void ProtocolHandlerRegistry::AddPredefinedHandler( 886 void ProtocolHandlerRegistry::AddPredefinedHandler(
872 const ProtocolHandler& handler) { 887 const ProtocolHandler& handler) {
873 DCHECK(!is_loaded_); // Must be called prior InitProtocolSettings. 888 DCHECK(!is_loaded_); // Must be called prior InitProtocolSettings.
874 RegisterProtocolHandler(handler); 889 RegisterProtocolHandler(handler);
875 SetDefault(handler); 890 SetDefault(handler);
876 } 891 }
877 892
878 net::URLRequestJobFactory::Interceptor* 893 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
879 ProtocolHandlerRegistry::CreateURLInterceptor() { 894 ProtocolHandlerRegistry::CreateJobInterceptorFactory() {
880 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
881 // this is always created on the UI thread (in profile_io's 896 // this is always created on the UI thread (in profile_io's
882 // InitializeOnUIThread. Any method calls must be done 897 // InitializeOnUIThread. Any method calls must be done
883 // on the IO thread (this is checked). 898 // on the IO thread (this is checked).
884 return new URLInterceptor(core_); 899 return scoped_ptr<JobInterceptorFactory>(new JobInterceptorFactory(core_));
885 } 900 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698