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/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "content/browser/appcache/chrome_appcache_service.h" | 12 #include "content/browser/appcache/chrome_appcache_service.h" |
13 #include "content/browser/fileapi/browser_file_system_helper.h" | |
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | |
15 #include "content/browser/histogram_internals_request_job.h" | |
16 #include "content/browser/net/view_blob_internals_job_factory.h" | |
17 #include "content/browser/net/view_http_cache_job_factory.h" | |
18 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
13 #include "content/browser/resource_context_impl.h" | 19 #include "content/browser/resource_context_impl.h" |
14 #include "content/browser/storage_partition_impl.h" | 20 #include "content/browser/storage_partition_impl.h" |
21 #include "content/browser/tcmalloc_internals_request_job.h" | |
15 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/storage_partition.h" | |
17 #include "content/public/common/content_constants.h" | 25 #include "content/public/common/content_constants.h" |
26 #include "content/public/common/url_constants.h" | |
18 #include "net/url_request/url_request_context_getter.h" | 27 #include "net/url_request/url_request_context_getter.h" |
28 #include "net/url_request/url_request_context.h" | |
29 #include "webkit/appcache/view_appcache_internals_job.h" | |
30 #include "webkit/blob/blob_data.h" | |
31 #include "webkit/blob/blob_url_request_job_factory.h" | |
32 #include "webkit/fileapi/file_system_url_request_job_factory.h" | |
33 | |
34 using appcache::AppCacheService; | |
35 using content::BrowserThread; | |
36 using fileapi::FileSystemContext; | |
37 using webkit_blob::BlobStorageController; | |
19 | 38 |
20 namespace content { | 39 namespace content { |
21 | 40 |
41 namespace { | |
42 | |
43 class BlobProtocolHandler : public webkit_blob::BlobProtocolHandler { | |
Charlie Reis
2012/09/17 22:51:12
Isn't it a little odd to have this much initializa
awong
2012/09/17 23:10:20
Yes, it doesn't belong here. However, until I can
| |
44 public: | |
45 BlobProtocolHandler( | |
46 webkit_blob::BlobStorageController* blob_storage_controller, | |
47 base::MessageLoopProxy* loop_proxy) | |
48 : webkit_blob::BlobProtocolHandler(blob_storage_controller, | |
49 loop_proxy) {} | |
50 | |
51 virtual ~BlobProtocolHandler() {} | |
52 | |
53 private: | |
54 virtual scoped_refptr<webkit_blob::BlobData> | |
55 LookupBlobData(net::URLRequest* request) const { | |
56 const ResourceRequestInfoImpl* info = | |
57 ResourceRequestInfoImpl::ForRequest(request); | |
58 if (!info) | |
59 return NULL; | |
60 return info->requested_blob_data(); | |
61 } | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(BlobProtocolHandler); | |
64 }; | |
65 | |
66 // Adds a bunch of debugging urls. We use an interceptor instead of a protocol | |
67 // handler because we want to reuse the chrome://scheme (everyone is familiar | |
68 // with it, and no need to expose the content/chrome separation through our UI). | |
69 class DeveloperProtocolHandler | |
70 : public net::URLRequestJobFactory::Interceptor { | |
71 public: | |
72 DeveloperProtocolHandler( | |
73 AppCacheService* appcache_service, | |
74 BlobStorageController* blob_storage_controller) | |
75 : appcache_service_(appcache_service), | |
76 blob_storage_controller_(blob_storage_controller) {} | |
77 virtual ~DeveloperProtocolHandler() {} | |
78 | |
79 virtual net::URLRequestJob* MaybeIntercept( | |
80 net::URLRequest* request, | |
81 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
82 // Check for chrome://view-http-cache/*, which uses its own job type. | |
83 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) | |
84 return ViewHttpCacheJobFactory::CreateJobForRequest(request, | |
85 network_delegate); | |
86 | |
87 // Next check for chrome://appcache-internals/, which uses its own job type. | |
88 if (request->url().SchemeIs(chrome::kChromeUIScheme) && | |
89 request->url().host() == chrome::kChromeUIAppCacheInternalsHost) { | |
90 return appcache::ViewAppCacheInternalsJobFactory::CreateJobForRequest( | |
91 request, network_delegate, appcache_service_); | |
92 } | |
93 | |
94 // Next check for chrome://blob-internals/, which uses its own job type. | |
95 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) { | |
96 return ViewBlobInternalsJobFactory::CreateJobForRequest( | |
97 request, network_delegate, blob_storage_controller_); | |
98 } | |
99 | |
100 #if defined(USE_TCMALLOC) | |
101 // Next check for chrome://tcmalloc/, which uses its own job type. | |
102 if (request->url().SchemeIs(chrome::kChromeUIScheme) && | |
103 request->url().host() == chrome::kChromeUITcmallocHost) { | |
104 return new TcmallocInternalsRequestJob(request, network_delegate); | |
105 } | |
106 #endif | |
107 | |
108 // Next check for chrome://histograms/, which uses its own job type. | |
109 if (request->url().SchemeIs(chrome::kChromeUIScheme) && | |
110 request->url().host() == chrome::kChromeUIHistogramHost) { | |
111 return new HistogramInternalsRequestJob(request, network_delegate); | |
112 } | |
113 | |
114 return NULL; | |
115 } | |
116 | |
117 virtual net::URLRequestJob* MaybeInterceptRedirect( | |
118 const GURL& location, | |
119 net::URLRequest* request, | |
120 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
121 return NULL; | |
122 } | |
123 | |
124 virtual net::URLRequestJob* MaybeInterceptResponse( | |
125 net::URLRequest* request, | |
126 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
127 return NULL; | |
128 } | |
129 | |
130 virtual bool WillHandleProtocol(const std::string& protocol) const { | |
131 return protocol == chrome::kChromeUIScheme; | |
132 } | |
133 | |
134 private: | |
135 AppCacheService* appcache_service_; | |
136 BlobStorageController* blob_storage_controller_; | |
137 }; | |
138 | |
139 void InitializeURLRequestContext( | |
140 net::URLRequestContextGetter* context_getter, | |
141 AppCacheService* appcache_service, | |
142 FileSystemContext* file_system_context, | |
143 ChromeBlobStorageContext* blob_storage_context) { | |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
145 if (!context_getter) | |
146 return; // tests. | |
147 net::URLRequestContext* context = context_getter->GetURLRequestContext(); | |
148 net::URLRequestJobFactory* job_factory = | |
149 const_cast<net::URLRequestJobFactory*>(context->job_factory()); | |
150 if (job_factory->IsHandledProtocol(chrome::kBlobScheme)) | |
151 return; // Already initialized this RequestContext. | |
152 | |
153 bool set_protocol = job_factory->SetProtocolHandler( | |
154 chrome::kBlobScheme, | |
155 new BlobProtocolHandler( | |
156 blob_storage_context->controller(), | |
157 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); | |
158 DCHECK(set_protocol); | |
159 set_protocol = job_factory->SetProtocolHandler( | |
160 chrome::kFileSystemScheme, | |
161 CreateFileSystemProtocolHandler(file_system_context)); | |
162 DCHECK(set_protocol); | |
163 | |
164 job_factory->AddInterceptor( | |
165 new DeveloperProtocolHandler(appcache_service, | |
166 blob_storage_context->controller())); | |
167 | |
168 // TODO(jam): Add the ProtocolHandlerRegistryIntercepter here! | |
169 } | |
170 | |
171 } // namespace | |
172 | |
22 StoragePartitionImplMap::StoragePartitionImplMap( | 173 StoragePartitionImplMap::StoragePartitionImplMap( |
23 BrowserContext* browser_context) | 174 BrowserContext* browser_context) |
24 : browser_context_(browser_context) { | 175 : browser_context_(browser_context) { |
25 } | 176 } |
26 | 177 |
27 StoragePartitionImplMap::~StoragePartitionImplMap() { | 178 StoragePartitionImplMap::~StoragePartitionImplMap() { |
28 STLDeleteContainerPairSecondPointers(partitions_.begin(), | 179 STLDeleteContainerPairSecondPointers(partitions_.begin(), |
29 partitions_.end()); | 180 partitions_.end()); |
30 } | 181 } |
31 | 182 |
32 StoragePartitionImpl* StoragePartitionImplMap::Get( | 183 StoragePartitionImpl* StoragePartitionImplMap::Get( |
33 const std::string& partition_id) { | 184 const std::string& partition_id) { |
34 // Find the previously created partition if it's available. | 185 // Find the previously created partition if it's available. |
35 std::map<std::string, StoragePartitionImpl*>::const_iterator it = | 186 std::map<std::string, StoragePartitionImpl*>::const_iterator it = |
36 partitions_.find(partition_id); | 187 partitions_.find(partition_id); |
37 if (it != partitions_.end()) | 188 if (it != partitions_.end()) |
38 return it->second; | 189 return it->second; |
39 | 190 |
40 // There was no previous partition, so let's make a new one. | 191 // There was no previous partition, so let's make a new one. |
41 StoragePartitionImpl* storage_partition = | 192 StoragePartitionImpl* partition = |
42 StoragePartitionImpl::Create(browser_context_, | 193 StoragePartitionImpl::Create(browser_context_, partition_id, |
43 partition_id, | |
44 browser_context_->GetPath()); | 194 browser_context_->GetPath()); |
45 partitions_[partition_id] = storage_partition; | 195 partitions_[partition_id] = partition; |
46 | 196 |
47 net::URLRequestContextGetter* request_context = partition_id.empty() ? | 197 // These calls must happen after StoragePartitionImpl::Create(). |
198 partition->SetURLRequestContext( | |
199 partition_id.empty() ? | |
48 browser_context_->GetRequestContext() : | 200 browser_context_->GetRequestContext() : |
49 browser_context_->GetRequestContextForStoragePartition(partition_id); | 201 browser_context_->GetRequestContextForStoragePartition(partition_id)); |
202 partition->SetMediaURLRequestContext( | |
203 partition_id.empty() ? | |
204 browser_context_->GetMediaRequestContext() : | |
205 browser_context_->GetMediaRequestContextForStoragePartition( | |
206 partition_id)); | |
50 | 207 |
51 PostCreateInitialization(storage_partition, request_context); | 208 PostCreateInitialization(partition); |
52 | 209 |
53 // TODO(ajwong): We need to remove this conditional by making | 210 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
54 // InitializeResourceContext() understand having different partition data | 211 // We should move this into a place where it is called once per |
55 // based on the renderer_id. | 212 // BrowserContext creation rather than piggybacking off the default context |
213 // creation. | |
56 if (partition_id.empty()) { | 214 if (partition_id.empty()) { |
57 InitializeResourceContext(browser_context_); | 215 InitializeResourceContext(browser_context_); |
58 } | 216 } |
59 | 217 |
60 return storage_partition; | 218 return partition; |
61 } | 219 } |
62 | 220 |
63 void StoragePartitionImplMap::ForEach( | 221 void StoragePartitionImplMap::ForEach( |
64 const BrowserContext::StoragePartitionCallback& callback) { | 222 const BrowserContext::StoragePartitionCallback& callback) { |
65 for (std::map<std::string, StoragePartitionImpl*>::const_iterator it = | 223 for (std::map<std::string, StoragePartitionImpl*>::const_iterator it = |
66 partitions_.begin(); | 224 partitions_.begin(); |
67 it != partitions_.end(); | 225 it != partitions_.end(); |
68 ++it) { | 226 ++it) { |
69 callback.Run(it->first, it->second); | 227 callback.Run(it->first, it->second); |
70 } | 228 } |
71 } | 229 } |
72 | 230 |
73 void StoragePartitionImplMap::PostCreateInitialization( | 231 void StoragePartitionImplMap::PostCreateInitialization( |
74 StoragePartitionImpl* partition, | 232 StoragePartitionImpl* partition) { |
75 net::URLRequestContextGetter* request_context_getter) { | |
76 // Check first to avoid memory leak in unittests. | 233 // Check first to avoid memory leak in unittests. |
77 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 234 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
78 BrowserThread::PostTask( | 235 BrowserThread::PostTask( |
79 BrowserThread::IO, FROM_HERE, | 236 BrowserThread::IO, FROM_HERE, |
80 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 237 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
81 partition->GetAppCacheService(), | 238 partition->GetAppCacheService(), |
82 browser_context_->IsOffTheRecord() ? FilePath() : | 239 browser_context_->IsOffTheRecord() ? FilePath() : |
83 partition->GetPath().Append(kAppCacheDirname), | 240 partition->GetPath().Append(kAppCacheDirname), |
84 browser_context_->GetResourceContext(), | 241 browser_context_->GetResourceContext(), |
85 make_scoped_refptr(request_context_getter), | 242 make_scoped_refptr(partition->GetURLRequestContext()), |
86 make_scoped_refptr( | 243 make_scoped_refptr( |
87 browser_context_->GetSpecialStoragePolicy()))); | 244 browser_context_->GetSpecialStoragePolicy()))); |
245 | |
246 // Add content's URLRequestContext's hooks. | |
247 BrowserThread::PostTask( | |
248 BrowserThread::IO, FROM_HERE, | |
249 base::Bind( | |
250 &InitializeURLRequestContext, | |
251 make_scoped_refptr(partition->GetURLRequestContext()), | |
252 make_scoped_refptr(partition->GetAppCacheService()), | |
253 make_scoped_refptr(partition->GetFileSystemContext()), | |
254 make_scoped_refptr( | |
255 ChromeBlobStorageContext::GetFor(browser_context_)))); | |
256 BrowserThread::PostTask( | |
257 BrowserThread::IO, FROM_HERE, | |
258 base::Bind( | |
259 &InitializeURLRequestContext, | |
260 make_scoped_refptr(partition->GetMediaURLRequestContext()), | |
261 make_scoped_refptr(partition->GetAppCacheService()), | |
262 make_scoped_refptr(partition->GetFileSystemContext()), | |
263 make_scoped_refptr( | |
264 ChromeBlobStorageContext::GetFor(browser_context_)))); | |
88 } | 265 } |
89 } | 266 } |
90 | 267 |
91 } // namespace content | 268 } // namespace content |
OLD | NEW |