distilled_content_store.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_
  5. #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "base/bind.h"
  10. #include "base/containers/lru_cache.h"
  11. #include "components/dom_distiller/core/article_entry.h"
  12. #include "components/dom_distiller/core/proto/distilled_article.pb.h"
  13. namespace dom_distiller {
  14. // The maximum number of items to keep in the cache before deleting some.
  15. const int kDefaultMaxNumCachedEntries = 32;
  16. // This is a simple interface for saving and loading of distilled content for an
  17. // ArticleEntry.
  18. class DistilledContentStore {
  19. public:
  20. typedef base::OnceCallback<void(bool /* success */,
  21. std::unique_ptr<DistilledArticleProto>)>
  22. LoadCallback;
  23. typedef base::OnceCallback<void(bool /* success */)> SaveCallback;
  24. virtual void SaveContent(const ArticleEntry& entry,
  25. const DistilledArticleProto& proto,
  26. SaveCallback callback) = 0;
  27. virtual void LoadContent(const ArticleEntry& entry,
  28. LoadCallback callback) = 0;
  29. DistilledContentStore() = default;
  30. virtual ~DistilledContentStore() = default;
  31. DistilledContentStore(const DistilledContentStore&) = delete;
  32. DistilledContentStore& operator=(const DistilledContentStore&) = delete;
  33. };
  34. // This content store keeps up to |max_num_entries| of the last accessed items
  35. // in its cache. Both loading and saving content is counted as access.
  36. // Lookup can be done based on entry ID or URL.
  37. class InMemoryContentStore : public DistilledContentStore {
  38. public:
  39. explicit InMemoryContentStore(const int max_num_entries);
  40. ~InMemoryContentStore() override;
  41. // DistilledContentStore implementation
  42. void SaveContent(const ArticleEntry& entry,
  43. const DistilledArticleProto& proto,
  44. SaveCallback callback) override;
  45. void LoadContent(const ArticleEntry& entry, LoadCallback callback) override;
  46. // Synchronously saves the content.
  47. void InjectContent(const ArticleEntry& entry,
  48. const DistilledArticleProto& proto);
  49. private:
  50. // The CacheDeletor gets called when anything is removed from the ContentMap.
  51. class CacheDeletor {
  52. public:
  53. explicit CacheDeletor(InMemoryContentStore* store);
  54. ~CacheDeletor();
  55. void operator()(DistilledArticleProto* proto);
  56. private:
  57. InMemoryContentStore* store_;
  58. };
  59. void AddUrlToIdMapping(const ArticleEntry& entry,
  60. const DistilledArticleProto& proto);
  61. void EraseUrlToIdMapping(const DistilledArticleProto& proto);
  62. typedef base::LRUCache<std::string,
  63. std::unique_ptr<DistilledArticleProto, CacheDeletor>>
  64. ContentMap;
  65. typedef std::unordered_map<std::string, std::string> UrlMap;
  66. ContentMap cache_;
  67. UrlMap url_to_id_;
  68. };
  69. } // namespace dom_distiller
  70. #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLED_CONTENT_STORE_H_