Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: storage/browser/quota/quota_manager.cc

Issue 1355793002: Convert QuotaManager::GetLRUOrigin into a QuotaEvictionPolicy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@respect_exceptions
Patch Set: address comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « storage/browser/quota/quota_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "storage/browser/quota/quota_manager.h" 5 #include "storage/browser/quota/quota_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 774 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 785
786 private: 786 private:
787 bool AppendEntry(const OriginInfoTableEntry& entry) { 787 bool AppendEntry(const OriginInfoTableEntry& entry) {
788 entries_.push_back(entry); 788 entries_.push_back(entry);
789 return true; 789 return true;
790 } 790 }
791 791
792 OriginInfoTableEntries entries_; 792 OriginInfoTableEntries entries_;
793 }; 793 };
794 794
795 class QuotaManager::LRUOriginEvictionPolicy : public QuotaEvictionPolicy {
796 public:
797 LRUOriginEvictionPolicy(QuotaManager* manager, StorageType type)
798 : manager_(manager), type_(type), weak_factory_(this) {}
799
800 ~LRUOriginEvictionPolicy() override {}
801
802 // Overridden from storage::QuotaEvictionPolicy:
803 void GetEvictionOrigin(const scoped_refptr<storage::SpecialStoragePolicy>&
804 special_storage_policy,
805 const std::set<GURL>& exceptions,
806 const storage::GetOriginCallback& callback) override {
807 if (manager_->db_disabled_) {
808 callback.Run(GURL());
809 return;
810 }
811
812 GURL* url = new GURL;
813 manager_->PostTaskAndReplyWithResultForDBThread(
814 FROM_HERE,
815 base::Bind(&GetLRUOriginOnDBThread, type_, exceptions,
816 manager_->special_storage_policy_, base::Unretained(url)),
817 base::Bind(&LRUOriginEvictionPolicy::DidGetLRUOrigin,
818 weak_factory_.GetWeakPtr(), callback, base::Owned(url)));
819 }
820
821 void DidGetLRUOrigin(const storage::GetOriginCallback& callback,
822 const GURL* origin,
823 bool success) {
824 manager_->DidDatabaseWork(success);
825 callback.Run(*origin);
826 }
827
828 private:
829 QuotaManager* manager_;
830 StorageType type_;
831 base::WeakPtrFactory<LRUOriginEvictionPolicy> weak_factory_;
832
833 DISALLOW_COPY_AND_ASSIGN(LRUOriginEvictionPolicy);
834 };
835
795 // QuotaManager --------------------------------------------------------------- 836 // QuotaManager ---------------------------------------------------------------
796 837
797 QuotaManager::QuotaManager( 838 QuotaManager::QuotaManager(
798 bool is_incognito, 839 bool is_incognito,
799 const base::FilePath& profile_path, 840 const base::FilePath& profile_path,
800 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, 841 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread,
801 const scoped_refptr<base::SequencedTaskRunner>& db_thread, 842 const scoped_refptr<base::SequencedTaskRunner>& db_thread,
802 const scoped_refptr<SpecialStoragePolicy>& special_storage_policy) 843 const scoped_refptr<SpecialStoragePolicy>& special_storage_policy)
803 : is_incognito_(is_incognito), 844 : is_incognito_(is_incognito),
804 profile_path_(profile_path), 845 profile_path_(profile_path),
805 proxy_(new QuotaManagerProxy(this, io_thread)), 846 proxy_(new QuotaManagerProxy(this, io_thread)),
806 db_disabled_(false), 847 db_disabled_(false),
807 eviction_disabled_(false), 848 eviction_disabled_(false),
808 io_thread_(io_thread), 849 io_thread_(io_thread),
809 db_thread_(db_thread), 850 db_thread_(db_thread),
810 temporary_quota_initialized_(false), 851 temporary_quota_initialized_(false),
811 temporary_quota_override_(-1), 852 temporary_quota_override_(-1),
812 desired_available_space_(-1), 853 desired_available_space_(-1),
813 special_storage_policy_(special_storage_policy), 854 special_storage_policy_(special_storage_policy),
814 get_disk_space_fn_(&CallSystemGetAmountOfFreeDiskSpace), 855 get_disk_space_fn_(&CallSystemGetAmountOfFreeDiskSpace),
815 storage_monitor_(new StorageMonitor(this)), 856 storage_monitor_(new StorageMonitor(this)),
816 is_getting_eviction_origin_(false), 857 is_getting_eviction_origin_(false),
817 weak_factory_(this) {} 858 weak_factory_(this) {
859 eviction_policy_map_.resize(kStorageTypeLast + 1);
860 }
818 861
819 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) { 862 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) {
820 LazyInitialize(); 863 LazyInitialize();
821 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback); 864 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback);
822 get_usage_info->Start(); 865 get_usage_info->Start();
823 } 866 }
824 867
825 void QuotaManager::GetUsageAndQuotaForWebApps( 868 void QuotaManager::GetUsageAndQuotaForWebApps(
826 const GURL& origin, 869 const GURL& origin,
827 StorageType type, 870 StorageType type,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 StorageType type, 964 StorageType type,
922 bool enabled) { 965 bool enabled) {
923 LazyInitialize(); 966 LazyInitialize();
924 DCHECK(GetUsageTracker(type)); 967 DCHECK(GetUsageTracker(type));
925 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled); 968 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled);
926 } 969 }
927 970
928 void QuotaManager::SetQuotaEvictionPolicy( 971 void QuotaManager::SetQuotaEvictionPolicy(
929 StorageType type, 972 StorageType type,
930 scoped_ptr<QuotaEvictionPolicy> policy) { 973 scoped_ptr<QuotaEvictionPolicy> policy) {
931 eviction_policy_map_.set(type, policy.Pass()); 974 eviction_policy_map_[type] = policy.release();
932 } 975 }
933 976
934 void QuotaManager::DeleteOriginData( 977 void QuotaManager::DeleteOriginData(
935 const GURL& origin, StorageType type, int quota_client_mask, 978 const GURL& origin, StorageType type, int quota_client_mask,
936 const StatusCallback& callback) { 979 const StatusCallback& callback) {
937 LazyInitialize(); 980 LazyInitialize();
938 981
939 if (origin.is_empty() || clients_.empty()) { 982 if (origin.is_empty() || clients_.empty()) {
940 callback.Run(kQuotaStatusOk); 983 callback.Run(kQuotaStatusOk);
941 return; 984 return;
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 DCHECK(GetUsageTracker(type)); 1330 DCHECK(GetUsageTracker(type));
1288 GetUsageTracker(type)->GetCachedOrigins(origins); 1331 GetUsageTracker(type)->GetCachedOrigins(origins);
1289 } 1332 }
1290 1333
1291 void QuotaManager::NotifyStorageAccessedInternal( 1334 void QuotaManager::NotifyStorageAccessedInternal(
1292 QuotaClient::ID client_id, 1335 QuotaClient::ID client_id,
1293 const GURL& origin, StorageType type, 1336 const GURL& origin, StorageType type,
1294 base::Time accessed_time) { 1337 base::Time accessed_time) {
1295 LazyInitialize(); 1338 LazyInitialize();
1296 if (type == kStorageTypeTemporary && is_getting_eviction_origin_) { 1339 if (type == kStorageTypeTemporary && is_getting_eviction_origin_) {
1297 // Record the accessed origins while GetLRUOrigin task is runing 1340 // Record the accessed origins while an eviction origin task is running
1298 // to filter out them from eviction. 1341 // to filter them from eviction.
1299 access_notified_origins_.insert(origin); 1342 access_notified_origins_.insert(origin);
1300 } 1343 }
1301 1344
1302 if (db_disabled_) 1345 if (db_disabled_)
1303 return; 1346 return;
1304 PostTaskAndReplyWithResultForDBThread( 1347 PostTaskAndReplyWithResultForDBThread(
1305 FROM_HERE, 1348 FROM_HERE,
1306 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time), 1349 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time),
1307 base::Bind(&QuotaManager::DidDatabaseWork, 1350 base::Bind(&QuotaManager::DidDatabaseWork,
1308 weak_factory_.GetWeakPtr())); 1351 weak_factory_.GetWeakPtr()));
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1476 is_getting_eviction_origin_ = false; 1519 is_getting_eviction_origin_ = false;
1477 } 1520 }
1478 1521
1479 void QuotaManager::GetEvictionOrigin(StorageType type, 1522 void QuotaManager::GetEvictionOrigin(StorageType type,
1480 const GetOriginCallback& callback) { 1523 const GetOriginCallback& callback) {
1481 LazyInitialize(); 1524 LazyInitialize();
1482 // This must not be called while there's an in-flight task. 1525 // This must not be called while there's an in-flight task.
1483 DCHECK(!is_getting_eviction_origin_); 1526 DCHECK(!is_getting_eviction_origin_);
1484 is_getting_eviction_origin_ = true; 1527 is_getting_eviction_origin_ = true;
1485 1528
1486 GetOriginCallback did_get_origin_callback = 1529 QuotaEvictionPolicy* policy = eviction_policy_map_[type];
1487 base::Bind(&QuotaManager::DidGetEvictionOrigin,
1488 weak_factory_.GetWeakPtr(), callback);
1489 1530
1490 auto eviction_policy = eviction_policy_map_.find(type); 1531 if (!policy) {
1491 if (eviction_policy != eviction_policy_map_.end()) { 1532 policy = new LRUOriginEvictionPolicy(this, type);
1492 eviction_policy->second->GetEvictionOrigin(special_storage_policy_, 1533 eviction_policy_map_[type] = policy;
1493 GetEvictionOriginExceptions(),
1494 did_get_origin_callback);
1495 return;
1496 } 1534 }
1497 1535
1498 // TODO(calamity): convert LRU origin retrieval into a QuotaEvictionPolicy. 1536 policy->GetEvictionOrigin(
1499 GetLRUOrigin(type, did_get_origin_callback); 1537 special_storage_policy_, GetEvictionOriginExceptions(),
1538 base::Bind(&QuotaManager::DidGetEvictionOrigin,
1539 weak_factory_.GetWeakPtr(), callback));
1500 } 1540 }
1501 1541
1502 void QuotaManager::EvictOriginData(const GURL& origin, 1542 void QuotaManager::EvictOriginData(const GURL& origin,
1503 StorageType type, 1543 StorageType type,
1504 const EvictOriginDataCallback& callback) { 1544 const EvictOriginDataCallback& callback) {
1505 DCHECK(io_thread_->BelongsToCurrentThread()); 1545 DCHECK(io_thread_->BelongsToCurrentThread());
1506 DCHECK_EQ(type, kStorageTypeTemporary); 1546 DCHECK_EQ(type, kStorageTypeTemporary);
1507 1547
1508 eviction_context_.evicted_origin = origin; 1548 eviction_context_.evicted_origin = origin;
1509 eviction_context_.evicted_type = type; 1549 eviction_context_.evicted_type = type;
(...skipping 11 matching lines...) Expand all
1521 1561
1522 UsageAndQuotaCallbackDispatcher* dispatcher = 1562 UsageAndQuotaCallbackDispatcher* dispatcher =
1523 new UsageAndQuotaCallbackDispatcher(this); 1563 new UsageAndQuotaCallbackDispatcher(this);
1524 GetUsageTracker(kStorageTypeTemporary) 1564 GetUsageTracker(kStorageTypeTemporary)
1525 ->GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback()); 1565 ->GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback());
1526 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback()); 1566 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback());
1527 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback()); 1567 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback());
1528 dispatcher->WaitForResults(callback); 1568 dispatcher->WaitForResults(callback);
1529 } 1569 }
1530 1570
1531 void QuotaManager::GetLRUOrigin(StorageType type,
1532 const GetOriginCallback& callback) {
1533 LazyInitialize();
1534 // This must not be called while there's an in-flight task.
1535 DCHECK(lru_origin_callback_.is_null());
1536 lru_origin_callback_ = callback;
1537 if (db_disabled_) {
1538 lru_origin_callback_.Run(GURL());
1539 lru_origin_callback_.Reset();
1540 return;
1541 }
1542
1543 GURL* url = new GURL;
1544 PostTaskAndReplyWithResultForDBThread(
1545 FROM_HERE,
1546 base::Bind(&GetLRUOriginOnDBThread, type, GetEvictionOriginExceptions(),
1547 special_storage_policy_, base::Unretained(url)),
1548 base::Bind(&QuotaManager::DidGetLRUOrigin, weak_factory_.GetWeakPtr(),
1549 base::Owned(url)));
1550 }
1551
1552 void QuotaManager::DidSetTemporaryGlobalOverrideQuota( 1571 void QuotaManager::DidSetTemporaryGlobalOverrideQuota(
1553 const QuotaCallback& callback, 1572 const QuotaCallback& callback,
1554 const int64* new_quota, 1573 const int64* new_quota,
1555 bool success) { 1574 bool success) {
1556 QuotaStatusCode status = kQuotaErrorInvalidAccess; 1575 QuotaStatusCode status = kQuotaErrorInvalidAccess;
1557 DidDatabaseWork(success); 1576 DidDatabaseWork(success);
1558 if (success) { 1577 if (success) {
1559 temporary_quota_override_ = *new_quota; 1578 temporary_quota_override_ = *new_quota;
1560 status = kQuotaStatusOk; 1579 status = kQuotaStatusOk;
1561 } 1580 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1593 base::TimeDelta::FromMilliseconds( 1612 base::TimeDelta::FromMilliseconds(
1594 kReportHistogramInterval), 1613 kReportHistogramInterval),
1595 this, &QuotaManager::ReportHistogram); 1614 this, &QuotaManager::ReportHistogram);
1596 1615
1597 db_initialization_callbacks_.Run(); 1616 db_initialization_callbacks_.Run();
1598 GetTemporaryGlobalQuota( 1617 GetTemporaryGlobalQuota(
1599 base::Bind(&QuotaManager::DidGetInitialTemporaryGlobalQuota, 1618 base::Bind(&QuotaManager::DidGetInitialTemporaryGlobalQuota,
1600 weak_factory_.GetWeakPtr())); 1619 weak_factory_.GetWeakPtr()));
1601 } 1620 }
1602 1621
1603 void QuotaManager::DidGetLRUOrigin(const GURL* origin,
1604 bool success) {
1605 DidDatabaseWork(success);
1606
1607 lru_origin_callback_.Run(*origin);
1608 lru_origin_callback_.Reset();
1609 }
1610
1611 void QuotaManager::DidGetInitialTemporaryGlobalQuota( 1622 void QuotaManager::DidGetInitialTemporaryGlobalQuota(
1612 QuotaStatusCode status, int64 quota_unused) { 1623 QuotaStatusCode status, int64 quota_unused) {
1613 if (eviction_disabled_) 1624 if (eviction_disabled_)
1614 return; 1625 return;
1615 1626
1616 std::set<GURL>* origins = new std::set<GURL>; 1627 std::set<GURL>* origins = new std::set<GURL>;
1617 temporary_usage_tracker_->GetCachedOrigins(origins); 1628 temporary_usage_tracker_->GetCachedOrigins(origins);
1618 // This will call the StartEviction() when initial origin registration 1629 // This will call the StartEviction() when initial origin registration
1619 // is completed. 1630 // is completed.
1620 PostTaskAndReplyWithResultForDBThread( 1631 PostTaskAndReplyWithResultForDBThread(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1655 // |database_|, therefore we can be sure that database_ is alive when this 1666 // |database_|, therefore we can be sure that database_ is alive when this
1656 // task runs. 1667 // task runs.
1657 base::PostTaskAndReplyWithResult( 1668 base::PostTaskAndReplyWithResult(
1658 db_thread_.get(), 1669 db_thread_.get(),
1659 from_here, 1670 from_here,
1660 base::Bind(task, base::Unretained(database_.get())), 1671 base::Bind(task, base::Unretained(database_.get())),
1661 reply); 1672 reply);
1662 } 1673 }
1663 1674
1664 } // namespace storage 1675 } // namespace storage
OLDNEW
« no previous file with comments | « storage/browser/quota/quota_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698