Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "components/dom_distiller/core/distilled_content_store.h" | |
| 9 #include "components/dom_distiller/core/dom_distiller_store.h" | 10 #include "components/dom_distiller/core/dom_distiller_store.h" |
| 10 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 11 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 11 #include "components/dom_distiller/core/task_tracker.h" | 12 #include "components/dom_distiller/core/task_tracker.h" |
| 12 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 13 | 14 |
| 14 namespace dom_distiller { | 15 namespace dom_distiller { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 ArticleEntry CreateSkeletonEntryForUrl(const GURL& url) { | 19 ArticleEntry CreateSkeletonEntryForUrl(const GURL& url) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 31 const DistilledArticleProto* article_proto, | 32 const DistilledArticleProto* article_proto, |
| 32 bool distillation_succeeded) { | 33 bool distillation_succeeded) { |
| 33 article_cb.Run(distillation_succeeded); | 34 article_cb.Run(distillation_succeeded); |
| 34 } | 35 } |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| 37 | 38 |
| 38 DomDistillerService::DomDistillerService( | 39 DomDistillerService::DomDistillerService( |
| 39 scoped_ptr<DomDistillerStoreInterface> store, | 40 scoped_ptr<DomDistillerStoreInterface> store, |
| 40 scoped_ptr<DistillerFactory> distiller_factory) | 41 scoped_ptr<DistillerFactory> distiller_factory) |
| 41 : store_(store.Pass()), distiller_factory_(distiller_factory.Pass()) {} | 42 : store_(store.Pass()), |
| 43 content_store_(new InMemoryContentStore()), | |
| 44 distiller_factory_(distiller_factory.Pass()) {} | |
| 42 | 45 |
| 43 DomDistillerService::~DomDistillerService() {} | 46 DomDistillerService::~DomDistillerService() {} |
| 44 | 47 |
| 45 syncer::SyncableService* DomDistillerService::GetSyncableService() const { | 48 syncer::SyncableService* DomDistillerService::GetSyncableService() const { |
| 46 return store_->GetSyncableService(); | 49 return store_->GetSyncableService(); |
| 47 } | 50 } |
| 48 | 51 |
| 49 const std::string DomDistillerService::AddToList( | 52 const std::string DomDistillerService::AddToList( |
| 50 const GURL& url, | 53 const GURL& url, |
| 51 const ArticleAvailableCallback& article_cb) { | 54 const ArticleAvailableCallback& article_cb) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 71 } | 74 } |
| 72 | 75 |
| 73 if (!article_cb.is_null()) { | 76 if (!article_cb.is_null()) { |
| 74 task_tracker->AddSaveCallback( | 77 task_tracker->AddSaveCallback( |
| 75 base::Bind(&RunArticleAvailableCallback, article_cb)); | 78 base::Bind(&RunArticleAvailableCallback, article_cb)); |
| 76 } | 79 } |
| 77 | 80 |
| 78 if (!is_already_added) { | 81 if (!is_already_added) { |
| 79 task_tracker->AddSaveCallback(base::Bind( | 82 task_tracker->AddSaveCallback(base::Bind( |
| 80 &DomDistillerService::AddDistilledPageToList, base::Unretained(this))); | 83 &DomDistillerService::AddDistilledPageToList, base::Unretained(this))); |
| 84 task_tracker->AddSaveCallback( | |
| 85 base::Bind(&DomDistillerService::AddDistilledContentToStore, | |
| 86 base::Unretained(this))); | |
| 81 task_tracker->StartDistiller(distiller_factory_.get()); | 87 task_tracker->StartDistiller(distiller_factory_.get()); |
| 88 task_tracker->StartBlobFetcher(); | |
| 82 } | 89 } |
| 83 | 90 |
| 84 return task_tracker->GetEntryId(); | 91 return task_tracker->GetEntryId(); |
| 85 } | 92 } |
| 86 | 93 |
| 87 std::vector<ArticleEntry> DomDistillerService::GetEntries() const { | 94 std::vector<ArticleEntry> DomDistillerService::GetEntries() const { |
| 88 return store_->GetEntries(); | 95 return store_->GetEntries(); |
| 89 } | 96 } |
| 90 | 97 |
| 91 scoped_ptr<ArticleEntry> DomDistillerService::RemoveEntry( | 98 scoped_ptr<ArticleEntry> DomDistillerService::RemoveEntry( |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 110 scoped_ptr<ViewerHandle> DomDistillerService::ViewEntry( | 117 scoped_ptr<ViewerHandle> DomDistillerService::ViewEntry( |
| 111 ViewRequestDelegate* delegate, | 118 ViewRequestDelegate* delegate, |
| 112 const std::string& entry_id) { | 119 const std::string& entry_id) { |
| 113 ArticleEntry entry; | 120 ArticleEntry entry; |
| 114 if (!store_->GetEntryById(entry_id, &entry)) { | 121 if (!store_->GetEntryById(entry_id, &entry)) { |
| 115 return scoped_ptr<ViewerHandle>(); | 122 return scoped_ptr<ViewerHandle>(); |
| 116 } | 123 } |
| 117 | 124 |
| 118 TaskTracker* task_tracker = GetOrCreateTaskTrackerForEntry(entry); | 125 TaskTracker* task_tracker = GetOrCreateTaskTrackerForEntry(entry); |
| 119 scoped_ptr<ViewerHandle> viewer_handle = task_tracker->AddViewer(delegate); | 126 scoped_ptr<ViewerHandle> viewer_handle = task_tracker->AddViewer(delegate); |
| 127 task_tracker->AddSaveCallback( | |
| 128 base::Bind(&DomDistillerService::AddDistilledContentToStore, | |
| 129 base::Unretained(this))); | |
| 120 task_tracker->StartDistiller(distiller_factory_.get()); | 130 task_tracker->StartDistiller(distiller_factory_.get()); |
| 131 task_tracker->StartBlobFetcher(); | |
| 121 | 132 |
| 122 return viewer_handle.Pass(); | 133 return viewer_handle.Pass(); |
| 123 } | 134 } |
| 124 | 135 |
| 125 scoped_ptr<ViewerHandle> DomDistillerService::ViewUrl( | 136 scoped_ptr<ViewerHandle> DomDistillerService::ViewUrl( |
| 126 ViewRequestDelegate* delegate, | 137 ViewRequestDelegate* delegate, |
| 127 const GURL& url) { | 138 const GURL& url) { |
| 128 if (!url.is_valid()) { | 139 if (!url.is_valid()) { |
| 129 return scoped_ptr<ViewerHandle>(); | 140 return scoped_ptr<ViewerHandle>(); |
| 130 } | 141 } |
| 131 | 142 |
| 132 TaskTracker* task_tracker = GetOrCreateTaskTrackerForUrl(url); | 143 TaskTracker* task_tracker = GetOrCreateTaskTrackerForUrl(url); |
| 133 scoped_ptr<ViewerHandle> viewer_handle = task_tracker->AddViewer(delegate); | 144 scoped_ptr<ViewerHandle> viewer_handle = task_tracker->AddViewer(delegate); |
| 145 task_tracker->AddSaveCallback( | |
| 146 base::Bind(&DomDistillerService::AddDistilledContentToStore, | |
|
shashi
2014/03/13 00:32:45
Feels like these 3 lines can be now refactored to
| |
| 147 base::Unretained(this))); | |
| 134 task_tracker->StartDistiller(distiller_factory_.get()); | 148 task_tracker->StartDistiller(distiller_factory_.get()); |
| 149 task_tracker->StartBlobFetcher(); | |
| 135 | 150 |
| 136 return viewer_handle.Pass(); | 151 return viewer_handle.Pass(); |
| 137 } | 152 } |
| 138 | 153 |
| 139 TaskTracker* DomDistillerService::GetOrCreateTaskTrackerForUrl( | 154 TaskTracker* DomDistillerService::GetOrCreateTaskTrackerForUrl( |
| 140 const GURL& url) { | 155 const GURL& url) { |
| 141 ArticleEntry entry; | 156 ArticleEntry entry; |
| 142 if (store_->GetEntryByUrl(url, &entry)) { | 157 if (store_->GetEntryByUrl(url, &entry)) { |
| 143 return GetOrCreateTaskTrackerForEntry(entry); | 158 return GetOrCreateTaskTrackerForEntry(entry); |
| 144 } | 159 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 170 TaskTracker* task_tracker = GetTaskTrackerForEntry(entry); | 185 TaskTracker* task_tracker = GetTaskTrackerForEntry(entry); |
| 171 if (task_tracker == NULL) { | 186 if (task_tracker == NULL) { |
| 172 task_tracker = CreateTaskTracker(entry); | 187 task_tracker = CreateTaskTracker(entry); |
| 173 } | 188 } |
| 174 return task_tracker; | 189 return task_tracker; |
| 175 } | 190 } |
| 176 | 191 |
| 177 TaskTracker* DomDistillerService::CreateTaskTracker(const ArticleEntry& entry) { | 192 TaskTracker* DomDistillerService::CreateTaskTracker(const ArticleEntry& entry) { |
| 178 TaskTracker::CancelCallback cancel_callback = | 193 TaskTracker::CancelCallback cancel_callback = |
| 179 base::Bind(&DomDistillerService::CancelTask, base::Unretained(this)); | 194 base::Bind(&DomDistillerService::CancelTask, base::Unretained(this)); |
| 180 TaskTracker* tracker = new TaskTracker(entry, cancel_callback); | 195 TaskTracker* tracker = |
| 196 new TaskTracker(entry, cancel_callback, content_store_.get()); | |
| 181 tasks_.push_back(tracker); | 197 tasks_.push_back(tracker); |
| 182 return tracker; | 198 return tracker; |
| 183 } | 199 } |
| 184 | 200 |
| 185 void DomDistillerService::CancelTask(TaskTracker* task) { | 201 void DomDistillerService::CancelTask(TaskTracker* task) { |
| 186 TaskList::iterator it = std::find(tasks_.begin(), tasks_.end(), task); | 202 TaskList::iterator it = std::find(tasks_.begin(), tasks_.end(), task); |
| 187 if (it != tasks_.end()) { | 203 if (it != tasks_.end()) { |
| 188 tasks_.weak_erase(it); | 204 tasks_.weak_erase(it); |
| 189 base::MessageLoop::current()->DeleteSoon(FROM_HERE, task); | 205 base::MessageLoop::current()->DeleteSoon(FROM_HERE, task); |
| 190 } | 206 } |
| 191 } | 207 } |
| 192 | 208 |
| 193 void DomDistillerService::AddDistilledPageToList( | 209 void DomDistillerService::AddDistilledPageToList( |
| 194 const ArticleEntry& entry, | 210 const ArticleEntry& entry, |
| 195 const DistilledArticleProto* article_proto, | 211 const DistilledArticleProto* article_proto, |
| 196 bool distillation_succeeded) { | 212 bool distillation_succeeded) { |
| 197 DCHECK(IsEntryValid(entry)); | 213 DCHECK(IsEntryValid(entry)); |
| 198 if (distillation_succeeded) { | 214 if (distillation_succeeded) { |
| 199 DCHECK(article_proto); | 215 DCHECK(article_proto); |
| 200 DCHECK_GT(article_proto->pages_size(), 0); | 216 DCHECK_GT(article_proto->pages_size(), 0); |
| 201 store_->AddEntry(entry); | 217 store_->AddEntry(entry); |
| 202 DCHECK_EQ(article_proto->pages_size(), entry.pages_size()); | 218 DCHECK_EQ(article_proto->pages_size(), entry.pages_size()); |
| 203 } | 219 } |
| 204 } | 220 } |
| 205 | 221 |
| 222 void DomDistillerService::AddDistilledContentToStore( | |
| 223 const ArticleEntry& entry, | |
| 224 const DistilledArticleProto* article_proto, | |
| 225 bool distillation_succeeded) { | |
| 226 DCHECK(IsEntryValid(entry)); | |
| 227 if (distillation_succeeded) { | |
| 228 DCHECK(article_proto); | |
| 229 DCHECK_GT(article_proto->pages_size(), 0); | |
| 230 content_store_->SaveContent( | |
| 231 entry, *article_proto, DistilledContentStore::SaveCallback()); | |
| 232 DCHECK_EQ(article_proto->pages_size(), entry.pages_size()); | |
| 233 } | |
| 234 } | |
| 235 | |
| 206 void DomDistillerService::AddObserver(DomDistillerObserver* observer) { | 236 void DomDistillerService::AddObserver(DomDistillerObserver* observer) { |
| 207 DCHECK(observer); | 237 DCHECK(observer); |
| 208 store_->AddObserver(observer); | 238 store_->AddObserver(observer); |
| 209 } | 239 } |
| 210 | 240 |
| 211 void DomDistillerService::RemoveObserver(DomDistillerObserver* observer) { | 241 void DomDistillerService::RemoveObserver(DomDistillerObserver* observer) { |
| 212 DCHECK(observer); | 242 DCHECK(observer); |
| 213 store_->RemoveObserver(observer); | 243 store_->RemoveObserver(observer); |
| 214 } | 244 } |
| 215 | 245 |
| 216 } // namespace dom_distiller | 246 } // namespace dom_distiller |
| OLD | NEW |