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" |
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 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 |
349 } // namespace | 360 } // namespace |
350 | 361 |
351 // static | 362 // static |
352 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( | 363 base::FilePath StoragePartitionImplMap::GetStoragePartitionPath( |
353 const std::string& partition_domain, | 364 const std::string& partition_domain, |
354 const std::string& partition_name) { | 365 const std::string& partition_name) { |
355 if (partition_domain.empty()) | 366 if (partition_domain.empty()) |
356 return base::FilePath(); | 367 return base::FilePath(); |
357 | 368 |
358 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); | 369 base::FilePath path = GetStoragePartitionDomainPath(partition_domain); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 StoragePartitionConfig partition_config( | 407 StoragePartitionConfig partition_config( |
397 partition_domain, partition_name, in_memory); | 408 partition_domain, partition_name, in_memory); |
398 | 409 |
399 PartitionMap::const_iterator it = partitions_.find(partition_config); | 410 PartitionMap::const_iterator it = partitions_.find(partition_config); |
400 if (it != partitions_.end()) | 411 if (it != partitions_.end()) |
401 return it->second; | 412 return it->second; |
402 | 413 |
403 base::FilePath partition_path = | 414 base::FilePath partition_path = |
404 browser_context_->GetPath().Append( | 415 browser_context_->GetPath().Append( |
405 GetStoragePartitionPath(partition_domain, partition_name)); | 416 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 |
406 StoragePartitionImpl* partition = | 424 StoragePartitionImpl* partition = |
407 StoragePartitionImpl::Create(browser_context_, in_memory, | 425 StoragePartitionImpl::Create(browser_context_, in_memory, |
408 partition_path); | 426 partition_path, cookie_store_map.Pass()); |
409 partitions_[partition_config] = partition; | 427 partitions_[partition_config] = partition; |
410 | 428 |
411 ChromeBlobStorageContext* blob_storage_context = | 429 ChromeBlobStorageContext* blob_storage_context = |
412 ChromeBlobStorageContext::GetFor(browser_context_); | 430 ChromeBlobStorageContext::GetFor(browser_context_); |
413 StreamContext* stream_context = StreamContext::GetFor(browser_context_); | 431 StreamContext* stream_context = StreamContext::GetFor(browser_context_); |
414 ProtocolHandlerMap protocol_handlers; | 432 ProtocolHandlerMap protocol_handlers; |
415 protocol_handlers[chrome::kBlobScheme] = | 433 protocol_handlers[chrome::kBlobScheme] = |
416 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 434 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
417 new BlobProtocolHandler(blob_storage_context, | 435 new BlobProtocolHandler(blob_storage_context, |
418 stream_context, | 436 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. | 579 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
562 // We should move this into a place where it is called once per | 580 // We should move this into a place where it is called once per |
563 // BrowserContext creation rather than piggybacking off the default context | 581 // BrowserContext creation rather than piggybacking off the default context |
564 // creation. | 582 // creation. |
565 // Note: moving this into Get() before partitions_[] is set causes reentrency. | 583 // Note: moving this into Get() before partitions_[] is set causes reentrency. |
566 if (!resource_context_initialized_) { | 584 if (!resource_context_initialized_) { |
567 resource_context_initialized_ = true; | 585 resource_context_initialized_ = true; |
568 InitializeResourceContext(browser_context_); | 586 InitializeResourceContext(browser_context_); |
569 } | 587 } |
570 | 588 |
571 // Check first to avoid memory leak in unittests. | 589 // In unittests, BrowserThread::IO may not be valid which would yield a |
572 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 590 // memory leak on a PostTask. Also, in content_unittests, the |
| 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 |
573 BrowserThread::PostTask( | 612 BrowserThread::PostTask( |
574 BrowserThread::IO, FROM_HERE, | 613 BrowserThread::IO, FROM_HERE, |
575 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 614 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
576 partition->GetAppCacheService(), | 615 partition->GetAppCacheService(), |
577 in_memory ? base::FilePath() : | 616 in_memory ? base::FilePath() : |
578 partition->GetPath().Append(kAppCacheDirname), | 617 partition->GetPath().Append(kAppCacheDirname), |
579 browser_context_->GetResourceContext(), | 618 browser_context_->GetResourceContext(), |
580 make_scoped_refptr(partition->GetURLRequestContext()), | 619 make_scoped_refptr(partition->GetURLRequestContext()), |
581 make_scoped_refptr( | 620 make_scoped_refptr( |
582 browser_context_->GetSpecialStoragePolicy()))); | 621 browser_context_->GetSpecialStoragePolicy()))); |
583 | 622 |
584 // We do not call InitializeURLRequestContext() for media contexts because, | 623 // We do not initialize the AppCacheService for media contexts because, |
585 // other than the HTTP cache, the media contexts share the same backing | 624 // other than the HTTP cache, the media contexts share the same backing |
586 // objects as their associated "normal" request context. Thus, the previous | 625 // objects as their associated "normal" request context. Thus, the previous |
587 // call serves to initialize the media request context for this storage | 626 // call serves to initialize the media request context for this storage |
588 // partition as well. | 627 // partition as well. |
589 } | 628 } |
590 } | 629 } |
591 | 630 |
| 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 |
592 } // namespace content | 674 } // namespace content |
OLD | NEW |