OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/appcache/appcache_storage_impl.h" | 5 #include "webkit/appcache/appcache_storage_impl.h" |
6 | 6 |
7 #include "app/sql/connection.h" | 7 #include "app/sql/connection.h" |
8 #include "app/sql/transaction.h" | 8 #include "app/sql/transaction.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 | 551 |
552 GURL url_; | 552 GURL url_; |
553 std::set<int64> cache_ids_in_use_; | 553 std::set<int64> cache_ids_in_use_; |
554 AppCacheEntry entry_; | 554 AppCacheEntry entry_; |
555 AppCacheEntry fallback_entry_; | 555 AppCacheEntry fallback_entry_; |
556 GURL fallback_url_; | 556 GURL fallback_url_; |
557 int64 cache_id_; | 557 int64 cache_id_; |
558 GURL manifest_url_; | 558 GURL manifest_url_; |
559 }; | 559 }; |
560 | 560 |
| 561 // Helpers for FindMainResponseTask::Run() |
561 namespace { | 562 namespace { |
562 bool SortByLength( | 563 bool SortByLength( |
563 const AppCacheDatabase::FallbackNameSpaceRecord& lhs, | 564 const AppCacheDatabase::FallbackNameSpaceRecord& lhs, |
564 const AppCacheDatabase::FallbackNameSpaceRecord& rhs) { | 565 const AppCacheDatabase::FallbackNameSpaceRecord& rhs) { |
565 return lhs.namespace_url.spec().length() > rhs.namespace_url.spec().length(); | 566 return lhs.namespace_url.spec().length() > rhs.namespace_url.spec().length(); |
566 } | 567 } |
567 } | 568 |
| 569 class NetworkNamespaceHelper { |
| 570 public: |
| 571 explicit NetworkNamespaceHelper(AppCacheDatabase* database) |
| 572 : database_(database) { |
| 573 } |
| 574 |
| 575 bool IsInNetworkNamespace(const GURL& url, int64 cache_id) { |
| 576 static const std::vector<GURL> kEmptyVector; |
| 577 typedef std::pair<WhiteListMap::iterator, bool> InsertResult; |
| 578 InsertResult result = namespaces_map_.insert( |
| 579 WhiteListMap::value_type(cache_id, kEmptyVector)); |
| 580 if (result.second) |
| 581 GetOnlineWhiteListForCache(cache_id, &result.first->second); |
| 582 return AppCache::IsInNetworkNamespace(url, result.first->second); |
| 583 } |
| 584 |
| 585 private: |
| 586 void GetOnlineWhiteListForCache( |
| 587 int64 cache_id, std::vector<GURL>* urls) { |
| 588 DCHECK(urls && urls->empty()); |
| 589 typedef std::vector<AppCacheDatabase::OnlineWhiteListRecord> |
| 590 WhiteListVector; |
| 591 WhiteListVector records; |
| 592 if (!database_->FindOnlineWhiteListForCache(cache_id, &records)) |
| 593 return; |
| 594 WhiteListVector::const_iterator iter = records.begin(); |
| 595 while (iter != records.end()) { |
| 596 urls->push_back(iter->namespace_url); |
| 597 ++iter; |
| 598 } |
| 599 } |
| 600 |
| 601 // Key is cache id |
| 602 typedef std::map<int64, std::vector<GURL> > WhiteListMap; |
| 603 WhiteListMap namespaces_map_; |
| 604 AppCacheDatabase* database_; |
| 605 }; |
| 606 } // namespace |
568 | 607 |
569 void AppCacheStorageImpl::FindMainResponseTask::Run() { | 608 void AppCacheStorageImpl::FindMainResponseTask::Run() { |
570 // We have a bias for hits from caches that are in use. | 609 // We have a bias for hits from caches that are in use. |
571 | 610 |
572 // TODO(michaeln): The heuristics around choosing amoungst | 611 // TODO(michaeln): The heuristics around choosing amoungst |
573 // multiple candidates is under specified, and just plain | 612 // multiple candidates is under specified, and just plain |
574 // not fully understood. Refine these over time. In particular, | 613 // not fully understood. Refine these over time. In particular, |
575 // * prefer candidates from newer caches | 614 // * prefer candidates from newer caches |
576 // * take into account the cache associated with the document | 615 // * take into account the cache associated with the document |
577 // that initiated the navigation | 616 // that initiated the navigation |
(...skipping 30 matching lines...) Expand all Loading... |
608 return; | 647 return; |
609 } | 648 } |
610 | 649 |
611 // Sort by namespace url string length, longest to shortest, | 650 // Sort by namespace url string length, longest to shortest, |
612 // since longer matches trump when matching a url to a namespace. | 651 // since longer matches trump when matching a url to a namespace. |
613 std::sort(fallbacks.begin(), fallbacks.end(), SortByLength); | 652 std::sort(fallbacks.begin(), fallbacks.end(), SortByLength); |
614 | 653 |
615 bool has_candidate = false; | 654 bool has_candidate = false; |
616 GURL candidate_fallback_namespace; | 655 GURL candidate_fallback_namespace; |
617 std::vector<AppCacheDatabase::FallbackNameSpaceRecord>::iterator iter; | 656 std::vector<AppCacheDatabase::FallbackNameSpaceRecord>::iterator iter; |
| 657 NetworkNamespaceHelper network_namespace_helper(database_); |
618 for (iter = fallbacks.begin(); iter < fallbacks.end(); ++iter) { | 658 for (iter = fallbacks.begin(); iter < fallbacks.end(); ++iter) { |
| 659 // Skip this fallback namespace if the requested url falls into a network |
| 660 // namespace of the containing appcache. |
| 661 if (network_namespace_helper.IsInNetworkNamespace(url_, iter->cache_id)) |
| 662 continue; |
| 663 |
619 if (has_candidate && | 664 if (has_candidate && |
620 (candidate_fallback_namespace.spec().length() > | 665 (candidate_fallback_namespace.spec().length() > |
621 iter->namespace_url.spec().length())) { | 666 iter->namespace_url.spec().length())) { |
622 break; // Stop iterating since longer namespace prefix matches win. | 667 break; // Stop iterating since longer namespace prefix matches win. |
623 } | 668 } |
624 | 669 |
625 if (StartsWithASCII(url_.spec(), iter->namespace_url.spec(), true)) { | 670 if (StartsWithASCII(url_.spec(), iter->namespace_url.spec(), true)) { |
626 bool is_cache_in_use = cache_ids_in_use_.find(iter->cache_id) != | 671 bool is_cache_in_use = cache_ids_in_use_.find(iter->cache_id) != |
627 cache_ids_in_use_.end(); | 672 cache_ids_in_use_.end(); |
628 | 673 |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1328 Disable(); | 1373 Disable(); |
1329 if (!is_incognito_) { | 1374 if (!is_incognito_) { |
1330 VLOG(1) << "Deleting existing appcache data and starting over."; | 1375 VLOG(1) << "Deleting existing appcache data and starting over."; |
1331 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE, | 1376 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE, |
1332 NewRunnableFunction(DeleteDirectory, cache_directory_)); | 1377 NewRunnableFunction(DeleteDirectory, cache_directory_)); |
1333 } | 1378 } |
1334 } | 1379 } |
1335 } | 1380 } |
1336 | 1381 |
1337 } // namespace appcache | 1382 } // namespace appcache |
OLD | NEW |