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

Side by Side Diff: chrome/browser/extensions/settings/settings_storage_quota_enforcer.cc

Issue 9284013: Extension Storage API: expose storage quota information to extensions, via: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" 5 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 12
13 namespace extensions { 13 namespace extensions {
14 14
15 namespace { 15 namespace {
16 16
17 const char* kExceededQuotaErrorMessage = "Quota exceeded"; 17 const char* kExceededQuotaErrorMessage = "Quota exceeded";
18 18
19 // Resources there are a quota for. 19 // Resources there are a quota for.
20 enum Resource { 20 enum Resource {
21 TOTAL_BYTES, 21 QUOTA_BYTES,
22 BYTES_PER_SETTING, 22 QUOTA_BYTES_PER_ITEM,
23 KEY_COUNT 23 MAX_ITEMS
24 }; 24 };
25 25
26 // Allocates a setting in a record of total and per-setting usage. 26 // Allocates a setting in a record of total and per-setting usage.
27 void Allocate( 27 void Allocate(
28 const std::string& key, 28 const std::string& key,
29 const Value& value, 29 const Value& value,
30 size_t* used_total, 30 size_t* used_total,
31 std::map<std::string, size_t>* used_per_setting) { 31 std::map<std::string, size_t>* used_per_setting) {
32 // Calculate the setting size based on its JSON serialization size. 32 // Calculate the setting size based on its JSON serialization size.
33 // TODO(kalman): Does this work with different encodings? 33 // TODO(kalman): Does this work with different encodings?
(...skipping 13 matching lines...) Expand all
47 size_t* used_total, 47 size_t* used_total,
48 std::map<std::string, size_t>* used_per_setting, 48 std::map<std::string, size_t>* used_per_setting,
49 const std::string& key) { 49 const std::string& key) {
50 *used_total -= (*used_per_setting)[key]; 50 *used_total -= (*used_per_setting)[key];
51 used_per_setting->erase(key); 51 used_per_setting->erase(key);
52 } 52 }
53 53
54 // Returns an error result and logs the quota exceeded to UMA. 54 // Returns an error result and logs the quota exceeded to UMA.
55 SettingsStorage::WriteResult QuotaExceededFor(Resource resource) { 55 SettingsStorage::WriteResult QuotaExceededFor(Resource resource) {
56 switch (resource) { 56 switch (resource) {
57 case TOTAL_BYTES: 57 case QUOTA_BYTES:
58 UMA_HISTOGRAM_COUNTS_100( 58 UMA_HISTOGRAM_COUNTS_100(
59 "Extensions.SettingsQuotaExceeded.TotalBytes", 1); 59 "Extensions.SettingsQuotaExceeded.TotalBytes", 1);
60 break; 60 break;
61 case BYTES_PER_SETTING: 61 case QUOTA_BYTES_PER_ITEM:
62 UMA_HISTOGRAM_COUNTS_100( 62 UMA_HISTOGRAM_COUNTS_100(
63 "Extensions.SettingsQuotaExceeded.BytesPerSetting", 1); 63 "Extensions.SettingsQuotaExceeded.BytesPerSetting", 1);
64 break; 64 break;
65 case KEY_COUNT: 65 case MAX_ITEMS:
66 UMA_HISTOGRAM_COUNTS_100( 66 UMA_HISTOGRAM_COUNTS_100(
67 "Extensions.SettingsQuotaExceeded.KeyCount", 1); 67 "Extensions.SettingsQuotaExceeded.KeyCount", 1);
68 break; 68 break;
69 default: 69 default:
70 NOTREACHED(); 70 NOTREACHED();
71 } 71 }
72 return SettingsStorage::WriteResult(kExceededQuotaErrorMessage); 72 return SettingsStorage::WriteResult(kExceededQuotaErrorMessage);
73 } 73 }
74 74
75 } // namespace 75 } // namespace
(...skipping 10 matching lines...) Expand all
86 86
87 for (DictionaryValue::Iterator it(maybe_settings.settings()); it.HasNext(); 87 for (DictionaryValue::Iterator it(maybe_settings.settings()); it.HasNext();
88 it.Advance()) { 88 it.Advance()) {
89 Allocate( 89 Allocate(
90 it.key(), it.value(), &used_total_, &used_per_setting_); 90 it.key(), it.value(), &used_total_, &used_per_setting_);
91 } 91 }
92 } 92 }
93 93
94 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() {} 94 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() {}
95 95
96 size_t SettingsStorageQuotaEnforcer::GetBytesInUse() {
97 // All SettingsStorage implementations rely on GetBytesInUse being
98 // implemented here.
99 return used_total_;
100 }
101
96 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( 102 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get(
97 const std::string& key) { 103 const std::string& key) {
98 return delegate_->Get(key); 104 return delegate_->Get(key);
99 } 105 }
100 106
101 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( 107 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get(
102 const std::vector<std::string>& keys) { 108 const std::vector<std::string>& keys) {
103 return delegate_->Get(keys); 109 return delegate_->Get(keys);
104 } 110 }
105 111
106 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get() { 112 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get() {
107 return delegate_->Get(); 113 return delegate_->Get();
108 } 114 }
109 115
110 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set( 116 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set(
111 WriteOptions options, const std::string& key, const Value& value) { 117 WriteOptions options, const std::string& key, const Value& value) {
112 size_t new_used_total = used_total_; 118 size_t new_used_total = used_total_;
113 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; 119 std::map<std::string, size_t> new_used_per_setting = used_per_setting_;
114 Allocate(key, value, &new_used_total, &new_used_per_setting); 120 Allocate(key, value, &new_used_total, &new_used_per_setting);
115 121
116 if (options != IGNORE_QUOTA) { 122 if (options != IGNORE_QUOTA) {
117 if (new_used_total > limits_.quota_bytes) { 123 if (new_used_total > limits_.quota_bytes) {
118 return QuotaExceededFor(TOTAL_BYTES); 124 return QuotaExceededFor(QUOTA_BYTES);
119 } 125 }
120 if (new_used_per_setting[key] > limits_.quota_bytes_per_setting) { 126 if (new_used_per_setting[key] > limits_.quota_bytes_per_item) {
121 return QuotaExceededFor(BYTES_PER_SETTING); 127 return QuotaExceededFor(QUOTA_BYTES_PER_ITEM);
122 } 128 }
123 if (new_used_per_setting.size() > limits_.max_keys) { 129 if (new_used_per_setting.size() > limits_.max_items) {
124 return QuotaExceededFor(KEY_COUNT); 130 return QuotaExceededFor(MAX_ITEMS);
125 } 131 }
126 } 132 }
127 133
128 WriteResult result = delegate_->Set(options, key, value); 134 WriteResult result = delegate_->Set(options, key, value);
129 if (result.HasError()) { 135 if (result.HasError()) {
130 return result; 136 return result;
131 } 137 }
132 138
133 used_total_ = new_used_total; 139 used_total_ = new_used_total;
134 used_per_setting_.swap(new_used_per_setting); 140 used_per_setting_.swap(new_used_per_setting);
135 return result; 141 return result;
136 } 142 }
137 143
138 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set( 144 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set(
139 WriteOptions options, const DictionaryValue& values) { 145 WriteOptions options, const DictionaryValue& values) {
140 size_t new_used_total = used_total_; 146 size_t new_used_total = used_total_;
141 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; 147 std::map<std::string, size_t> new_used_per_setting = used_per_setting_;
142 for (DictionaryValue::Iterator it(values); it.HasNext(); it.Advance()) { 148 for (DictionaryValue::Iterator it(values); it.HasNext(); it.Advance()) {
143 Allocate(it.key(), it.value(), &new_used_total, &new_used_per_setting); 149 Allocate(it.key(), it.value(), &new_used_total, &new_used_per_setting);
144 150
145 if (options != IGNORE_QUOTA && 151 if (options != IGNORE_QUOTA &&
146 new_used_per_setting[it.key()] > limits_.quota_bytes_per_setting) { 152 new_used_per_setting[it.key()] > limits_.quota_bytes_per_item) {
147 return QuotaExceededFor(BYTES_PER_SETTING); 153 return QuotaExceededFor(QUOTA_BYTES_PER_ITEM);
148 } 154 }
149 } 155 }
150 156
151 if (options != IGNORE_QUOTA) { 157 if (options != IGNORE_QUOTA) {
152 if (new_used_total > limits_.quota_bytes) { 158 if (new_used_total > limits_.quota_bytes) {
153 return QuotaExceededFor(TOTAL_BYTES); 159 return QuotaExceededFor(QUOTA_BYTES);
154 } 160 }
155 if (new_used_per_setting.size() > limits_.max_keys) { 161 if (new_used_per_setting.size() > limits_.max_items) {
156 return QuotaExceededFor(KEY_COUNT); 162 return QuotaExceededFor(MAX_ITEMS);
157 } 163 }
158 } 164 }
159 165
160 WriteResult result = delegate_->Set(options, values); 166 WriteResult result = delegate_->Set(options, values);
161 if (result.HasError()) { 167 if (result.HasError()) {
162 return result; 168 return result;
163 } 169 }
164 170
165 used_total_ = new_used_total; 171 used_total_ = new_used_total;
166 used_per_setting_ = new_used_per_setting; 172 used_per_setting_ = new_used_per_setting;
(...skipping 30 matching lines...) Expand all
197 return result; 203 return result;
198 } 204 }
199 205
200 while (!used_per_setting_.empty()) { 206 while (!used_per_setting_.empty()) {
201 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); 207 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first);
202 } 208 }
203 return result; 209 return result;
204 } 210 }
205 211
206 } // namespace extensions 212 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698