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

Side by Side Diff: content/browser/storage_partition_impl_map.cc

Issue 11308362: Add StoragePartition's ProtocolHandlers at URLRequestContext construction time. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync (r179907) Created 7 years, 10 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 "content/browser/storage_partition_impl_map.h" 5 #include "content/browser/storage_partition_impl_map.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 28 matching lines...) Expand all
39 #include "webkit/fileapi/file_system_url_request_job_factory.h" 39 #include "webkit/fileapi/file_system_url_request_job_factory.h"
40 40
41 using appcache::AppCacheService; 41 using appcache::AppCacheService;
42 using fileapi::FileSystemContext; 42 using fileapi::FileSystemContext;
43 using webkit_blob::BlobStorageController; 43 using webkit_blob::BlobStorageController;
44 44
45 namespace content { 45 namespace content {
46 46
47 namespace { 47 namespace {
48 48
49 class BlobProtocolHandler : public webkit_blob::BlobProtocolHandler { 49 class BlobProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
50 public: 50 public:
51 BlobProtocolHandler( 51 BlobProtocolHandler(ChromeBlobStorageContext* blob_storage_context,
52 webkit_blob::BlobStorageController* blob_storage_controller, 52 fileapi::FileSystemContext* file_system_context)
53 fileapi::FileSystemContext* file_system_context, 53 : blob_storage_context_(blob_storage_context),
54 base::MessageLoopProxy* loop_proxy) 54 file_system_context_(file_system_context) {}
55 : webkit_blob::BlobProtocolHandler(blob_storage_controller,
56 file_system_context,
57 loop_proxy) {}
58 55
59 virtual ~BlobProtocolHandler() {} 56 virtual ~BlobProtocolHandler() {}
60 57
58 virtual net::URLRequestJob* MaybeCreateJob(
59 net::URLRequest* request,
60 net::NetworkDelegate* network_delegate) const OVERRIDE {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
62 if (!webkit_blob_protocol_handler_impl_) {
63 webkit_blob_protocol_handler_impl_.reset(
64 new WebKitBlobProtocolHandlerImpl(blob_storage_context_->controller(),
65 file_system_context_));
66 }
67 return webkit_blob_protocol_handler_impl_->MaybeCreateJob(request,
68 network_delegate);
69 }
70
61 private: 71 private:
62 virtual scoped_refptr<webkit_blob::BlobData> 72 // An implementation of webkit_blob::BlobProtocolHandler that gets
63 LookupBlobData(net::URLRequest* request) const { 73 // the BlobData from ResourceRequestInfoImpl.
64 const ResourceRequestInfoImpl* info = 74 class WebKitBlobProtocolHandlerImpl
65 ResourceRequestInfoImpl::ForRequest(request); 75 : public webkit_blob::BlobProtocolHandler {
66 if (!info) 76 public:
67 return NULL; 77 WebKitBlobProtocolHandlerImpl(
68 return info->requested_blob_data(); 78 webkit_blob::BlobStorageController* blob_storage_controller,
69 } 79 fileapi::FileSystemContext* file_system_context)
80 : webkit_blob::BlobProtocolHandler(
81 blob_storage_controller, file_system_context,
82 BrowserThread::GetMessageLoopProxyForThread(
83 BrowserThread::FILE)) {}
84
85 virtual ~WebKitBlobProtocolHandlerImpl() {}
86
87 private:
88 // webkit_blob::BlobProtocolHandler implementation.
89 virtual scoped_refptr<webkit_blob::BlobData>
90 LookupBlobData(net::URLRequest* request) const OVERRIDE {
91 const ResourceRequestInfoImpl* info =
92 ResourceRequestInfoImpl::ForRequest(request);
93 if (!info)
94 return NULL;
95 return info->requested_blob_data();
96 }
97
98 DISALLOW_COPY_AND_ASSIGN(WebKitBlobProtocolHandlerImpl);
99 };
100
101 const scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
102 const scoped_refptr<fileapi::FileSystemContext> file_system_context_;
103
104 mutable scoped_ptr<WebKitBlobProtocolHandlerImpl>
105 webkit_blob_protocol_handler_impl_;
70 106
71 DISALLOW_COPY_AND_ASSIGN(BlobProtocolHandler); 107 DISALLOW_COPY_AND_ASSIGN(BlobProtocolHandler);
72 }; 108 };
73 109
74 // Adds a bunch of debugging urls. We use an interceptor instead of a protocol 110 // Adds a bunch of debugging urls. We use an interceptor instead of a protocol
75 // handler because we want to reuse the chrome://scheme (everyone is familiar 111 // handler because we want to reuse the chrome://scheme (everyone is familiar
76 // with it, and no need to expose the content/chrome separation through our UI). 112 // with it, and no need to expose the content/chrome separation through our UI).
77 class DeveloperProtocolHandler 113 class DeveloperProtocolHandler
78 : public net::URLRequestJobFactory::Interceptor { 114 : public net::URLRequestJobFactory::ProtocolHandler {
79 public: 115 public:
80 DeveloperProtocolHandler( 116 DeveloperProtocolHandler(
81 AppCacheService* appcache_service, 117 AppCacheService* appcache_service,
82 BlobStorageController* blob_storage_controller) 118 ChromeBlobStorageContext* blob_storage_context)
83 : appcache_service_(appcache_service), 119 : appcache_service_(appcache_service),
84 blob_storage_controller_(blob_storage_controller) {} 120 blob_storage_context_(blob_storage_context) {}
85 virtual ~DeveloperProtocolHandler() {} 121 virtual ~DeveloperProtocolHandler() {}
86 122
87 virtual net::URLRequestJob* MaybeIntercept( 123 virtual net::URLRequestJob* MaybeCreateJob(
88 net::URLRequest* request, 124 net::URLRequest* request,
89 net::NetworkDelegate* network_delegate) const OVERRIDE { 125 net::NetworkDelegate* network_delegate) const OVERRIDE {
90 // Check for chrome://view-http-cache/*, which uses its own job type. 126 // Check for chrome://view-http-cache/*, which uses its own job type.
91 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) 127 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
92 return ViewHttpCacheJobFactory::CreateJobForRequest(request, 128 return ViewHttpCacheJobFactory::CreateJobForRequest(request,
93 network_delegate); 129 network_delegate);
94 130
95 // Next check for chrome://appcache-internals/, which uses its own job type. 131 // Next check for chrome://appcache-internals/, which uses its own job type.
96 if (request->url().SchemeIs(chrome::kChromeUIScheme) && 132 if (request->url().SchemeIs(chrome::kChromeUIScheme) &&
97 request->url().host() == chrome::kChromeUIAppCacheInternalsHost) { 133 request->url().host() == chrome::kChromeUIAppCacheInternalsHost) {
98 return appcache::ViewAppCacheInternalsJobFactory::CreateJobForRequest( 134 return appcache::ViewAppCacheInternalsJobFactory::CreateJobForRequest(
99 request, network_delegate, appcache_service_); 135 request, network_delegate, appcache_service_);
100 } 136 }
101 137
102 // Next check for chrome://blob-internals/, which uses its own job type. 138 // Next check for chrome://blob-internals/, which uses its own job type.
103 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) { 139 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) {
104 return ViewBlobInternalsJobFactory::CreateJobForRequest( 140 return ViewBlobInternalsJobFactory::CreateJobForRequest(
105 request, network_delegate, blob_storage_controller_); 141 request, network_delegate, blob_storage_context_->controller());
106 } 142 }
107 143
108 #if defined(USE_TCMALLOC) 144 #if defined(USE_TCMALLOC)
109 // Next check for chrome://tcmalloc/, which uses its own job type. 145 // Next check for chrome://tcmalloc/, which uses its own job type.
110 if (request->url().SchemeIs(chrome::kChromeUIScheme) && 146 if (request->url().SchemeIs(chrome::kChromeUIScheme) &&
111 request->url().host() == chrome::kChromeUITcmallocHost) { 147 request->url().host() == chrome::kChromeUITcmallocHost) {
112 return new TcmallocInternalsRequestJob(request, network_delegate); 148 return new TcmallocInternalsRequestJob(request, network_delegate);
113 } 149 }
114 #endif 150 #endif
115 151
116 // Next check for chrome://histograms/, which uses its own job type. 152 // Next check for chrome://histograms/, which uses its own job type.
117 if (request->url().SchemeIs(chrome::kChromeUIScheme) && 153 if (request->url().SchemeIs(chrome::kChromeUIScheme) &&
118 request->url().host() == chrome::kChromeUIHistogramHost) { 154 request->url().host() == chrome::kChromeUIHistogramHost) {
119 return new HistogramInternalsRequestJob(request, network_delegate); 155 return new HistogramInternalsRequestJob(request, network_delegate);
120 } 156 }
121 157
122 return NULL; 158 return NULL;
123 } 159 }
124 160
125 virtual net::URLRequestJob* MaybeInterceptRedirect(
126 const GURL& location,
127 net::URLRequest* request,
128 net::NetworkDelegate* network_delegate) const OVERRIDE {
129 return NULL;
130 }
131
132 virtual net::URLRequestJob* MaybeInterceptResponse(
133 net::URLRequest* request,
134 net::NetworkDelegate* network_delegate) const OVERRIDE {
135 return NULL;
136 }
137
138 virtual bool WillHandleProtocol(const std::string& protocol) const {
139 return protocol == chrome::kChromeUIScheme;
140 }
141
142 private: 161 private:
143 AppCacheService* appcache_service_; 162 AppCacheService* appcache_service_;
144 BlobStorageController* blob_storage_controller_; 163 ChromeBlobStorageContext* blob_storage_context_;
145 }; 164 };
146 165
147 void InitializeURLRequestContext(
148 net::URLRequestContextGetter* context_getter,
149 AppCacheService* appcache_service,
150 FileSystemContext* file_system_context,
151 ChromeBlobStorageContext* blob_storage_context,
152 ResourceContext* resource_context,
153 bool off_the_record) {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
155 if (!context_getter)
156 return; // tests.
157
158 // This code only modifies the URLRequestJobFactory on the context
159 // to handle blob: URLs, filesystem: URLs, and to let AppCache intercept
160 // the appropriate requests. This is in addition to the slew of other
161 // initializtion that is done in during creation of the URLRequestContext.
162 // We cannot yet centralize this code because URLRequestContext needs
163 // to be created before the StoragePartition context.
164 //
165 // TODO(ajwong): Fix the ordering so all the initialization is in one spot.
166 net::URLRequestContext* context = context_getter->GetURLRequestContext();
167 net::URLRequestJobFactory* job_factory =
168 const_cast<net::URLRequestJobFactory*>(context->job_factory());
169
170 // Note: if this is called twice with 2 request contexts that share one job
171 // factory (as is the case with a media request context and its related
172 // normal request context) then this will early exit.
173 if (job_factory->IsHandledProtocol(chrome::kBlobScheme))
174 return; // Already initialized this JobFactory.
175
176 bool set_protocol = job_factory->SetProtocolHandler(
177 chrome::kBlobScheme,
178 new BlobProtocolHandler(
179 blob_storage_context->controller(),
180 file_system_context,
181 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)));
182 DCHECK(set_protocol);
183 set_protocol = job_factory->SetProtocolHandler(
184 chrome::kFileSystemScheme,
185 CreateFileSystemProtocolHandler(file_system_context));
186 DCHECK(set_protocol);
187
188 job_factory->AddInterceptor(
189 new DeveloperProtocolHandler(appcache_service,
190 blob_storage_context->controller()));
191
192 set_protocol = job_factory->SetProtocolHandler(
193 chrome::kChromeUIScheme,
194 URLDataManagerBackend::CreateProtocolHandler(
195 GetURLDataManagerForResourceContext(resource_context),
196 off_the_record));
197 DCHECK(set_protocol);
198
199 set_protocol = job_factory->SetProtocolHandler(
200 chrome::kChromeDevToolsScheme,
201 CreateDevToolsProtocolHandler(
202 GetURLDataManagerForResourceContext(resource_context),
203 off_the_record));
204 DCHECK(set_protocol);
205
206 // TODO(jam): Add the ProtocolHandlerRegistryIntercepter here!
207 }
208
209 // These constants are used to create the directory structure under the profile 166 // These constants are used to create the directory structure under the profile
210 // where renderers with a non-default storage partition keep their persistent 167 // where renderers with a non-default storage partition keep their persistent
211 // state. This will contain a set of directories that partially mirror the 168 // state. This will contain a set of directories that partially mirror the
212 // directory structure of BrowserContext::GetPath(). 169 // directory structure of BrowserContext::GetPath().
213 // 170 //
214 // The kStoragePartitionDirname contains an extensions directory which is 171 // The kStoragePartitionDirname contains an extensions directory which is
215 // further partitioned by extension id, followed by another level of directories 172 // further partitioned by extension id, followed by another level of directories
216 // for the "default" extension storage partition and one directory for each 173 // for the "default" extension storage partition and one directory for each
217 // persistent partition used by a webview tag. Example: 174 // persistent partition used by a webview tag. Example:
218 // 175 //
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 return it->second; 437 return it->second;
481 438
482 FilePath partition_path = 439 FilePath partition_path =
483 browser_context_->GetPath().Append( 440 browser_context_->GetPath().Append(
484 GetStoragePartitionPath(partition_domain, partition_name)); 441 GetStoragePartitionPath(partition_domain, partition_name));
485 StoragePartitionImpl* partition = 442 StoragePartitionImpl* partition =
486 StoragePartitionImpl::Create(browser_context_, in_memory, 443 StoragePartitionImpl::Create(browser_context_, in_memory,
487 partition_path); 444 partition_path);
488 partitions_[partition_config] = partition; 445 partitions_[partition_config] = partition;
489 446
447 ChromeBlobStorageContext* blob_storage_context =
448 ChromeBlobStorageContext::GetFor(browser_context_);
449 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> blob_protocol_handler(
450 new BlobProtocolHandler(blob_storage_context,
451 partition->GetFileSystemContext()));
452 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
453 file_system_protocol_handler(
454 CreateFileSystemProtocolHandler(partition->GetFileSystemContext()));
455 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
456 developer_protocol_handler(
457 new DeveloperProtocolHandler(partition->GetAppCacheService(),
458 blob_storage_context));
459 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
460 chrome_protocol_handler(
461 URLDataManagerBackend::CreateProtocolHandler(
462 browser_context_->GetResourceContext(),
463 browser_context_->IsOffTheRecord()));
464 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
465 chrome_devtools_protocol_handler(
466 CreateDevToolsProtocolHandler(browser_context_->GetResourceContext(),
467 browser_context_->IsOffTheRecord()));
468
490 // These calls must happen after StoragePartitionImpl::Create(). 469 // These calls must happen after StoragePartitionImpl::Create().
491 partition->SetURLRequestContext( 470 partition->SetURLRequestContext(
492 partition_domain.empty() ? 471 partition_domain.empty() ?
493 browser_context_->GetRequestContext() : 472 GetContentClient()->browser()->CreateRequestContext(browser_context_,
494 browser_context_->GetRequestContextForStoragePartition( 473 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(),
495 partition->GetPath(), in_memory)); 474 developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(),
475 chrome_devtools_protocol_handler.Pass()) :
476 GetContentClient()->browser()->CreateRequestContextForStoragePartition(
477 browser_context_, partition->GetPath(), in_memory,
478 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(),
479 developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(),
480 chrome_devtools_protocol_handler.Pass()));
496 partition->SetMediaURLRequestContext( 481 partition->SetMediaURLRequestContext(
497 partition_domain.empty() ? 482 partition_domain.empty() ?
498 browser_context_->GetMediaRequestContext() : 483 browser_context_->GetMediaRequestContext() :
499 browser_context_->GetMediaRequestContextForStoragePartition( 484 browser_context_->GetMediaRequestContextForStoragePartition(
500 partition->GetPath(), in_memory)); 485 partition->GetPath(), in_memory));
501 486
502 PostCreateInitialization(partition, in_memory); 487 PostCreateInitialization(partition, in_memory);
503 488
504 return partition; 489 return partition;
505 } 490 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 BrowserThread::IO, FROM_HERE, 589 BrowserThread::IO, FROM_HERE,
605 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, 590 base::Bind(&ChromeAppCacheService::InitializeOnIOThread,
606 partition->GetAppCacheService(), 591 partition->GetAppCacheService(),
607 in_memory ? FilePath() : 592 in_memory ? FilePath() :
608 partition->GetPath().Append(kAppCacheDirname), 593 partition->GetPath().Append(kAppCacheDirname),
609 browser_context_->GetResourceContext(), 594 browser_context_->GetResourceContext(),
610 make_scoped_refptr(partition->GetURLRequestContext()), 595 make_scoped_refptr(partition->GetURLRequestContext()),
611 make_scoped_refptr( 596 make_scoped_refptr(
612 browser_context_->GetSpecialStoragePolicy()))); 597 browser_context_->GetSpecialStoragePolicy())));
613 598
614 // Add content's URLRequestContext's hooks.
615 BrowserThread::PostTask(
616 BrowserThread::IO, FROM_HERE,
617 base::Bind(
618 &InitializeURLRequestContext,
619 make_scoped_refptr(partition->GetURLRequestContext()),
620 make_scoped_refptr(partition->GetAppCacheService()),
621 make_scoped_refptr(partition->GetFileSystemContext()),
622 make_scoped_refptr(
623 ChromeBlobStorageContext::GetFor(browser_context_)),
624 browser_context_->GetResourceContext(),
625 browser_context_->IsOffTheRecord()));
626
627 // We do not call InitializeURLRequestContext() for media contexts because, 599 // We do not call InitializeURLRequestContext() for media contexts because,
628 // other than the HTTP cache, the media contexts share the same backing 600 // other than the HTTP cache, the media contexts share the same backing
629 // objects as their associated "normal" request context. Thus, the previous 601 // objects as their associated "normal" request context. Thus, the previous
630 // call serves to initialize the media request context for this storage 602 // call serves to initialize the media request context for this storage
631 // partition as well. 603 // partition as well.
632 } 604 }
633 } 605 }
634 606
635 } // namespace content 607 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698