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

Unified Diff: content/browser/dom_storage/dom_storage_namespace.cc

Issue 1953703004: Purge browser cache for dom storage in a smarter way (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dom_storage
Patch Set: Fixes. Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/dom_storage/dom_storage_namespace.cc
diff --git a/content/browser/dom_storage/dom_storage_namespace.cc b/content/browser/dom_storage/dom_storage_namespace.cc
index 6f976f0d50158c387456251c8094c0ae19f9cb81..a6c181d4cb7377edc0d1788607aa1856ff9aaf72 100644
--- a/content/browser/dom_storage/dom_storage_namespace.cc
+++ b/content/browser/dom_storage/dom_storage_namespace.cc
@@ -7,12 +7,24 @@
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
+#include "base/sys_info.h"
#include "content/browser/dom_storage/dom_storage_area.h"
#include "content/browser/dom_storage/dom_storage_task_runner.h"
#include "content/browser/dom_storage/session_storage_database.h"
#include "content/common/dom_storage/dom_storage_types.h"
namespace content {
+namespace {
+// Limits on the cache size and number of areas in memory, over which the areas
+// are purged.
+#if defined(OS_ANDROID)
+const size_t kMaxStorageAreaCount = 20;
+const uint64_t kMaxCacheSize = 2 * 1024 * 1024;
+#else
+const size_t kMaxStorageAreaCount = 100;
+const uint64_t kMaxCacheSize = 20 * 1024 * 1024;
+#endif
ssid 2016/05/10 02:04:16 Not sure if I should move these to dom_storage_typ
+} // namespace
DOMStorageNamespace::DOMStorageNamespace(
const base::FilePath& directory,
@@ -30,7 +42,8 @@ DOMStorageNamespace::DOMStorageNamespace(
: namespace_id_(namespace_id),
persistent_namespace_id_(persistent_namespace_id),
task_runner_(task_runner),
- session_storage_database_(session_storage_database) {
+ session_storage_database_(session_storage_database),
+ is_low_end_device_(base::SysInfo::IsLowEndDevice()) {
DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
}
@@ -120,6 +133,13 @@ void DOMStorageNamespace::DeleteSessionStorageOrigin(const GURL& origin) {
void DOMStorageNamespace::PurgeMemory(PurgeOption option) {
if (directory_.empty())
return; // We can't purge w/o backing on disk.
+
+ if (option == PURGE_IF_NEEDED) {
+ if (!ShouldPurgeMemory())
+ return;
+ option = PURGE_UNOPENED;
+ }
+
AreaMap::iterator it = areas_.begin();
while (it != areas_.end()) {
const AreaHolder& holder = it->second;
@@ -131,7 +151,7 @@ void DOMStorageNamespace::PurgeMemory(PurgeOption option) {
// we can drop it from memory.
holder.area_->ScheduleImmediateCommit();
}
- ++it;
+ it++;
continue;
}
@@ -148,9 +168,35 @@ void DOMStorageNamespace::PurgeMemory(PurgeOption option) {
// for opened areas.
holder.area_->PurgeMemory();
}
+ it++;
+ }
+}
- ++it;
+bool DOMStorageNamespace::ShouldPurgeMemory() {
+ // Purging is done based on the cache sizes without including the database
+ // size since it can be expensive trying to estimate the sqlite usage for all
+ // databases.
+ size_t total_cache_size = 0;
+ size_t total_area_count = 0;
+ unsigned inactive_area_count = 0;
+ for (AreaMap::const_iterator it = areas_.begin(); it != areas_.end(); ++it) {
+ if (it->second.area_->IsLoadedInMemory()) {
+ total_cache_size += it->second.area_->map_usage_in_bytes();
+ ++total_area_count;
+ if (it->second.open_count_ == 0)
+ ++inactive_area_count;
+ }
}
+
+ // Purge caches of closed databases on low end devices.
+ if (is_low_end_device_ && inactive_area_count)
+ return true;
+
+ if (total_cache_size > kMaxCacheSize ||
+ total_area_count > kMaxStorageAreaCount)
+ return true;
+
+ return false;
}
void DOMStorageNamespace::Shutdown() {
@@ -167,15 +213,6 @@ void DOMStorageNamespace::Flush() {
}
}
-unsigned int DOMStorageNamespace::CountInMemoryAreas() const {
- unsigned int area_count = 0;
- for (AreaMap::const_iterator it = areas_.begin(); it != areas_.end(); ++it) {
- if (it->second.area_->IsLoadedInMemory())
- ++area_count;
- }
- return area_count;
-}
-
void DOMStorageNamespace::OnMemoryDump(
base::trace_event::ProcessMemoryDump* pmd) {
DCHECK(task_runner_->IsRunningOnPrimarySequence());
« no previous file with comments | « content/browser/dom_storage/dom_storage_namespace.h ('k') | content/browser/dom_storage/local_storage_database_adapter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698