| 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/storage_api.h" | 5 #include "chrome/browser/extensions/api/storage/storage_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 | 107 |
| 108 return true; | 108 return true; |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Concrete settings functions | 111 // Concrete settings functions |
| 112 | 112 |
| 113 namespace { | 113 namespace { |
| 114 | 114 |
| 115 // Adds all StringValues from a ListValue to a vector of strings. | 115 // Adds all StringValues from a ListValue to a vector of strings. |
| 116 void AddAllStringValues(const ListValue& from, std::vector<std::string>* to) { | 116 void AddAllStringValues(const base::ListValue& from, |
| 117 std::vector<std::string>* to) { |
| 117 DCHECK(to->empty()); | 118 DCHECK(to->empty()); |
| 118 std::string as_string; | 119 std::string as_string; |
| 119 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { | 120 for (base::ListValue::const_iterator it = from.begin(); |
| 121 it != from.end(); ++it) { |
| 120 if ((*it)->GetAsString(&as_string)) { | 122 if ((*it)->GetAsString(&as_string)) { |
| 121 to->push_back(as_string); | 123 to->push_back(as_string); |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 } | 126 } |
| 125 | 127 |
| 126 // Gets the keys of a DictionaryValue. | 128 // Gets the keys of a DictionaryValue. |
| 127 std::vector<std::string> GetKeys(const DictionaryValue& dict) { | 129 std::vector<std::string> GetKeys(const base::DictionaryValue& dict) { |
| 128 std::vector<std::string> keys; | 130 std::vector<std::string> keys; |
| 129 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 131 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 130 keys.push_back(it.key()); | 132 keys.push_back(it.key()); |
| 131 } | 133 } |
| 132 return keys; | 134 return keys; |
| 133 } | 135 } |
| 134 | 136 |
| 135 // Creates quota heuristics for settings modification. | 137 // Creates quota heuristics for settings modification. |
| 136 void GetModificationQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) { | 138 void GetModificationQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) { |
| 137 QuotaLimitHeuristic::Config longLimitConfig = { | 139 QuotaLimitHeuristic::Config longLimitConfig = { |
| 138 // See storage.json for current value. | 140 // See storage.json for current value. |
| 139 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR, | 141 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 155 new ExtensionsQuotaService::SustainedLimit( | 157 new ExtensionsQuotaService::SustainedLimit( |
| 156 base::TimeDelta::FromMinutes(10), | 158 base::TimeDelta::FromMinutes(10), |
| 157 shortLimitConfig, | 159 shortLimitConfig, |
| 158 new QuotaLimitHeuristic::SingletonBucketMapper(), | 160 new QuotaLimitHeuristic::SingletonBucketMapper(), |
| 159 "MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE")); | 161 "MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE")); |
| 160 }; | 162 }; |
| 161 | 163 |
| 162 } // namespace | 164 } // namespace |
| 163 | 165 |
| 164 bool StorageStorageAreaGetFunction::RunWithStorage(ValueStore* storage) { | 166 bool StorageStorageAreaGetFunction::RunWithStorage(ValueStore* storage) { |
| 165 Value* input = NULL; | 167 base::Value* input = NULL; |
| 166 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 168 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 167 | 169 |
| 168 switch (input->GetType()) { | 170 switch (input->GetType()) { |
| 169 case Value::TYPE_NULL: | 171 case base::Value::TYPE_NULL: |
| 170 return UseReadResult(storage->Get()); | 172 return UseReadResult(storage->Get()); |
| 171 | 173 |
| 172 case Value::TYPE_STRING: { | 174 case base::Value::TYPE_STRING: { |
| 173 std::string as_string; | 175 std::string as_string; |
| 174 input->GetAsString(&as_string); | 176 input->GetAsString(&as_string); |
| 175 return UseReadResult(storage->Get(as_string)); | 177 return UseReadResult(storage->Get(as_string)); |
| 176 } | 178 } |
| 177 | 179 |
| 178 case Value::TYPE_LIST: { | 180 case base::Value::TYPE_LIST: { |
| 179 std::vector<std::string> as_string_list; | 181 std::vector<std::string> as_string_list; |
| 180 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 182 AddAllStringValues(*static_cast<base::ListValue*>(input), |
| 183 &as_string_list); |
| 181 return UseReadResult(storage->Get(as_string_list)); | 184 return UseReadResult(storage->Get(as_string_list)); |
| 182 } | 185 } |
| 183 | 186 |
| 184 case Value::TYPE_DICTIONARY: { | 187 case base::Value::TYPE_DICTIONARY: { |
| 185 DictionaryValue* as_dict = static_cast<DictionaryValue*>(input); | 188 base::DictionaryValue* as_dict = static_cast<base::DictionaryValue*>(input
); |
| 186 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict)); | 189 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict)); |
| 187 if (result->HasError()) { | 190 if (result->HasError()) { |
| 188 return UseReadResult(result.Pass()); | 191 return UseReadResult(result.Pass()); |
| 189 } | 192 } |
| 190 | 193 |
| 191 DictionaryValue* with_default_values = as_dict->DeepCopy(); | 194 base::DictionaryValue* with_default_values = as_dict->DeepCopy(); |
| 192 with_default_values->MergeDictionary(result->settings().get()); | 195 with_default_values->MergeDictionary(result->settings().get()); |
| 193 return UseReadResult( | 196 return UseReadResult( |
| 194 ValueStore::MakeReadResult(with_default_values)); | 197 ValueStore::MakeReadResult(with_default_values)); |
| 195 } | 198 } |
| 196 | 199 |
| 197 default: | 200 default: |
| 198 return UseReadResult( | 201 return UseReadResult( |
| 199 ValueStore::MakeReadResult(kUnsupportedArgumentType)); | 202 ValueStore::MakeReadResult(kUnsupportedArgumentType)); |
| 200 } | 203 } |
| 201 } | 204 } |
| 202 | 205 |
| 203 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage( | 206 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage( |
| 204 ValueStore* storage) { | 207 ValueStore* storage) { |
| 205 Value* input = NULL; | 208 base::Value* input = NULL; |
| 206 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 209 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 207 | 210 |
| 208 size_t bytes_in_use = 0; | 211 size_t bytes_in_use = 0; |
| 209 | 212 |
| 210 switch (input->GetType()) { | 213 switch (input->GetType()) { |
| 211 case Value::TYPE_NULL: | 214 case base::Value::TYPE_NULL: |
| 212 bytes_in_use = storage->GetBytesInUse(); | 215 bytes_in_use = storage->GetBytesInUse(); |
| 213 break; | 216 break; |
| 214 | 217 |
| 215 case Value::TYPE_STRING: { | 218 case base::Value::TYPE_STRING: { |
| 216 std::string as_string; | 219 std::string as_string; |
| 217 input->GetAsString(&as_string); | 220 input->GetAsString(&as_string); |
| 218 bytes_in_use = storage->GetBytesInUse(as_string); | 221 bytes_in_use = storage->GetBytesInUse(as_string); |
| 219 break; | 222 break; |
| 220 } | 223 } |
| 221 | 224 |
| 222 case Value::TYPE_LIST: { | 225 case base::Value::TYPE_LIST: { |
| 223 std::vector<std::string> as_string_list; | 226 std::vector<std::string> as_string_list; |
| 224 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 227 AddAllStringValues(*static_cast<base::ListValue*>(input), |
| 228 &as_string_list); |
| 225 bytes_in_use = storage->GetBytesInUse(as_string_list); | 229 bytes_in_use = storage->GetBytesInUse(as_string_list); |
| 226 break; | 230 break; |
| 227 } | 231 } |
| 228 | 232 |
| 229 default: | 233 default: |
| 230 error_ = kUnsupportedArgumentType; | 234 error_ = kUnsupportedArgumentType; |
| 231 return false; | 235 return false; |
| 232 } | 236 } |
| 233 | 237 |
| 234 SetResult(Value::CreateIntegerValue(bytes_in_use)); | 238 SetResult(base::Value::CreateIntegerValue(bytes_in_use)); |
| 235 return true; | 239 return true; |
| 236 } | 240 } |
| 237 | 241 |
| 238 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) { | 242 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) { |
| 239 DictionaryValue* input = NULL; | 243 base::DictionaryValue* input = NULL; |
| 240 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 244 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 241 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input)); | 245 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input)); |
| 242 } | 246 } |
| 243 | 247 |
| 244 void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics( | 248 void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics( |
| 245 QuotaLimitHeuristics* heuristics) const { | 249 QuotaLimitHeuristics* heuristics) const { |
| 246 GetModificationQuotaLimitHeuristics(heuristics); | 250 GetModificationQuotaLimitHeuristics(heuristics); |
| 247 } | 251 } |
| 248 | 252 |
| 249 bool StorageStorageAreaRemoveFunction::RunWithStorage(ValueStore* storage) { | 253 bool StorageStorageAreaRemoveFunction::RunWithStorage(ValueStore* storage) { |
| 250 Value* input = NULL; | 254 base::Value* input = NULL; |
| 251 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 255 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 252 | 256 |
| 253 switch (input->GetType()) { | 257 switch (input->GetType()) { |
| 254 case Value::TYPE_STRING: { | 258 case base::Value::TYPE_STRING: { |
| 255 std::string as_string; | 259 std::string as_string; |
| 256 input->GetAsString(&as_string); | 260 input->GetAsString(&as_string); |
| 257 return UseWriteResult(storage->Remove(as_string)); | 261 return UseWriteResult(storage->Remove(as_string)); |
| 258 } | 262 } |
| 259 | 263 |
| 260 case Value::TYPE_LIST: { | 264 case base::Value::TYPE_LIST: { |
| 261 std::vector<std::string> as_string_list; | 265 std::vector<std::string> as_string_list; |
| 262 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 266 AddAllStringValues(*static_cast<base::ListValue*>(input), |
| 267 &as_string_list); |
| 263 return UseWriteResult(storage->Remove(as_string_list)); | 268 return UseWriteResult(storage->Remove(as_string_list)); |
| 264 } | 269 } |
| 265 | 270 |
| 266 default: | 271 default: |
| 267 return UseWriteResult( | 272 return UseWriteResult( |
| 268 ValueStore::MakeWriteResult(kUnsupportedArgumentType)); | 273 ValueStore::MakeWriteResult(kUnsupportedArgumentType)); |
| 269 }; | 274 }; |
| 270 } | 275 } |
| 271 | 276 |
| 272 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics( | 277 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics( |
| 273 QuotaLimitHeuristics* heuristics) const { | 278 QuotaLimitHeuristics* heuristics) const { |
| 274 GetModificationQuotaLimitHeuristics(heuristics); | 279 GetModificationQuotaLimitHeuristics(heuristics); |
| 275 } | 280 } |
| 276 | 281 |
| 277 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) { | 282 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) { |
| 278 return UseWriteResult(storage->Clear()); | 283 return UseWriteResult(storage->Clear()); |
| 279 } | 284 } |
| 280 | 285 |
| 281 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics( | 286 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics( |
| 282 QuotaLimitHeuristics* heuristics) const { | 287 QuotaLimitHeuristics* heuristics) const { |
| 283 GetModificationQuotaLimitHeuristics(heuristics); | 288 GetModificationQuotaLimitHeuristics(heuristics); |
| 284 } | 289 } |
| 285 | 290 |
| 286 } // namespace extensions | 291 } // namespace extensions |
| OLD | NEW |