Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: components/dom_distiller/core/distilled_content_store.cc

Issue 314573005: [dom_distiller] Add support for lookup by URL for InMemoryContentStore. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/message_loop/message_loop.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) { 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
17 // after InMemoryContentStore has been destroyed.
18 cache_.Clear();
16 } 19 }
17 20
18 void InMemoryContentStore::SaveContent( 21 void InMemoryContentStore::SaveContent(
19 const ArticleEntry& entry, 22 const ArticleEntry& entry,
20 const DistilledArticleProto& proto, 23 const DistilledArticleProto& proto,
21 InMemoryContentStore::SaveCallback callback) { 24 InMemoryContentStore::SaveCallback callback) {
22 InjectContent(entry, proto); 25 InjectContent(entry, proto);
23 if (!callback.is_null()) { 26 if (!callback.is_null()) {
24 base::MessageLoop::current()->PostTask(FROM_HERE, 27 base::MessageLoop::current()->PostTask(FROM_HERE,
25 base::Bind(callback, true)); 28 base::Bind(callback, true));
26 } 29 }
27 } 30 }
28 31
29 void InMemoryContentStore::LoadContent( 32 void InMemoryContentStore::LoadContent(
30 const ArticleEntry& entry, 33 const ArticleEntry& entry,
31 InMemoryContentStore::LoadCallback callback) { 34 InMemoryContentStore::LoadCallback callback) {
32 if (callback.is_null()) 35 if (callback.is_null())
33 return; 36 return;
34 37
35 ContentMap::const_iterator it = cache_.Get(entry.entry_id()); 38 ContentMap::const_iterator it = cache_.Get(entry.entry_id());
36 bool success = it != cache_.end(); 39 bool success = it != cache_.end();
40 if (!success) {
41 // Could not find article by entry ID, so try looking it up by URL.
42 for (int i = 0; i < entry.pages_size(); ++i) {
43 URLMap::const_iterator url_it = url_to_id_.find(entry.pages(i).url());
44 if (url_it != url_to_id_.end()) {
45 it = cache_.Get(url_it->second);
46 success = it != cache_.end();
47 if (success) {
48 break;
49 }
50 }
51 }
52 }
37 scoped_ptr<DistilledArticleProto> distilled_article; 53 scoped_ptr<DistilledArticleProto> distilled_article;
38 if (success) { 54 if (success) {
39 distilled_article.reset(new DistilledArticleProto(it->second)); 55 distilled_article.reset(new DistilledArticleProto(it->second));
40 } else { 56 } else {
41 distilled_article.reset(new DistilledArticleProto()); 57 distilled_article.reset(new DistilledArticleProto());
42 } 58 }
43 base::MessageLoop::current()->PostTask( 59 base::MessageLoop::current()->PostTask(
44 FROM_HERE, 60 FROM_HERE,
45 base::Bind(callback, success, base::Passed(&distilled_article))); 61 base::Bind(callback, success, base::Passed(&distilled_article)));
46 } 62 }
47 63
48 void InMemoryContentStore::InjectContent(const ArticleEntry& entry, 64 void InMemoryContentStore::InjectContent(const ArticleEntry& entry,
49 const DistilledArticleProto& proto) { 65 const DistilledArticleProto& proto) {
50 cache_.Put(entry.entry_id(), proto); 66 cache_.Put(entry.entry_id(), proto);
67 AddUrlToIdMapping(entry, proto);
68 }
69
70 void InMemoryContentStore::AddUrlToIdMapping(
71 const ArticleEntry& entry,
72 const DistilledArticleProto& proto) {
73 for (int i = 0; i < proto.pages_size(); i++) {
74 const DistilledPageProto& page = proto.pages(i);
75 if (page.has_url()) {
76 url_to_id_.insert(
77 std::pair<std::string, std::string>(page.url(), entry.entry_id()));
cjhopman 2014/06/06 17:33:05 how about just: url_to_id_[page.url()] = entry.ent
nyquist 2014/06/06 19:00:21 Done.
78 }
79 }
80 }
81
82 void InMemoryContentStore::EraseUrlToIdMapping(
83 const DistilledArticleProto& proto) {
84 for (int i = 0; i < proto.pages_size(); i++) {
85 const DistilledPageProto& page = proto.pages(i);
86 if (page.has_url()) {
87 url_to_id_.erase(page.url());
88 }
89 }
90 }
91
92 InMemoryContentStore::CacheDeletor::CacheDeletor(InMemoryContentStore* store)
93 : store_(store) {
94 }
95
96 InMemoryContentStore::CacheDeletor::~CacheDeletor() {
97 }
98
99 void InMemoryContentStore::CacheDeletor::operator()(
100 const DistilledArticleProto& proto) {
101 // When InMemoryContentStore is deleted, the |store_| pointer becomes invalid,
102 // but since the ContentMap is cleared in the InMemoryContentStore destructor,
103 // this should never be called after the destructor.
104 store_->EraseUrlToIdMapping(proto);
51 } 105 }
52 106
53 } // namespace dom_distiller 107 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698