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 #include "components/dom_distiller/core/distilled_content_store.h" | 5 #include "components/dom_distiller/core/distilled_content_store.h" |
6 | 6 |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
8 | 8 |
9 namespace dom_distiller { | 9 namespace dom_distiller { |
10 | 10 |
11 InMemoryContentStore::InMemoryContentStore(const int max_num_entries) | 11 InMemoryContentStore::InMemoryContentStore(const int max_num_entries) |
12 : cache_(max_num_entries, CacheDeletor(this)) { | 12 : cache_(max_num_entries) {} |
13 } | |
14 | 13 |
15 InMemoryContentStore::~InMemoryContentStore() { | 14 InMemoryContentStore::~InMemoryContentStore() { |
16 // Clear the cache before destruction to ensure the CacheDeletor is not called | 15 // Clear the cache before destruction to ensure the CacheDeletor is not called |
17 // after InMemoryContentStore has been destroyed. | 16 // after InMemoryContentStore has been destroyed. |
18 cache_.Clear(); | 17 cache_.Clear(); |
19 } | 18 } |
20 | 19 |
21 void InMemoryContentStore::SaveContent( | 20 void InMemoryContentStore::SaveContent( |
22 const ArticleEntry& entry, | 21 const ArticleEntry& entry, |
23 const DistilledArticleProto& proto, | 22 const DistilledArticleProto& proto, |
(...skipping 21 matching lines...) Expand all Loading... |
45 it = cache_.Get(url_it->second); | 44 it = cache_.Get(url_it->second); |
46 success = it != cache_.end(); | 45 success = it != cache_.end(); |
47 if (success) { | 46 if (success) { |
48 break; | 47 break; |
49 } | 48 } |
50 } | 49 } |
51 } | 50 } |
52 } | 51 } |
53 scoped_ptr<DistilledArticleProto> distilled_article; | 52 scoped_ptr<DistilledArticleProto> distilled_article; |
54 if (success) { | 53 if (success) { |
55 distilled_article.reset(new DistilledArticleProto(it->second)); | 54 distilled_article.reset(new DistilledArticleProto(*it->second)); |
56 } else { | 55 } else { |
57 distilled_article.reset(new DistilledArticleProto()); | 56 distilled_article.reset(new DistilledArticleProto()); |
58 } | 57 } |
59 base::ThreadTaskRunnerHandle::Get()->PostTask( | 58 base::ThreadTaskRunnerHandle::Get()->PostTask( |
60 FROM_HERE, | 59 FROM_HERE, |
61 base::Bind(callback, success, base::Passed(&distilled_article))); | 60 base::Bind(callback, success, base::Passed(&distilled_article))); |
62 } | 61 } |
63 | 62 |
64 void InMemoryContentStore::InjectContent(const ArticleEntry& entry, | 63 void InMemoryContentStore::InjectContent(const ArticleEntry& entry, |
65 const DistilledArticleProto& proto) { | 64 const DistilledArticleProto& proto) { |
66 cache_.Put(entry.entry_id(), proto); | 65 cache_.Put(entry.entry_id(), |
| 66 scoped_ptr<DistilledArticleProto, CacheDeletor>( |
| 67 new DistilledArticleProto(proto), CacheDeletor(this))); |
67 AddUrlToIdMapping(entry, proto); | 68 AddUrlToIdMapping(entry, proto); |
68 } | 69 } |
69 | 70 |
70 void InMemoryContentStore::AddUrlToIdMapping( | 71 void InMemoryContentStore::AddUrlToIdMapping( |
71 const ArticleEntry& entry, | 72 const ArticleEntry& entry, |
72 const DistilledArticleProto& proto) { | 73 const DistilledArticleProto& proto) { |
73 for (int i = 0; i < proto.pages_size(); i++) { | 74 for (int i = 0; i < proto.pages_size(); i++) { |
74 const DistilledPageProto& page = proto.pages(i); | 75 const DistilledPageProto& page = proto.pages(i); |
75 if (page.has_url()) { | 76 if (page.has_url()) { |
76 url_to_id_[page.url()] = entry.entry_id(); | 77 url_to_id_[page.url()] = entry.entry_id(); |
(...skipping 12 matching lines...) Expand all Loading... |
89 } | 90 } |
90 | 91 |
91 InMemoryContentStore::CacheDeletor::CacheDeletor(InMemoryContentStore* store) | 92 InMemoryContentStore::CacheDeletor::CacheDeletor(InMemoryContentStore* store) |
92 : store_(store) { | 93 : store_(store) { |
93 } | 94 } |
94 | 95 |
95 InMemoryContentStore::CacheDeletor::~CacheDeletor() { | 96 InMemoryContentStore::CacheDeletor::~CacheDeletor() { |
96 } | 97 } |
97 | 98 |
98 void InMemoryContentStore::CacheDeletor::operator()( | 99 void InMemoryContentStore::CacheDeletor::operator()( |
99 const DistilledArticleProto& proto) { | 100 DistilledArticleProto* proto) { |
100 // When InMemoryContentStore is deleted, the |store_| pointer becomes invalid, | 101 // When InMemoryContentStore is deleted, the |store_| pointer becomes invalid, |
101 // but since the ContentMap is cleared in the InMemoryContentStore destructor, | 102 // but since the ContentMap is cleared in the InMemoryContentStore destructor, |
102 // this should never be called after the destructor. | 103 // this should never be called after the destructor. |
103 store_->EraseUrlToIdMapping(proto); | 104 store_->EraseUrlToIdMapping(*proto); |
| 105 delete proto; |
104 } | 106 } |
105 | 107 |
106 } // namespace dom_distiller | 108 } // namespace dom_distiller |
OLD | NEW |