summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/dom_storage/local_storage_cached_areas.cc
blob: 1f2133c87d64c97a0a599d56a5889a771dde93f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/dom_storage/local_storage_cached_areas.h"

#include "base/metrics/histogram_macros.h"
#include "base/sys_info.h"
#include "content/common/dom_storage/dom_storage_namespace_ids.h"
#include "content/common/dom_storage/dom_storage_types.h"
#include "content/public/common/content_features.h"
#include "content/renderer/dom_storage/local_storage_cached_area.h"
#include "content/renderer/render_thread_impl.h"

namespace content {
namespace {
const size_t kTotalCacheLimitInBytesLowEnd = 1 * 1024 * 1024;
const size_t kTotalCacheLimitInBytes = 5 * 1024 * 1024;

// An empty namespace is the local storage namespace.
constexpr const char kLocalStorageNamespaceId[] = "";
}  // namespace

LocalStorageCachedAreas::LocalStorageCachedAreas(
    mojom::StoragePartitionService* storage_partition_service,
    blink::scheduler::WebMainThreadScheduler* main_thread_scheduler)
    : storage_partition_service_(storage_partition_service),
      total_cache_limit_(base::SysInfo::IsLowEndDevice()
                             ? kTotalCacheLimitInBytesLowEnd
                             : kTotalCacheLimitInBytes),
      main_thread_scheduler_(main_thread_scheduler) {}

LocalStorageCachedAreas::~LocalStorageCachedAreas() {}

scoped_refptr<LocalStorageCachedArea> LocalStorageCachedAreas::GetCachedArea(
    const url::Origin& origin) {
  return GetCachedArea(kLocalStorageNamespaceId, origin,
                       main_thread_scheduler_);
}

scoped_refptr<LocalStorageCachedArea>
LocalStorageCachedAreas::GetSessionStorageArea(const std::string& namespace_id,
                                               const url::Origin& origin) {
  DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
  return GetCachedArea(namespace_id, origin, main_thread_scheduler_);
}

void LocalStorageCachedAreas::CloneNamespace(
    const std::string& source_namespace,
    const std::string& destination_namespace) {
  DCHECK(base::FeatureList::IsEnabled(features::kMojoSessionStorage));
  DCHECK_EQ(kSessionStorageNamespaceIdLength, source_namespace.size());
  DCHECK_EQ(kSessionStorageNamespaceIdLength, destination_namespace.size());

  auto namespace_it = cached_namespaces_.find(source_namespace);
  if (namespace_it == cached_namespaces_.end()) {
    namespace_it =
        cached_namespaces_
            .emplace(std::make_pair(source_namespace, DOMStorageNamespace()))
            .first;
    storage_partition_service_->OpenSessionStorage(
        source_namespace,
        mojo::MakeRequest(&namespace_it->second.session_storage_namespace));
  }
  DCHECK(namespace_it->second.session_storage_namespace);
  namespace_it->second.session_storage_namespace->Clone(destination_namespace);
}

size_t LocalStorageCachedAreas::TotalCacheSize() const {
  size_t total = 0;
  for (const auto& it : cached_namespaces_)
    total += it.second.TotalCacheSize();
  return total;
}

void LocalStorageCachedAreas::ClearAreasIfNeeded() {
  if (TotalCacheSize() < total_cache_limit_)
    return;

  base::EraseIf(cached_namespaces_,
                [](auto& pair) { return pair.second.CleanUpUnusedAreas(); });
}

scoped_refptr<LocalStorageCachedArea> LocalStorageCachedAreas::GetCachedArea(
    const std::string& namespace_id,
    const url::Origin& origin,
    blink::scheduler::WebMainThreadScheduler* scheduler) {
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class CacheMetrics {
    kMiss = 0,    // Area not in cache.
    kHit = 1,     // Area with refcount = 0 loaded from cache.
    kUnused = 2,  // Cache was not used. Area had refcount > 0.
    kMaxValue = kUnused,
  };

  auto namespace_it = cached_namespaces_.find(namespace_id);
  CacheMetrics metric;
  scoped_refptr<LocalStorageCachedArea> result;
  DOMStorageNamespace* dom_namespace = nullptr;
  if (namespace_it == cached_namespaces_.end()) {
    metric = CacheMetrics::kMiss;
  } else {
    dom_namespace = &namespace_it->second;
    auto cache_it = dom_namespace->cached_areas.find(origin);
    if (cache_it == dom_namespace->cached_areas.end()) {
      metric = CacheMetrics::kMiss;
    } else {
      if (cache_it->second->HasOneRef()) {
        metric = CacheMetrics::kHit;
      } else {
        metric = CacheMetrics::kUnused;
      }
      result = cache_it->second;
    }
  }
  if (namespace_id == kLocalStorageNamespaceId)
    UMA_HISTOGRAM_ENUMERATION("LocalStorage.RendererAreaCacheHit", metric);
  else
    LOCAL_HISTOGRAM_ENUMERATION("SessionStorage.RendererAreaCacheHit", metric);

  if (!result) {
    ClearAreasIfNeeded();
    if (!dom_namespace) {
      dom_namespace = &cached_namespaces_[namespace_id];
    }
    if (namespace_id == kLocalStorageNamespaceId) {
      result = base::MakeRefCounted<LocalStorageCachedArea>(
          origin, storage_partition_service_, this, scheduler);
    } else {
      DCHECK(base::FeatureList::IsEnabled(features::kMojoSessionStorage));
      if (!dom_namespace->session_storage_namespace) {
        storage_partition_service_->OpenSessionStorage(
            namespace_id,
            mojo::MakeRequest(&dom_namespace->session_storage_namespace));
      }
      result = base::MakeRefCounted<LocalStorageCachedArea>(
          namespace_id, origin, dom_namespace->session_storage_namespace.get(),
          this, scheduler);
    }
    dom_namespace->cached_areas.emplace(origin, result);
  }
  return result;
}

LocalStorageCachedAreas::DOMStorageNamespace::DOMStorageNamespace() {}
LocalStorageCachedAreas::DOMStorageNamespace::~DOMStorageNamespace() {}
LocalStorageCachedAreas::DOMStorageNamespace::DOMStorageNamespace(
    LocalStorageCachedAreas::DOMStorageNamespace&& other) = default;

size_t LocalStorageCachedAreas::DOMStorageNamespace::TotalCacheSize() const {
  size_t total = 0;
  for (const auto& it : cached_areas)
    total += it.second.get()->memory_used();
  return total;
}

bool LocalStorageCachedAreas::DOMStorageNamespace::CleanUpUnusedAreas() {
  base::EraseIf(cached_areas,
                [](auto& pair) { return pair.second->HasOneRef(); });
  return cached_areas.empty();
}

}  // namespace content