[go: nahoru, domu]

blob: b90b655525181dab1d5dfd8ecee2bd4c96d03e7b [file] [log] [blame]
Avi Drissman69b874f2022-09-15 19:11:141// Copyright 2016 The Chromium Authors
michaeln10e5fc352017-02-07 02:07:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "storage/browser/quota/quota_settings.h"
6
7#include <algorithm>
Victor Costanb410d1a2020-02-12 05:03:228#include <limits>
Jarryd451ab7b2019-02-12 06:39:069#include <memory>
Victor Costanb410d1a2020-02-12 05:03:2210#include <utility>
michaeln10e5fc352017-02-07 02:07:5811
Avi Drissman2e85357a2023-01-12 21:57:3212#include "base/functional/bind.h"
Lei Zhang9cb338a2021-07-14 21:56:1713#include "base/no_destructor.h"
michaelnfa4c89402017-04-11 02:36:2014#include "base/rand_util.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0315#include "base/system/sys_info.h"
Gabriel Charette77285f42020-02-26 17:01:2316#include "base/task/thread_pool.h"
Etienne Pierre-Doray57cf4702018-11-16 17:04:1417#include "base/threading/scoped_blocking_call.h"
Kevin Marshallf1bf4e52017-08-15 19:37:5818#include "build/build_config.h"
Jarryd1f02b902019-11-07 03:29:5719#include "storage/browser/quota/quota_device_info_helper.h"
Jarryd7a79f0662019-01-24 07:26:4420#include "storage/browser/quota/quota_features.h"
Oscar Johansson357dd5c2018-08-07 10:42:0221#include "storage/browser/quota/quota_macros.h"
michaeln10e5fc352017-02-07 02:07:5822
23namespace storage {
24
michaelnfa4c89402017-04-11 02:36:2025namespace {
26
Ramin Halavati57e618932019-10-31 12:46:4727const int64_t kMBytes = 1024 * 1024;
28const int kRandomizedPercentage = 10;
Ayu Ishii802a1ee2022-11-30 17:40:1929const double kDefaultPerStorageKeyRatio = 0.75;
Ramin Halavatif8e6df82020-10-05 21:39:4630const double kIncognitoQuotaRatioLowerBound = 0.15;
31const double kIncognitoQuotaRatioUpperBound = 0.2;
Ramin Halavati57e618932019-10-31 12:46:4732
michaelnfa4c89402017-04-11 02:36:2033// Skews |value| by +/- |percent|.
34int64_t RandomizeByPercent(int64_t value, int percent) {
35 double random_percent = (base::RandDouble() - 0.5) * percent * 2;
36 return value + (value * (random_percent / 100.0));
37}
38
Victor Costanb410d1a2020-02-12 05:03:2239QuotaSettings CalculateIncognitoDynamicSettings(
Peter Kastinga0b914dc2022-07-14 18:43:1940 uint64_t physical_memory_amount) {
Ramin Halavatif8e6df82020-10-05 21:39:4641 // The incognito pool size is a fraction of the amount of system memory.
42 double incognito_pool_size_ratio =
43 kIncognitoQuotaRatioLowerBound +
44 (base::RandDouble() *
45 (kIncognitoQuotaRatioUpperBound - kIncognitoQuotaRatioLowerBound));
Ramin Halavati57e618932019-10-31 12:46:4746
Victor Costanb410d1a2020-02-12 05:03:2247 QuotaSettings settings;
Ramin Halavatif8e6df82020-10-05 21:39:4648 settings.pool_size =
49 static_cast<int64_t>(physical_memory_amount * incognito_pool_size_ratio);
Ayu Ishii802a1ee2022-11-30 17:40:1950 settings.per_storage_key_quota = settings.pool_size / 3;
51 settings.session_only_per_storage_key_quota = settings.per_storage_key_quota;
Ramin Halavati57e618932019-10-31 12:46:4752 settings.refresh_interval = base::TimeDelta::Max();
53 return settings;
54}
55
Anton Bikineev3ac3d302021-05-15 17:54:0156absl::optional<QuotaSettings> CalculateNominalDynamicSettings(
michaeln10e5fc352017-02-07 02:07:5857 const base::FilePath& partition_path,
Jarryd451ab7b2019-02-12 06:39:0658 bool is_incognito,
Jarryd1f02b902019-11-07 03:29:5759 QuotaDeviceInfoHelper* device_info_helper) {
Etienne Bergeron436d42212019-02-26 17:15:1260 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
61 base::BlockingType::MAY_BLOCK);
michaeln10e5fc352017-02-07 02:07:5862
63 if (is_incognito) {
Ramin Halavati57e618932019-10-31 12:46:4764 return CalculateIncognitoDynamicSettings(
Jarryd1f02b902019-11-07 03:29:5765 device_info_helper->AmountOfPhysicalMemory());
michaeln10e5fc352017-02-07 02:07:5866 }
67
Victor Costanf39940a2019-12-02 19:37:2968 // The fraction of the device's storage the browser is willing to use for
69 // temporary storage.
Junbo Ke0f792b52021-01-26 22:58:1370 const double kTemporaryPoolSizeRatio = features::kPoolSizeRatio.Get();
71
72 // The fixed size in bytes the browser is willing to use for temporary
73 // storage. If both the ratio and the absolute size are set, the lower value
74 // will be honored.
75 const int64_t kTemporaryPoolSizeFixed =
76 static_cast<int64_t>(features::kPoolSizeBytes.Get());
michaeln10e5fc352017-02-07 02:07:5877
Joshua Bell1e5b5702018-02-28 06:36:2978 // The amount of the device's storage the browser attempts to
Joshua Bell9508bf4e2018-02-23 18:05:2079 // keep free. If there is less than this amount of storage free
80 // on the device, Chrome will grant 0 quota to origins.
Joshua Bell1e5b5702018-02-28 06:36:2981 //
Joshua Bell73b18e32018-05-02 23:06:2982 // Prior to M66, this was 10% of total storage instead of a fixed value on
83 // all devices. Now the minimum of a fixed value (2GB) and 10% is used to
84 // limit the reserve on devices with plenty of storage, but scale down for
85 // devices with extremely limited storage.
86 // * 1TB storage -- min(100GB,2GB) = 2GB
87 // * 500GB storage -- min(50GB,2GB) = 2GB
88 // * 64GB storage -- min(6GB,2GB) = 2GB
89 // * 16GB storage -- min(1.6GB,2GB) = 1.6GB
90 // * 8GB storage -- min(800MB,2GB) = 800MB
Junbo Ke0f792b52021-01-26 22:58:1391 const int64_t kShouldRemainAvailableFixed =
92 static_cast<int64_t>(features::kShouldRemainAvailableBytes.Get());
93 const double kShouldRemainAvailableRatio =
94 features::kShouldRemainAvailableRatio.Get();
michaeln10e5fc352017-02-07 02:07:5895
Joshua Bell1e5b5702018-02-28 06:36:2996 // The amount of the device's storage the browser attempts to
Joshua Bell9508bf4e2018-02-23 18:05:2097 // keep free at all costs. Data will be aggressively evicted.
Joshua Bell1e5b5702018-02-28 06:36:2998 //
Joshua Bell73b18e32018-05-02 23:06:2999 // Prior to M66, this was 1% of total storage instead of a fixed value on
100 // all devices. Now the minimum of a fixed value (1GB) and 1% is used to
101 // limit the reserve on devices with plenty of storage, but scale down for
102 // devices with extremely limited storage.
103 // * 1TB storage -- min(10GB,1GB) = 1GB
104 // * 500GB storage -- min(5GB,1GB) = 1GB
105 // * 64GB storage -- min(640MB,1GB) = 640MB
106 // * 16GB storage -- min(160MB,1GB) = 160MB
107 // * 8GB storage -- min(80MB,1GB) = 80MB
Junbo Ke0f792b52021-01-26 22:58:13108 const int64_t kMustRemainAvailableFixed =
109 static_cast<int64_t>(features::kMustRemainAvailableBytes.Get());
110 const double kMustRemainAvailableRatio =
111 features::kMustRemainAvailableRatio.Get();
michaeln10e5fc352017-02-07 02:07:58112
Ayu Ishii802a1ee2022-11-30 17:40:19113 // The fraction of the temporary pool that can be utilized by a single
114 // StorageKey.
115 const double kPerStorageKeyTemporaryRatio = kDefaultPerStorageKeyRatio;
michaeln10e5fc352017-02-07 02:07:58116
michaelnfa4c89402017-04-11 02:36:20117 // SessionOnly (or ephemeral) origins are allotted a fraction of what
118 // normal origins are provided, and the amount is capped to a hard limit.
Ayu Ishii802a1ee2022-11-30 17:40:19119 const double kSessionOnlyStorageKeyQuotaRatio = 0.1; // 10%
120 const int64_t kMaxSessionOnlyStorageKeyQuota = 300 * kMBytes;
michaelnfa4c89402017-04-11 02:36:20121
Victor Costanb410d1a2020-02-12 05:03:22122 QuotaSettings settings;
michaeln10e5fc352017-02-07 02:07:58123
Jarryd1f02b902019-11-07 03:29:57124 int64_t total = device_info_helper->AmountOfTotalDiskSpace(partition_path);
michaeln10e5fc352017-02-07 02:07:58125 if (total == -1) {
126 LOG(ERROR) << "Unable to compute QuotaSettings.";
Anton Bikineev3ac3d302021-05-15 17:54:01127 return absl::nullopt;
michaeln10e5fc352017-02-07 02:07:58128 }
129
Junbo Ke0f792b52021-01-26 22:58:13130 // Pool size calculated by ratio.
131 int64_t pool_size_by_ratio = total * kTemporaryPoolSizeRatio;
132
133 int64_t pool_size =
134 kTemporaryPoolSizeFixed > 0
135 ? std::min(kTemporaryPoolSizeFixed, pool_size_by_ratio)
136 : pool_size_by_ratio;
michaeln10e5fc352017-02-07 02:07:58137
138 settings.pool_size = pool_size;
Joshua Bell73b18e32018-05-02 23:06:29139 settings.should_remain_available =
140 std::min(kShouldRemainAvailableFixed,
141 static_cast<int64_t>(total * kShouldRemainAvailableRatio));
142 settings.must_remain_available =
143 std::min(kMustRemainAvailableFixed,
144 static_cast<int64_t>(total * kMustRemainAvailableRatio));
Ayu Ishii802a1ee2022-11-30 17:40:19145 settings.per_storage_key_quota = pool_size * kPerStorageKeyTemporaryRatio;
146 settings.session_only_per_storage_key_quota = std::min(
147 RandomizeByPercent(kMaxSessionOnlyStorageKeyQuota, kRandomizedPercentage),
148 static_cast<int64_t>(settings.per_storage_key_quota *
149 kSessionOnlyStorageKeyQuotaRatio));
Peter Kastinge5a38ed2021-10-02 03:06:35150 settings.refresh_interval = base::Seconds(60);
michaeln10e5fc352017-02-07 02:07:58151 return settings;
152}
153
154} // namespace
taptede6d878e2017-06-24 01:53:45155
156void GetNominalDynamicSettings(const base::FilePath& partition_path,
157 bool is_incognito,
Jarryd1f02b902019-11-07 03:29:57158 QuotaDeviceInfoHelper* device_info_helper,
taptede6d878e2017-06-24 01:53:45159 OptionalQuotaSettingsCallback callback) {
Gabriel Charette77285f42020-02-26 17:01:23160 base::ThreadPool::PostTaskAndReplyWithResult(
taptede6d878e2017-06-24 01:53:45161 FROM_HERE,
Gabriel Charette77285f42020-02-26 17:01:23162 {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
taptede6d878e2017-06-24 01:53:45163 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
164 base::BindOnce(&CalculateNominalDynamicSettings, partition_path,
Jarryd1f02b902019-11-07 03:29:57165 is_incognito, base::Unretained(device_info_helper)),
taptede6d878e2017-06-24 01:53:45166 std::move(callback));
167}
168
Jarryd1f02b902019-11-07 03:29:57169QuotaDeviceInfoHelper* GetDefaultDeviceInfoHelper() {
170 static base::NoDestructor<QuotaDeviceInfoHelper> singleton;
Jarryd451ab7b2019-02-12 06:39:06171 return singleton.get();
172}
173
Ramin Halavatif8e6df82020-10-05 21:39:46174double GetIncognitoQuotaRatioLowerBound_ForTesting() {
175 return kIncognitoQuotaRatioLowerBound;
176}
177double GetIncognitoQuotaRatioUpperBound_ForTesting() {
178 return kIncognitoQuotaRatioUpperBound;
179}
180
taptede6d878e2017-06-24 01:53:45181} // namespace storage