123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright 2014 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_
- #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_
- #include <memory>
- #include <string>
- #include <unordered_map>
- #include "base/bind.h"
- #include "base/containers/lru_cache.h"
- #include "components/dom_distiller/core/article_entry.h"
- #include "components/dom_distiller/core/proto/distilled_article.pb.h"
- namespace dom_distiller {
- // The maximum number of items to keep in the cache before deleting some.
- const int kDefaultMaxNumCachedEntries = 32;
- // This is a simple interface for saving and loading of distilled content for an
- // ArticleEntry.
- class DistilledContentStore {
- public:
- typedef base::OnceCallback<void(bool /* success */,
- std::unique_ptr<DistilledArticleProto>)>
- LoadCallback;
- typedef base::OnceCallback<void(bool /* success */)> SaveCallback;
- virtual void SaveContent(const ArticleEntry& entry,
- const DistilledArticleProto& proto,
- SaveCallback callback) = 0;
- virtual void LoadContent(const ArticleEntry& entry,
- LoadCallback callback) = 0;
- DistilledContentStore() = default;
- virtual ~DistilledContentStore() = default;
- DistilledContentStore(const DistilledContentStore&) = delete;
- DistilledContentStore& operator=(const DistilledContentStore&) = delete;
- };
- // This content store keeps up to |max_num_entries| of the last accessed items
- // in its cache. Both loading and saving content is counted as access.
- // Lookup can be done based on entry ID or URL.
- class InMemoryContentStore : public DistilledContentStore {
- public:
- explicit InMemoryContentStore(const int max_num_entries);
- ~InMemoryContentStore() override;
- // DistilledContentStore implementation
- void SaveContent(const ArticleEntry& entry,
- const DistilledArticleProto& proto,
- SaveCallback callback) override;
- void LoadContent(const ArticleEntry& entry, LoadCallback callback) override;
- // Synchronously saves the content.
- void InjectContent(const ArticleEntry& entry,
- const DistilledArticleProto& proto);
- private:
- // The CacheDeletor gets called when anything is removed from the ContentMap.
- class CacheDeletor {
- public:
- explicit CacheDeletor(InMemoryContentStore* store);
- ~CacheDeletor();
- void operator()(DistilledArticleProto* proto);
- private:
- InMemoryContentStore* store_;
- };
- void AddUrlToIdMapping(const ArticleEntry& entry,
- const DistilledArticleProto& proto);
- void EraseUrlToIdMapping(const DistilledArticleProto& proto);
- typedef base::LRUCache<std::string,
- std::unique_ptr<DistilledArticleProto, CacheDeletor>>
- ContentMap;
- typedef std::unordered_map<std::string, std::string> UrlMap;
- ContentMap cache_;
- UrlMap url_to_id_;
- };
- } // namespace dom_distiller
- #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_
|