| 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.h" | |
| 22 #include "content/browser/resource_context_impl.h" | 21 #include "content/browser/resource_context_impl.h" |
| 23 #include "content/browser/storage_partition_impl.h" | 22 #include "content/browser/storage_partition_impl.h" |
| 24 #include "content/browser/streams/stream.h" | 23 #include "content/browser/streams/stream.h" |
| 25 #include "content/browser/streams/stream_context.h" | 24 #include "content/browser/streams/stream_context.h" |
| 26 #include "content/browser/streams/stream_registry.h" | 25 #include "content/browser/streams/stream_registry.h" |
| 27 #include "content/browser/streams/stream_url_request_job.h" | 26 #include "content/browser/streams/stream_url_request_job.h" |
| 28 #include "content/browser/webui/url_data_manager_backend.h" | 27 #include "content/browser/webui/url_data_manager_backend.h" |
| 29 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
| 30 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/content_browser_client.h" | 30 #include "content/public/browser/content_browser_client.h" |
| 32 #include "content/public/browser/cookie_store_factory.h" | |
| 33 #include "content/public/browser/storage_partition.h" | 31 #include "content/public/browser/storage_partition.h" |
| 34 #include "content/public/common/content_constants.h" | 32 #include "content/public/common/content_constants.h" |
| 35 #include "content/public/common/url_constants.h" | 33 #include "content/public/common/url_constants.h" |
| 36 #include "crypto/sha2.h" | 34 #include "crypto/sha2.h" |
| 37 #include "net/cookies/cookie_monster.h" | |
| 38 #include "net/url_request/url_request_context.h" | 35 #include "net/url_request/url_request_context.h" |
| 39 #include "net/url_request/url_request_context_getter.h" | 36 #include "net/url_request/url_request_context_getter.h" |
| 40 #include "webkit/browser/blob/blob_url_request_job_factory.h" | 37 #include "webkit/browser/blob/blob_url_request_job_factory.h" |
| 41 #include "webkit/browser/fileapi/file_system_url_request_job_factory.h" | 38 #include "webkit/browser/fileapi/file_system_url_request_job_factory.h" |
| 42 #include "webkit/common/blob/blob_data.h" | 39 #include "webkit/common/blob/blob_data.h" |
| 43 | 40 |
| 44 using appcache::AppCacheService; | 41 using appcache::AppCacheService; |
| 45 using fileapi::FileSystemContext; | 42 using fileapi::FileSystemContext; |
| 46 using webkit_blob::BlobStorageController; | 43 using webkit_blob::BlobStorageController; |
| 47 | 44 |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 // can be no colllisions on the move. | 339 // can be no colllisions on the move. |
| 343 base::Move(path, trash_directory.Append(path.BaseName())); | 340 base::Move(path, trash_directory.Append(path.BaseName())); |
| 344 } | 341 } |
| 345 } | 342 } |
| 346 | 343 |
| 347 file_access_runner->PostTask( | 344 file_access_runner->PostTask( |
| 348 FROM_HERE, | 345 FROM_HERE, |
| 349 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_directory, true)); | 346 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_directory, true)); |
| 350 } | 347 } |
| 351 | 348 |
| 352 void AttachDefaultCookieStoreOnIOThread( | |
| 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 } // namespace | 349 } // namespace |
| 361 | 350 |
| 362 // static | 351 // static |
| 363 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( | 352 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( |
| 364 const std::string& partition_domain, | 353 const std::string& partition_domain, |
| 365 const std::string& partition_name) { | 354 const std::string& partition_name) { |
| 366 if (partition_domain.empty()) | 355 if (partition_domain.empty()) |
| 367 return base::FilePath(); | 356 return base::FilePath(); |
| 368 | 357 |
| 369 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); | 358 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 StoragePartitionConfig partition_config( | 396 StoragePartitionConfig partition_config( |
| 408 partition_domain, partition_name, in_memory); | 397 partition_domain, partition_name, in_memory); |
| 409 | 398 |
| 410 PartitionMap::const_iterator it = partitions_.find(partition_config); | 399 PartitionMap::const_iterator it = partitions_.find(partition_config); |
| 411 if (it != partitions_.end()) | 400 if (it != partitions_.end()) |
| 412 return it->second; | 401 return it->second; |
| 413 | 402 |
| 414 base::FilePath partition_path = | 403 base::FilePath partition_path = |
| 415 browser_context_->GetPath().Append( | 404 browser_context_->GetPath().Append( |
| 416 GetStoragePartitionPath(partition_domain, partition_name)); | 405 GetStoragePartitionPath(partition_domain, partition_name)); |
| 417 | |
| 418 // Create the cookie stores. | |
| 419 scoped_ptr<CookieStoreMap> cookie_store_map( | |
| 420 CreateCookieStores(partition_path, | |
| 421 in_memory, | |
| 422 partition_domain.empty())); | |
| 423 | |
| 424 StoragePartitionImpl* partition = | 406 StoragePartitionImpl* partition = |
| 425 StoragePartitionImpl::Create(browser_context_, in_memory, | 407 StoragePartitionImpl::Create(browser_context_, in_memory, |
| 426 partition_path, cookie_store_map.Pass()); | 408 partition_path); |
| 427 partitions_[partition_config] = partition; | 409 partitions_[partition_config] = partition; |
| 428 | 410 |
| 429 ChromeBlobStorageContext* blob_storage_context = | 411 ChromeBlobStorageContext* blob_storage_context = |
| 430 ChromeBlobStorageContext::GetFor(browser_context_); | 412 ChromeBlobStorageContext::GetFor(browser_context_); |
| 431 StreamContext* stream_context = StreamContext::GetFor(browser_context_); | 413 StreamContext* stream_context = StreamContext::GetFor(browser_context_); |
| 432 ProtocolHandlerMap protocol_handlers; | 414 ProtocolHandlerMap protocol_handlers; |
| 433 protocol_handlers[chrome::kBlobScheme] = | 415 protocol_handlers[chrome::kBlobScheme] = |
| 434 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 416 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
| 435 new BlobProtocolHandler(blob_storage_context, | 417 new BlobProtocolHandler(blob_storage_context, |
| 436 stream_context, | 418 stream_context, |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 561 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
| 580 // We should move this into a place where it is called once per | 562 // We should move this into a place where it is called once per |
| 581 // BrowserContext creation rather than piggybacking off the default context | 563 // BrowserContext creation rather than piggybacking off the default context |
| 582 // creation. | 564 // creation. |
| 583 // Note: moving this into Get() before partitions_[] is set causes reentrency. | 565 // Note: moving this into Get() before partitions_[] is set causes reentrency. |
| 584 if (!resource_context_initialized_) { | 566 if (!resource_context_initialized_) { |
| 585 resource_context_initialized_ = true; | 567 resource_context_initialized_ = true; |
| 586 InitializeResourceContext(browser_context_); | 568 InitializeResourceContext(browser_context_); |
| 587 } | 569 } |
| 588 | 570 |
| 589 // In unittests, BrowserThread::IO may not be valid which would yield a | 571 // Check first to avoid memory leak in unittests. |
| 590 // memory leak on a PostTask. Also, in content_unittests, the | 572 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 591 // URLRequestContext is NULL. | |
| 592 // | |
| 593 // TODO(ajwong): Should default ContentBrowserClient return a non-null | |
| 594 // URLRequestContext? | |
| 595 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | |
| 596 partition->GetURLRequestContext()) { | |
| 597 // The main URLRequestContextGetter was first created just before | |
| 598 // PostCreateInitialization() is called thus attaching the CookieStore here | |
| 599 // is guaranteed to occur before anyone else can see the main request | |
| 600 // context including the media request context. | |
| 601 // | |
| 602 // We pass the cookie store for chrome::kHttpScheme because we know this | |
| 603 // will return the default cookie store. | |
| 604 BrowserThread::PostTask( | |
| 605 BrowserThread::IO, FROM_HERE, | |
| 606 base::Bind(&AttachDefaultCookieStoreOnIOThread, | |
| 607 make_scoped_refptr(partition->GetURLRequestContext()), | |
| 608 make_scoped_refptr( | |
| 609 partition->GetCookieStoreMap().GetForScheme( | |
| 610 chrome::kHttpScheme)))); | |
| 611 | |
| 612 BrowserThread::PostTask( | 573 BrowserThread::PostTask( |
| 613 BrowserThread::IO, FROM_HERE, | 574 BrowserThread::IO, FROM_HERE, |
| 614 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 575 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
| 615 partition->GetAppCacheService(), | 576 partition->GetAppCacheService(), |
| 616 in_memory ? base::FilePath() : | 577 in_memory ? base::FilePath() : |
| 617 partition->GetPath().Append(kAppCacheDirname), | 578 partition->GetPath().Append(kAppCacheDirname), |
| 618 browser_context_->GetResourceContext(), | 579 browser_context_->GetResourceContext(), |
| 619 make_scoped_refptr(partition->GetURLRequestContext()), | 580 make_scoped_refptr(partition->GetURLRequestContext()), |
| 620 make_scoped_refptr( | 581 make_scoped_refptr( |
| 621 browser_context_->GetSpecialStoragePolicy()))); | 582 browser_context_->GetSpecialStoragePolicy()))); |
| 622 | 583 |
| 623 // We do not initialize the AppCacheService for media contexts because, | 584 // We do not call InitializeURLRequestContext() for media contexts because, |
| 624 // other than the HTTP cache, the media contexts share the same backing | 585 // other than the HTTP cache, the media contexts share the same backing |
| 625 // objects as their associated "normal" request context. Thus, the previous | 586 // objects as their associated "normal" request context. Thus, the previous |
| 626 // call serves to initialize the media request context for this storage | 587 // call serves to initialize the media request context for this storage |
| 627 // partition as well. | 588 // partition as well. |
| 628 } | 589 } |
| 629 } | 590 } |
| 630 | 591 |
| 631 scoped_ptr<CookieStoreMap> StoragePartitionImplMap::CreateCookieStores( | |
| 632 const base::FilePath& partition_path, | |
| 633 bool in_memory_partition, | |
| 634 bool is_default_partition) { | |
| 635 scoped_ptr<CookieStoreMap> cookie_store_map(new CookieStoreMap()); | |
| 636 std::map<std::string, CookieStoreConfig> cookie_store_configs; | |
| 637 cookie_store_configs[BrowserContext::kDefaultCookieScheme] = | |
| 638 CookieStoreConfig(); | |
| 639 | |
| 640 browser_context_->OverrideCookieStoreConfigs( | |
| 641 partition_path, in_memory_partition, is_default_partition, | |
| 642 &cookie_store_configs); | |
| 643 | |
| 644 for (std::map<std::string, CookieStoreConfig>::const_iterator it = | |
| 645 cookie_store_configs.begin(); | |
| 646 it != cookie_store_configs.end(); | |
| 647 ++it) { | |
| 648 scoped_refptr<net::CookieStore> cookie_store = | |
| 649 CreateCookieStore(it->second); | |
| 650 if (it->first == BrowserContext::kDefaultCookieScheme) { | |
| 651 cookie_store_map->SetForScheme(chrome::kHttpScheme, cookie_store); | |
| 652 cookie_store_map->SetForScheme(content::kHttpsScheme, cookie_store); | |
| 653 | |
| 654 // If file scheme is enabled, share the same cookie store as http. This is | |
| 655 // legacy behavior. A complete valid, alternate approach is to use a | |
| 656 // different cookie database. See comments inside CookieMonster about | |
| 657 // separating out file cookies into their own CookieMonster. | |
| 658 if ((cookie_store_configs.find(chrome::kFileScheme) == | |
| 659 cookie_store_configs.end()) | |
| 660 && cookie_store->GetCookieMonster()->IsCookieableScheme( | |
| 661 chrome::kFileScheme)) { | |
| 662 cookie_store_map->SetForScheme(chrome::kFileScheme, cookie_store); | |
| 663 } | |
| 664 } else { | |
| 665 const char* schemes[] = { it->first.c_str() }; | |
| 666 cookie_store->GetCookieMonster()->SetCookieableSchemes(schemes, 1); | |
| 667 cookie_store_map->SetForScheme(it->first, cookie_store); | |
| 668 } | |
| 669 } | |
| 670 | |
| 671 return cookie_store_map.Pass(); | |
| 672 } | |
| 673 | |
| 674 } // namespace content | 592 } // namespace content |
| OLD | NEW |