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 | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 16 | #include "storage/browser/quota/quota_disk_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 | |
| 24 | // Skews |value| by +/- |percent|. |
| 25 | int64_t RandomizeByPercent(int64_t value, int percent) { |
| 26 | double random_percent = (base::RandDouble() - 0.5) * percent * 2; |
| 27 | return value + (value * (random_percent / 100.0)); |
| 28 | } |
| 29 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 30 | base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings( |
| 31 | const base::FilePath& partition_path, |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 32 | bool is_incognito, |
| 33 | QuotaDiskInfoHelper* disk_info_helper) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame^] | 34 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 35 | base::BlockingType::MAY_BLOCK); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 36 | const int64_t kMBytes = 1024 * 1024; |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 37 | const int kRandomizedPercentage = 10; |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 38 | |
| 39 | if (is_incognito) { |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 40 | // The incognito pool size is a fraction of the amount of system memory, |
| 41 | // and the amount is capped to a hard limit. |
| 42 | const double kIncognitoPoolSizeRatio = 0.1; // 10% |
| 43 | const int64_t kMaxIncognitoPoolSize = 300 * kMBytes; |
| 44 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 45 | storage::QuotaSettings settings; |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 46 | settings.pool_size = std::min( |
| 47 | RandomizeByPercent(kMaxIncognitoPoolSize, kRandomizedPercentage), |
| 48 | static_cast<int64_t>(base::SysInfo::AmountOfPhysicalMemory() * |
| 49 | kIncognitoPoolSizeRatio)); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 50 | settings.per_host_quota = settings.pool_size / 3; |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 51 | settings.session_only_per_host_quota = settings.per_host_quota; |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 52 | settings.refresh_interval = base::TimeDelta::Max(); |
| 53 | return settings; |
| 54 | } |
| 55 | |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 56 | // The fraction of the device's storage the browser is willing to |
| 57 | // use for temporary storage. |
| 58 | // Check Finch for an experimental value to use as temporary pool size ratio |
| 59 | // if experiment is enabled, otherwise fallback to ~66% for chromeOS and |
| 60 | // ~33% otherwise. |
| 61 | const double kTemporaryPoolSizeRatio = |
| 62 | features::kExperimentalPoolSizeRatio.Get(); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 63 | |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 64 | // The amount of the device's storage the browser attempts to |
Joshua Bell | 9508bf4e | 2018-02-23 18:05:20 | [diff] [blame] | 65 | // keep free. If there is less than this amount of storage free |
| 66 | // on the device, Chrome will grant 0 quota to origins. |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 67 | // |
Joshua Bell | 73b18e3 | 2018-05-02 23:06:29 | [diff] [blame] | 68 | // Prior to M66, this was 10% of total storage instead of a fixed value on |
| 69 | // all devices. Now the minimum of a fixed value (2GB) and 10% is used to |
| 70 | // limit the reserve on devices with plenty of storage, but scale down for |
| 71 | // devices with extremely limited storage. |
| 72 | // * 1TB storage -- min(100GB,2GB) = 2GB |
| 73 | // * 500GB storage -- min(50GB,2GB) = 2GB |
| 74 | // * 64GB storage -- min(6GB,2GB) = 2GB |
| 75 | // * 16GB storage -- min(1.6GB,2GB) = 1.6GB |
| 76 | // * 8GB storage -- min(800MB,2GB) = 800MB |
| 77 | const int64_t kShouldRemainAvailableFixed = 2048 * kMBytes; // 2GB |
| 78 | const double kShouldRemainAvailableRatio = 0.1; // 10% |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 79 | |
Joshua Bell | 1e5b570 | 2018-02-28 06:36:29 | [diff] [blame] | 80 | // The amount of the device's storage the browser attempts to |
Joshua Bell | 9508bf4e | 2018-02-23 18:05:20 | [diff] [blame] | 81 | // keep free at all costs. Data will be aggressively evicted. |
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 1% of total storage instead of a fixed value on |
| 84 | // all devices. Now the minimum of a fixed value (1GB) and 1% 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(10GB,1GB) = 1GB |
| 88 | // * 500GB storage -- min(5GB,1GB) = 1GB |
| 89 | // * 64GB storage -- min(640MB,1GB) = 640MB |
| 90 | // * 16GB storage -- min(160MB,1GB) = 160MB |
| 91 | // * 8GB storage -- min(80MB,1GB) = 80MB |
| 92 | const int64_t kMustRemainAvailableFixed = 1024 * kMBytes; // 1GB |
| 93 | const double kMustRemainAvailableRatio = 0.01; // 1% |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 94 | |
| 95 | // Determines the portion of the temp pool that can be |
| 96 | // utilized by a single host (ie. 5 for 20%). |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 97 | const double kPerHostTemporaryRatio = features::kPerHostRatio.Get(); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 98 | |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 99 | // SessionOnly (or ephemeral) origins are allotted a fraction of what |
| 100 | // normal origins are provided, and the amount is capped to a hard limit. |
| 101 | const double kSessionOnlyHostQuotaRatio = 0.1; // 10% |
| 102 | const int64_t kMaxSessionOnlyHostQuota = 300 * kMBytes; |
| 103 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 104 | storage::QuotaSettings settings; |
| 105 | |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 106 | int64_t total = disk_info_helper->AmountOfTotalDiskSpace(partition_path); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 107 | if (total == -1) { |
| 108 | LOG(ERROR) << "Unable to compute QuotaSettings."; |
| 109 | return base::nullopt; |
| 110 | } |
| 111 | |
Joshua Bell | ac70773 | 2018-03-05 21:22:59 | [diff] [blame] | 112 | int64_t pool_size = total * kTemporaryPoolSizeRatio; |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 113 | |
| 114 | settings.pool_size = pool_size; |
Joshua Bell | 73b18e3 | 2018-05-02 23:06:29 | [diff] [blame] | 115 | settings.should_remain_available = |
| 116 | std::min(kShouldRemainAvailableFixed, |
| 117 | static_cast<int64_t>(total * kShouldRemainAvailableRatio)); |
| 118 | settings.must_remain_available = |
| 119 | std::min(kMustRemainAvailableFixed, |
| 120 | static_cast<int64_t>(total * kMustRemainAvailableRatio)); |
Jarryd | 7a79f066 | 2019-01-24 07:26:44 | [diff] [blame] | 121 | settings.per_host_quota = pool_size * kPerHostTemporaryRatio; |
michaeln | fa4c8940 | 2017-04-11 02:36:20 | [diff] [blame] | 122 | settings.session_only_per_host_quota = std::min( |
| 123 | RandomizeByPercent(kMaxSessionOnlyHostQuota, kRandomizedPercentage), |
| 124 | static_cast<int64_t>(settings.per_host_quota * |
| 125 | kSessionOnlyHostQuotaRatio)); |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 126 | settings.refresh_interval = base::TimeDelta::FromSeconds(60); |
| 127 | return settings; |
| 128 | } |
| 129 | |
| 130 | } // namespace |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 131 | |
| 132 | void GetNominalDynamicSettings(const base::FilePath& partition_path, |
| 133 | bool is_incognito, |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 134 | QuotaDiskInfoHelper* disk_info_helper, |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 135 | OptionalQuotaSettingsCallback callback) { |
| 136 | base::PostTaskWithTraitsAndReplyWithResult( |
| 137 | FROM_HERE, |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 138 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 139 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 140 | base::BindOnce(&CalculateNominalDynamicSettings, partition_path, |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 141 | is_incognito, base::Unretained(disk_info_helper)), |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 142 | std::move(callback)); |
| 143 | } |
| 144 | |
Jarryd | 451ab7b | 2019-02-12 06:39:06 | [diff] [blame] | 145 | QuotaDiskInfoHelper* GetDefaultDiskInfoHelper() { |
| 146 | static base::NoDestructor<QuotaDiskInfoHelper> singleton; |
| 147 | return singleton.get(); |
| 148 | } |
| 149 | |
tapted | e6d878e | 2017-06-24 01:53:45 | [diff] [blame] | 150 | } // namespace storage |