| 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..8bedd16f185c865fc535fd4c533ae9f969205be0 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,101 @@
|
|
|
| #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 ResultStatus {
|
| + FRESH = 0,
|
| + STALE = 1
|
| +};
|
| +
|
| +// 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();
|
| - ~WebserviceCache();
|
| + enum QueryType {
|
| + WEBSTORE = 0,
|
| + PEOPLE = 1
|
| + };
|
| +
|
| + explicit WebserviceCache(content::BrowserContext* context);
|
| + virtual ~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);
|
| + // valid. Otherwise an CacheResult object with the result field set to NULL.
|
| + // A query consists of a query 'type' and the query itself. The two current
|
| + // types of queries supported are webstore queries and people search queries.
|
| + // The type silos the query into it's own separate domain, preventing any
|
| + // mixing of the queries.
|
| + const CacheResult Get(QueryType type, const std::string& query);
|
|
|
| // 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;
|
| + struct Payload {
|
| + Payload(const base::Time& time,
|
| + const base::DictionaryValue* result)
|
| + : time(time), result(result) {}
|
| + Payload() {}
|
| +
|
| + base::Time time;
|
| + const base::DictionaryValue* result;
|
| + };
|
| +
|
| 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. The returned dictionary
|
| + // will be owned by the data_store_ cached_dict, and freed on the destruction
|
| + // of our dictionary datastore.
|
| + base::DictionaryValue* DictFromPayload(const Payload& payload);
|
| +
|
| + // Trims our MRU cache, making sure that any element that is removed is also
|
| + // removed from the dictionary data store.
|
| + void TrimCache();
|
| +
|
| + // Prepends a type string to the given query and returns a new query string.
|
| + std::string PrependType(QueryType type, const std::string& query);
|
| +
|
| Cache cache_;
|
| + scoped_refptr<DictionaryDataStore> data_store_;
|
| +
|
| + bool cache_loaded_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(WebserviceCache);
|
| };
|
|
|