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> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/containers/hash_tables.h" |
11 #include "base/containers/mru_cache.h" | 12 #include "base/containers/mru_cache.h" |
12 #include "components/dom_distiller/core/article_entry.h" | 13 #include "components/dom_distiller/core/article_entry.h" |
13 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 14 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
14 | 15 |
15 namespace dom_distiller { | 16 namespace dom_distiller { |
16 | 17 |
17 // The maximum number of items to keep in the cache before deleting some. | 18 // The maximum number of items to keep in the cache before deleting some. |
18 const int kDefaultMaxNumCachedEntries = 32; | 19 const int kDefaultMaxNumCachedEntries = 32; |
19 | 20 |
20 // This is a simple interface for saving and loading of distilled content for an | 21 // This is a simple interface for saving and loading of distilled content for an |
(...skipping 12 matching lines...) Expand all Loading... |
33 | 34 |
34 DistilledContentStore() {}; | 35 DistilledContentStore() {}; |
35 virtual ~DistilledContentStore() {}; | 36 virtual ~DistilledContentStore() {}; |
36 | 37 |
37 private: | 38 private: |
38 DISALLOW_COPY_AND_ASSIGN(DistilledContentStore); | 39 DISALLOW_COPY_AND_ASSIGN(DistilledContentStore); |
39 }; | 40 }; |
40 | 41 |
41 // This content store keeps up to |max_num_entries| of the last accessed items | 42 // This content store keeps up to |max_num_entries| of the last accessed items |
42 // in its cache. Both loading and saving content is counted as access. | 43 // in its cache. Both loading and saving content is counted as access. |
| 44 // Lookup can be done based on entry ID or URL. |
43 class InMemoryContentStore : public DistilledContentStore { | 45 class InMemoryContentStore : public DistilledContentStore { |
44 public: | 46 public: |
45 explicit InMemoryContentStore(const int max_num_entries); | 47 explicit InMemoryContentStore(const int max_num_entries); |
46 virtual ~InMemoryContentStore(); | 48 virtual ~InMemoryContentStore(); |
47 | 49 |
48 // DistilledContentStore implementation | 50 // DistilledContentStore implementation |
49 virtual void SaveContent(const ArticleEntry& entry, | 51 virtual void SaveContent(const ArticleEntry& entry, |
50 const DistilledArticleProto& proto, | 52 const DistilledArticleProto& proto, |
51 SaveCallback callback) OVERRIDE; | 53 SaveCallback callback) OVERRIDE; |
52 virtual void LoadContent(const ArticleEntry& entry, | 54 virtual void LoadContent(const ArticleEntry& entry, |
53 LoadCallback callback) OVERRIDE; | 55 LoadCallback callback) OVERRIDE; |
54 | 56 |
55 // Synchronously saves the content. | 57 // Synchronously saves the content. |
56 void InjectContent(const ArticleEntry& entry, | 58 void InjectContent(const ArticleEntry& entry, |
57 const DistilledArticleProto& proto); | 59 const DistilledArticleProto& proto); |
58 | 60 |
59 private: | 61 private: |
60 typedef base::MRUCache<std::string, DistilledArticleProto> ContentMap; | 62 // The CacheDeletor gets called when anything is removed from the ContentMap. |
| 63 class CacheDeletor { |
| 64 public: |
| 65 explicit CacheDeletor(InMemoryContentStore* store); |
| 66 ~CacheDeletor(); |
| 67 void operator()(const DistilledArticleProto& proto); |
| 68 |
| 69 private: |
| 70 InMemoryContentStore* store_; |
| 71 }; |
| 72 |
| 73 void AddUrlToIdMapping(const ArticleEntry& entry, |
| 74 const DistilledArticleProto& proto); |
| 75 |
| 76 void EraseUrlToIdMapping(const DistilledArticleProto& proto); |
| 77 |
| 78 typedef base::MRUCacheBase<std::string, |
| 79 DistilledArticleProto, |
| 80 InMemoryContentStore::CacheDeletor> ContentMap; |
| 81 typedef base::hash_map<std::string, std::string> UrlMap; |
| 82 |
61 ContentMap cache_; | 83 ContentMap cache_; |
| 84 UrlMap url_to_id_; |
62 }; | 85 }; |
| 86 |
63 } // dom_distiller | 87 } // dom_distiller |
64 | 88 |
65 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_ | 89 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_ |
OLD | NEW |