| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 } | 66 } |
| 67 | 67 |
| 68 void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) { | 68 void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) { |
| 69 bool success = RunWithStorage(storage); | 69 bool success = RunWithStorage(storage); |
| 70 BrowserThread::PostTask( | 70 BrowserThread::PostTask( |
| 71 BrowserThread::UI, | 71 BrowserThread::UI, |
| 72 FROM_HERE, | 72 FROM_HERE, |
| 73 base::Bind(&SettingsFunction::SendResponse, this, success)); | 73 base::Bind(&SettingsFunction::SendResponse, this, success)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool SettingsFunction::UseReadResult(ValueStore::ReadResult read_result) { | 76 bool SettingsFunction::UseReadResult(ValueStore::ReadResult result, |
| 77 if (read_result->HasError()) { | 77 ValueStore* storage) { |
| 78 error_ = read_result->error().message; | 78 if (result->HasError()) |
| 79 return false; | 79 return HandleError(result->error(), storage); |
| 80 } | |
| 81 | 80 |
| 82 base::DictionaryValue* result = new base::DictionaryValue(); | 81 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 83 result->Swap(&read_result->settings()); | 82 dict->Swap(&result->settings()); |
| 84 SetResult(result); | 83 SetResult(dict); |
| 85 return true; | 84 return true; |
| 86 } | 85 } |
| 87 | 86 |
| 88 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result) { | 87 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result, |
| 89 if (result->HasError()) { | 88 ValueStore* storage) { |
| 90 error_ = result->error().message; | 89 if (result->HasError()) |
| 91 return false; | 90 return HandleError(result->error(), storage); |
| 92 } | |
| 93 | 91 |
| 94 if (!result->changes().empty()) { | 92 if (!result->changes().empty()) { |
| 95 observers_->Notify( | 93 observers_->Notify( |
| 96 &SettingsObserver::OnSettingsChanged, | 94 &SettingsObserver::OnSettingsChanged, |
| 97 extension_id(), | 95 extension_id(), |
| 98 settings_namespace_, | 96 settings_namespace_, |
| 99 ValueStoreChange::ToJson(result->changes())); | 97 ValueStoreChange::ToJson(result->changes())); |
| 100 } | 98 } |
| 101 | 99 |
| 102 return true; | 100 return true; |
| 103 } | 101 } |
| 104 | 102 |
| 103 bool SettingsFunction::HandleError(const ValueStore::Error& error, |
| 104 ValueStore* storage) { |
| 105 // If the method failed due to corruption, and we haven't tried to fix it, we |
| 106 // can try to restore the storage and re-run it. Otherwise, the method has |
| 107 // failed. |
| 108 if (error.code == ValueStore::CORRUPTION && !tried_restoring_storage_) { |
| 109 tried_restoring_storage_ = true; |
| 110 |
| 111 // If the corruption is on a particular key, try to restore that key and |
| 112 // re-run. |
| 113 if (error.key.get() && storage->RestoreKey(*error.key)) |
| 114 return RunWithStorage(storage); |
| 115 |
| 116 // If the full database is corrupted, try to restore the whole thing and |
| 117 // re-run. |
| 118 if (storage->Restore()) |
| 119 return RunWithStorage(storage); |
| 120 } |
| 121 |
| 122 error_ = error.message; |
| 123 return false; |
| 124 } |
| 125 |
| 105 // Concrete settings functions | 126 // Concrete settings functions |
| 106 | 127 |
| 107 namespace { | 128 namespace { |
| 108 | 129 |
| 109 // Adds all StringValues from a ListValue to a vector of strings. | 130 // Adds all StringValues from a ListValue to a vector of strings. |
| 110 void AddAllStringValues(const base::ListValue& from, | 131 void AddAllStringValues(const base::ListValue& from, |
| 111 std::vector<std::string>* to) { | 132 std::vector<std::string>* to) { |
| 112 DCHECK(to->empty()); | 133 DCHECK(to->empty()); |
| 113 std::string as_string; | 134 std::string as_string; |
| 114 for (base::ListValue::const_iterator it = from.begin(); | 135 for (base::ListValue::const_iterator it = from.begin(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 }; | 175 }; |
| 155 | 176 |
| 156 } // namespace | 177 } // namespace |
| 157 | 178 |
| 158 bool StorageStorageAreaGetFunction::RunWithStorage(ValueStore* storage) { | 179 bool StorageStorageAreaGetFunction::RunWithStorage(ValueStore* storage) { |
| 159 base::Value* input = NULL; | 180 base::Value* input = NULL; |
| 160 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 181 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 161 | 182 |
| 162 switch (input->GetType()) { | 183 switch (input->GetType()) { |
| 163 case base::Value::TYPE_NULL: | 184 case base::Value::TYPE_NULL: |
| 164 return UseReadResult(storage->Get()); | 185 return UseReadResult(storage->Get(), storage); |
| 165 | 186 |
| 166 case base::Value::TYPE_STRING: { | 187 case base::Value::TYPE_STRING: { |
| 167 std::string as_string; | 188 std::string as_string; |
| 168 input->GetAsString(&as_string); | 189 input->GetAsString(&as_string); |
| 169 return UseReadResult(storage->Get(as_string)); | 190 return UseReadResult(storage->Get(as_string), storage); |
| 170 } | 191 } |
| 171 | 192 |
| 172 case base::Value::TYPE_LIST: { | 193 case base::Value::TYPE_LIST: { |
| 173 std::vector<std::string> as_string_list; | 194 std::vector<std::string> as_string_list; |
| 174 AddAllStringValues(*static_cast<base::ListValue*>(input), | 195 AddAllStringValues(*static_cast<base::ListValue*>(input), |
| 175 &as_string_list); | 196 &as_string_list); |
| 176 return UseReadResult(storage->Get(as_string_list)); | 197 return UseReadResult(storage->Get(as_string_list), storage); |
| 177 } | 198 } |
| 178 | 199 |
| 179 case base::Value::TYPE_DICTIONARY: { | 200 case base::Value::TYPE_DICTIONARY: { |
| 180 base::DictionaryValue* as_dict = static_cast<base::DictionaryValue*>(input
); | 201 base::DictionaryValue* as_dict = |
| 202 static_cast<base::DictionaryValue*>(input); |
| 181 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict)); | 203 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict)); |
| 182 if (result->HasError()) { | 204 if (result->HasError()) { |
| 183 return UseReadResult(result.Pass()); | 205 return UseReadResult(result.Pass(), storage); |
| 184 } | 206 } |
| 185 | 207 |
| 186 base::DictionaryValue* with_default_values = as_dict->DeepCopy(); | 208 base::DictionaryValue* with_default_values = as_dict->DeepCopy(); |
| 187 with_default_values->MergeDictionary(&result->settings()); | 209 with_default_values->MergeDictionary(&result->settings()); |
| 188 return UseReadResult( | 210 return UseReadResult( |
| 189 ValueStore::MakeReadResult(make_scoped_ptr(with_default_values))); | 211 ValueStore::MakeReadResult(make_scoped_ptr(with_default_values)), |
| 212 storage); |
| 190 } | 213 } |
| 191 | 214 |
| 192 default: | 215 default: |
| 193 EXTENSION_FUNCTION_VALIDATE(false); | 216 EXTENSION_FUNCTION_VALIDATE(false); |
| 194 return false; | 217 return false; |
| 195 } | 218 } |
| 196 } | 219 } |
| 197 | 220 |
| 198 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage( | 221 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage( |
| 199 ValueStore* storage) { | 222 ValueStore* storage) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 227 return false; | 250 return false; |
| 228 } | 251 } |
| 229 | 252 |
| 230 SetResult(new base::FundamentalValue(static_cast<int>(bytes_in_use))); | 253 SetResult(new base::FundamentalValue(static_cast<int>(bytes_in_use))); |
| 231 return true; | 254 return true; |
| 232 } | 255 } |
| 233 | 256 |
| 234 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) { | 257 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) { |
| 235 base::DictionaryValue* input = NULL; | 258 base::DictionaryValue* input = NULL; |
| 236 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 259 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 237 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input)); | 260 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input), storage); |
| 238 } | 261 } |
| 239 | 262 |
| 240 void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics( | 263 void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics( |
| 241 QuotaLimitHeuristics* heuristics) const { | 264 QuotaLimitHeuristics* heuristics) const { |
| 242 GetModificationQuotaLimitHeuristics(heuristics); | 265 GetModificationQuotaLimitHeuristics(heuristics); |
| 243 } | 266 } |
| 244 | 267 |
| 245 bool StorageStorageAreaRemoveFunction::RunWithStorage(ValueStore* storage) { | 268 bool StorageStorageAreaRemoveFunction::RunWithStorage(ValueStore* storage) { |
| 246 base::Value* input = NULL; | 269 base::Value* input = NULL; |
| 247 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 270 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 248 | 271 |
| 249 switch (input->GetType()) { | 272 switch (input->GetType()) { |
| 250 case base::Value::TYPE_STRING: { | 273 case base::Value::TYPE_STRING: { |
| 251 std::string as_string; | 274 std::string as_string; |
| 252 input->GetAsString(&as_string); | 275 input->GetAsString(&as_string); |
| 253 return UseWriteResult(storage->Remove(as_string)); | 276 return UseWriteResult(storage->Remove(as_string), storage); |
| 254 } | 277 } |
| 255 | 278 |
| 256 case base::Value::TYPE_LIST: { | 279 case base::Value::TYPE_LIST: { |
| 257 std::vector<std::string> as_string_list; | 280 std::vector<std::string> as_string_list; |
| 258 AddAllStringValues(*static_cast<base::ListValue*>(input), | 281 AddAllStringValues(*static_cast<base::ListValue*>(input), |
| 259 &as_string_list); | 282 &as_string_list); |
| 260 return UseWriteResult(storage->Remove(as_string_list)); | 283 return UseWriteResult(storage->Remove(as_string_list), storage); |
| 261 } | 284 } |
| 262 | 285 |
| 263 default: | 286 default: |
| 264 EXTENSION_FUNCTION_VALIDATE(false); | 287 EXTENSION_FUNCTION_VALIDATE(false); |
| 265 return false; | 288 return false; |
| 266 }; | 289 }; |
| 267 } | 290 } |
| 268 | 291 |
| 269 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics( | 292 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics( |
| 270 QuotaLimitHeuristics* heuristics) const { | 293 QuotaLimitHeuristics* heuristics) const { |
| 271 GetModificationQuotaLimitHeuristics(heuristics); | 294 GetModificationQuotaLimitHeuristics(heuristics); |
| 272 } | 295 } |
| 273 | 296 |
| 274 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) { | 297 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) { |
| 275 return UseWriteResult(storage->Clear()); | 298 return UseWriteResult(storage->Clear(), storage); |
| 276 } | 299 } |
| 277 | 300 |
| 278 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics( | 301 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics( |
| 279 QuotaLimitHeuristics* heuristics) const { | 302 QuotaLimitHeuristics* heuristics) const { |
| 280 GetModificationQuotaLimitHeuristics(heuristics); | 303 GetModificationQuotaLimitHeuristics(heuristics); |
| 281 } | 304 } |
| 282 | 305 |
| 283 } // namespace extensions | 306 } // namespace extensions |
| OLD | NEW |