OLD | NEW |
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 Loading... |
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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 return it->second; | 438 return it->second; |
482 | 439 |
483 base::FilePath partition_path = | 440 base::FilePath partition_path = |
484 browser_context_->GetPath().Append( | 441 browser_context_->GetPath().Append( |
485 GetStoragePartitionPath(partition_domain, partition_name)); | 442 GetStoragePartitionPath(partition_domain, partition_name)); |
486 StoragePartitionImpl* partition = | 443 StoragePartitionImpl* partition = |
487 StoragePartitionImpl::Create(browser_context_, in_memory, | 444 StoragePartitionImpl::Create(browser_context_, in_memory, |
488 partition_path); | 445 partition_path); |
489 partitions_[partition_config] = partition; | 446 partitions_[partition_config] = partition; |
490 | 447 |
| 448 ChromeBlobStorageContext* blob_storage_context = |
| 449 ChromeBlobStorageContext::GetFor(browser_context_); |
| 450 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> blob_protocol_handler( |
| 451 new BlobProtocolHandler(blob_storage_context, |
| 452 partition->GetFileSystemContext())); |
| 453 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 454 file_system_protocol_handler( |
| 455 CreateFileSystemProtocolHandler(partition->GetFileSystemContext())); |
| 456 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 457 developer_protocol_handler( |
| 458 new DeveloperProtocolHandler(partition->GetAppCacheService(), |
| 459 blob_storage_context)); |
| 460 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 461 chrome_protocol_handler( |
| 462 URLDataManagerBackend::CreateProtocolHandler( |
| 463 browser_context_->GetResourceContext(), |
| 464 browser_context_->IsOffTheRecord())); |
| 465 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 466 chrome_devtools_protocol_handler( |
| 467 CreateDevToolsProtocolHandler(browser_context_->GetResourceContext(), |
| 468 browser_context_->IsOffTheRecord())); |
| 469 |
491 // These calls must happen after StoragePartitionImpl::Create(). | 470 // These calls must happen after StoragePartitionImpl::Create(). |
492 partition->SetURLRequestContext( | 471 if (partition_domain.empty()) { |
493 partition_domain.empty() ? | 472 partition->SetURLRequestContext( |
494 browser_context_->GetRequestContext() : | 473 GetContentClient()->browser()->CreateRequestContext(browser_context_, |
495 browser_context_->GetRequestContextForStoragePartition( | 474 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(), |
496 partition->GetPath(), in_memory)); | 475 developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(), |
| 476 chrome_devtools_protocol_handler.Pass())); |
| 477 } else { |
| 478 partition->SetURLRequestContext( |
| 479 GetContentClient()->browser()->CreateRequestContextForStoragePartition( |
| 480 browser_context_, partition->GetPath(), in_memory, |
| 481 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(), |
| 482 developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(), |
| 483 chrome_devtools_protocol_handler.Pass())); |
| 484 } |
497 partition->SetMediaURLRequestContext( | 485 partition->SetMediaURLRequestContext( |
498 partition_domain.empty() ? | 486 partition_domain.empty() ? |
499 browser_context_->GetMediaRequestContext() : | 487 browser_context_->GetMediaRequestContext() : |
500 browser_context_->GetMediaRequestContextForStoragePartition( | 488 browser_context_->GetMediaRequestContextForStoragePartition( |
501 partition->GetPath(), in_memory)); | 489 partition->GetPath(), in_memory)); |
502 | 490 |
503 PostCreateInitialization(partition, in_memory); | 491 PostCreateInitialization(partition, in_memory); |
504 | 492 |
505 return partition; | 493 return partition; |
506 } | 494 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 BrowserThread::IO, FROM_HERE, | 593 BrowserThread::IO, FROM_HERE, |
606 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 594 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
607 partition->GetAppCacheService(), | 595 partition->GetAppCacheService(), |
608 in_memory ? base::FilePath() : | 596 in_memory ? base::FilePath() : |
609 partition->GetPath().Append(kAppCacheDirname), | 597 partition->GetPath().Append(kAppCacheDirname), |
610 browser_context_->GetResourceContext(), | 598 browser_context_->GetResourceContext(), |
611 make_scoped_refptr(partition->GetURLRequestContext()), | 599 make_scoped_refptr(partition->GetURLRequestContext()), |
612 make_scoped_refptr( | 600 make_scoped_refptr( |
613 browser_context_->GetSpecialStoragePolicy()))); | 601 browser_context_->GetSpecialStoragePolicy()))); |
614 | 602 |
615 // Add content's URLRequestContext's hooks. | |
616 BrowserThread::PostTask( | |
617 BrowserThread::IO, FROM_HERE, | |
618 base::Bind( | |
619 &InitializeURLRequestContext, | |
620 make_scoped_refptr(partition->GetURLRequestContext()), | |
621 make_scoped_refptr(partition->GetAppCacheService()), | |
622 make_scoped_refptr(partition->GetFileSystemContext()), | |
623 make_scoped_refptr( | |
624 ChromeBlobStorageContext::GetFor(browser_context_)), | |
625 browser_context_->GetResourceContext(), | |
626 browser_context_->IsOffTheRecord())); | |
627 | |
628 // We do not call InitializeURLRequestContext() for media contexts because, | 603 // We do not call InitializeURLRequestContext() for media contexts because, |
629 // other than the HTTP cache, the media contexts share the same backing | 604 // other than the HTTP cache, the media contexts share the same backing |
630 // objects as their associated "normal" request context. Thus, the previous | 605 // objects as their associated "normal" request context. Thus, the previous |
631 // call serves to initialize the media request context for this storage | 606 // call serves to initialize the media request context for this storage |
632 // partition as well. | 607 // partition as well. |
633 } | 608 } |
634 } | 609 } |
635 | 610 |
636 } // namespace content | 611 } // namespace content |
OLD | NEW |