1 // Copyright 2014 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.
5 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
7 #include "base/prefs/writeable_pref_store.h"
8 #include "base/values.h"
12 ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore* store)
13 : backing_store_(store),
14 scoped_observer_(this) {
15 scoped_observer_.Add(backing_store_);
18 ChromeStorageImpl::~ChromeStorageImpl() {}
20 void ChromeStorageImpl::Put(const std::string& key,
21 scoped_ptr<std::string> data) {
22 scoped_ptr<base::StringValue> string_value(
23 new base::StringValue(std::string()));
24 string_value->GetString()->swap(*data);
25 backing_store_->SetValue(key, string_value.release());
28 void ChromeStorageImpl::Get(
29 const std::string& key,
30 scoped_ptr<Storage::Callback> data_ready) const {
31 // |Get()| should not be const, so this is just a thunk that fixes that.
32 const_cast<ChromeStorageImpl*>(this)->DoGet(key, data_ready.Pass());
35 void ChromeStorageImpl::OnPrefValueChanged(const std::string& key) {}
37 void ChromeStorageImpl::OnInitializationCompleted(bool succeeded) {
38 for (std::vector<Request*>::iterator iter =
39 outstanding_requests_.begin();
40 iter != outstanding_requests_.end(); ++iter) {
41 DoGet((*iter)->key, (*iter)->callback.Pass());
44 outstanding_requests_.clear();
47 void ChromeStorageImpl::DoGet(
48 const std::string& key,
49 scoped_ptr<Storage::Callback> data_ready) {
50 if (!backing_store_->IsInitializationComplete()) {
51 outstanding_requests_.push_back(
52 new Request(key, data_ready.Pass()));
56 const base::Value* value = NULL;
57 const base::StringValue* string_value = NULL;
58 if (backing_store_->GetValue(key, &value) &&
59 value->GetAsString(&string_value)) {
60 (*data_ready)(true, key, string_value->GetString());
62 (*data_ready)(false, key, std::string());
66 ChromeStorageImpl::Request::Request(const std::string& key,
67 scoped_ptr<Storage::Callback> callback)
69 callback(callback.Pass()) {}
71 } // namespace autofill