OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "third_party/libaddressinput/chromium/chrome_storage_impl.h" |
| 6 |
| 7 #include "base/prefs/writeable_pref_store.h" |
| 8 #include "base/values.h" |
| 9 |
| 10 ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore* store) |
| 11 : backing_store_(store), |
| 12 scoped_observer_(this) { |
| 13 scoped_observer_.Add(backing_store_); |
| 14 } |
| 15 |
| 16 ChromeStorageImpl::~ChromeStorageImpl() { |
| 17 // TODO(estade): this shouldn't be necessary. |
| 18 for (std::vector<Request*>::iterator iter = |
| 19 outstanding_requests_.begin(); |
| 20 iter != outstanding_requests_.end(); ++iter) { |
| 21 (*(*iter)->callback)(false, (*iter)->key, std::string()); |
| 22 } |
| 23 } |
| 24 |
| 25 void ChromeStorageImpl::Put(const std::string& key, const std::string& data) { |
| 26 backing_store_->SetValue(key, new base::StringValue(data)); |
| 27 } |
| 28 |
| 29 void ChromeStorageImpl::Get( |
| 30 const std::string& key, |
| 31 scoped_ptr<Storage::Callback> data_ready) const { |
| 32 // |Get()| should not be const, so this is just a thunk that fixes that. |
| 33 const_cast<ChromeStorageImpl*>(this)->DoGet(key, data_ready.Pass()); |
| 34 } |
| 35 |
| 36 void ChromeStorageImpl::OnPrefValueChanged(const std::string& key) {} |
| 37 |
| 38 void ChromeStorageImpl::OnInitializationCompleted(bool succeeded) { |
| 39 for (std::vector<Request*>::iterator iter = |
| 40 outstanding_requests_.begin(); |
| 41 iter != outstanding_requests_.end(); ++iter) { |
| 42 DoGet((*iter)->key, (*iter)->callback.Pass()); |
| 43 } |
| 44 |
| 45 outstanding_requests_.clear(); |
| 46 } |
| 47 |
| 48 void ChromeStorageImpl::DoGet( |
| 49 const std::string& key, |
| 50 scoped_ptr<Storage::Callback> data_ready) { |
| 51 if (!backing_store_->IsInitializationComplete()) { |
| 52 outstanding_requests_.push_back( |
| 53 new Request(key, data_ready.Pass())); |
| 54 return; |
| 55 } |
| 56 |
| 57 const base::Value* value; |
| 58 std::string result; |
| 59 if (backing_store_->GetValue(key, &value) && |
| 60 value->GetAsString(&result)) { |
| 61 (*data_ready)(true, key, result); |
| 62 } else { |
| 63 (*data_ready)(false, key, std::string()); |
| 64 } |
| 65 } |
| 66 |
| 67 ChromeStorageImpl::Request::Request(const std::string& key, |
| 68 scoped_ptr<Storage::Callback> callback) |
| 69 : key(key), |
| 70 callback(callback.Pass()) {} |
OLD | NEW |