| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_ | 5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_ |
| 6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_ | 6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_ |
| 7 | 7 |
| 8 #include <string> |
| 9 |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/containers/hash_tables.h" | 11 #include "base/containers/mru_cache.h" |
| 10 #include "components/dom_distiller/core/article_entry.h" | 12 #include "components/dom_distiller/core/article_entry.h" |
| 11 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 13 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 12 | 14 |
| 13 namespace dom_distiller { | 15 namespace dom_distiller { |
| 14 | 16 |
| 17 // The maximum number of items to keep in the cache before deleting some. |
| 18 const int kDefaultMaxNumCachedEntries = 32; |
| 19 |
| 15 // This is a simple interface for saving and loading of distilled content for an | 20 // This is a simple interface for saving and loading of distilled content for an |
| 16 // ArticleEntry. | 21 // ArticleEntry. |
| 17 class DistilledContentStore { | 22 class DistilledContentStore { |
| 18 public: | 23 public: |
| 19 typedef base::Callback< | 24 typedef base::Callback< |
| 20 void(bool /* success */, scoped_ptr<DistilledArticleProto>)> LoadCallback; | 25 void(bool /* success */, scoped_ptr<DistilledArticleProto>)> LoadCallback; |
| 21 typedef base::Callback<void(bool /* success */)> SaveCallback; | 26 typedef base::Callback<void(bool /* success */)> SaveCallback; |
| 22 | 27 |
| 23 virtual void SaveContent(const ArticleEntry& entry, | 28 virtual void SaveContent(const ArticleEntry& entry, |
| 24 const DistilledArticleProto& proto, | 29 const DistilledArticleProto& proto, |
| 25 SaveCallback callback) = 0; | 30 SaveCallback callback) = 0; |
| 26 virtual void LoadContent(const ArticleEntry& entry, | 31 virtual void LoadContent(const ArticleEntry& entry, |
| 27 LoadCallback callback) const = 0; | 32 LoadCallback callback) = 0; |
| 28 | 33 |
| 29 DistilledContentStore() {}; | 34 DistilledContentStore() {}; |
| 30 virtual ~DistilledContentStore() {}; | 35 virtual ~DistilledContentStore() {}; |
| 36 |
| 31 private: | 37 private: |
| 32 DISALLOW_COPY_AND_ASSIGN(DistilledContentStore); | 38 DISALLOW_COPY_AND_ASSIGN(DistilledContentStore); |
| 33 }; | 39 }; |
| 34 | 40 |
| 35 // This content store keeps anything put in it around forever. Its memory use | 41 // This content store keeps up to |max_num_entries| of the last accessed items |
| 36 // then may grow very large. | 42 // in its cache. Both loading and saving content is counted as access. |
| 37 // | |
| 38 // TODO(cjhopman): Do something about unbound memory growth. | |
| 39 class InMemoryContentStore : public DistilledContentStore { | 43 class InMemoryContentStore : public DistilledContentStore { |
| 40 public: | 44 public: |
| 41 InMemoryContentStore(); | 45 explicit InMemoryContentStore(const int max_num_entries); |
| 42 virtual ~InMemoryContentStore(); | 46 virtual ~InMemoryContentStore(); |
| 43 | 47 |
| 44 // DistilledContentStore implementation | 48 // DistilledContentStore implementation |
| 45 virtual void SaveContent(const ArticleEntry& entry, | 49 virtual void SaveContent(const ArticleEntry& entry, |
| 46 const DistilledArticleProto& proto, | 50 const DistilledArticleProto& proto, |
| 47 SaveCallback callback) OVERRIDE; | 51 SaveCallback callback) OVERRIDE; |
| 48 virtual void LoadContent(const ArticleEntry& entry, | 52 virtual void LoadContent(const ArticleEntry& entry, |
| 49 LoadCallback callback) const OVERRIDE; | 53 LoadCallback callback) OVERRIDE; |
| 50 | 54 |
| 51 // Synchronously saves the content. | 55 // Synchronously saves the content. |
| 52 void InjectContent(const ArticleEntry& entry, | 56 void InjectContent(const ArticleEntry& entry, |
| 53 const DistilledArticleProto& proto); | 57 const DistilledArticleProto& proto); |
| 54 | 58 |
| 55 private: | 59 private: |
| 56 typedef base::hash_map<std::string, DistilledArticleProto> ContentMap; | 60 typedef base::MRUCache<std::string, DistilledArticleProto> ContentMap; |
| 57 ContentMap cache_; | 61 ContentMap cache_; |
| 58 }; | 62 }; |
| 59 } // dom_distiller | 63 } // dom_distiller |
| 60 | 64 |
| 61 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_ | 65 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_ |
| OLD | NEW |