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/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/containers/mru_cache.h" | 11 #include "base/containers/mru_cache.h" |
| 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/ui/app_list/search/common/dictionary_data_store.h" |
| 16 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
14 | 17 |
15 namespace base { | 18 namespace base { |
16 class DictionaryValue; | 19 class DictionaryValue; |
17 } | 20 } |
18 | 21 |
| 22 namespace content { |
| 23 class BrowserContext; |
| 24 } |
| 25 |
19 namespace app_list { | 26 namespace app_list { |
20 | 27 |
| 28 enum ResultStatus { |
| 29 FRESH = 0, |
| 30 STALE = 1 |
| 31 }; |
| 32 |
| 33 // Pair of values, first indicating whether this is a fresh result or stale, |
| 34 // the second holding the actual value. |
| 35 typedef std::pair<ResultStatus, const base::DictionaryValue*> CacheResult; |
| 36 |
21 // WebserviceCache manages a cache of search results which should be valid | 37 // WebserviceCache manages a cache of search results which should be valid |
22 // during an input session. This will reduce unnecessary queries for typing | 38 // during an input session. This will reduce unnecessary queries for typing |
23 // backspace or so on. This is not meant to hold cache entries across multiple | 39 // backspace or so on. This is not meant to hold cache entries across multiple |
24 // search sessions. | 40 // search sessions. |
25 class WebserviceCache { | 41 class WebserviceCache : public BrowserContextKeyedService, |
| 42 public base::SupportsWeakPtr<WebserviceCache> { |
26 public: | 43 public: |
27 WebserviceCache(); | 44 enum QueryType { |
28 ~WebserviceCache(); | 45 WEBSTORE = 0, |
| 46 PEOPLE = 1 |
| 47 }; |
| 48 |
| 49 explicit WebserviceCache(content::BrowserContext* context); |
| 50 virtual ~WebserviceCache(); |
29 | 51 |
30 // Checks the current cache and returns the value for the |query| if it's | 52 // Checks the current cache and returns the value for the |query| if it's |
31 // valid. Otherwise returns NULL. | 53 // valid. Otherwise an CacheResult object with the result field set to NULL. |
32 const base::DictionaryValue* Get(const std::string& query); | 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. |
| 56 // The type silos the query into it's own separate domain, preventing any |
| 57 // mixing of the queries. |
| 58 const CacheResult Get(QueryType type, const std::string& query); |
33 | 59 |
34 // Puts the new result to the query. | 60 // Puts the new result to the query. |
35 void Put(const std::string& query, scoped_ptr<base::DictionaryValue> result); | 61 void Put(QueryType type, |
| 62 const std::string& query, |
| 63 scoped_ptr<base::DictionaryValue> result); |
36 | 64 |
37 private: | 65 private: |
38 typedef std::pair<base::Time, base::DictionaryValue*> Payload; | 66 struct Payload { |
| 67 Payload(const base::Time& time, |
| 68 const base::DictionaryValue* result) |
| 69 : time(time), result(result) {} |
| 70 Payload() {} |
| 71 |
| 72 base::Time time; |
| 73 const base::DictionaryValue* result; |
| 74 }; |
| 75 |
39 class CacheDeletor { | 76 class CacheDeletor { |
40 public: | 77 public: |
41 void operator()(Payload& payload); | 78 void operator()(Payload& payload); |
42 }; | 79 }; |
43 typedef base::MRUCacheBase<std::string, Payload, CacheDeletor> Cache; | 80 typedef base::MRUCacheBase<std::string, Payload, CacheDeletor> Cache; |
44 | 81 |
| 82 // Callback for when the cache is loaded from the dictionary data store. |
| 83 void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); |
| 84 |
| 85 // Populates the payload parameter with the corresponding payload stored |
| 86 // in the given dictionary. If the dictionary is invalid for any reason, |
| 87 // this method will return false. |
| 88 bool PayloadFromDict(const base::DictionaryValue* dict, |
| 89 Payload* payload); |
| 90 |
| 91 // Returns a dictionary value for a given payload. The returned dictionary |
| 92 // will be owned by the data_store_ cached_dict, and freed on the destruction |
| 93 // of our dictionary datastore. |
| 94 base::DictionaryValue* DictFromPayload(const Payload& payload); |
| 95 |
| 96 // Trims our MRU cache, making sure that any element that is removed is also |
| 97 // removed from the dictionary data store. |
| 98 void TrimCache(); |
| 99 |
| 100 // Prepends a type string to the given query and returns a new query string. |
| 101 std::string PrependType(QueryType type, const std::string& query); |
| 102 |
45 Cache cache_; | 103 Cache cache_; |
| 104 scoped_refptr<DictionaryDataStore> data_store_; |
| 105 |
| 106 bool cache_loaded_; |
46 | 107 |
47 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); | 108 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); |
48 }; | 109 }; |
49 | 110 |
50 } // namespace app_list | 111 } // namespace app_list |
51 | 112 |
52 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 113 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
OLD | NEW |