Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/common/webservice_cache.h |
| diff --git a/chrome/browser/ui/app_list/search/common/webservice_cache.h b/chrome/browser/ui/app_list/search/common/webservice_cache.h |
| index 942e8c51b0e6df70123971ef4862bc2c3505d040..7aa03f6c8d0fc62a3e5e963a6df2661a537331ac 100644 |
| --- a/chrome/browser/ui/app_list/search/common/webservice_cache.h |
| +++ b/chrome/browser/ui/app_list/search/common/webservice_cache.h |
| @@ -9,40 +9,96 @@ |
| #include "base/basictypes.h" |
| #include "base/containers/mru_cache.h" |
| +#include "base/memory/ref_counted.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/time/time.h" |
| +#include "chrome/browser/ui/app_list/search/common/dictionary_data_store.h" |
| +#include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
| namespace base { |
| class DictionaryValue; |
| } |
| +namespace content { |
| +class BrowserContext; |
| +} |
| + |
| namespace app_list { |
| +enum QueryType { |
|
xiyuan
2013/10/29 02:32:24
Move into the WebserviceCache class.
rkc
2013/10/29 20:36:24
Done.
|
| + WEBSTORE = 0, |
| + PEOPLE = 1 |
| +}; |
| + |
| +enum ResultStatus { |
| + FRESH = 0, |
| + STALE = 1 |
| +}; |
| + |
| +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.
|
| + Payload(QueryType type, |
| + const base::Time& time, |
| + const base::DictionaryValue* result) |
| + : type(type), time(time), result(result) {} |
| + |
| + Payload() {} |
| + |
| + QueryType type; |
| + base::Time time; |
| + const base::DictionaryValue* result; |
| +}; |
| + |
| +// Pair of values, first indicating whether this is a fresh result or stale, |
| +// the second holding the actual value. |
| +typedef std::pair<ResultStatus, const base::DictionaryValue*> CacheResult; |
| + |
| // WebserviceCache manages a cache of search results which should be valid |
| // during an input session. This will reduce unnecessary queries for typing |
| // backspace or so on. This is not meant to hold cache entries across multiple |
| // search sessions. |
| -class WebserviceCache { |
| +class WebserviceCache : public BrowserContextKeyedService, |
| + public base::SupportsWeakPtr<WebserviceCache> { |
| public: |
| - WebserviceCache(); |
| + explicit WebserviceCache(content::BrowserContext* context); |
| ~WebserviceCache(); |
| // Checks the current cache and returns the value for the |query| if it's |
| // valid. Otherwise returns NULL. |
| - const base::DictionaryValue* Get(const std::string& query); |
| + 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
|
| // Puts the new result to the query. |
| - void Put(const std::string& query, scoped_ptr<base::DictionaryValue> result); |
| + void Put(QueryType type, |
| + const std::string& query, |
| + scoped_ptr<base::DictionaryValue> result); |
| private: |
| - typedef std::pair<base::Time, base::DictionaryValue*> Payload; |
| class CacheDeletor { |
| public: |
| void operator()(Payload& payload); |
| }; |
| typedef base::MRUCacheBase<std::string, Payload, CacheDeletor> Cache; |
| + // Callback for when the cache is loaded from the dictionary data store. |
| + void OnCacheLoaded(scoped_ptr<base::DictionaryValue>); |
| + |
| + // Populates the payload parameter with the corresponding payload stored |
| + // in the given dictionary. If the dictionary is invalid for any reason, |
| + // this method will return false. |
| + bool PayloadFromDict(const base::DictionaryValue* dict, |
| + Payload* payload); |
| + |
| + // Returns a dictionary value for a given payload. |
| + 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.
|
| + |
| + // Trims our MRU cache, making sure that any element that is removed is also |
| + // removed from the dictionary data store. |
| + void TrimCache(); |
| + |
| + content::BrowserContext* browser_context_; |
| Cache cache_; |
| + scoped_refptr<DictionaryDataStore> data_store_; |
| + |
| + bool cache_loaded_; |
| DISALLOW_COPY_AND_ASSIGN(WebserviceCache); |
| }; |