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