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 use the following birthday attack approximation to calculate the max |
| 228 // number 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 CHECK(IsStringUTF8(partition_domain)); |
| 251 |
| 252 FilePath path = FilePath(kStoragePartitionDirname).Append(kExtensionsDirname) |
| 253 .Append(FilePath::FromUTF8Unsafe(partition_domain)); |
| 254 |
| 255 if (!partition_name.empty()) { |
| 256 // For analysis of why we can ignore collisions, see the comment above |
| 257 // kPartitionNameHashBytes. |
| 258 char buffer[kPartitionNameHashBytes]; |
| 259 crypto::SHA256HashString(partition_name, &buffer[0], |
| 260 sizeof(buffer)); |
| 261 return path.AppendASCII(base::HexEncode(buffer, sizeof(buffer))); |
| 262 } |
| 263 |
| 264 return path.Append(kDefaultPartitionDirname); |
| 265 } |
| 266 |
| 267 |
188 StoragePartitionImplMap::StoragePartitionImplMap( | 268 StoragePartitionImplMap::StoragePartitionImplMap( |
189 BrowserContext* browser_context) | 269 BrowserContext* browser_context) |
190 : browser_context_(browser_context), | 270 : browser_context_(browser_context), |
191 resource_context_initialized_(false) { | 271 resource_context_initialized_(false) { |
192 } | 272 } |
193 | 273 |
194 StoragePartitionImplMap::~StoragePartitionImplMap() { | 274 StoragePartitionImplMap::~StoragePartitionImplMap() { |
195 STLDeleteContainerPairSecondPointers(partitions_.begin(), | 275 STLDeleteContainerPairSecondPointers(partitions_.begin(), |
196 partitions_.end()); | 276 partitions_.end()); |
197 } | 277 } |
198 | 278 |
199 StoragePartitionImpl* StoragePartitionImplMap::Get( | 279 StoragePartitionImpl* StoragePartitionImplMap::Get( |
200 const std::string& partition_domain, | 280 const std::string& partition_domain, |
201 const std::string& partition_name, | 281 const std::string& partition_name, |
202 bool in_memory) { | 282 bool in_memory) { |
203 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 283 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
204 // We should move this into a place where it is called once per | 284 // We should move this into a place where it is called once per |
205 // BrowserContext creation rather than piggybacking off the default context | 285 // BrowserContext creation rather than piggybacking off the default context |
206 // creation. | 286 // creation. |
207 if (!resource_context_initialized_) { | 287 if (!resource_context_initialized_) { |
208 resource_context_initialized_ = true; | 288 resource_context_initialized_ = true; |
209 InitializeResourceContext(browser_context_); | 289 InitializeResourceContext(browser_context_); |
210 } | 290 } |
211 | 291 |
212 // Find the previously created partition if it's available. | 292 // Find the previously created partition if it's available. |
213 StoragePartitionImpl::StoragePartitionConfig partition_config( | 293 StoragePartitionConfig partition_config( |
214 partition_domain, partition_name, in_memory); | 294 partition_domain, partition_name, in_memory); |
215 | 295 |
216 PartitionMap::const_iterator it = partitions_.find(partition_config); | 296 PartitionMap::const_iterator it = partitions_.find(partition_config); |
217 if (it != partitions_.end()) | 297 if (it != partitions_.end()) |
218 return it->second; | 298 return it->second; |
219 | 299 |
220 // There was no previous partition, so let's make a new one. | 300 FilePath partition_path = |
| 301 browser_context_->GetPath().Append( |
| 302 GetStoragePartitionPath(partition_domain, partition_name)); |
221 StoragePartitionImpl* partition = | 303 StoragePartitionImpl* partition = |
222 StoragePartitionImpl::Create(browser_context_, partition_config, | 304 StoragePartitionImpl::Create(browser_context_, in_memory, |
223 browser_context_->GetPath()); | 305 partition_path); |
224 partitions_[partition_config] = partition; | 306 partitions_[partition_config] = partition; |
225 | 307 |
226 // These calls must happen after StoragePartitionImpl::Create(). | 308 // These calls must happen after StoragePartitionImpl::Create(). |
227 partition->SetURLRequestContext( | 309 partition->SetURLRequestContext( |
228 partition_domain.empty() ? | 310 partition_domain.empty() ? |
229 browser_context_->GetRequestContext() : | 311 browser_context_->GetRequestContext() : |
230 browser_context_->GetRequestContextForStoragePartition( | 312 browser_context_->GetRequestContextForStoragePartition( |
231 partition->GetPath(), in_memory)); | 313 partition->GetPath(), in_memory)); |
232 partition->SetMediaURLRequestContext( | 314 partition->SetMediaURLRequestContext( |
233 partition_domain.empty() ? | 315 partition_domain.empty() ? |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 359 |
278 // We do not call InitializeURLRequestContext() for media contexts because, | 360 // We do not call InitializeURLRequestContext() for media contexts because, |
279 // other than the HTTP cache, the media contexts share the same backing | 361 // other than the HTTP cache, the media contexts share the same backing |
280 // objects as their associated "normal" request context. Thus, the previous | 362 // objects as their associated "normal" request context. Thus, the previous |
281 // call serves to initialize the media request context for this storage | 363 // call serves to initialize the media request context for this storage |
282 // partition as well. | 364 // partition as well. |
283 } | 365 } |
284 } | 366 } |
285 | 367 |
286 } // namespace content | 368 } // namespace content |
OLD | NEW |