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 #ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 5 #ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
6 #define CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 6 #define CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/containers/mru_cache.h" | 10 #include "base/containers/mru_cache.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 // mixing of the queries. | 57 // mixing of the queries. |
58 const CacheResult Get(QueryType type, const std::string& query); | 58 const CacheResult Get(QueryType type, const std::string& query); |
59 | 59 |
60 // Puts the new result to the query. | 60 // Puts the new result to the query. |
61 void Put(QueryType type, | 61 void Put(QueryType type, |
62 const std::string& query, | 62 const std::string& query, |
63 scoped_ptr<base::DictionaryValue> result); | 63 scoped_ptr<base::DictionaryValue> result); |
64 | 64 |
65 private: | 65 private: |
66 struct Payload { | 66 struct Payload { |
67 Payload(const base::Time& time, | 67 Payload(const base::Time& time, scoped_ptr<base::DictionaryValue> result); |
68 const base::DictionaryValue* result) | 68 Payload(); |
69 : time(time), result(result) {} | 69 ~Payload(); |
70 Payload() {} | |
71 | 70 |
72 base::Time time; | 71 base::Time time; |
73 const base::DictionaryValue* result; | 72 scoped_ptr<base::DictionaryValue> result; |
74 }; | 73 }; |
75 | 74 |
76 class CacheDeletor { | 75 typedef base::MRUCache<std::string, scoped_ptr<Payload>> Cache; |
danakj
2016/03/07 21:27:10
using
vmpstr
2016/03/07 21:45:32
Done.
| |
77 public: | |
78 void operator()(const Payload& payload); | |
79 }; | |
80 typedef base:: | |
81 MRUCacheBase<std::string, Payload, std::less<std::string>, CacheDeletor> | |
82 Cache; | |
83 | 76 |
84 // Callback for when the cache is loaded from the dictionary data store. | 77 // Callback for when the cache is loaded from the dictionary data store. |
85 void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); | 78 void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); |
86 | 79 |
87 // Populates the payload parameter with the corresponding payload stored | 80 // Populates the payload parameter with the corresponding payload stored |
88 // in the given dictionary. If the dictionary is invalid for any reason, | 81 // in the given dictionary. If the dictionary is invalid for any reason, |
89 // this method will return false. | 82 // this method will return false. |
90 bool PayloadFromDict(const base::DictionaryValue* dict, | 83 bool PayloadFromDict(const base::DictionaryValue* dict, Payload* payload); |
91 Payload* payload); | |
92 | 84 |
93 // Returns a dictionary value for a given payload. The returned dictionary | 85 // Returns a dictionary value for a given payload. The returned dictionary |
94 // will be owned by the data_store_ cached_dict, and freed on the destruction | 86 // will be owned by the data_store_ cached_dict, and freed on the destruction |
95 // of our dictionary datastore. | 87 // of our dictionary datastore. |
96 base::DictionaryValue* DictFromPayload(const Payload& payload); | 88 base::DictionaryValue* DictFromPayload(const Payload& payload); |
97 | 89 |
98 // Trims our MRU cache, making sure that any element that is removed is also | 90 // Trims our MRU cache, making sure that any element that is removed is also |
99 // removed from the dictionary data store. | 91 // removed from the dictionary data store. |
100 void TrimCache(); | 92 void TrimCache(); |
101 | 93 |
102 // Prepends a type string to the given query and returns a new query string. | 94 // Prepends a type string to the given query and returns a new query string. |
103 std::string PrependType(QueryType type, const std::string& query); | 95 std::string PrependType(QueryType type, const std::string& query); |
104 | 96 |
105 Cache cache_; | 97 Cache cache_; |
106 scoped_refptr<DictionaryDataStore> data_store_; | 98 scoped_refptr<DictionaryDataStore> data_store_; |
107 | 99 |
108 bool cache_loaded_; | 100 bool cache_loaded_; |
109 | 101 |
110 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); | 102 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); |
111 }; | 103 }; |
112 | 104 |
113 } // namespace app_list | 105 } // namespace app_list |
114 | 106 |
115 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 107 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
OLD | NEW |