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_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "content/browser/appcache/chrome_appcache_service.h" | 17 #include "content/browser/appcache/chrome_appcache_service.h" |
| 18 #include "content/browser/fileapi/browser_file_system_helper.h" | 18 #include "content/browser/fileapi/browser_file_system_helper.h" |
| 19 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 19 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 20 #include "content/browser/loader/resource_request_info_impl.h" | 20 #include "content/browser/loader/resource_request_info_impl.h" |
| 21 #include "content/browser/net/cookie_store_map_impl.h" | |
| 21 #include "content/browser/resource_context_impl.h" | 22 #include "content/browser/resource_context_impl.h" |
| 22 #include "content/browser/storage_partition_impl.h" | 23 #include "content/browser/storage_partition_impl.h" |
| 23 #include "content/browser/streams/stream.h" | 24 #include "content/browser/streams/stream.h" |
| 24 #include "content/browser/streams/stream_context.h" | 25 #include "content/browser/streams/stream_context.h" |
| 25 #include "content/browser/streams/stream_registry.h" | 26 #include "content/browser/streams/stream_registry.h" |
| 26 #include "content/browser/streams/stream_url_request_job.h" | 27 #include "content/browser/streams/stream_url_request_job.h" |
| 27 #include "content/browser/webui/url_data_manager_backend.h" | 28 #include "content/browser/webui/url_data_manager_backend.h" |
| 28 #include "content/public/browser/browser_context.h" | 29 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/content_browser_client.h" | 31 #include "content/public/browser/content_browser_client.h" |
| 32 #include "content/public/browser/cookie_store_factory.h" | |
| 31 #include "content/public/browser/storage_partition.h" | 33 #include "content/public/browser/storage_partition.h" |
| 32 #include "content/public/common/content_constants.h" | 34 #include "content/public/common/content_constants.h" |
| 33 #include "content/public/common/url_constants.h" | 35 #include "content/public/common/url_constants.h" |
| 34 #include "crypto/sha2.h" | 36 #include "crypto/sha2.h" |
| 37 #include "net/cookies/cookie_monster.h" | |
| 35 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 36 #include "net/url_request/url_request_context_getter.h" | 39 #include "net/url_request/url_request_context_getter.h" |
| 37 #include "webkit/browser/blob/blob_url_request_job_factory.h" | 40 #include "webkit/browser/blob/blob_url_request_job_factory.h" |
| 38 #include "webkit/browser/fileapi/file_system_url_request_job_factory.h" | 41 #include "webkit/browser/fileapi/file_system_url_request_job_factory.h" |
| 39 #include "webkit/common/blob/blob_data.h" | 42 #include "webkit/common/blob/blob_data.h" |
| 40 | 43 |
| 41 using appcache::AppCacheService; | 44 using appcache::AppCacheService; |
| 42 using fileapi::FileSystemContext; | 45 using fileapi::FileSystemContext; |
| 43 using webkit_blob::BlobStorageController; | 46 using webkit_blob::BlobStorageController; |
| 44 | 47 |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 // can be no colllisions on the move. | 342 // can be no colllisions on the move. |
| 340 base::Move(path, trash_directory.Append(path.BaseName())); | 343 base::Move(path, trash_directory.Append(path.BaseName())); |
| 341 } | 344 } |
| 342 } | 345 } |
| 343 | 346 |
| 344 file_access_runner->PostTask( | 347 file_access_runner->PostTask( |
| 345 FROM_HERE, | 348 FROM_HERE, |
| 346 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_directory, true)); | 349 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_directory, true)); |
| 347 } | 350 } |
| 348 | 351 |
| 352 void AttachCookieStoreOnIOThread( | |
| 353 const scoped_refptr<net::URLRequestContextGetter>& url_request_context, | |
| 354 const scoped_refptr<net::CookieStore> cookie_store) { | |
| 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 356 url_request_context->GetURLRequestContext()->set_cookie_store( | |
| 357 cookie_store.get()); | |
| 358 } | |
| 359 | |
| 360 net::CookieStore* CreateInMemoryCookieStore( | |
| 361 const CookieStoreConfig& default_config, | |
| 362 bool is_default) { | |
| 363 // See CreatePersistentCookieStore for note about delegates and |is_default|. | |
| 364 if (is_default) { | |
| 365 return CreateCookieStore( | |
| 366 CookieStoreConfig::InMemoryWithOptions( | |
| 367 default_config.storage_policy, default_config.cookie_delegate)); | |
| 368 } else { | |
| 369 return CreateCookieStore(CookieStoreConfig::InMemoryWithOptions( | |
| 370 default_config.storage_policy, NULL)); | |
| 371 } | |
| 372 } | |
| 373 | |
| 374 net::CookieStore* CreatePersistentCookieStore( | |
| 375 const CookieStoreConfig& default_config, | |
| 376 const base::FilePath& partition_path, | |
| 377 bool is_default) { | |
| 378 // Cookie delegates are not used in the non-default partition. If support | |
| 379 // is to be added for other partitions, then the delegate must be modified | |
| 380 // so it knows which partition cookie changes are sourcing from. | |
| 381 if (is_default) { | |
| 382 return CreateCookieStore(default_config); | |
| 383 } else { | |
| 384 return CreateCookieStore( | |
| 385 CookieStoreConfig::Persistent( | |
| 386 partition_path.Append(kCookieFilename), | |
| 387 default_config.session_cookie_mode)); | |
| 388 } | |
| 389 } | |
| 390 | |
| 349 } // namespace | 391 } // namespace |
| 350 | 392 |
| 351 // static | 393 // static |
| 352 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( | 394 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( |
| 353 const std::string& partition_domain, | 395 const std::string& partition_domain, |
| 354 const std::string& partition_name) { | 396 const std::string& partition_name) { |
| 355 if (partition_domain.empty()) | 397 if (partition_domain.empty()) |
| 356 return base::FilePath(); | 398 return base::FilePath(); |
| 357 | 399 |
| 358 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); | 400 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 396 StoragePartitionConfig partition_config( | 438 StoragePartitionConfig partition_config( |
| 397 partition_domain, partition_name, in_memory); | 439 partition_domain, partition_name, in_memory); |
| 398 | 440 |
| 399 PartitionMap::const_iterator it = partitions_.find(partition_config); | 441 PartitionMap::const_iterator it = partitions_.find(partition_config); |
| 400 if (it != partitions_.end()) | 442 if (it != partitions_.end()) |
| 401 return it->second; | 443 return it->second; |
| 402 | 444 |
| 403 base::FilePath partition_path = | 445 base::FilePath partition_path = |
| 404 browser_context_->GetPath().Append( | 446 browser_context_->GetPath().Append( |
| 405 GetStoragePartitionPath(partition_domain, partition_name)); | 447 GetStoragePartitionPath(partition_domain, partition_name)); |
| 448 | |
| 449 // Create the cookie stores. | |
| 450 scoped_ptr<CookieStoreMapImpl> cookie_store_map(new CookieStoreMapImpl()); | |
| 451 CookieStoreConfig default_config = browser_context_->GetCookieStoreConfig(); | |
| 452 bool in_memory_cookies = in_memory || default_config.in_memory; | |
| 453 browser_context_->OverrideCookieStoreMap(in_memory_cookies, partition_path, | |
| 454 partition_domain.empty(), | |
| 455 cookie_store_map.get()); | |
| 456 | |
| 457 // Add in cookie jars for Http, Https and file (if enabled). | |
| 458 scoped_refptr<net::CookieStore> http_cookie_store = | |
| 459 cookie_store_map->GetForScheme(chrome::kHttpScheme); | |
| 460 if (!http_cookie_store.get()) { | |
| 461 DCHECK(!cookie_store_map->GetForScheme(chrome::kHttpsScheme)); | |
| 462 | |
| 463 if (in_memory_cookies) { | |
| 464 http_cookie_store = CreateInMemoryCookieStore(default_config, | |
| 465 partition_domain.empty()); | |
| 466 } else { | |
| 467 http_cookie_store = CreatePersistentCookieStore(default_config, | |
| 468 partition_path, | |
| 469 partition_domain.empty()); | |
| 470 } | |
| 471 | |
| 472 cookie_store_map->SetForScheme(chrome::kHttpScheme, http_cookie_store); | |
| 473 cookie_store_map->SetForScheme(chrome::kHttpsScheme, http_cookie_store); | |
| 474 } | |
| 475 // There are places in code, such as data deletion, that assume both | |
| 476 // http and https share the same cookie jar. | |
| 477 DCHECK(cookie_store_map->GetForScheme(chrome::kHttpsScheme) == | |
| 478 http_cookie_store); | |
| 479 | |
| 480 // If file scheme is enable, share the same cookie store as http. This is | |
|
Matt Perry
2013/08/07 21:48:56
enabled*
awong
2013/08/16 00:54:54
Done.
| |
| 481 // legacy behavior. A complete valid, alternate approach is to use a | |
| 482 // different cookie database. See comments inside CookieMonster about | |
| 483 // separating out file cookies into their own CookieMonster. | |
| 484 if (!cookie_store_map->GetForScheme(chrome::kFileScheme)) { | |
| 485 if (http_cookie_store->GetCookieMonster()->IsCookieableScheme( | |
| 486 chrome::kFileScheme)) { | |
| 487 cookie_store_map->SetForScheme(chrome::kFileScheme, http_cookie_store); | |
| 488 } | |
| 489 } | |
| 490 | |
| 406 StoragePartitionImpl* partition = | 491 StoragePartitionImpl* partition = |
| 407 StoragePartitionImpl::Create(browser_context_, in_memory, | 492 StoragePartitionImpl::Create(browser_context_, in_memory, |
| 408 partition_path); | 493 partition_path, cookie_store_map.Pass()); |
| 409 partitions_[partition_config] = partition; | 494 partitions_[partition_config] = partition; |
| 410 | 495 |
| 411 ChromeBlobStorageContext* blob_storage_context = | 496 ChromeBlobStorageContext* blob_storage_context = |
| 412 ChromeBlobStorageContext::GetFor(browser_context_); | 497 ChromeBlobStorageContext::GetFor(browser_context_); |
| 413 StreamContext* stream_context = StreamContext::GetFor(browser_context_); | 498 StreamContext* stream_context = StreamContext::GetFor(browser_context_); |
| 414 ProtocolHandlerMap protocol_handlers; | 499 ProtocolHandlerMap protocol_handlers; |
| 415 protocol_handlers[chrome::kBlobScheme] = | 500 protocol_handlers[chrome::kBlobScheme] = |
| 416 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 501 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
| 417 new BlobProtocolHandler(blob_storage_context, | 502 new BlobProtocolHandler(blob_storage_context, |
| 418 stream_context, | 503 stream_context, |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 646 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
| 562 // We should move this into a place where it is called once per | 647 // We should move this into a place where it is called once per |
| 563 // BrowserContext creation rather than piggybacking off the default context | 648 // BrowserContext creation rather than piggybacking off the default context |
| 564 // creation. | 649 // creation. |
| 565 // Note: moving this into Get() before partitions_[] is set causes reentrency. | 650 // Note: moving this into Get() before partitions_[] is set causes reentrency. |
| 566 if (!resource_context_initialized_) { | 651 if (!resource_context_initialized_) { |
| 567 resource_context_initialized_ = true; | 652 resource_context_initialized_ = true; |
| 568 InitializeResourceContext(browser_context_); | 653 InitializeResourceContext(browser_context_); |
| 569 } | 654 } |
| 570 | 655 |
| 571 // Check first to avoid memory leak in unittests. | 656 // In unittests, BrowserThread::IO may not be valid which would yield a |
| 572 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 657 // memory leak on a PostTask. Also, in content_unittests, the |
| 658 // URLRequestContext is NULL. | |
| 659 // | |
| 660 // TODO(ajwong): Should default ContentBrowserClient return a non-null | |
| 661 // URLRequestContext? | |
| 662 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | |
| 663 partition->GetURLRequestContext()) { | |
| 664 // The main URLRequestContextGetter was first created just before | |
| 665 // PostCreateInitialization() is called thus attaching the CookieStore here | |
| 666 // is guaranteed to occur before anyone else can see the main request | |
| 667 // context including the media request context. | |
| 668 BrowserThread::PostTask( | |
| 669 BrowserThread::IO, FROM_HERE, | |
| 670 base::Bind(&AttachCookieStoreOnIOThread, | |
| 671 make_scoped_refptr(partition->GetURLRequestContext()), | |
| 672 make_scoped_refptr( | |
| 673 partition->GetCookieStoreMap().GetForScheme( | |
| 674 chrome::kHttpScheme)))); | |
| 675 | |
| 573 BrowserThread::PostTask( | 676 BrowserThread::PostTask( |
| 574 BrowserThread::IO, FROM_HERE, | 677 BrowserThread::IO, FROM_HERE, |
| 575 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 678 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
| 576 partition->GetAppCacheService(), | 679 partition->GetAppCacheService(), |
| 577 in_memory ? base::FilePath() : | 680 in_memory ? base::FilePath() : |
| 578 partition->GetPath().Append(kAppCacheDirname), | 681 partition->GetPath().Append(kAppCacheDirname), |
| 579 browser_context_->GetResourceContext(), | 682 browser_context_->GetResourceContext(), |
| 580 make_scoped_refptr(partition->GetURLRequestContext()), | 683 make_scoped_refptr(partition->GetURLRequestContext()), |
| 581 make_scoped_refptr( | 684 make_scoped_refptr( |
| 582 browser_context_->GetSpecialStoragePolicy()))); | 685 browser_context_->GetSpecialStoragePolicy()))); |
| 583 | 686 |
| 584 // We do not call InitializeURLRequestContext() for media contexts because, | 687 // We do not initialize the AppCacheService for media contexts because, |
| 585 // other than the HTTP cache, the media contexts share the same backing | 688 // other than the HTTP cache, the media contexts share the same backing |
| 586 // objects as their associated "normal" request context. Thus, the previous | 689 // objects as their associated "normal" request context. Thus, the previous |
| 587 // call serves to initialize the media request context for this storage | 690 // call serves to initialize the media request context for this storage |
| 588 // partition as well. | 691 // partition as well. |
| 589 } | 692 } |
| 590 } | 693 } |
| 591 | 694 |
| 592 } // namespace content | 695 } // namespace content |
| OLD | NEW |