Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/api/storage/settings_storage_quota_enforcer. h" | 5 #include "chrome/browser/extensions/api/storage/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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/value_store/value_store_util.h" | 14 #include "chrome/browser/value_store/value_store_util.h" |
| 14 #include "extensions/common/extension_api.h" | 15 #include "extensions/common/extension_api.h" |
| 15 | 16 |
| 16 namespace util = value_store_util; | 17 namespace util = value_store_util; |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 ValueStore::QUOTA_EXCEEDED, | 81 ValueStore::QUOTA_EXCEEDED, |
| 81 base::StringPrintf("%s quota exceeded", name), | 82 base::StringPrintf("%s quota exceeded", name), |
| 82 key.Pass())); | 83 key.Pass())); |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace | 86 } // namespace |
| 86 | 87 |
| 87 SettingsStorageQuotaEnforcer::SettingsStorageQuotaEnforcer( | 88 SettingsStorageQuotaEnforcer::SettingsStorageQuotaEnforcer( |
| 88 const Limits& limits, ValueStore* delegate) | 89 const Limits& limits, ValueStore* delegate) |
| 89 : limits_(limits), delegate_(delegate), used_total_(0) { | 90 : limits_(limits), delegate_(delegate), used_total_(0) { |
| 90 ReadResult maybe_settings = delegate_->Get(); | 91 CalculateUsage(); |
| 91 if (maybe_settings->HasError()) { | |
| 92 LOG(WARNING) << "Failed to get initial settings for quota: " << | |
| 93 maybe_settings->error().message; | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 for (base::DictionaryValue::Iterator it(maybe_settings->settings()); | |
| 98 !it.IsAtEnd(); it.Advance()) { | |
| 99 Allocate(it.key(), it.value(), &used_total_, &used_per_setting_); | |
| 100 } | |
| 101 } | 92 } |
| 102 | 93 |
| 103 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() {} | 94 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() {} |
| 104 | 95 |
| 105 size_t SettingsStorageQuotaEnforcer::GetBytesInUse(const std::string& key) { | 96 size_t SettingsStorageQuotaEnforcer::GetBytesInUse(const std::string& key) { |
| 106 std::map<std::string, size_t>::iterator maybe_used = | 97 std::map<std::string, size_t>::iterator maybe_used = |
| 107 used_per_setting_.find(key); | 98 used_per_setting_.find(key); |
| 108 return maybe_used == used_per_setting_.end() ? 0u : maybe_used->second; | 99 return maybe_used == used_per_setting_.end() ? 0u : maybe_used->second; |
| 109 } | 100 } |
| 110 | 101 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 } | 213 } |
| 223 return result.Pass(); | 214 return result.Pass(); |
| 224 } | 215 } |
| 225 | 216 |
| 226 ValueStore::WriteResult SettingsStorageQuotaEnforcer::Clear() { | 217 ValueStore::WriteResult SettingsStorageQuotaEnforcer::Clear() { |
| 227 WriteResult result = delegate_->Clear(); | 218 WriteResult result = delegate_->Clear(); |
| 228 if (result->HasError()) { | 219 if (result->HasError()) { |
| 229 return result.Pass(); | 220 return result.Pass(); |
| 230 } | 221 } |
| 231 | 222 |
| 232 while (!used_per_setting_.empty()) { | 223 used_per_setting_.clear(); |
| 233 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); | 224 used_total_ = 0; |
| 234 } | |
| 235 return result.Pass(); | 225 return result.Pass(); |
| 236 } | 226 } |
| 237 | 227 |
| 228 bool SettingsStorageQuotaEnforcer::Restore() { | |
| 229 if (delegate_->Restore()) { | |
| 230 // If we failed, we can't calculate the usage. | |
|
not at google - send to devlin
2014/02/14 19:35:56
nit: comment seems like it's in the wrong place, b
Devlin
2014/02/18 23:55:22
Done.
| |
| 231 CalculateUsage(); | |
| 232 return true; | |
| 233 } | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 237 bool SettingsStorageQuotaEnforcer::RestoreKey(const std::string& key) { | |
| 238 if (delegate_->RestoreKey(key)) { | |
|
not at google - send to devlin
2014/02/14 19:35:56
nit: this would also look neater if you reversed t
Devlin
2014/02/18 23:55:22
Done.
| |
| 239 ReadResult result = Get(key); | |
| 240 // If the key was deleted as a result of the Restore() call, free it. | |
| 241 if (!result->settings().HasKey(key) && | |
| 242 ContainsKey(used_per_setting_, key)) { | |
| 243 Free(&used_total_, &used_per_setting_, key); | |
| 244 } | |
| 245 return true; | |
| 246 } | |
| 247 return false; | |
| 248 } | |
| 249 | |
| 250 void SettingsStorageQuotaEnforcer::CalculateUsage() { | |
| 251 ReadResult maybe_settings = delegate_->Get(); | |
| 252 if (maybe_settings->HasError()) { | |
| 253 LOG(WARNING) << "Failed to get initial settings for quota: " | |
| 254 << maybe_settings->error().message; | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 for (base::DictionaryValue::Iterator it(maybe_settings->settings()); | |
| 259 !it.IsAtEnd(); | |
| 260 it.Advance()) { | |
| 261 Allocate(it.key(), it.value(), &used_total_, &used_per_setting_); | |
| 262 } | |
| 263 } | |
| 264 | |
| 238 } // namespace extensions | 265 } // namespace extensions |
| OLD | NEW |