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 "base/string_number_conversions.h" | |
12 #include "content/browser/appcache/chrome_appcache_service.h" | 13 #include "content/browser/appcache/chrome_appcache_service.h" |
13 #include "content/browser/fileapi/browser_file_system_helper.h" | 14 #include "content/browser/fileapi/browser_file_system_helper.h" |
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 15 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
15 #include "content/browser/histogram_internals_request_job.h" | 16 #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_blob_internals_job_factory.h" |
17 #include "content/browser/net/view_http_cache_job_factory.h" | 18 #include "content/browser/net/view_http_cache_job_factory.h" |
18 #include "content/browser/renderer_host/resource_request_info_impl.h" | 19 #include "content/browser/renderer_host/resource_request_info_impl.h" |
19 #include "content/browser/resource_context_impl.h" | 20 #include "content/browser/resource_context_impl.h" |
20 #include "content/browser/storage_partition_impl.h" | 21 #include "content/browser/storage_partition_impl.h" |
21 #include "content/browser/tcmalloc_internals_request_job.h" | 22 #include "content/browser/tcmalloc_internals_request_job.h" |
22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/storage_partition.h" | 25 #include "content/public/browser/storage_partition.h" |
25 #include "content/public/common/content_constants.h" | 26 #include "content/public/common/content_constants.h" |
26 #include "content/public/common/url_constants.h" | 27 #include "content/public/common/url_constants.h" |
28 #include "crypto/sha2.h" | |
27 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
28 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
29 #include "webkit/appcache/view_appcache_internals_job.h" | 31 #include "webkit/appcache/view_appcache_internals_job.h" |
30 #include "webkit/blob/blob_data.h" | 32 #include "webkit/blob/blob_data.h" |
31 #include "webkit/blob/blob_url_request_job_factory.h" | 33 #include "webkit/blob/blob_url_request_job_factory.h" |
32 #include "webkit/fileapi/file_system_url_request_job_factory.h" | 34 #include "webkit/fileapi/file_system_url_request_job_factory.h" |
33 | 35 |
34 using appcache::AppCacheService; | 36 using appcache::AppCacheService; |
35 using fileapi::FileSystemContext; | 37 using fileapi::FileSystemContext; |
36 using webkit_blob::BlobStorageController; | 38 using webkit_blob::BlobStorageController; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 CreateFileSystemProtocolHandler(file_system_context)); | 178 CreateFileSystemProtocolHandler(file_system_context)); |
177 DCHECK(set_protocol); | 179 DCHECK(set_protocol); |
178 | 180 |
179 job_factory->AddInterceptor( | 181 job_factory->AddInterceptor( |
180 new DeveloperProtocolHandler(appcache_service, | 182 new DeveloperProtocolHandler(appcache_service, |
181 blob_storage_context->controller())); | 183 blob_storage_context->controller())); |
182 | 184 |
183 // TODO(jam): Add the ProtocolHandlerRegistryIntercepter here! | 185 // TODO(jam): Add the ProtocolHandlerRegistryIntercepter here! |
184 } | 186 } |
185 | 187 |
188 // These constants are used to create the directory structure under the profile | |
189 // where renderers with a non-default storage partition keep their persistent | |
190 // state. This will contain a set of directories that partially mirror the | |
191 // directory structure of BrowserContext::GetPath(). | |
192 // | |
193 // The kStoragePartitionDirname contains an extensions directory which is | |
194 // further partitioned by extension id, followed by another level of directories | |
195 // for the "default" extension storage partition and one directory for each | |
196 // persistent partition used by a webview tag. Example: | |
197 // | |
198 // Storage/ext/ABCDEF/def | |
199 // Storage/ext/ABCDEF/hash(partition name) | |
200 // | |
201 // The code in GetStoragePartitionPath() constructs these path names. | |
202 // | |
203 // TODO(nasko): Move extension related path code out of content. | |
204 const FilePath::CharType kStoragePartitionDirname[] = | |
205 FILE_PATH_LITERAL("Storage"); | |
206 const FilePath::CharType kExtensionsDirname[] = | |
207 FILE_PATH_LITERAL("ext"); | |
208 const FilePath::CharType kDefaultPartitionDirname[] = | |
209 FILE_PATH_LITERAL("def"); | |
210 | |
211 // Because partition names are user specified, they can be arbitrarily long | |
212 // which makes them unsuitable for paths names. We use a truncation of a | |
213 // SHA256 hash to perform a deterministic shortening of the string. The | |
214 // kPartitionNameHashBytes constant controls the length of the truncation. | |
215 // We use 6 bytes, which gives us 99.999% reliability against collisions over | |
216 // 1 million partition domains. | |
217 // | |
218 // Analysis: | |
219 // We assume that all partition names within one partition domain are | |
220 // controlled by the the same entity. Thus there is no chance for adverserial | |
221 // attack and all we care about is accidental collision. To get 5 9s over | |
222 // 1 million domains, we need the probability of a collision in any one domain | |
223 // to be | |
224 // | |
225 // p < nroot(1000000, .99999) ~= 10^-11 | |
226 // | |
227 // We the following birthday attack approximation to caculate the max number | |
228 // of unique names for this probability: | |
229 // | |
230 // n(p,H) = sqrt(2*H * ln(1/(1-p))) | |
231 // | |
232 // For a 6-byte hash, H = 2^(6*8). n(10^-11, H) ~= 75 | |
233 // | |
234 // An average partition domain is likely to have less than 10 unique | |
235 // partition names which is far lower than 75. | |
236 // | |
237 // Note, that for 4 9s of reliability, the limit is 237 partition names per | |
238 // partition domain. | |
239 const int kPartitionNameHashBytes = 6; | |
240 | |
186 } // namespace | 241 } // namespace |
187 | 242 |
243 // static | |
244 FilePath StoragePartitionImplMap::GetStoragePartitionPath( | |
245 const std::string& partition_domain, | |
246 const std::string& partition_name) { | |
247 if (partition_domain.empty()) | |
248 return FilePath(); | |
249 | |
250 FilePath path = FilePath(kStoragePartitionDirname).Append(kExtensionsDirname) | |
251 .AppendASCII(partition_domain); | |
nasko
2012/11/12 16:47:28
I thought we wanted to avoid the ASCII constraint?
awong
2012/11/12 21:36:16
Bad merge. Thanks for catching that.
| |
252 | |
253 if (!partition_name.empty()) { | |
254 // For analysis of why we can ignore collisions, see the comment above | |
255 // kPartitionNameHashBytes. | |
256 char buffer[kPartitionNameHashBytes]; | |
257 crypto::SHA256HashString(partition_name, &buffer[0], | |
258 sizeof(buffer)); | |
259 return path.AppendASCII(base::HexEncode(buffer, sizeof(buffer))); | |
260 } | |
261 | |
262 return path.Append(kDefaultPartitionDirname); | |
263 } | |
264 | |
265 | |
188 StoragePartitionImplMap::StoragePartitionImplMap( | 266 StoragePartitionImplMap::StoragePartitionImplMap( |
189 BrowserContext* browser_context) | 267 BrowserContext* browser_context) |
190 : browser_context_(browser_context), | 268 : browser_context_(browser_context), |
191 resource_context_initialized_(false) { | 269 resource_context_initialized_(false) { |
192 } | 270 } |
193 | 271 |
194 StoragePartitionImplMap::~StoragePartitionImplMap() { | 272 StoragePartitionImplMap::~StoragePartitionImplMap() { |
195 STLDeleteContainerPairSecondPointers(partitions_.begin(), | 273 STLDeleteContainerPairSecondPointers(partitions_.begin(), |
196 partitions_.end()); | 274 partitions_.end()); |
197 } | 275 } |
198 | 276 |
199 StoragePartitionImpl* StoragePartitionImplMap::Get( | 277 StoragePartitionImpl* StoragePartitionImplMap::Get( |
200 const std::string& partition_domain, | 278 const std::string& partition_domain, |
201 const std::string& partition_name, | 279 const std::string& partition_name, |
202 bool in_memory) { | 280 bool in_memory) { |
203 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 281 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
204 // We should move this into a place where it is called once per | 282 // We should move this into a place where it is called once per |
205 // BrowserContext creation rather than piggybacking off the default context | 283 // BrowserContext creation rather than piggybacking off the default context |
206 // creation. | 284 // creation. |
207 if (!resource_context_initialized_) { | 285 if (!resource_context_initialized_) { |
208 resource_context_initialized_ = true; | 286 resource_context_initialized_ = true; |
209 InitializeResourceContext(browser_context_); | 287 InitializeResourceContext(browser_context_); |
210 } | 288 } |
211 | 289 |
212 // Find the previously created partition if it's available. | 290 // Find the previously created partition if it's available. |
213 StoragePartitionImpl::StoragePartitionConfig partition_config( | 291 StoragePartitionConfig partition_config( |
214 partition_domain, partition_name, in_memory); | 292 partition_domain, partition_name, in_memory); |
215 | 293 |
216 PartitionMap::const_iterator it = partitions_.find(partition_config); | 294 PartitionMap::const_iterator it = partitions_.find(partition_config); |
217 if (it != partitions_.end()) | 295 if (it != partitions_.end()) |
218 return it->second; | 296 return it->second; |
219 | 297 |
220 // There was no previous partition, so let's make a new one. | 298 FilePath partition_path = |
299 browser_context_->GetPath().Append( | |
300 GetStoragePartitionPath(partition_domain, partition_name)); | |
221 StoragePartitionImpl* partition = | 301 StoragePartitionImpl* partition = |
222 StoragePartitionImpl::Create(browser_context_, partition_config, | 302 StoragePartitionImpl::Create(browser_context_, in_memory, |
223 browser_context_->GetPath()); | 303 partition_path); |
224 partitions_[partition_config] = partition; | 304 partitions_[partition_config] = partition; |
225 | 305 |
226 // These calls must happen after StoragePartitionImpl::Create(). | 306 // These calls must happen after StoragePartitionImpl::Create(). |
227 partition->SetURLRequestContext( | 307 partition->SetURLRequestContext( |
228 partition_domain.empty() ? | 308 partition_domain.empty() ? |
229 browser_context_->GetRequestContext() : | 309 browser_context_->GetRequestContext() : |
230 browser_context_->GetRequestContextForStoragePartition( | 310 browser_context_->GetRequestContextForStoragePartition( |
231 partition->GetPath(), in_memory)); | 311 partition->GetPath(), in_memory)); |
232 partition->SetMediaURLRequestContext( | 312 partition->SetMediaURLRequestContext( |
233 partition_domain.empty() ? | 313 partition_domain.empty() ? |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 | 357 |
278 // We do not call InitializeURLRequestContext() for media contexts because, | 358 // We do not call InitializeURLRequestContext() for media contexts because, |
279 // other than the HTTP cache, the media contexts share the same backing | 359 // other than the HTTP cache, the media contexts share the same backing |
280 // objects as their associated "normal" request context. Thus, the previous | 360 // objects as their associated "normal" request context. Thus, the previous |
281 // call serves to initialize the media request context for this storage | 361 // call serves to initialize the media request context for this storage |
282 // partition as well. | 362 // partition as well. |
283 } | 363 } |
284 } | 364 } |
285 | 365 |
286 } // namespace content | 366 } // namespace content |
OLD | NEW |