| 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 <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/containers/mru_cache.h" | 11 #include "base/containers/mru_cache.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" | 15 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "ui/app_list/search/dictionary_data_store.h" | 16 #include "ui/app_list/search/dictionary_data_store.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class DictionaryValue; | 19 class DictionaryValue; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 class BrowserContext; | 23 class BrowserContext; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 53 // valid. Otherwise an CacheResult object with the result field set to NULL. | 53 // valid. Otherwise an CacheResult object with the result field set to NULL. |
| 54 // A query consists of a query 'type' and the query itself. The two current | 54 // A query consists of a query 'type' and the query itself. The two current |
| 55 // types of queries supported are webstore queries and people search queries. | 55 // types of queries supported are webstore queries and people search queries. |
| 56 // The type silos the query into it's own separate domain, preventing any | 56 // The type silos the query into it's own separate domain, preventing any |
| 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 std::unique_ptr<base::DictionaryValue> result); |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 struct Payload { | 66 struct Payload { |
| 67 Payload(const base::Time& time, scoped_ptr<base::DictionaryValue> result); | 67 Payload(const base::Time& time, |
| 68 std::unique_ptr<base::DictionaryValue> result); |
| 68 Payload(); | 69 Payload(); |
| 69 ~Payload(); | 70 ~Payload(); |
| 70 | 71 |
| 71 Payload& operator=(Payload&& other); | 72 Payload& operator=(Payload&& other); |
| 72 | 73 |
| 73 base::Time time; | 74 base::Time time; |
| 74 scoped_ptr<base::DictionaryValue> result; | 75 std::unique_ptr<base::DictionaryValue> result; |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 using Cache = base::MRUCache<std::string, scoped_ptr<Payload>>; | 78 using Cache = base::MRUCache<std::string, std::unique_ptr<Payload>>; |
| 78 | 79 |
| 79 // Callback for when the cache is loaded from the dictionary data store. | 80 // Callback for when the cache is loaded from the dictionary data store. |
| 80 void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); | 81 void OnCacheLoaded(std::unique_ptr<base::DictionaryValue>); |
| 81 | 82 |
| 82 // Populates the payload parameter with the corresponding payload stored | 83 // Populates the payload parameter with the corresponding payload stored |
| 83 // in the given dictionary. If the dictionary is invalid for any reason, | 84 // in the given dictionary. If the dictionary is invalid for any reason, |
| 84 // this method will return false. | 85 // this method will return false. |
| 85 bool PayloadFromDict(const base::DictionaryValue* dict, Payload* payload); | 86 bool PayloadFromDict(const base::DictionaryValue* dict, Payload* payload); |
| 86 | 87 |
| 87 // Returns a dictionary value for a given payload. The returned dictionary | 88 // Returns a dictionary value for a given payload. The returned dictionary |
| 88 // will be owned by the data_store_ cached_dict, and freed on the destruction | 89 // will be owned by the data_store_ cached_dict, and freed on the destruction |
| 89 // of our dictionary datastore. | 90 // of our dictionary datastore. |
| 90 base::DictionaryValue* DictFromPayload(const Payload& payload); | 91 base::DictionaryValue* DictFromPayload(const Payload& payload); |
| 91 | 92 |
| 92 // Trims our MRU cache, making sure that any element that is removed is also | 93 // Trims our MRU cache, making sure that any element that is removed is also |
| 93 // removed from the dictionary data store. | 94 // removed from the dictionary data store. |
| 94 void TrimCache(); | 95 void TrimCache(); |
| 95 | 96 |
| 96 // Prepends a type string to the given query and returns a new query string. | 97 // Prepends a type string to the given query and returns a new query string. |
| 97 std::string PrependType(QueryType type, const std::string& query); | 98 std::string PrependType(QueryType type, const std::string& query); |
| 98 | 99 |
| 99 Cache cache_; | 100 Cache cache_; |
| 100 scoped_refptr<DictionaryDataStore> data_store_; | 101 scoped_refptr<DictionaryDataStore> data_store_; |
| 101 | 102 |
| 102 bool cache_loaded_; | 103 bool cache_loaded_; |
| 103 | 104 |
| 104 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); | 105 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 } // namespace app_list | 108 } // namespace app_list |
| 108 | 109 |
| 109 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 110 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
| OLD | NEW |