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.restore_old_session_cookies)); |
| 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<CookieStoreMap> cookie_store_map(new CookieStoreMapImpl()); |
| 451 CookieStoreConfig default_config = browser_context_->GetCookieStoreConfig(); |
| 452 bool in_memory_cookies = in_memory || default_config.in_memory; |
| 453 content::GetContentClient()->browser()->OverrideCookieStoreMap( |
| 454 browser_context_, |
| 455 in_memory_cookies, partition_path, |
| 456 partition_domain.empty(), |
| 457 cookie_store_map.get()); |
| 458 |
| 459 // Add in cookie jars for Http, Https and file (if enabled). |
| 460 scoped_refptr<net::CookieStore> http_cookie_store = |
| 461 cookie_store_map->GetForScheme(chrome::kHttpScheme); |
| 462 if (!http_cookie_store.get()) { |
| 463 DCHECK(!cookie_store_map->GetForScheme(chrome::kHttpsScheme)); |
| 464 |
| 465 if (in_memory_cookies) { |
| 466 http_cookie_store = CreateInMemoryCookieStore(default_config, |
| 467 partition_domain.empty()); |
| 468 } else { |
| 469 http_cookie_store = CreatePersistentCookieStore(default_config, |
| 470 partition_path, |
| 471 partition_domain.empty()); |
| 472 } |
| 473 |
| 474 cookie_store_map->SetForScheme(chrome::kHttpScheme, http_cookie_store); |
| 475 cookie_store_map->SetForScheme(chrome::kHttpsScheme, http_cookie_store); |
| 476 if (http_cookie_store->GetCookieMonster()->IsCookieableScheme( |
| 477 chrome::kFileScheme)) { |
| 478 cookie_store_map->SetForScheme(chrome::kFileScheme, http_cookie_store); |
| 479 } |
| 480 } |
| 481 // There are places in code, such as data deletion, that assume both |
| 482 // http and https share the same cookie jar. |
| 483 DCHECK(cookie_store_map->GetForScheme(chrome::kHttpsScheme) == |
| 484 http_cookie_store); |
| 485 |
406 StoragePartitionImpl* partition = | 486 StoragePartitionImpl* partition = |
407 StoragePartitionImpl::Create(browser_context_, in_memory, | 487 StoragePartitionImpl::Create(browser_context_, in_memory, |
408 partition_path); | 488 partition_path, cookie_store_map.Pass()); |
409 partitions_[partition_config] = partition; | 489 partitions_[partition_config] = partition; |
410 | 490 |
411 ChromeBlobStorageContext* blob_storage_context = | 491 ChromeBlobStorageContext* blob_storage_context = |
412 ChromeBlobStorageContext::GetFor(browser_context_); | 492 ChromeBlobStorageContext::GetFor(browser_context_); |
413 StreamContext* stream_context = StreamContext::GetFor(browser_context_); | 493 StreamContext* stream_context = StreamContext::GetFor(browser_context_); |
414 ProtocolHandlerMap protocol_handlers; | 494 ProtocolHandlerMap protocol_handlers; |
415 protocol_handlers[chrome::kBlobScheme] = | 495 protocol_handlers[chrome::kBlobScheme] = |
416 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 496 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
417 new BlobProtocolHandler(blob_storage_context, | 497 new BlobProtocolHandler(blob_storage_context, |
418 stream_context, | 498 stream_context, |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 // TODO(ajwong): ResourceContexts no longer have any storage related state. | 637 // TODO(ajwong): ResourceContexts no longer have any storage related state. |
558 // We should move this into a place where it is called once per | 638 // We should move this into a place where it is called once per |
559 // BrowserContext creation rather than piggybacking off the default context | 639 // BrowserContext creation rather than piggybacking off the default context |
560 // creation. | 640 // creation. |
561 // Note: moving this into Get() before partitions_[] is set causes reentrency. | 641 // Note: moving this into Get() before partitions_[] is set causes reentrency. |
562 if (!resource_context_initialized_) { | 642 if (!resource_context_initialized_) { |
563 resource_context_initialized_ = true; | 643 resource_context_initialized_ = true; |
564 InitializeResourceContext(browser_context_); | 644 InitializeResourceContext(browser_context_); |
565 } | 645 } |
566 | 646 |
567 // Check first to avoid memory leak in unittests. | 647 // In unittests, BrowserThread::IO may not be valid which would yield a |
568 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 648 // memory leak on a PostTask. Also, in content_unittests, the |
| 649 // URLRequestContext is NULL. |
| 650 // |
| 651 // TODO(ajwong): Should default ContentBrowserClient return a non-null |
| 652 // URLRequestContext? |
| 653 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
| 654 partition->GetURLRequestContext()) { |
| 655 // The main URLRequestContextGetter was first created just before |
| 656 // PostCreateInitialization() is called thus attaching the CookieStore here |
| 657 // is guaranteed to occur before anyone else can see the main request |
| 658 // context including the media request context. |
| 659 BrowserThread::PostTask( |
| 660 BrowserThread::IO, FROM_HERE, |
| 661 base::Bind(&AttachCookieStoreOnIOThread, |
| 662 make_scoped_refptr(partition->GetURLRequestContext()), |
| 663 make_scoped_refptr( |
| 664 partition->GetCookieStoreMap().GetForScheme( |
| 665 chrome::kHttpScheme)))); |
| 666 |
569 BrowserThread::PostTask( | 667 BrowserThread::PostTask( |
570 BrowserThread::IO, FROM_HERE, | 668 BrowserThread::IO, FROM_HERE, |
571 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 669 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
572 partition->GetAppCacheService(), | 670 partition->GetAppCacheService(), |
573 in_memory ? base::FilePath() : | 671 in_memory ? base::FilePath() : |
574 partition->GetPath().Append(kAppCacheDirname), | 672 partition->GetPath().Append(kAppCacheDirname), |
575 browser_context_->GetResourceContext(), | 673 browser_context_->GetResourceContext(), |
576 make_scoped_refptr(partition->GetURLRequestContext()), | 674 make_scoped_refptr(partition->GetURLRequestContext()), |
577 make_scoped_refptr( | 675 make_scoped_refptr( |
578 browser_context_->GetSpecialStoragePolicy()))); | 676 browser_context_->GetSpecialStoragePolicy()))); |
579 | 677 |
580 // We do not call InitializeURLRequestContext() for media contexts because, | 678 // We do not initialize the AppCacheService for media contexts because, |
581 // other than the HTTP cache, the media contexts share the same backing | 679 // other than the HTTP cache, the media contexts share the same backing |
582 // objects as their associated "normal" request context. Thus, the previous | 680 // objects as their associated "normal" request context. Thus, the previous |
583 // call serves to initialize the media request context for this storage | 681 // call serves to initialize the media request context for this storage |
584 // partition as well. | 682 // partition as well. |
585 } | 683 } |
586 } | 684 } |
587 | 685 |
588 } // namespace content | 686 } // namespace content |
OLD | NEW |