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.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_STORE_H_
10 #include "base/bind.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/containers/mru_cache.h"
13 #include "components/dom_distiller/core/article_entry.h"
14 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
16 namespace dom_distiller
{
18 // The maximum number of items to keep in the cache before deleting some.
19 const int kDefaultMaxNumCachedEntries
= 32;
21 // This is a simple interface for saving and loading of distilled content for an
23 class DistilledContentStore
{
25 typedef base::Callback
<
26 void(bool /* success */, scoped_ptr
<DistilledArticleProto
>)> LoadCallback
;
27 typedef base::Callback
<void(bool /* success */)> SaveCallback
;
29 virtual void SaveContent(const ArticleEntry
& entry
,
30 const DistilledArticleProto
& proto
,
31 SaveCallback callback
) = 0;
32 virtual void LoadContent(const ArticleEntry
& entry
,
33 LoadCallback callback
) = 0;
35 DistilledContentStore() {};
36 virtual ~DistilledContentStore() {};
39 DISALLOW_COPY_AND_ASSIGN(DistilledContentStore
);
42 // This content store keeps up to |max_num_entries| of the last accessed items
43 // in its cache. Both loading and saving content is counted as access.
44 // Lookup can be done based on entry ID or URL.
45 class InMemoryContentStore
: public DistilledContentStore
{
47 explicit InMemoryContentStore(const int max_num_entries
);
48 virtual ~InMemoryContentStore();
50 // DistilledContentStore implementation
51 virtual void SaveContent(const ArticleEntry
& entry
,
52 const DistilledArticleProto
& proto
,
53 SaveCallback callback
) OVERRIDE
;
54 virtual void LoadContent(const ArticleEntry
& entry
,
55 LoadCallback callback
) OVERRIDE
;
57 // Synchronously saves the content.
58 void InjectContent(const ArticleEntry
& entry
,
59 const DistilledArticleProto
& proto
);
62 // The CacheDeletor gets called when anything is removed from the ContentMap.
65 explicit CacheDeletor(InMemoryContentStore
* store
);
67 void operator()(const DistilledArticleProto
& proto
);
70 InMemoryContentStore
* store_
;
73 void AddUrlToIdMapping(const ArticleEntry
& entry
,
74 const DistilledArticleProto
& proto
);
76 void EraseUrlToIdMapping(const DistilledArticleProto
& proto
);
78 typedef base::MRUCacheBase
<std::string
,
79 DistilledArticleProto
,
80 InMemoryContentStore::CacheDeletor
> ContentMap
;
81 typedef base::hash_map
<std::string
, std::string
> UrlMap
;
89 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_