OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "ui/app_list/search/history_data_store.h" | 5 #include "ui/app_list/search/history_data_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
14 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
| 15 #include "base/memory/ptr_util.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 | 18 |
18 namespace app_list { | 19 namespace app_list { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const char kKeyVersion[] = "version"; | 23 const char kKeyVersion[] = "version"; |
23 const char kCurrentVersion[] = "1"; | 24 const char kCurrentVersion[] = "1"; |
24 | 25 |
(...skipping 27 matching lines...) Expand all Loading... |
52 // "s" : [ | 53 // "s" : [ |
53 // "result id of 1st (oldest) secondary association", | 54 // "result id of 1st (oldest) secondary association", |
54 // ... | 55 // ... |
55 // "result id of the newest secondary association" | 56 // "result id of the newest secondary association" |
56 // ], | 57 // ], |
57 // "t" : "last_update_timestamp" | 58 // "t" : "last_update_timestamp" |
58 // }, | 59 // }, |
59 // ... | 60 // ... |
60 // } | 61 // } |
61 // } | 62 // } |
62 scoped_ptr<HistoryData::Associations> Parse( | 63 std::unique_ptr<HistoryData::Associations> Parse( |
63 scoped_ptr<base::DictionaryValue> dict) { | 64 std::unique_ptr<base::DictionaryValue> dict) { |
64 std::string version; | 65 std::string version; |
65 if (!dict->GetStringWithoutPathExpansion(kKeyVersion, &version) || | 66 if (!dict->GetStringWithoutPathExpansion(kKeyVersion, &version) || |
66 version != kCurrentVersion) { | 67 version != kCurrentVersion) { |
67 return nullptr; | 68 return nullptr; |
68 } | 69 } |
69 | 70 |
70 const base::DictionaryValue* assoc_dict = NULL; | 71 const base::DictionaryValue* assoc_dict = NULL; |
71 if (!dict->GetDictionaryWithoutPathExpansion(kKeyAssociations, &assoc_dict) || | 72 if (!dict->GetDictionaryWithoutPathExpansion(kKeyAssociations, &assoc_dict) || |
72 !assoc_dict) { | 73 !assoc_dict) { |
73 return nullptr; | 74 return nullptr; |
74 } | 75 } |
75 | 76 |
76 scoped_ptr<HistoryData::Associations> data(new HistoryData::Associations); | 77 std::unique_ptr<HistoryData::Associations> data( |
| 78 new HistoryData::Associations); |
77 for (base::DictionaryValue::Iterator it(*assoc_dict); !it.IsAtEnd(); | 79 for (base::DictionaryValue::Iterator it(*assoc_dict); !it.IsAtEnd(); |
78 it.Advance()) { | 80 it.Advance()) { |
79 const base::DictionaryValue* entry_dict = NULL; | 81 const base::DictionaryValue* entry_dict = NULL; |
80 if (!it.value().GetAsDictionary(&entry_dict)) | 82 if (!it.value().GetAsDictionary(&entry_dict)) |
81 continue; | 83 continue; |
82 | 84 |
83 std::string primary; | 85 std::string primary; |
84 std::string update_time_string; | 86 std::string update_time_string; |
85 if (!entry_dict->GetStringWithoutPathExpansion(kKeyPrimary, &primary) || | 87 if (!entry_dict->GetStringWithoutPathExpansion(kKeyPrimary, &primary) || |
86 !entry_dict->GetStringWithoutPathExpansion(kKeyUpdateTime, | 88 !entry_dict->GetStringWithoutPathExpansion(kKeyUpdateTime, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 on_flushed.Run(); | 139 on_flushed.Run(); |
138 } | 140 } |
139 | 141 |
140 void HistoryDataStore::Load( | 142 void HistoryDataStore::Load( |
141 const HistoryDataStore::OnLoadedCallback& on_loaded) { | 143 const HistoryDataStore::OnLoadedCallback& on_loaded) { |
142 if (data_store_.get()) { | 144 if (data_store_.get()) { |
143 data_store_->Load(base::Bind( | 145 data_store_->Load(base::Bind( |
144 &HistoryDataStore::OnDictionaryLoadedCallback, this, on_loaded)); | 146 &HistoryDataStore::OnDictionaryLoadedCallback, this, on_loaded)); |
145 } else { | 147 } else { |
146 OnDictionaryLoadedCallback(on_loaded, | 148 OnDictionaryLoadedCallback(on_loaded, |
147 make_scoped_ptr(cached_dict_->DeepCopy())); | 149 base::WrapUnique(cached_dict_->DeepCopy())); |
148 } | 150 } |
149 } | 151 } |
150 | 152 |
151 void HistoryDataStore::SetPrimary(const std::string& query, | 153 void HistoryDataStore::SetPrimary(const std::string& query, |
152 const std::string& result) { | 154 const std::string& result) { |
153 base::DictionaryValue* entry_dict = GetEntryDict(query); | 155 base::DictionaryValue* entry_dict = GetEntryDict(query); |
154 entry_dict->SetWithoutPathExpansion(kKeyPrimary, | 156 entry_dict->SetWithoutPathExpansion(kKeyPrimary, |
155 new base::StringValue(result)); | 157 new base::StringValue(result)); |
156 if (data_store_.get()) | 158 if (data_store_.get()) |
157 data_store_->ScheduleWrite(); | 159 data_store_->ScheduleWrite(); |
158 } | 160 } |
159 | 161 |
160 void HistoryDataStore::SetSecondary( | 162 void HistoryDataStore::SetSecondary( |
161 const std::string& query, | 163 const std::string& query, |
162 const HistoryData::SecondaryDeque& results) { | 164 const HistoryData::SecondaryDeque& results) { |
163 scoped_ptr<base::ListValue> results_list(new base::ListValue); | 165 std::unique_ptr<base::ListValue> results_list(new base::ListValue); |
164 for (size_t i = 0; i < results.size(); ++i) | 166 for (size_t i = 0; i < results.size(); ++i) |
165 results_list->AppendString(results[i]); | 167 results_list->AppendString(results[i]); |
166 | 168 |
167 base::DictionaryValue* entry_dict = GetEntryDict(query); | 169 base::DictionaryValue* entry_dict = GetEntryDict(query); |
168 entry_dict->SetWithoutPathExpansion(kKeySecondary, results_list.release()); | 170 entry_dict->SetWithoutPathExpansion(kKeySecondary, results_list.release()); |
169 if (data_store_.get()) | 171 if (data_store_.get()) |
170 data_store_->ScheduleWrite(); | 172 data_store_->ScheduleWrite(); |
171 } | 173 } |
172 | 174 |
173 void HistoryDataStore::SetUpdateTime(const std::string& query, | 175 void HistoryDataStore::SetUpdateTime(const std::string& query, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 // Creates one if none exists. Ownership is taken in the set call after. | 210 // Creates one if none exists. Ownership is taken in the set call after. |
209 entry_dict = new base::DictionaryValue; | 211 entry_dict = new base::DictionaryValue; |
210 assoc_dict->SetWithoutPathExpansion(query, entry_dict); | 212 assoc_dict->SetWithoutPathExpansion(query, entry_dict); |
211 } | 213 } |
212 | 214 |
213 return entry_dict; | 215 return entry_dict; |
214 } | 216 } |
215 | 217 |
216 void HistoryDataStore::OnDictionaryLoadedCallback( | 218 void HistoryDataStore::OnDictionaryLoadedCallback( |
217 OnLoadedCallback callback, | 219 OnLoadedCallback callback, |
218 scoped_ptr<base::DictionaryValue> dict) { | 220 std::unique_ptr<base::DictionaryValue> dict) { |
219 if (!dict) { | 221 if (!dict) { |
220 callback.Run(nullptr); | 222 callback.Run(nullptr); |
221 } else { | 223 } else { |
222 callback.Run(Parse(std::move(dict))); | 224 callback.Run(Parse(std::move(dict))); |
223 } | 225 } |
224 } | 226 } |
225 | 227 |
226 } // namespace app_list | 228 } // namespace app_list |
OLD | NEW |