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

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: 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
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),
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 DCHECK(GetUsageTracker(type)); 1328 DCHECK(GetUsageTracker(type));
1288 GetUsageTracker(type)->GetCachedOrigins(origins); 1329 GetUsageTracker(type)->GetCachedOrigins(origins);
1289 } 1330 }
1290 1331
1291 void QuotaManager::NotifyStorageAccessedInternal( 1332 void QuotaManager::NotifyStorageAccessedInternal(
1292 QuotaClient::ID client_id, 1333 QuotaClient::ID client_id,
1293 const GURL& origin, StorageType type, 1334 const GURL& origin, StorageType type,
1294 base::Time accessed_time) { 1335 base::Time accessed_time) {
1295 LazyInitialize(); 1336 LazyInitialize();
1296 if (type == kStorageTypeTemporary && is_getting_eviction_origin_) { 1337 if (type == kStorageTypeTemporary && is_getting_eviction_origin_) {
1297 // Record the accessed origins while GetLRUOrigin task is runing 1338 // Record the accessed origins while an eviction origin task is running
1298 // to filter out them from eviction. 1339 // to filter them from eviction.
1299 access_notified_origins_.insert(origin); 1340 access_notified_origins_.insert(origin);
1300 } 1341 }
1301 1342
1302 if (db_disabled_) 1343 if (db_disabled_)
1303 return; 1344 return;
1304 PostTaskAndReplyWithResultForDBThread( 1345 PostTaskAndReplyWithResultForDBThread(
1305 FROM_HERE, 1346 FROM_HERE,
1306 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time), 1347 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time),
1307 base::Bind(&QuotaManager::DidDatabaseWork, 1348 base::Bind(&QuotaManager::DidDatabaseWork,
1308 weak_factory_.GetWeakPtr())); 1349 weak_factory_.GetWeakPtr()));
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1439 &unlimited_origins); 1480 &unlimited_origins);
1440 1481
1441 UMA_HISTOGRAM_COUNTS("Quota.NumberOfPersistentStorageOrigins", 1482 UMA_HISTOGRAM_COUNTS("Quota.NumberOfPersistentStorageOrigins",
1442 num_origins); 1483 num_origins);
1443 UMA_HISTOGRAM_COUNTS("Quota.NumberOfProtectedPersistentStorageOrigins", 1484 UMA_HISTOGRAM_COUNTS("Quota.NumberOfProtectedPersistentStorageOrigins",
1444 protected_origins); 1485 protected_origins);
1445 UMA_HISTOGRAM_COUNTS("Quota.NumberOfUnlimitedPersistentStorageOrigins", 1486 UMA_HISTOGRAM_COUNTS("Quota.NumberOfUnlimitedPersistentStorageOrigins",
1446 unlimited_origins); 1487 unlimited_origins);
1447 } 1488 }
1448 1489
1449 void QuotaManager::GetEvictionOrigin(StorageType type,
1450 const GetOriginCallback& callback) {
1451 LazyInitialize();
1452 is_getting_eviction_origin_ = true;
1453
1454 GetOriginCallback did_get_origin_callback =
1455 base::Bind(&QuotaManager::DidGetEvictionOrigin,
1456 weak_factory_.GetWeakPtr(), callback);
1457
1458 auto eviction_policy = eviction_policy_map_.find(type);
1459 if (eviction_policy != eviction_policy_map_.end()) {
1460 eviction_policy->second->GetEvictionOrigin(special_storage_policy_,
1461 GetEvictionOriginExceptions(),
1462 did_get_origin_callback);
1463 }
1464
1465 // TODO(calamity): convert LRU origin retrieval into a QuotaEvictionPolicy.
1466 GetLRUOrigin(type, did_get_origin_callback);
1467 }
1468
1469 std::set<GURL> QuotaManager::GetEvictionOriginExceptions() { 1490 std::set<GURL> QuotaManager::GetEvictionOriginExceptions() {
1470 std::set<GURL> exceptions; 1491 std::set<GURL> exceptions;
1471 for (const auto& p : origins_in_use_) { 1492 for (const auto& p : origins_in_use_) {
1472 if (p.second > 0) 1493 if (p.second > 0)
1473 exceptions.insert(p.first); 1494 exceptions.insert(p.first);
1474 } 1495 }
1475 1496
1476 for (const auto& p : origins_in_error_) { 1497 for (const auto& p : origins_in_error_) {
1477 if (p.second > QuotaManager::kThresholdOfErrorsToBeBlacklisted) 1498 if (p.second > QuotaManager::kThresholdOfErrorsToBeBlacklisted)
1478 exceptions.insert(p.first); 1499 exceptions.insert(p.first);
1479 } 1500 }
1480 1501
1481 return exceptions; 1502 return exceptions;
1482 } 1503 }
1483 1504
1505 void QuotaManager::GetEvictionOrigin(StorageType type,
1506 const GetOriginCallback& callback) {
1507 LazyInitialize();
1508 is_getting_eviction_origin_ = true;
1509
1510 if (eviction_policy_map_.find(type) == eviction_policy_map_.end()) {
1511 eviction_policy_map_.set(
1512 type, make_scoped_ptr(new LRUOriginEvictionPolicy(this, type)));
raymes 2015/09/22 06:58:58 How about setting the LRU policy to the default po
calamity 2015/09/24 06:50:31 There are several storage types and I believe only
1513 }
1514
1515 eviction_policy_map_.find(type)->second->GetEvictionOrigin(
raymes 2015/09/22 06:58:58 We can avoid the additional map lookup here
calamity 2015/09/24 06:50:31 Done.
1516 special_storage_policy_, GetEvictionOriginExceptions(),
1517 base::Bind(&QuotaManager::DidGetEvictionOrigin,
1518 weak_factory_.GetWeakPtr(), callback));
1519 }
1520
1484 void QuotaManager::DidGetEvictionOrigin(const GetOriginCallback& callback, 1521 void QuotaManager::DidGetEvictionOrigin(const GetOriginCallback& callback,
1485 const GURL& origin) { 1522 const GURL& origin) {
1486 // Make sure the returned origin is (still) not in the origin_in_use_ set 1523 // Make sure the returned origin is (still) not in the origin_in_use_ set
1487 // and has not been accessed since we posted the task. 1524 // and has not been accessed since we posted the task.
1488 if (origins_in_use_.find(origin) != origins_in_use_.end() || 1525 if (origins_in_use_.find(origin) != origins_in_use_.end() ||
1489 access_notified_origins_.find(origin) != access_notified_origins_.end()) { 1526 access_notified_origins_.find(origin) != access_notified_origins_.end()) {
1490 callback.Run(GURL()); 1527 callback.Run(GURL());
1491 } else { 1528 } else {
1492 callback.Run(origin); 1529 callback.Run(origin);
1493 } 1530 }
(...skipping 24 matching lines...) Expand all
1518 1555
1519 UsageAndQuotaCallbackDispatcher* dispatcher = 1556 UsageAndQuotaCallbackDispatcher* dispatcher =
1520 new UsageAndQuotaCallbackDispatcher(this); 1557 new UsageAndQuotaCallbackDispatcher(this);
1521 GetUsageTracker(kStorageTypeTemporary) 1558 GetUsageTracker(kStorageTypeTemporary)
1522 ->GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback()); 1559 ->GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback());
1523 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback()); 1560 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback());
1524 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback()); 1561 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback());
1525 dispatcher->WaitForResults(callback); 1562 dispatcher->WaitForResults(callback);
1526 } 1563 }
1527 1564
1528 void QuotaManager::GetLRUOrigin(StorageType type,
raymes 2015/09/22 06:58:58 nit: This function still exists in the header
calamity 2015/09/24 06:50:31 Done.
1529 const GetOriginCallback& callback) {
1530 LazyInitialize();
1531 // This must not be called while there's an in-flight task.
1532 DCHECK(lru_origin_callback_.is_null());
1533 lru_origin_callback_ = callback;
1534 if (db_disabled_) {
1535 lru_origin_callback_.Run(GURL());
1536 lru_origin_callback_.Reset();
1537 return;
1538 }
1539
1540 GURL* url = new GURL;
1541 PostTaskAndReplyWithResultForDBThread(
1542 FROM_HERE,
1543 base::Bind(&GetLRUOriginOnDBThread, type, GetEvictionOriginExceptions(),
1544 special_storage_policy_, base::Unretained(url)),
1545 base::Bind(&QuotaManager::DidGetLRUOrigin, weak_factory_.GetWeakPtr(),
1546 base::Owned(url)));
1547 }
1548
1549 void QuotaManager::DidSetTemporaryGlobalOverrideQuota( 1565 void QuotaManager::DidSetTemporaryGlobalOverrideQuota(
1550 const QuotaCallback& callback, 1566 const QuotaCallback& callback,
1551 const int64* new_quota, 1567 const int64* new_quota,
1552 bool success) { 1568 bool success) {
1553 QuotaStatusCode status = kQuotaErrorInvalidAccess; 1569 QuotaStatusCode status = kQuotaErrorInvalidAccess;
1554 DidDatabaseWork(success); 1570 DidDatabaseWork(success);
1555 if (success) { 1571 if (success) {
1556 temporary_quota_override_ = *new_quota; 1572 temporary_quota_override_ = *new_quota;
1557 status = kQuotaStatusOk; 1573 status = kQuotaStatusOk;
1558 } 1574 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1590 base::TimeDelta::FromMilliseconds( 1606 base::TimeDelta::FromMilliseconds(
1591 kReportHistogramInterval), 1607 kReportHistogramInterval),
1592 this, &QuotaManager::ReportHistogram); 1608 this, &QuotaManager::ReportHistogram);
1593 1609
1594 db_initialization_callbacks_.Run(); 1610 db_initialization_callbacks_.Run();
1595 GetTemporaryGlobalQuota( 1611 GetTemporaryGlobalQuota(
1596 base::Bind(&QuotaManager::DidGetInitialTemporaryGlobalQuota, 1612 base::Bind(&QuotaManager::DidGetInitialTemporaryGlobalQuota,
1597 weak_factory_.GetWeakPtr())); 1613 weak_factory_.GetWeakPtr()));
1598 } 1614 }
1599 1615
1600 void QuotaManager::DidGetLRUOrigin(const GURL* origin,
1601 bool success) {
1602 DidDatabaseWork(success);
1603
1604 lru_origin_callback_.Run(*origin);
1605 lru_origin_callback_.Reset();
1606 }
1607
1608 void QuotaManager::DidGetInitialTemporaryGlobalQuota( 1616 void QuotaManager::DidGetInitialTemporaryGlobalQuota(
1609 QuotaStatusCode status, int64 quota_unused) { 1617 QuotaStatusCode status, int64 quota_unused) {
1610 if (eviction_disabled_) 1618 if (eviction_disabled_)
1611 return; 1619 return;
1612 1620
1613 std::set<GURL>* origins = new std::set<GURL>; 1621 std::set<GURL>* origins = new std::set<GURL>;
1614 temporary_usage_tracker_->GetCachedOrigins(origins); 1622 temporary_usage_tracker_->GetCachedOrigins(origins);
1615 // This will call the StartEviction() when initial origin registration 1623 // This will call the StartEviction() when initial origin registration
1616 // is completed. 1624 // is completed.
1617 PostTaskAndReplyWithResultForDBThread( 1625 PostTaskAndReplyWithResultForDBThread(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1652 // |database_|, therefore we can be sure that database_ is alive when this 1660 // |database_|, therefore we can be sure that database_ is alive when this
1653 // task runs. 1661 // task runs.
1654 base::PostTaskAndReplyWithResult( 1662 base::PostTaskAndReplyWithResult(
1655 db_thread_.get(), 1663 db_thread_.get(),
1656 from_here, 1664 from_here,
1657 base::Bind(task, base::Unretained(database_.get())), 1665 base::Bind(task, base::Unretained(database_.get())),
1658 reply); 1666 reply);
1659 } 1667 }
1660 1668
1661 } // namespace storage 1669 } // namespace storage
OLDNEW
« storage/browser/quota/quota_manager.h ('K') | « storage/browser/quota/quota_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698