| 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 "chrome/browser/ui/app_list/search/common/webservice_cache.h" | 5 #include "chrome/browser/ui/app_list/search/common/webservice_cache.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 | 15 |
| 15 namespace app_list { | 16 namespace app_list { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const unsigned int kWebserviceCacheMaxSize = 1000; | 19 const unsigned int kWebserviceCacheMaxSize = 1000; |
| 19 const unsigned int kWebserviceCacheTimeLimitInMinutes = 1; | 20 const unsigned int kWebserviceCacheTimeLimitInMinutes = 1; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 50 return std::make_pair(FRESH, iter->second->result.get()); | 51 return std::make_pair(FRESH, iter->second->result.get()); |
| 51 } else { | 52 } else { |
| 52 return std::make_pair(STALE, iter->second->result.get()); | 53 return std::make_pair(STALE, iter->second->result.get()); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 return std::make_pair(STALE, static_cast<base::DictionaryValue*>(NULL)); | 56 return std::make_pair(STALE, static_cast<base::DictionaryValue*>(NULL)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void WebserviceCache::Put(QueryType type, | 59 void WebserviceCache::Put(QueryType type, |
| 59 const std::string& query, | 60 const std::string& query, |
| 60 scoped_ptr<base::DictionaryValue> result) { | 61 std::unique_ptr<base::DictionaryValue> result) { |
| 61 if (result) { | 62 if (result) { |
| 62 std::string typed_query = PrependType(type, query); | 63 std::string typed_query = PrependType(type, query); |
| 63 scoped_ptr<Payload> scoped_payload( | 64 std::unique_ptr<Payload> scoped_payload( |
| 64 new Payload(base::Time::Now(), std::move(result))); | 65 new Payload(base::Time::Now(), std::move(result))); |
| 65 Payload* payload = scoped_payload.get(); | 66 Payload* payload = scoped_payload.get(); |
| 66 | 67 |
| 67 cache_.Put(typed_query, std::move(scoped_payload)); | 68 cache_.Put(typed_query, std::move(scoped_payload)); |
| 68 // If the cache isn't loaded yet, we're fine with losing queries since | 69 // If the cache isn't loaded yet, we're fine with losing queries since |
| 69 // a 1000 entry cache should load really quickly so the chance of a user | 70 // a 1000 entry cache should load really quickly so the chance of a user |
| 70 // already having typed a 3 character search before the cache has loaded is | 71 // already having typed a 3 character search before the cache has loaded is |
| 71 // very unlikely. | 72 // very unlikely. |
| 72 if (cache_loaded_) { | 73 if (cache_loaded_) { |
| 73 data_store_->cached_dict()->Set(typed_query, DictFromPayload(*payload)); | 74 data_store_->cached_dict()->Set(typed_query, DictFromPayload(*payload)); |
| 74 data_store_->ScheduleWrite(); | 75 data_store_->ScheduleWrite(); |
| 75 if (cache_.size() > kWebserviceCacheMaxSize) | 76 if (cache_.size() > kWebserviceCacheMaxSize) |
| 76 TrimCache(); | 77 TrimCache(); |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 | 81 |
| 81 void WebserviceCache::OnCacheLoaded(scoped_ptr<base::DictionaryValue>) { | 82 void WebserviceCache::OnCacheLoaded(std::unique_ptr<base::DictionaryValue>) { |
| 82 if (!data_store_->cached_dict()) | 83 if (!data_store_->cached_dict()) |
| 83 return; | 84 return; |
| 84 | 85 |
| 85 std::vector<std::string> cleanup_keys; | 86 std::vector<std::string> cleanup_keys; |
| 86 for (base::DictionaryValue::Iterator it(*data_store_->cached_dict()); | 87 for (base::DictionaryValue::Iterator it(*data_store_->cached_dict()); |
| 87 !it.IsAtEnd(); | 88 !it.IsAtEnd(); |
| 88 it.Advance()) { | 89 it.Advance()) { |
| 89 const base::DictionaryValue* payload_dict; | 90 const base::DictionaryValue* payload_dict; |
| 90 scoped_ptr<Payload> payload(new Payload); | 91 std::unique_ptr<Payload> payload(new Payload); |
| 91 if (!it.value().GetAsDictionary(&payload_dict) || | 92 if (!it.value().GetAsDictionary(&payload_dict) || |
| 92 !payload_dict || | 93 !payload_dict || |
| 93 !PayloadFromDict(payload_dict, payload.get())) { | 94 !PayloadFromDict(payload_dict, payload.get())) { |
| 94 // In case we don't have a valid payload associated with a given query, | 95 // In case we don't have a valid payload associated with a given query, |
| 95 // clean up that query from our data store. | 96 // clean up that query from our data store. |
| 96 cleanup_keys.push_back(it.key()); | 97 cleanup_keys.push_back(it.key()); |
| 97 continue; | 98 continue; |
| 98 } | 99 } |
| 99 cache_.Put(it.key(), std::move(payload)); | 100 cache_.Put(it.key(), std::move(payload)); |
| 100 } | 101 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 if (!dict->GetDictionary(kKeyResult, &result)) | 117 if (!dict->GetDictionary(kKeyResult, &result)) |
| 117 return false; | 118 return false; |
| 118 | 119 |
| 119 int64_t time_val; | 120 int64_t time_val; |
| 120 base::StringToInt64(time_string, &time_val); | 121 base::StringToInt64(time_string, &time_val); |
| 121 | 122 |
| 122 // The result dictionary will be owned by the cache, hence create a copy | 123 // The result dictionary will be owned by the cache, hence create a copy |
| 123 // instead of returning the original reference. The new dictionary will be | 124 // instead of returning the original reference. The new dictionary will be |
| 124 // owned by our MRU cache. | 125 // owned by our MRU cache. |
| 125 *payload = Payload(base::Time::FromInternalValue(time_val), | 126 *payload = Payload(base::Time::FromInternalValue(time_val), |
| 126 make_scoped_ptr(result->DeepCopy())); | 127 base::WrapUnique(result->DeepCopy())); |
| 127 return true; | 128 return true; |
| 128 } | 129 } |
| 129 | 130 |
| 130 base::DictionaryValue* WebserviceCache::DictFromPayload( | 131 base::DictionaryValue* WebserviceCache::DictFromPayload( |
| 131 const Payload& payload) { | 132 const Payload& payload) { |
| 132 base::DictionaryValue* dict = new base::DictionaryValue(); | 133 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 133 dict->SetString(kKeyResultTime, base::Int64ToString( | 134 dict->SetString(kKeyResultTime, base::Int64ToString( |
| 134 payload.time.ToInternalValue())); | 135 payload.time.ToInternalValue())); |
| 135 // The payload will still keep ownership of it's result dict, hence put a | 136 // The payload will still keep ownership of it's result dict, hence put a |
| 136 // a copy of the result dictionary here. This dictionary will be owned by | 137 // a copy of the result dictionary here. This dictionary will be owned by |
| (...skipping 18 matching lines...) Expand all Loading... |
| 155 case WEBSTORE: | 156 case WEBSTORE: |
| 156 return kWebstoreQueryPrefix + query; | 157 return kWebstoreQueryPrefix + query; |
| 157 case PEOPLE: | 158 case PEOPLE: |
| 158 return kPeopleQueryPrefix + query; | 159 return kPeopleQueryPrefix + query; |
| 159 default: | 160 default: |
| 160 return query; | 161 return query; |
| 161 } | 162 } |
| 162 } | 163 } |
| 163 | 164 |
| 164 WebserviceCache::Payload::Payload(const base::Time& time, | 165 WebserviceCache::Payload::Payload(const base::Time& time, |
| 165 scoped_ptr<base::DictionaryValue> result) | 166 std::unique_ptr<base::DictionaryValue> result) |
| 166 : time(time), result(std::move(result)) {} | 167 : time(time), result(std::move(result)) {} |
| 167 | 168 |
| 168 WebserviceCache::Payload::Payload() = default; | 169 WebserviceCache::Payload::Payload() = default; |
| 169 | 170 |
| 170 WebserviceCache::Payload::~Payload() = default; | 171 WebserviceCache::Payload::~Payload() = default; |
| 171 | 172 |
| 172 WebserviceCache::Payload& WebserviceCache::Payload::operator=(Payload&& other) { | 173 WebserviceCache::Payload& WebserviceCache::Payload::operator=(Payload&& other) { |
| 173 time = std::move(other.time); | 174 time = std::move(other.time); |
| 174 result = std::move(other.result); | 175 result = std::move(other.result); |
| 175 return *this; | 176 return *this; |
| 176 } | 177 } |
| 177 | 178 |
| 178 } // namespace app_list | 179 } // namespace app_list |
| OLD | NEW |