Chromium Code Reviews| 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_number_conversions.h" | |
| 11 #include "base/string_util.h" | 12 #include "base/string_util.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 is contains an extensions directory which is | |
|
nasko
2012/11/08 05:03:54
No need for "is" after the constant name.
| |
| 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 tags. Example: | |
| 197 // | |
| 198 // Storage/ext/ABCDEF/def | |
| 199 // Storage/ext/ABCDEF/hash(partition_name) | |
| 200 // | |
| 201 // The code in GetPartitionPath() constructs these path names. | |
| 202 const FilePath::CharType kStoragePartitionDirname[] = | |
| 203 FILE_PATH_LITERAL("Storage"); | |
| 204 const FilePath::CharType kExtensionsDirname[] = | |
| 205 FILE_PATH_LITERAL("ext"); | |
| 206 const FilePath::CharType kDefaultPartitionDirname[] = | |
| 207 FILE_PATH_LITERAL("def"); | |
| 208 | |
| 209 // Because partition names are user specified, they can be arbitrarily long | |
| 210 // which makes them unsuitable for paths names. We use a truncation of a | |
| 211 // SHA256 hash to perform a deterministic shortening of the string. The | |
| 212 // kPartitionNameHashBytes constant controls the length of the truncation. | |
| 213 // We use 6 bytes, which gives us 99.999% reliability against collisions over | |
| 214 // 1 million partition domains. | |
| 215 // | |
| 216 // Analysis: | |
| 217 // We assume that all partition names within one partition domain are | |
| 218 // controlled by the the same entity. Thus there is no chance for adverserial | |
| 219 // attack and all we care about is accidental collision. To get 5 9s over | |
| 220 // 1 million domains, we need the probability of a collision in any one domain | |
| 221 // to be | |
| 222 // | |
| 223 // p < nroot(1000000, .99999) ~= 10^-11. | |
| 224 // | |
| 225 // We the following birthday attack approximation to caculate the max number | |
| 226 // of unique names for this probability: | |
| 227 // | |
| 228 // n(p,H) = sqrt(2*H * ln(1/(1-p))) | |
| 229 // | |
| 230 // For a 6-byte hash, H = 2^(6*8). n(10^-11, H) ~= 75 | |
| 231 // | |
| 232 // An average partition domain is likely to have less than 10 unique | |
| 233 // partition names which is far lower than 75. | |
| 234 // | |
| 235 // Note, that for 4 9s of reliability, the limit is 237 partition names per | |
| 236 // partition domain. | |
|
nasko
2012/11/08 05:03:54
Awesome comment!
| |
| 237 const int kPartitionNameHashBytes = 6; | |
| 238 | |
| 186 } // namespace | 239 } // namespace |
| 187 | 240 |
| 241 // static | |
| 242 FilePath StoragePartitionImplMap::GetStoragePartitionPath( | |
| 243 const StoragePartitionDescriptor& descriptor) { | |
| 244 if (descriptor.partition_domain.empty()) | |
| 245 return FilePath(); | |
| 246 | |
| 247 FilePath path = FilePath(kStoragePartitionDirname).Append(kExtensionsDirname) | |
| 248 .AppendASCII(descriptor.partition_domain); | |
| 249 | |
| 250 if (!descriptor.partition_name.empty()) { | |
| 251 // For analysis of why this is safe, see the comment on | |
| 252 // kPartitionNameHashBytes. | |
| 253 char buffer[kPartitionNameHashBytes]; | |
| 254 crypto::SHA256HashString(descriptor.partition_name, &buffer[0], | |
| 255 sizeof(buffer)); | |
| 256 return path.AppendASCII(base::HexEncode(buffer, sizeof(buffer))); | |
| 257 } | |
| 258 | |
| 259 return path.Append(kDefaultPartitionDirname); | |
| 260 } | |
| 261 | |
| 262 | |
| 188 StoragePartitionImplMap::StoragePartitionImplMap( | 263 StoragePartitionImplMap::StoragePartitionImplMap( |
| 189 BrowserContext* browser_context) | 264 BrowserContext* browser_context) |
| 190 : browser_context_(browser_context), | 265 : browser_context_(browser_context), |
| 191 resource_context_initialized_(false) { | 266 resource_context_initialized_(false) { |
| 192 } | 267 } |
| 193 | 268 |
| 194 StoragePartitionImplMap::~StoragePartitionImplMap() { | 269 StoragePartitionImplMap::~StoragePartitionImplMap() { |
| 195 STLDeleteContainerPairSecondPointers(partitions_.begin(), | 270 STLDeleteContainerPairSecondPointers(partitions_.begin(), |
| 196 partitions_.end()); | 271 partitions_.end()); |
| 197 } | 272 } |
| 198 | 273 |
| 199 StoragePartitionImpl* StoragePartitionImplMap::Get( | 274 StoragePartitionImpl* StoragePartitionImplMap::Get( |
| 200 const std::string& partition_domain, | 275 const std::string& partition_domain, |
| 201 const std::string& partition_name, | 276 const std::string& partition_name, |
| 202 bool in_memory) { | 277 bool in_memory) { |
| 203 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 278 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
| 204 // We should move this into a place where it is called once per | 279 // We should move this into a place where it is called once per |
| 205 // BrowserContext creation rather than piggybacking off the default context | 280 // BrowserContext creation rather than piggybacking off the default context |
| 206 // creation. | 281 // creation. |
| 207 if (!resource_context_initialized_) { | 282 if (!resource_context_initialized_) { |
| 208 resource_context_initialized_ = true; | 283 resource_context_initialized_ = true; |
| 209 InitializeResourceContext(browser_context_); | 284 InitializeResourceContext(browser_context_); |
| 210 } | 285 } |
| 211 | 286 |
| 212 // Find the previously created partition if it's available. | 287 // Find the previously created partition if it's available. |
| 213 StoragePartitionImpl::StoragePartitionDescriptor partition_descriptor( | 288 StoragePartitionDescriptor partition_descriptor( |
| 214 partition_domain, partition_name, in_memory); | 289 partition_domain, partition_name, in_memory); |
| 215 | 290 |
| 216 PartitionsMap::const_iterator it = partitions_.find(partition_descriptor); | 291 PartitionsMap::const_iterator it = partitions_.find(partition_descriptor); |
| 217 if (it != partitions_.end()) | 292 if (it != partitions_.end()) |
| 218 return it->second; | 293 return it->second; |
| 219 | 294 |
| 220 // There was no previous partition, so let's make a new one. | 295 FilePath partition_path = |
| 296 browser_context_->GetPath().Append( | |
| 297 GetStoragePartitionPath(partition_descriptor)); | |
| 221 StoragePartitionImpl* partition = | 298 StoragePartitionImpl* partition = |
| 222 StoragePartitionImpl::Create(browser_context_, partition_descriptor, | 299 StoragePartitionImpl::Create(browser_context_, in_memory, |
| 223 browser_context_->GetPath()); | 300 partition_path); |
| 224 partitions_[partition_descriptor] = partition; | 301 partitions_[partition_descriptor] = partition; |
| 225 | 302 |
| 226 // These calls must happen after StoragePartitionImpl::Create(). | 303 // These calls must happen after StoragePartitionImpl::Create(). |
| 227 partition->SetURLRequestContext( | 304 partition->SetURLRequestContext( |
| 228 partition_domain.empty() ? | 305 partition_domain.empty() ? |
| 229 browser_context_->GetRequestContext() : | 306 browser_context_->GetRequestContext() : |
| 230 browser_context_->GetRequestContextForStoragePartition( | 307 browser_context_->GetRequestContextForStoragePartition( |
| 231 partition->GetPath(), in_memory)); | 308 partition->GetPath(), in_memory)); |
| 232 partition->SetMediaURLRequestContext( | 309 partition->SetMediaURLRequestContext( |
| 233 partition_domain.empty() ? | 310 partition_domain.empty() ? |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 | 354 |
| 278 // We do not call InitializeURLRequestContext() for media contexts because, | 355 // We do not call InitializeURLRequestContext() for media contexts because, |
| 279 // other than the HTTP cache, the media contexts share the same backing | 356 // other than the HTTP cache, the media contexts share the same backing |
| 280 // objects as their associated "normal" request context. Thus, the previous | 357 // objects as their associated "normal" request context. Thus, the previous |
| 281 // call serves to initialize the media request context for this storage | 358 // call serves to initialize the media request context for this storage |
| 282 // partition as well. | 359 // partition as well. |
| 283 } | 360 } |
| 284 } | 361 } |
| 285 | 362 |
| 286 } // namespace content | 363 } // namespace content |
| OLD | NEW |