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