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

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

Issue 2667533003: Allow DomDistillerService to have no store. (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « no previous file | ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/dom_distiller_service.h" 5 #include "components/dom_distiller/core/dom_distiller_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/guid.h" 9 #include "base/guid.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 : store_(std::move(store)), 48 : store_(std::move(store)),
49 content_store_(new InMemoryContentStore(kDefaultMaxNumCachedEntries)), 49 content_store_(new InMemoryContentStore(kDefaultMaxNumCachedEntries)),
50 distiller_factory_(std::move(distiller_factory)), 50 distiller_factory_(std::move(distiller_factory)),
51 distiller_page_factory_(std::move(distiller_page_factory)), 51 distiller_page_factory_(std::move(distiller_page_factory)),
52 distilled_page_prefs_(std::move(distilled_page_prefs)) {} 52 distilled_page_prefs_(std::move(distilled_page_prefs)) {}
53 53
54 DomDistillerService::~DomDistillerService() { 54 DomDistillerService::~DomDistillerService() {
55 } 55 }
56 56
57 syncer::SyncableService* DomDistillerService::GetSyncableService() const { 57 syncer::SyncableService* DomDistillerService::GetSyncableService() const {
58 if (!store_) {
59 return nullptr;
60 }
58 return store_->GetSyncableService(); 61 return store_->GetSyncableService();
59 } 62 }
60 63
61 std::unique_ptr<DistillerPage> DomDistillerService::CreateDefaultDistillerPage( 64 std::unique_ptr<DistillerPage> DomDistillerService::CreateDefaultDistillerPage(
62 const gfx::Size& render_view_size) { 65 const gfx::Size& render_view_size) {
63 return distiller_page_factory_->CreateDistillerPage(render_view_size); 66 return distiller_page_factory_->CreateDistillerPage(render_view_size);
64 } 67 }
65 68
66 std::unique_ptr<DistillerPage> 69 std::unique_ptr<DistillerPage>
67 DomDistillerService::CreateDefaultDistillerPageWithHandle( 70 DomDistillerService::CreateDefaultDistillerPageWithHandle(
68 std::unique_ptr<SourcePageHandle> handle) { 71 std::unique_ptr<SourcePageHandle> handle) {
69 return distiller_page_factory_->CreateDistillerPageWithHandle( 72 return distiller_page_factory_->CreateDistillerPageWithHandle(
70 std::move(handle)); 73 std::move(handle));
71 } 74 }
72 75
73 const std::string DomDistillerService::AddToList( 76 const std::string DomDistillerService::AddToList(
74 const GURL& url, 77 const GURL& url,
75 std::unique_ptr<DistillerPage> distiller_page, 78 std::unique_ptr<DistillerPage> distiller_page,
76 const ArticleAvailableCallback& article_cb) { 79 const ArticleAvailableCallback& article_cb) {
77 ArticleEntry entry; 80 ArticleEntry entry;
78 const bool is_already_added = store_->GetEntryByUrl(url, &entry); 81 const bool is_already_added = store_ && store_->GetEntryByUrl(url, &entry);
79 82
80 TaskTracker* task_tracker = nullptr; 83 TaskTracker* task_tracker = nullptr;
81 if (is_already_added) { 84 if (is_already_added) {
82 task_tracker = GetTaskTrackerForEntry(entry); 85 task_tracker = GetTaskTrackerForEntry(entry);
83 if (task_tracker == NULL) { 86 if (task_tracker == NULL) {
84 // Entry is in the store but there is no task tracker. This could 87 // Entry is in the store but there is no task tracker. This could
85 // happen when distillation has already completed. For now just return 88 // happen when distillation has already completed. For now just return
86 // true. 89 // true.
87 // TODO(shashishekhar): Change this to check if article is available, 90 // TODO(shashishekhar): Change this to check if article is available,
88 // An article may not be available for a variety of reasons, e.g. 91 // An article may not be available for a variety of reasons, e.g.
(...skipping 16 matching lines...) Expand all
105 &DomDistillerService::AddDistilledPageToList, base::Unretained(this))); 108 &DomDistillerService::AddDistilledPageToList, base::Unretained(this)));
106 task_tracker->StartDistiller(distiller_factory_.get(), 109 task_tracker->StartDistiller(distiller_factory_.get(),
107 std::move(distiller_page)); 110 std::move(distiller_page));
108 task_tracker->StartBlobFetcher(); 111 task_tracker->StartBlobFetcher();
109 } 112 }
110 113
111 return task_tracker->GetEntryId(); 114 return task_tracker->GetEntryId();
112 } 115 }
113 116
114 bool DomDistillerService::HasEntry(const std::string& entry_id) { 117 bool DomDistillerService::HasEntry(const std::string& entry_id) {
115 return store_->GetEntryById(entry_id, NULL); 118 return store_ && store_->GetEntryById(entry_id, NULL);
116 } 119 }
117 120
118 std::string DomDistillerService::GetUrlForEntry(const std::string& entry_id) { 121 std::string DomDistillerService::GetUrlForEntry(const std::string& entry_id) {
119 ArticleEntry entry; 122 ArticleEntry entry;
120 if (store_->GetEntryById(entry_id, &entry)) { 123 if (store_ && store_->GetEntryById(entry_id, &entry)) {
121 return entry.pages().Get(0).url(); 124 return entry.pages().Get(0).url();
122 } 125 }
123 return ""; 126 return "";
124 } 127 }
125 128
126 std::vector<ArticleEntry> DomDistillerService::GetEntries() const { 129 std::vector<ArticleEntry> DomDistillerService::GetEntries() const {
130 if (!store_) {
131 return std::vector<ArticleEntry>();
132 }
127 return store_->GetEntries(); 133 return store_->GetEntries();
128 } 134 }
129 135
130 std::unique_ptr<ArticleEntry> DomDistillerService::RemoveEntry( 136 std::unique_ptr<ArticleEntry> DomDistillerService::RemoveEntry(
131 const std::string& entry_id) { 137 const std::string& entry_id) {
132 std::unique_ptr<ArticleEntry> entry(new ArticleEntry); 138 std::unique_ptr<ArticleEntry> entry(new ArticleEntry);
133 entry->set_entry_id(entry_id); 139 entry->set_entry_id(entry_id);
134 TaskTracker* task_tracker = GetTaskTrackerForEntry(*entry); 140 TaskTracker* task_tracker = GetTaskTrackerForEntry(*entry);
135 if (task_tracker != NULL) { 141 if (task_tracker != NULL) {
136 task_tracker->CancelSaveCallbacks(); 142 task_tracker->CancelSaveCallbacks();
137 } 143 }
138 144
139 if (!store_->GetEntryById(entry_id, entry.get())) { 145 if (!store_ || !store_->GetEntryById(entry_id, entry.get())) {
140 return std::unique_ptr<ArticleEntry>(); 146 return std::unique_ptr<ArticleEntry>();
141 } 147 }
142 148
143 if (store_->RemoveEntry(*entry)) { 149 if (store_->RemoveEntry(*entry)) {
144 return entry; 150 return entry;
145 } 151 }
146 return std::unique_ptr<ArticleEntry>(); 152 return std::unique_ptr<ArticleEntry>();
147 } 153 }
148 154
149 std::unique_ptr<ViewerHandle> DomDistillerService::ViewEntry( 155 std::unique_ptr<ViewerHandle> DomDistillerService::ViewEntry(
150 ViewRequestDelegate* delegate, 156 ViewRequestDelegate* delegate,
151 std::unique_ptr<DistillerPage> distiller_page, 157 std::unique_ptr<DistillerPage> distiller_page,
152 const std::string& entry_id) { 158 const std::string& entry_id) {
153 ArticleEntry entry; 159 ArticleEntry entry;
154 if (!store_->GetEntryById(entry_id, &entry)) { 160 if (!store_ || !store_->GetEntryById(entry_id, &entry)) {
155 return std::unique_ptr<ViewerHandle>(); 161 return std::unique_ptr<ViewerHandle>();
156 } 162 }
157 163
158 TaskTracker* task_tracker = nullptr; 164 TaskTracker* task_tracker = nullptr;
159 bool was_created = GetOrCreateTaskTrackerForEntry(entry, &task_tracker); 165 bool was_created = GetOrCreateTaskTrackerForEntry(entry, &task_tracker);
160 std::unique_ptr<ViewerHandle> viewer_handle = 166 std::unique_ptr<ViewerHandle> viewer_handle =
161 task_tracker->AddViewer(delegate); 167 task_tracker->AddViewer(delegate);
162 if (was_created) { 168 if (was_created) {
163 task_tracker->StartDistiller(distiller_factory_.get(), 169 task_tracker->StartDistiller(distiller_factory_.get(),
164 std::move(distiller_page)); 170 std::move(distiller_page));
(...skipping 22 matching lines...) Expand all
187 task_tracker->StartBlobFetcher(); 193 task_tracker->StartBlobFetcher();
188 } 194 }
189 195
190 return viewer_handle; 196 return viewer_handle;
191 } 197 }
192 198
193 bool DomDistillerService::GetOrCreateTaskTrackerForUrl( 199 bool DomDistillerService::GetOrCreateTaskTrackerForUrl(
194 const GURL& url, 200 const GURL& url,
195 TaskTracker** task_tracker) { 201 TaskTracker** task_tracker) {
196 ArticleEntry entry; 202 ArticleEntry entry;
197 if (store_->GetEntryByUrl(url, &entry)) { 203 if (store_ && store_->GetEntryByUrl(url, &entry)) {
198 return GetOrCreateTaskTrackerForEntry(entry, task_tracker); 204 return GetOrCreateTaskTrackerForEntry(entry, task_tracker);
199 } 205 }
200 206
201 *task_tracker = GetTaskTrackerForUrl(url); 207 *task_tracker = GetTaskTrackerForUrl(url);
202 if (*task_tracker) { 208 if (*task_tracker) {
203 return false; 209 return false;
204 } 210 }
205 211
206 ArticleEntry skeleton_entry = CreateSkeletonEntryForUrl(url); 212 ArticleEntry skeleton_entry = CreateSkeletonEntryForUrl(url);
207 *task_tracker = CreateTaskTracker(skeleton_entry); 213 *task_tracker = CreateTaskTracker(skeleton_entry);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 tasks_.weak_erase(it); 260 tasks_.weak_erase(it);
255 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, task); 261 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, task);
256 } 262 }
257 } 263 }
258 264
259 void DomDistillerService::AddDistilledPageToList( 265 void DomDistillerService::AddDistilledPageToList(
260 const ArticleEntry& entry, 266 const ArticleEntry& entry,
261 const DistilledArticleProto* article_proto, 267 const DistilledArticleProto* article_proto,
262 bool distillation_succeeded) { 268 bool distillation_succeeded) {
263 DCHECK(IsEntryValid(entry)); 269 DCHECK(IsEntryValid(entry));
264 if (distillation_succeeded) { 270 if (store_ && distillation_succeeded) {
265 DCHECK(article_proto); 271 DCHECK(article_proto);
266 DCHECK_GT(article_proto->pages_size(), 0); 272 DCHECK_GT(article_proto->pages_size(), 0);
267 store_->AddEntry(entry); 273 store_->AddEntry(entry);
268 DCHECK_EQ(article_proto->pages_size(), entry.pages_size()); 274 DCHECK_EQ(article_proto->pages_size(), entry.pages_size());
269 } 275 }
270 } 276 }
271 277
272 void DomDistillerService::AddObserver(DomDistillerObserver* observer) { 278 void DomDistillerService::AddObserver(DomDistillerObserver* observer) {
273 DCHECK(observer); 279 DCHECK(observer);
274 store_->AddObserver(observer); 280 if (store_) {
281 store_->AddObserver(observer);
282 }
275 } 283 }
276 284
277 void DomDistillerService::RemoveObserver(DomDistillerObserver* observer) { 285 void DomDistillerService::RemoveObserver(DomDistillerObserver* observer) {
278 DCHECK(observer); 286 DCHECK(observer);
279 store_->RemoveObserver(observer); 287 if (store_) {
288 store_->RemoveObserver(observer);
289 }
280 } 290 }
281 291
282 DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() { 292 DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() {
283 return distilled_page_prefs_.get(); 293 return distilled_page_prefs_.get();
284 } 294 }
285 295
286 } // namespace dom_distiller 296 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « no previous file | ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698