| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "third_party/libaddressinput/chromium/chrome_storage_impl.h" | 5 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/prefs/writeable_pref_store.h" | 8 #include "base/prefs/writeable_pref_store.h" |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "third_party/libaddressinput/chromium/fallback_data_store.h" |
| 9 | 11 |
| 10 namespace autofill { | 12 namespace autofill { |
| 11 | 13 |
| 12 ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore* store) | 14 ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore* store) |
| 13 : backing_store_(store), | 15 : backing_store_(store), |
| 14 scoped_observer_(this) { | 16 scoped_observer_(this) { |
| 15 scoped_observer_.Add(backing_store_); | 17 scoped_observer_.Add(backing_store_); |
| 16 } | 18 } |
| 17 | 19 |
| 18 ChromeStorageImpl::~ChromeStorageImpl() {} | 20 ChromeStorageImpl::~ChromeStorageImpl() {} |
| 19 | 21 |
| 20 void ChromeStorageImpl::Put(const std::string& key, | 22 void ChromeStorageImpl::Put(const std::string& key, const std::string& data) { |
| 21 scoped_ptr<std::string> data) { | 23 scoped_ptr<base::StringValue> string_value(new base::StringValue(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()); | 24 backing_store_->SetValue(key, string_value.release()); |
| 26 } | 25 } |
| 27 | 26 |
| 28 void ChromeStorageImpl::Get( | 27 void ChromeStorageImpl::Get(const std::string& key, |
| 29 const std::string& key, | 28 const Storage::Callback& data_ready) const { |
| 30 scoped_ptr<Storage::Callback> data_ready) const { | 29 LOG(ERROR) << __FUNCTION__; |
| 31 // |Get()| should not be const, so this is just a thunk that fixes that. | 30 // |Get()| should not be const, so this is just a thunk that fixes that. |
| 32 const_cast<ChromeStorageImpl*>(this)->DoGet(key, data_ready.Pass()); | 31 const_cast<ChromeStorageImpl*>(this)->DoGet(key, data_ready); |
| 33 } | 32 } |
| 34 | 33 |
| 35 void ChromeStorageImpl::OnPrefValueChanged(const std::string& key) {} | 34 void ChromeStorageImpl::OnPrefValueChanged(const std::string& key) {} |
| 36 | 35 |
| 37 void ChromeStorageImpl::OnInitializationCompleted(bool succeeded) { | 36 void ChromeStorageImpl::OnInitializationCompleted(bool succeeded) { |
| 38 for (std::vector<Request*>::iterator iter = | 37 LOG(ERROR) << __FUNCTION__; |
| 39 outstanding_requests_.begin(); | 38 for (std::vector<Request*>::iterator iter = outstanding_requests_.begin(); |
| 40 iter != outstanding_requests_.end(); ++iter) { | 39 iter != outstanding_requests_.end(); ++iter) { |
| 41 DoGet((*iter)->key, (*iter)->callback.Pass()); | 40 DoGet((*iter)->key, (*iter)->callback); |
| 42 } | 41 } |
| 43 | 42 |
| 44 outstanding_requests_.clear(); | 43 outstanding_requests_.clear(); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void ChromeStorageImpl::DoGet( | 46 void ChromeStorageImpl::DoGet(const std::string& key, |
| 48 const std::string& key, | 47 const Storage::Callback& data_ready) { |
| 49 scoped_ptr<Storage::Callback> data_ready) { | 48 LOG(ERROR) << __FUNCTION__; |
| 50 if (!backing_store_->IsInitializationComplete()) { | 49 if (!backing_store_->IsInitializationComplete()) { |
| 51 outstanding_requests_.push_back( | 50 LOG(ERROR) << "Backing store initialization is not complete."; |
| 52 new Request(key, data_ready.Pass())); | 51 outstanding_requests_.push_back(new Request(key, data_ready)); |
| 53 return; | 52 return; |
| 54 } | 53 } |
| 55 | 54 |
| 56 const base::Value* value = NULL; | 55 const base::Value* value = NULL; |
| 57 const base::StringValue* string_value = NULL; | 56 const base::StringValue* string_value = NULL; |
| 57 std::string fallback_data; |
| 58 if (backing_store_->GetValue(key, &value) && | 58 if (backing_store_->GetValue(key, &value) && |
| 59 value->GetAsString(&string_value)) { | 59 value->GetAsString(&string_value)) { |
| 60 (*data_ready)(true, key, string_value->GetString()); | 60 LOG(ERROR) << "Found data in the store."; |
| 61 data_ready(true, key, string_value->GetString()); |
| 62 } else if (FallbackDataStore::Get(key, &fallback_data)) { |
| 63 LOG(ERROR) << "Found data in the fallback"; |
| 64 data_ready(true, key, fallback_data); |
| 61 } else { | 65 } else { |
| 62 (*data_ready)(false, key, std::string()); | 66 LOG(ERROR) << "Did not find any data anywhere."; |
| 67 data_ready(false, key, std::string()); |
| 63 } | 68 } |
| 64 } | 69 } |
| 65 | 70 |
| 66 ChromeStorageImpl::Request::Request(const std::string& key, | 71 ChromeStorageImpl::Request::Request(const std::string& key, |
| 67 scoped_ptr<Storage::Callback> callback) | 72 const Callback& callback) |
| 68 : key(key), | 73 : key(key), |
| 69 callback(callback.Pass()) {} | 74 callback(callback) {} |
| 70 | 75 |
| 71 } // namespace autofill | 76 } // namespace autofill |
| OLD | NEW |