michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // 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> |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 8 | #include <memory> |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 9 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 10 | #include "base/bind.h" |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 11 | #include "base/rand_util.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 12 | #include "base/system/sys_info.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 13 | #include "base/task/post_task.h" |
Etienne Pierre-Doray | 57cf470 | 2018-11-16 17:04:14 | [diff] [blame] | 14 | #include "base/threading/scoped_blocking_call.h" |
Kevin Marshall | f1bf4e5 | 2017-08-15 19:37:58 | [diff] [blame] | 15 | #include "build/build_config.h" |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 16 | #include "storage/browser/quota/quota_device_info_helper.h" |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 17 | #include "storage/browser/quota/quota_features.h" |
Oscar Johansson | 357dd5c | 2018-08-07 10:42:02 | [diff] [blame] | 18 | #include "storage/browser/quota/quota_macros.h" |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 19 | |
| 20 | namespace storage { |
| 21 | |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 22 | namespace { |
| 23 | |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 24 | const int64_t kMBytes = 1024 * 1024; |
| 25 | const int kRandomizedPercentage = 10; |
| 26 | |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 27 | // Skews |value| by +/- |percent|. |
| 28 | int64_t RandomizeByPercent(int64_t value, int percent) { |
| 29 | double random_percent = (base::RandDouble() - 0.5) * percent * 2; |
| 30 | return value + (value * (random_percent / 100.0)); |
| 31 | } |
| 32 | |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 33 | storage::QuotaSettings CalculateIncognitoDynamicSettings( |
| 34 | int64_t physical_memory_amount) { |
| 35 | // The incognito pool size is a fraction of the amount of system memory, |
| 36 | // and the amount is capped to a hard limit. |
Jarryd | 50d305f7b | 2019-11-09 01:21:35 | [diff] [blame] | 37 | double incognito_pool_size_ratio = 0.1; // 10% |
| 38 | int64_t max_incognito_pool_size = 300 * kMBytes; |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 39 | if (base::FeatureList::IsEnabled(features::kIncognitoDynamicQuota)) { |
| 40 | const double lower_bound = features::kIncognitoQuotaRatioLowerBound.Get(); |
| 41 | const double upper_bound = features::kIncognitoQuotaRatioUpperBound.Get(); |
Jarryd | 50d305f7b | 2019-11-09 01:21:35 | [diff] [blame] | 42 | incognito_pool_size_ratio = |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 43 | lower_bound + (base::RandDouble() * (upper_bound - lower_bound)); |
Jarryd | 50d305f7b | 2019-11-09 01:21:35 | [diff] [blame] | 44 | max_incognito_pool_size = std::numeric_limits<int64_t>::max(); |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 45 | } else { |
Jarryd | 50d305f7b | 2019-11-09 01:21:35 | [diff] [blame] | 46 | max_incognito_pool_size = |
| 47 | RandomizeByPercent(max_incognito_pool_size, kRandomizedPercentage); |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | storage::QuotaSettings settings; |
| 51 | settings.pool_size = std::min( |
Jarryd | 50d305f7b | 2019-11-09 01:21:35 | [diff] [blame] | 52 | max_incognito_pool_size, |
| 53 | static_cast<int64_t>(physical_memory_amount * incognito_pool_size_ratio)); |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 54 | settings.per_host_quota = settings.pool_size / 3; |
| 55 | settings.session_only_per_host_quota = settings.per_host_quota; |
| 56 | settings.refresh_interval = base::TimeDelta::Max(); |
| 57 | return settings; |
| 58 | } |
| 59 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 60 | base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings( |
| 61 | const base::FilePath& partition_path, |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 62 | bool is_incognito, |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 63 | QuotaDeviceInfoHelper* device_info_helper) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 64 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 65 | base::BlockingType::MAY_BLOCK); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 66 | |
| 67 | if (is_incognito) { |
Ramin Halavati | 57e61893 | 2019-10-31 12:46:47 | [diff] [blame] | 68 | return CalculateIncognitoDynamicSettings( |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 69 | device_info_helper->AmountOfPhysicalMemory()); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 70 | } |
| 71 | |
Victor Costan | f39940a | 2019-12-02 19:37:29 | [diff] [blame^] | 72 | // The fraction of the device's storage the browser is willing to use for |
| 73 | // temporary storage. |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 74 | const double kTemporaryPoolSizeRatio = |
Jarryd | 1cc7327 | 2019-07-22 18:31:37 | [diff] [blame] | 75 | base::FeatureList::IsEnabled(features::kQuotaUnlimitedPoolSize) |
| 76 | ? 1.0 |
| 77 | : features::kExperimentalPoolSizeRatio.Get(); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 78 | |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 79 | // The amount of the device's storage the browser attempts to |
Joshua Bell | 9508bf4e | 2018-02-23 18:05:20 | [diff] [blame] | 80 | // keep free. If there is less than this amount of storage free |
| 81 | // on the device, Chrome will grant 0 quota to origins. |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 82 | // |
Joshua Bell | 73b18e3 | 2018-05-02 23:06:29 | [diff] [blame] | 83 | // Prior to M66, this was 10% of total storage instead of a fixed value on |
| 84 | // all devices. Now the minimum of a fixed value (2GB) and 10% is used to |
| 85 | // limit the reserve on devices with plenty of storage, but scale down for |
| 86 | // devices with extremely limited storage. |
| 87 | // * 1TB storage -- min(100GB,2GB) = 2GB |
| 88 | // * 500GB storage -- min(50GB,2GB) = 2GB |
| 89 | // * 64GB storage -- min(6GB,2GB) = 2GB |
| 90 | // * 16GB storage -- min(1.6GB,2GB) = 1.6GB |
| 91 | // * 8GB storage -- min(800MB,2GB) = 800MB |
| 92 | const int64_t kShouldRemainAvailableFixed = 2048 * kMBytes; // 2GB |
| 93 | const double kShouldRemainAvailableRatio = 0.1; // 10% |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 94 | |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 95 | // The amount of the device's storage the browser attempts to |
Joshua Bell | 9508bf4e | 2018-02-23 18:05:20 | [diff] [blame] | 96 | // keep free at all costs. Data will be aggressively evicted. |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 97 | // |
Joshua Bell | 73b18e3 | 2018-05-02 23:06:29 | [diff] [blame] | 98 | // Prior to M66, this was 1% of total storage instead of a fixed value on |
| 99 | // all devices. Now the minimum of a fixed value (1GB) and 1% is used to |
| 100 | // limit the reserve on devices with plenty of storage, but scale down for |
| 101 | // devices with extremely limited storage. |
| 102 | // * 1TB storage -- min(10GB,1GB) = 1GB |
| 103 | // * 500GB storage -- min(5GB,1GB) = 1GB |
| 104 | // * 64GB storage -- min(640MB,1GB) = 640MB |
| 105 | // * 16GB storage -- min(160MB,1GB) = 160MB |
| 106 | // * 8GB storage -- min(80MB,1GB) = 80MB |
| 107 | const int64_t kMustRemainAvailableFixed = 1024 * kMBytes; // 1GB |
| 108 | const double kMustRemainAvailableRatio = 0.01; // 1% |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 109 | |
Victor Costan | f39940a | 2019-12-02 19:37:29 | [diff] [blame^] | 110 | // The fraction of the temporary pool that can be utilized by a single host. |
Jarryd | 1cc7327 | 2019-07-22 18:31:37 | [diff] [blame] | 111 | const double kPerHostTemporaryRatio = |
| 112 | base::FeatureList::IsEnabled(features::kQuotaUnlimitedPoolSize) |
| 113 | ? 1.0 |
| 114 | : features::kPerHostRatio.Get(); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 115 | |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 116 | // SessionOnly (or ephemeral) origins are allotted a fraction of what |
| 117 | // normal origins are provided, and the amount is capped to a hard limit. |
| 118 | const double kSessionOnlyHostQuotaRatio = 0.1; // 10% |
| 119 | const int64_t kMaxSessionOnlyHostQuota = 300 * kMBytes; |
| 120 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 121 | storage::QuotaSettings settings; |
| 122 | |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 123 | int64_t total = device_info_helper->AmountOfTotalDiskSpace(partition_path); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 124 | if (total == -1) { |
| 125 | LOG(ERROR) << "Unable to compute QuotaSettings."; |
| 126 | return base::nullopt; |
| 127 | } |
| 128 | |
Joshua Bell | ac70773 | 2018-03-05 21:22:59 | [diff] [blame] | 129 | int64_t pool_size = total * kTemporaryPoolSizeRatio; |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 130 | |
| 131 | settings.pool_size = pool_size; |
Joshua Bell | 73b18e3 | 2018-05-02 23:06:29 | [diff] [blame] | 132 | settings.should_remain_available = |
| 133 | std::min(kShouldRemainAvailableFixed, |
| 134 | static_cast<int64_t>(total * kShouldRemainAvailableRatio)); |
| 135 | settings.must_remain_available = |
| 136 | std::min(kMustRemainAvailableFixed, |
| 137 | static_cast<int64_t>(total * kMustRemainAvailableRatio)); |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 138 | settings.per_host_quota = pool_size * kPerHostTemporaryRatio; |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 139 | settings.session_only_per_host_quota = std::min( |
| 140 | RandomizeByPercent(kMaxSessionOnlyHostQuota, kRandomizedPercentage), |
| 141 | static_cast<int64_t>(settings.per_host_quota * |
| 142 | kSessionOnlyHostQuotaRatio)); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 143 | settings.refresh_interval = base::TimeDelta::FromSeconds(60); |
| 144 | return settings; |
| 145 | } |
| 146 | |
| 147 | } // namespace |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 148 | |
| 149 | void GetNominalDynamicSettings(const base::FilePath& partition_path, |
| 150 | bool is_incognito, |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 151 | QuotaDeviceInfoHelper* device_info_helper, |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 152 | OptionalQuotaSettingsCallback callback) { |
Sami Kyostila | d5265e9 | 2019-07-31 19:59:45 | [diff] [blame] | 153 | base::PostTaskAndReplyWithResult( |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 154 | FROM_HERE, |
Ben Kelly | 8980b4e | 2019-09-23 20:46:29 | [diff] [blame] | 155 | {base::ThreadPool(), base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 156 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 157 | base::BindOnce(&CalculateNominalDynamicSettings, partition_path, |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 158 | is_incognito, base::Unretained(device_info_helper)), |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 159 | std::move(callback)); |
| 160 | } |
| 161 | |
Jarryd | 1f02b90 | 2019-11-07 03:29:57 | [diff] [blame] | 162 | QuotaDeviceInfoHelper* GetDefaultDeviceInfoHelper() { |
| 163 | static base::NoDestructor<QuotaDeviceInfoHelper> singleton; |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 164 | return singleton.get(); |
| 165 | } |
| 166 | |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 167 | } // namespace storage |