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

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

Issue 1343273003: Integrate SiteEngagementEvictionPolicy with QuotaManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add_eviction_policy
Patch Set: export Created 5 years, 2 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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 int64 usage, 310 int64 usage,
311 int64 global_limited_usage, 311 int64 global_limited_usage,
312 int64 quota, 312 int64 quota,
313 int64 available_disk_space) 313 int64 available_disk_space)
314 : usage(usage), 314 : usage(usage),
315 global_limited_usage(global_limited_usage), 315 global_limited_usage(global_limited_usage),
316 quota(quota), 316 quota(quota),
317 available_disk_space(available_disk_space) { 317 available_disk_space(available_disk_space) {
318 } 318 }
319 319
320 QuotaEvictionPolicy::Info::Info() {}
321 QuotaEvictionPolicy::Info::~Info() {}
322
323 bool QuotaEvictionPolicy::NeedsOriginUsageMap() {
324 return false;
325 }
326
327 bool QuotaEvictionPolicy::NeedsGlobalQuota() {
328 return false;
329 }
330
320 class UsageAndQuotaCallbackDispatcher 331 class UsageAndQuotaCallbackDispatcher
321 : public QuotaTask, 332 : public QuotaTask,
322 public base::SupportsWeakPtr<UsageAndQuotaCallbackDispatcher> { 333 public base::SupportsWeakPtr<UsageAndQuotaCallbackDispatcher> {
323 public: 334 public:
324 explicit UsageAndQuotaCallbackDispatcher(QuotaManager* manager) 335 explicit UsageAndQuotaCallbackDispatcher(QuotaManager* manager)
325 : QuotaTask(manager), 336 : QuotaTask(manager),
326 has_usage_(false), 337 has_usage_(false),
327 has_global_limited_usage_(false), 338 has_global_limited_usage_(false),
328 has_quota_(false), 339 has_quota_(false),
329 has_available_disk_space_(false), 340 has_available_disk_space_(false),
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 } 532 }
522 533
523 GetUsageInfoCallback callback_; 534 GetUsageInfoCallback callback_;
524 UsageInfoEntries entries_; 535 UsageInfoEntries entries_;
525 int remaining_trackers_; 536 int remaining_trackers_;
526 base::WeakPtrFactory<GetUsageInfoTask> weak_factory_; 537 base::WeakPtrFactory<GetUsageInfoTask> weak_factory_;
527 538
528 DISALLOW_COPY_AND_ASSIGN(GetUsageInfoTask); 539 DISALLOW_COPY_AND_ASSIGN(GetUsageInfoTask);
529 }; 540 };
530 541
542 class QuotaManager::GetEvictionOriginTask : public QuotaTask {
michaeln 2015/10/06 00:35:09 This task may not bee needed? At the start of an
calamity 2015/10/07 02:42:46 Ah ok, cool. Deleted this.
543 public:
544 GetEvictionOriginTask(
545 QuotaManager* manager,
546 scoped_refptr<SpecialStoragePolicy> special_storage_policy,
547 QuotaEvictionPolicy* eviction_policy,
548 const GetOriginCallback& callback)
549 : QuotaTask(manager),
550 eviction_info_(new QuotaEvictionPolicy::Info()),
551 special_storage_policy_(special_storage_policy),
552 eviction_policy_(eviction_policy),
553 callback_(callback),
554 remaining_tasks_(0),
555 weak_factory_(this) {}
556
557 protected:
558 void Run() override {
559 if (eviction_policy_->NeedsOriginUsageMap()) {
560 // This will populate cached hosts and usage info.
561 manager()
562 ->GetUsageTracker(kStorageTypeTemporary)
563 ->GetGlobalUsage(base::Bind(&GetEvictionOriginTask::DidGetGlobalUsage,
michaeln 2015/10/06 00:35:09 The base::BarrierClosure (barrier_closure.h) class
calamity 2015/10/07 02:42:46 Acknowledged.
564 weak_factory_.GetWeakPtr(),
565 kStorageTypeTemporary));
566 ++remaining_tasks_;
567 }
568
569 if (eviction_policy_->NeedsGlobalQuota()) {
570 manager()->GetTemporaryGlobalQuota(
571 base::Bind(&GetEvictionOriginTask::DidGetTemporaryGlobalQuota,
572 weak_factory_.GetWeakPtr()));
573 ++remaining_tasks_;
574 }
575
576 if (remaining_tasks_ == 0)
577 CallCompleted();
578 }
579
580 void Completed() override {
581 eviction_policy_->GetEvictionOrigin(
582 eviction_info_.Pass(), special_storage_policy_.get(), callback_);
583 DeleteSoon();
584 }
585
586 void Aborted() override {
587 callback_.Run(GURL());
588 DeleteSoon();
589 }
590
591 private:
592 void DidGetGlobalUsage(StorageType type, int64, int64) {
593 DCHECK(manager()->GetUsageTracker(type));
594 manager()->GetUsageTracker(type)->GetCachedOriginsUsage(
595 &eviction_info_->origin_usage_map);
596
597 if (--remaining_tasks_ == 0)
598 CallCompleted();
599 }
600
601 void DidGetTemporaryGlobalQuota(QuotaStatusCode status, int64 quota) {
602 if (status == kQuotaStatusOk)
603 eviction_info_->global_quota = quota;
604
605 if (--remaining_tasks_ == 0)
606 CallCompleted();
607 }
608
609 QuotaManager* manager() const {
610 return static_cast<QuotaManager*>(observer());
611 }
612
613 scoped_ptr<QuotaEvictionPolicy::Info> eviction_info_;
614 scoped_refptr<SpecialStoragePolicy> special_storage_policy_;
615 QuotaEvictionPolicy* eviction_policy_;
616 GetOriginCallback callback_;
617 int remaining_tasks_;
618 base::WeakPtrFactory<GetEvictionOriginTask> weak_factory_;
619
620 DISALLOW_COPY_AND_ASSIGN(GetEvictionOriginTask);
621 };
622
531 class QuotaManager::OriginDataDeleter : public QuotaTask { 623 class QuotaManager::OriginDataDeleter : public QuotaTask {
532 public: 624 public:
533 OriginDataDeleter(QuotaManager* manager, 625 OriginDataDeleter(QuotaManager* manager,
534 const GURL& origin, 626 const GURL& origin,
535 StorageType type, 627 StorageType type,
536 int quota_client_mask, 628 int quota_client_mask,
537 const StatusCallback& callback) 629 const StatusCallback& callback)
538 : QuotaTask(manager), 630 : QuotaTask(manager),
539 origin_(origin), 631 origin_(origin),
540 type_(type), 632 type_(type),
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 db_disabled_(false), 898 db_disabled_(false),
807 eviction_disabled_(false), 899 eviction_disabled_(false),
808 io_thread_(io_thread), 900 io_thread_(io_thread),
809 db_thread_(db_thread), 901 db_thread_(db_thread),
810 temporary_quota_initialized_(false), 902 temporary_quota_initialized_(false),
811 temporary_quota_override_(-1), 903 temporary_quota_override_(-1),
812 desired_available_space_(-1), 904 desired_available_space_(-1),
813 special_storage_policy_(special_storage_policy), 905 special_storage_policy_(special_storage_policy),
814 get_disk_space_fn_(&CallSystemGetAmountOfFreeDiskSpace), 906 get_disk_space_fn_(&CallSystemGetAmountOfFreeDiskSpace),
815 storage_monitor_(new StorageMonitor(this)), 907 storage_monitor_(new StorageMonitor(this)),
816 weak_factory_(this) { 908 is_getting_eviction_origin_(false),
817 } 909 weak_factory_(this) {}
818 910
819 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) { 911 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) {
820 LazyInitialize(); 912 LazyInitialize();
821 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback); 913 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback);
822 get_usage_info->Start(); 914 get_usage_info->Start();
823 } 915 }
824 916
825 void QuotaManager::GetUsageAndQuotaForWebApps( 917 void QuotaManager::GetUsageAndQuotaForWebApps(
826 const GURL& origin, 918 const GURL& origin,
827 StorageType type, 919 StorageType type,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 1010
919 void QuotaManager::SetUsageCacheEnabled(QuotaClient::ID client_id, 1011 void QuotaManager::SetUsageCacheEnabled(QuotaClient::ID client_id,
920 const GURL& origin, 1012 const GURL& origin,
921 StorageType type, 1013 StorageType type,
922 bool enabled) { 1014 bool enabled) {
923 LazyInitialize(); 1015 LazyInitialize();
924 DCHECK(GetUsageTracker(type)); 1016 DCHECK(GetUsageTracker(type));
925 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled); 1017 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled);
926 } 1018 }
927 1019
1020 void QuotaManager::SetQuotaEvictionPolicy(
1021 StorageType type,
1022 scoped_ptr<QuotaEvictionPolicy> policy) {
1023 eviction_policy_map_.set(type, policy.Pass());
1024 }
1025
928 void QuotaManager::DeleteOriginData( 1026 void QuotaManager::DeleteOriginData(
929 const GURL& origin, StorageType type, int quota_client_mask, 1027 const GURL& origin, StorageType type, int quota_client_mask,
930 const StatusCallback& callback) { 1028 const StatusCallback& callback) {
931 LazyInitialize(); 1029 LazyInitialize();
932 1030
933 if (origin.is_empty() || clients_.empty()) { 1031 if (origin.is_empty() || clients_.empty()) {
934 callback.Run(kQuotaStatusOk); 1032 callback.Run(kQuotaStatusOk);
935 return; 1033 return;
936 } 1034 }
937 1035
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
1280 LazyInitialize(); 1378 LazyInitialize();
1281 DCHECK(GetUsageTracker(type)); 1379 DCHECK(GetUsageTracker(type));
1282 GetUsageTracker(type)->GetCachedOrigins(origins); 1380 GetUsageTracker(type)->GetCachedOrigins(origins);
1283 } 1381 }
1284 1382
1285 void QuotaManager::NotifyStorageAccessedInternal( 1383 void QuotaManager::NotifyStorageAccessedInternal(
1286 QuotaClient::ID client_id, 1384 QuotaClient::ID client_id,
1287 const GURL& origin, StorageType type, 1385 const GURL& origin, StorageType type,
1288 base::Time accessed_time) { 1386 base::Time accessed_time) {
1289 LazyInitialize(); 1387 LazyInitialize();
1290 if (type == kStorageTypeTemporary && !lru_origin_callback_.is_null()) { 1388 if (type == kStorageTypeTemporary && is_getting_eviction_origin_) {
1291 // Record the accessed origins while GetLRUOrigin task is runing 1389 // Record the accessed origins while GetLRUOrigin task is runing
1292 // to filter out them from eviction. 1390 // to filter out them from eviction.
1293 access_notified_origins_.insert(origin); 1391 access_notified_origins_.insert(origin);
1294 } 1392 }
1295 1393
1296 if (db_disabled_) 1394 if (db_disabled_)
1297 return; 1395 return;
1298 PostTaskAndReplyWithResultForDBThread( 1396 PostTaskAndReplyWithResultForDBThread(
1299 FROM_HERE, 1397 FROM_HERE,
1300 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time), 1398 base::Bind(&UpdateAccessTimeOnDBThread, origin, type, accessed_time),
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 UMA_HISTOGRAM_COUNTS("Quota.NumberOfPersistentStorageOrigins", 1533 UMA_HISTOGRAM_COUNTS("Quota.NumberOfPersistentStorageOrigins",
1436 num_origins); 1534 num_origins);
1437 UMA_HISTOGRAM_COUNTS("Quota.NumberOfProtectedPersistentStorageOrigins", 1535 UMA_HISTOGRAM_COUNTS("Quota.NumberOfProtectedPersistentStorageOrigins",
1438 protected_origins); 1536 protected_origins);
1439 UMA_HISTOGRAM_COUNTS("Quota.NumberOfUnlimitedPersistentStorageOrigins", 1537 UMA_HISTOGRAM_COUNTS("Quota.NumberOfUnlimitedPersistentStorageOrigins",
1440 unlimited_origins); 1538 unlimited_origins);
1441 } 1539 }
1442 1540
1443 void QuotaManager::GetEvictionOrigin(StorageType type, 1541 void QuotaManager::GetEvictionOrigin(StorageType type,
1444 const GetOriginCallback& callback) { 1542 const GetOriginCallback& callback) {
1445 GetLRUOrigin(type, callback); 1543 LazyInitialize();
1544 // This must not be called while there's an in-flight task.
1545 DCHECK(!is_getting_eviction_origin_);
1546 is_getting_eviction_origin_ = true;
1547
1548 GetOriginCallback did_get_origin_callback =
1549 base::Bind(&QuotaManager::DidGetEvictionOrigin,
1550 weak_factory_.GetWeakPtr(), callback);
1551
1552 auto eviction_policy = eviction_policy_map_.find(type);
1553 if (eviction_policy != eviction_policy_map_.end()) {
1554 GetEvictionOriginTask* task = new GetEvictionOriginTask(
1555 this, special_storage_policy_, eviction_policy->second,
1556 did_get_origin_callback);
1557 task->Start();
1558 return;
1559 }
1560
1561 // TODO(calamity): convert LRU origin retrieval into a QuotaEvictionPolicy.
1562 GetLRUOrigin(type, did_get_origin_callback);
1563 }
1564
1565 void QuotaManager::DidGetEvictionOrigin(const GetOriginCallback& callback,
1566 const GURL& origin) {
1567 callback.Run(origin);
1568
1569 is_getting_eviction_origin_ = false;
1446 } 1570 }
1447 1571
1448 void QuotaManager::EvictOriginData(const GURL& origin, 1572 void QuotaManager::EvictOriginData(const GURL& origin,
1449 StorageType type, 1573 StorageType type,
1450 const EvictOriginDataCallback& callback) { 1574 const EvictOriginDataCallback& callback) {
1451 DCHECK(io_thread_->BelongsToCurrentThread()); 1575 DCHECK(io_thread_->BelongsToCurrentThread());
1452 DCHECK_EQ(type, kStorageTypeTemporary); 1576 DCHECK_EQ(type, kStorageTypeTemporary);
1453 1577
1454 eviction_context_.evicted_origin = origin; 1578 eviction_context_.evicted_origin = origin;
1455 eviction_context_.evicted_type = type; 1579 eviction_context_.evicted_type = type;
(...skipping 23 matching lines...) Expand all
1479 LazyInitialize(); 1603 LazyInitialize();
1480 // This must not be called while there's an in-flight task. 1604 // This must not be called while there's an in-flight task.
1481 DCHECK(lru_origin_callback_.is_null()); 1605 DCHECK(lru_origin_callback_.is_null());
1482 lru_origin_callback_ = callback; 1606 lru_origin_callback_ = callback;
1483 if (db_disabled_) { 1607 if (db_disabled_) {
1484 lru_origin_callback_.Run(GURL()); 1608 lru_origin_callback_.Run(GURL());
1485 lru_origin_callback_.Reset(); 1609 lru_origin_callback_.Reset();
1486 return; 1610 return;
1487 } 1611 }
1488 1612
1613 // TODO(calamity): make all QuotaEvictionPolicies aware of these exceptions.
1489 std::set<GURL>* exceptions = new std::set<GURL>; 1614 std::set<GURL>* exceptions = new std::set<GURL>;
1490 for (std::map<GURL, int>::const_iterator p = origins_in_use_.begin(); 1615 for (std::map<GURL, int>::const_iterator p = origins_in_use_.begin();
1491 p != origins_in_use_.end(); 1616 p != origins_in_use_.end();
1492 ++p) { 1617 ++p) {
1493 if (p->second > 0) 1618 if (p->second > 0)
1494 exceptions->insert(p->first); 1619 exceptions->insert(p->first);
1495 } 1620 }
1496 for (std::map<GURL, int>::const_iterator p = origins_in_error_.begin(); 1621 for (std::map<GURL, int>::const_iterator p = origins_in_error_.begin();
1497 p != origins_in_error_.end(); 1622 p != origins_in_error_.end();
1498 ++p) { 1623 ++p) {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1625 // |database_|, therefore we can be sure that database_ is alive when this 1750 // |database_|, therefore we can be sure that database_ is alive when this
1626 // task runs. 1751 // task runs.
1627 base::PostTaskAndReplyWithResult( 1752 base::PostTaskAndReplyWithResult(
1628 db_thread_.get(), 1753 db_thread_.get(),
1629 from_here, 1754 from_here,
1630 base::Bind(task, base::Unretained(database_.get())), 1755 base::Bind(task, base::Unretained(database_.get())),
1631 reply); 1756 reply);
1632 } 1757 }
1633 1758
1634 } // namespace storage 1759 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698