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/memory/scoped_ptr.h"
8 #include "base/prefs/writeable_pref_store.h"
9 #include "base/values.h"
10 #include "third_party/libaddressinput/chromium/fallback_data_store.h"
14 ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore
* store
)
15 : backing_store_(store
),
16 scoped_observer_(this) {
17 scoped_observer_
.Add(backing_store_
);
20 ChromeStorageImpl::~ChromeStorageImpl() {}
22 void ChromeStorageImpl::Put(const std::string
& key
, std::string
* data
) {
24 scoped_ptr
<std::string
> owned_data(data
);
25 scoped_ptr
<base::StringValue
> string_value(
26 new base::StringValue(std::string()));
27 string_value
->GetString()->swap(*owned_data
);
28 backing_store_
->SetValue(key
, string_value
.release(),
29 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
32 void ChromeStorageImpl::Get(const std::string
& key
,
33 const Storage::Callback
& data_ready
) const {
34 // |Get()| should not be const, so this is just a thunk that fixes that.
35 const_cast<ChromeStorageImpl
*>(this)->DoGet(key
, data_ready
);
38 void ChromeStorageImpl::OnPrefValueChanged(const std::string
& key
) {}
40 void ChromeStorageImpl::OnInitializationCompleted(bool succeeded
) {
41 for (std::vector
<Request
*>::iterator iter
= outstanding_requests_
.begin();
42 iter
!= outstanding_requests_
.end(); ++iter
) {
43 DoGet((*iter
)->key
, (*iter
)->callback
);
46 outstanding_requests_
.clear();
49 void ChromeStorageImpl::DoGet(const std::string
& key
,
50 const Storage::Callback
& data_ready
) {
51 if (!backing_store_
->IsInitializationComplete()) {
52 outstanding_requests_
.push_back(new Request(key
, data_ready
));
56 const base::Value
* value
= NULL
;
57 scoped_ptr
<std::string
> data(new std::string
);
58 if (backing_store_
->GetValue(key
, &value
) && value
->GetAsString(data
.get())) {
59 data_ready(true, key
, data
.release());
60 } else if (FallbackDataStore::Get(key
, data
.get())) {
61 data_ready(true, key
, data
.release());
63 data_ready(false, key
, NULL
);
67 ChromeStorageImpl::Request::Request(const std::string
& key
,
68 const Callback
& callback
)
72 } // namespace autofill