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/message_loop/message_loop.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, CacheDeletor(this)) { |
13 } | 13 } |
14 | 14 |
15 InMemoryContentStore::~InMemoryContentStore() { | 15 InMemoryContentStore::~InMemoryContentStore() { |
16 // Clear the cache before destruction to ensure the CacheDeletor is not called | 16 // Clear the cache before destruction to ensure the CacheDeletor is not called |
17 // after InMemoryContentStore has been destroyed. | 17 // after InMemoryContentStore has been destroyed. |
18 cache_.Clear(); | 18 cache_.Clear(); |
19 } | 19 } |
20 | 20 |
21 void InMemoryContentStore::SaveContent( | 21 void InMemoryContentStore::SaveContent( |
22 const ArticleEntry& entry, | 22 const ArticleEntry& entry, |
23 const DistilledArticleProto& proto, | 23 const DistilledArticleProto& proto, |
24 InMemoryContentStore::SaveCallback callback) { | 24 InMemoryContentStore::SaveCallback callback) { |
25 InjectContent(entry, proto); | 25 InjectContent(entry, proto); |
26 if (!callback.is_null()) { | 26 if (!callback.is_null()) { |
27 base::MessageLoop::current()->PostTask(FROM_HERE, | 27 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
28 base::Bind(callback, true)); | 28 base::Bind(callback, true)); |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 void InMemoryContentStore::LoadContent( | 32 void InMemoryContentStore::LoadContent( |
33 const ArticleEntry& entry, | 33 const ArticleEntry& entry, |
34 InMemoryContentStore::LoadCallback callback) { | 34 InMemoryContentStore::LoadCallback callback) { |
35 if (callback.is_null()) | 35 if (callback.is_null()) |
36 return; | 36 return; |
37 | 37 |
38 ContentMap::const_iterator it = cache_.Get(entry.entry_id()); | 38 ContentMap::const_iterator it = cache_.Get(entry.entry_id()); |
(...skipping 10 matching lines...) Expand all Loading... |
49 } | 49 } |
50 } | 50 } |
51 } | 51 } |
52 } | 52 } |
53 scoped_ptr<DistilledArticleProto> distilled_article; | 53 scoped_ptr<DistilledArticleProto> distilled_article; |
54 if (success) { | 54 if (success) { |
55 distilled_article.reset(new DistilledArticleProto(it->second)); | 55 distilled_article.reset(new DistilledArticleProto(it->second)); |
56 } else { | 56 } else { |
57 distilled_article.reset(new DistilledArticleProto()); | 57 distilled_article.reset(new DistilledArticleProto()); |
58 } | 58 } |
59 base::MessageLoop::current()->PostTask( | 59 base::ThreadTaskRunnerHandle::Get()->PostTask( |
60 FROM_HERE, | 60 FROM_HERE, |
61 base::Bind(callback, success, base::Passed(&distilled_article))); | 61 base::Bind(callback, success, base::Passed(&distilled_article))); |
62 } | 62 } |
63 | 63 |
64 void InMemoryContentStore::InjectContent(const ArticleEntry& entry, | 64 void InMemoryContentStore::InjectContent(const ArticleEntry& entry, |
65 const DistilledArticleProto& proto) { | 65 const DistilledArticleProto& proto) { |
66 cache_.Put(entry.entry_id(), proto); | 66 cache_.Put(entry.entry_id(), proto); |
67 AddUrlToIdMapping(entry, proto); | 67 AddUrlToIdMapping(entry, proto); |
68 } | 68 } |
69 | 69 |
(...skipping 27 matching lines...) Expand all Loading... |
97 | 97 |
98 void InMemoryContentStore::CacheDeletor::operator()( | 98 void InMemoryContentStore::CacheDeletor::operator()( |
99 const DistilledArticleProto& proto) { | 99 const DistilledArticleProto& proto) { |
100 // When InMemoryContentStore is deleted, the |store_| pointer becomes invalid, | 100 // When InMemoryContentStore is deleted, the |store_| pointer becomes invalid, |
101 // but since the ContentMap is cleared in the InMemoryContentStore destructor, | 101 // but since the ContentMap is cleared in the InMemoryContentStore destructor, |
102 // this should never be called after the destructor. | 102 // this should never be called after the destructor. |
103 store_->EraseUrlToIdMapping(proto); | 103 store_->EraseUrlToIdMapping(proto); |
104 } | 104 } |
105 | 105 |
106 } // namespace dom_distiller | 106 } // namespace dom_distiller |
OLD | NEW |