Don't preload rarely seen large images
[chromium-blink-merge.git] / components / dom_distiller / core / distilled_content_store.h
blob0063dd37166eb4b993277aaafaa0861c12f05d13
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_
8 #include <string>
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
22 // ArticleEntry.
23 class DistilledContentStore {
24 public:
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() {};
38 private:
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 {
46 public:
47 explicit InMemoryContentStore(const int max_num_entries);
48 ~InMemoryContentStore() override;
50 // DistilledContentStore implementation
51 void SaveContent(const ArticleEntry& entry,
52 const DistilledArticleProto& proto,
53 SaveCallback callback) override;
54 void LoadContent(const ArticleEntry& entry, LoadCallback callback) override;
56 // Synchronously saves the content.
57 void InjectContent(const ArticleEntry& entry,
58 const DistilledArticleProto& proto);
60 private:
61 // The CacheDeletor gets called when anything is removed from the ContentMap.
62 class CacheDeletor {
63 public:
64 explicit CacheDeletor(InMemoryContentStore* store);
65 ~CacheDeletor();
66 void operator()(const DistilledArticleProto& proto);
68 private:
69 InMemoryContentStore* store_;
72 void AddUrlToIdMapping(const ArticleEntry& entry,
73 const DistilledArticleProto& proto);
75 void EraseUrlToIdMapping(const DistilledArticleProto& proto);
77 typedef base::MRUCacheBase<std::string,
78 DistilledArticleProto,
79 InMemoryContentStore::CacheDeletor> ContentMap;
80 typedef base::hash_map<std::string, std::string> UrlMap;
82 ContentMap cache_;
83 UrlMap url_to_id_;
86 } // dom_distiller
88 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_CONTENT_CACHE_H_