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 QueryType { | |
xiyuan
2013/10/29 02:32:24
Move into the WebserviceCache class.
rkc
2013/10/29 20:36:24
Done.
| |
29 WEBSTORE = 0, | |
30 PEOPLE = 1 | |
31 }; | |
32 | |
33 enum ResultStatus { | |
34 FRESH = 0, | |
35 STALE = 1 | |
36 }; | |
37 | |
38 struct Payload { | |
xiyuan
2013/10/29 02:32:24
Is this used outside WebserviceCache? If not, move
rkc
2013/10/29 20:36:24
Done.
| |
39 Payload(QueryType type, | |
40 const base::Time& time, | |
41 const base::DictionaryValue* result) | |
42 : type(type), time(time), result(result) {} | |
43 | |
44 Payload() {} | |
45 | |
46 QueryType type; | |
47 base::Time time; | |
48 const base::DictionaryValue* result; | |
49 }; | |
50 | |
51 // Pair of values, first indicating whether this is a fresh result or stale, | |
52 // the second holding the actual value. | |
53 typedef std::pair<ResultStatus, const base::DictionaryValue*> CacheResult; | |
54 | |
21 // WebserviceCache manages a cache of search results which should be valid | 55 // WebserviceCache manages a cache of search results which should be valid |
22 // during an input session. This will reduce unnecessary queries for typing | 56 // 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 | 57 // backspace or so on. This is not meant to hold cache entries across multiple |
24 // search sessions. | 58 // search sessions. |
25 class WebserviceCache { | 59 class WebserviceCache : public BrowserContextKeyedService, |
60 public base::SupportsWeakPtr<WebserviceCache> { | |
26 public: | 61 public: |
27 WebserviceCache(); | 62 explicit WebserviceCache(content::BrowserContext* context); |
28 ~WebserviceCache(); | 63 ~WebserviceCache(); |
29 | 64 |
30 // Checks the current cache and returns the value for the |query| if it's | 65 // Checks the current cache and returns the value for the |query| if it's |
31 // valid. Otherwise returns NULL. | 66 // valid. Otherwise returns NULL. |
32 const base::DictionaryValue* Get(const std::string& query); | 67 const CacheResult Get(QueryType type, const std::string& query); |
xiyuan
2013/10/29 02:32:24
It seems that we only use FRESH result. Why do we
rkc
2013/10/29 20:36:24
Whether the value is stale or fresh, we still disp
| |
33 | 68 |
34 // Puts the new result to the query. | 69 // Puts the new result to the query. |
35 void Put(const std::string& query, scoped_ptr<base::DictionaryValue> result); | 70 void Put(QueryType type, |
71 const std::string& query, | |
72 scoped_ptr<base::DictionaryValue> result); | |
36 | 73 |
37 private: | 74 private: |
38 typedef std::pair<base::Time, base::DictionaryValue*> Payload; | |
39 class CacheDeletor { | 75 class CacheDeletor { |
40 public: | 76 public: |
41 void operator()(Payload& payload); | 77 void operator()(Payload& payload); |
42 }; | 78 }; |
43 typedef base::MRUCacheBase<std::string, Payload, CacheDeletor> Cache; | 79 typedef base::MRUCacheBase<std::string, Payload, CacheDeletor> Cache; |
44 | 80 |
81 // Callback for when the cache is loaded from the dictionary data store. | |
82 void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); | |
83 | |
84 // Populates the payload parameter with the corresponding payload stored | |
85 // in the given dictionary. If the dictionary is invalid for any reason, | |
86 // this method will return false. | |
87 bool PayloadFromDict(const base::DictionaryValue* dict, | |
88 Payload* payload); | |
89 | |
90 // Returns a dictionary value for a given payload. | |
91 base::DictionaryValue* DictFromPayload(const Payload& payload); | |
xiyuan
2013/10/29 02:32:24
Document ownership of the returned object. Or make
rkc
2013/10/29 20:36:24
Done.
| |
92 | |
93 // Trims our MRU cache, making sure that any element that is removed is also | |
94 // removed from the dictionary data store. | |
95 void TrimCache(); | |
96 | |
97 content::BrowserContext* browser_context_; | |
45 Cache cache_; | 98 Cache cache_; |
99 scoped_refptr<DictionaryDataStore> data_store_; | |
100 | |
101 bool cache_loaded_; | |
46 | 102 |
47 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); | 103 DISALLOW_COPY_AND_ASSIGN(WebserviceCache); |
48 }; | 104 }; |
49 | 105 |
50 } // namespace app_list | 106 } // namespace app_list |
51 | 107 |
52 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ | 108 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_WEBSERVICE_CACHE_H_ |
OLD | NEW |