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) { | |
mmenke
2013/01/08 17:19:26
I think this is more complicated than necessary.
pauljensen
2013/01/21 06:24:56
Done. The lazy-init does require making webkit_bl
| |
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_; | |
mmenke
2013/01/08 17:19:26
Think just calling this "webkit_blob_protocol_hand
pauljensen
2013/01/21 06:24:56
With the mutable keyword it won't fit either way s
| |
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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 return it->second; | 488 return it->second; |
473 | 489 |
474 FilePath partition_path = | 490 FilePath partition_path = |
475 browser_context_->GetPath().Append( | 491 browser_context_->GetPath().Append( |
476 GetStoragePartitionPath(partition_domain, partition_name)); | 492 GetStoragePartitionPath(partition_domain, partition_name)); |
477 StoragePartitionImpl* partition = | 493 StoragePartitionImpl* partition = |
478 StoragePartitionImpl::Create(browser_context_, in_memory, | 494 StoragePartitionImpl::Create(browser_context_, in_memory, |
479 partition_path); | 495 partition_path); |
480 partitions_[partition_config] = partition; | 496 partitions_[partition_config] = partition; |
481 | 497 |
498 ChromeBlobStorageContext* blob_storage_context = | |
499 ChromeBlobStorageContext::GetFor(browser_context_); | |
500 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> blob_protocol_handler( | |
501 BlobProtocolHandler::Create(blob_storage_context, | |
502 partition->GetFileSystemContext())); | |
503 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> | |
504 file_system_protocol_handler( | |
505 CreateFileSystemProtocolHandler(partition->GetFileSystemContext())); | |
506 scoped_ptr<net::URLRequestJobFactory::Interceptor> developer_protocol_handler( | |
507 new DeveloperProtocolHandler(partition->GetAppCacheService(), | |
508 blob_storage_context)); | |
509 | |
482 // These calls must happen after StoragePartitionImpl::Create(). | 510 // These calls must happen after StoragePartitionImpl::Create(). |
483 partition->SetURLRequestContext( | 511 partition->SetURLRequestContext( |
484 partition_domain.empty() ? | 512 partition_domain.empty() ? |
485 browser_context_->GetRequestContext() : | 513 GetContentClient()->browser()->CreateRequestContext(browser_context_, |
486 browser_context_->GetRequestContextForStoragePartition( | 514 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(), |
487 partition->GetPath(), in_memory)); | 515 developer_protocol_handler.Pass()) : |
516 GetContentClient()->browser()->CreateRequestContextForStoragePartition( | |
517 browser_context_, partition->GetPath(), in_memory, | |
518 blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(), | |
519 developer_protocol_handler.Pass())); | |
488 partition->SetMediaURLRequestContext( | 520 partition->SetMediaURLRequestContext( |
489 partition_domain.empty() ? | 521 partition_domain.empty() ? |
490 browser_context_->GetMediaRequestContext() : | 522 browser_context_->GetMediaRequestContext() : |
491 browser_context_->GetMediaRequestContextForStoragePartition( | 523 browser_context_->GetMediaRequestContextForStoragePartition( |
492 partition->GetPath(), in_memory)); | 524 partition->GetPath(), in_memory)); |
493 | 525 |
494 PostCreateInitialization(partition, in_memory); | 526 PostCreateInitialization(partition, in_memory); |
495 | 527 |
496 return partition; | 528 return partition; |
497 } | 529 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
586 BrowserThread::IO, FROM_HERE, | 618 BrowserThread::IO, FROM_HERE, |
587 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 619 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
588 partition->GetAppCacheService(), | 620 partition->GetAppCacheService(), |
589 in_memory ? FilePath() : | 621 in_memory ? FilePath() : |
590 partition->GetPath().Append(kAppCacheDirname), | 622 partition->GetPath().Append(kAppCacheDirname), |
591 browser_context_->GetResourceContext(), | 623 browser_context_->GetResourceContext(), |
592 make_scoped_refptr(partition->GetURLRequestContext()), | 624 make_scoped_refptr(partition->GetURLRequestContext()), |
593 make_scoped_refptr( | 625 make_scoped_refptr( |
594 browser_context_->GetSpecialStoragePolicy()))); | 626 browser_context_->GetSpecialStoragePolicy()))); |
595 | 627 |
596 // Add content's URLRequestContext's hooks. | |
597 BrowserThread::PostTask( | |
598 BrowserThread::IO, FROM_HERE, | |
599 base::Bind( | |
600 &InitializeURLRequestContext, | |
601 make_scoped_refptr(partition->GetURLRequestContext()), | |
602 make_scoped_refptr(partition->GetAppCacheService()), | |
603 make_scoped_refptr(partition->GetFileSystemContext()), | |
604 make_scoped_refptr( | |
605 ChromeBlobStorageContext::GetFor(browser_context_)))); | |
mmenke
2013/01/08 17:19:26
Does this really need to be removed? We are still
pauljensen
2013/01/21 06:24:56
The reason I started all this was to eliminate Ini
| |
606 | |
607 // We do not call InitializeURLRequestContext() for media contexts because, | 628 // We do not call InitializeURLRequestContext() for media contexts because, |
608 // other than the HTTP cache, the media contexts share the same backing | 629 // other than the HTTP cache, the media contexts share the same backing |
609 // objects as their associated "normal" request context. Thus, the previous | 630 // objects as their associated "normal" request context. Thus, the previous |
610 // call serves to initialize the media request context for this storage | 631 // call serves to initialize the media request context for this storage |
611 // partition as well. | 632 // partition as well. |
612 } | 633 } |
613 } | 634 } |
614 | 635 |
615 } // namespace content | 636 } // namespace content |
OLD | NEW |