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