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/task_tracker.h" | 5 #include "components/dom_distiller/core/task_tracker.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.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/proto/distilled_article.pb.h" | 10 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
10 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 11 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
11 | 12 |
12 namespace dom_distiller { | 13 namespace dom_distiller { |
13 | 14 |
14 ViewerHandle::ViewerHandle(CancelCallback callback) | 15 ViewerHandle::ViewerHandle(CancelCallback callback) |
15 : cancel_callback_(callback) {} | 16 : cancel_callback_(callback) {} |
16 | 17 |
17 ViewerHandle::~ViewerHandle() { | 18 ViewerHandle::~ViewerHandle() { |
18 if (!cancel_callback_.is_null()) { | 19 if (!cancel_callback_.is_null()) { |
19 cancel_callback_.Run(); | 20 cancel_callback_.Run(); |
20 } | 21 } |
21 } | 22 } |
22 | 23 |
23 TaskTracker::TaskTracker(const ArticleEntry& entry, CancelCallback callback) | 24 TaskTracker::TaskTracker(const ArticleEntry& entry, |
25 CancelCallback callback, | |
26 DistilledContentStore* content_store) | |
24 : cancel_callback_(callback), | 27 : cancel_callback_(callback), |
28 content_store_(content_store), | |
25 entry_(entry), | 29 entry_(entry), |
26 distilled_article_(), | 30 distilled_article_(), |
27 content_ready_(false), | 31 content_ready_(false), |
28 destruction_allowed_(true), | 32 destruction_allowed_(true), |
29 weak_ptr_factory_(this) {} | 33 weak_ptr_factory_(this) {} |
30 | 34 |
31 TaskTracker::~TaskTracker() { | 35 TaskTracker::~TaskTracker() { |
32 DCHECK(destruction_allowed_); | 36 DCHECK(destruction_allowed_); |
33 DCHECK(viewers_.empty()); | 37 DCHECK(viewers_.empty()); |
34 } | 38 } |
35 | 39 |
36 void TaskTracker::StartDistiller(DistillerFactory* factory) { | 40 void TaskTracker::StartDistiller(DistillerFactory* factory) { |
37 if (distiller_) { | 41 if (distiller_) { |
38 return; | 42 return; |
39 } | 43 } |
40 if (entry_.pages_size() == 0) { | 44 if (entry_.pages_size() == 0) { |
41 return; | 45 return; |
42 } | 46 } |
43 | |
44 GURL url(entry_.pages(0).url()); | 47 GURL url(entry_.pages(0).url()); |
45 DCHECK(url.is_valid()); | 48 DCHECK(url.is_valid()); |
46 | 49 |
47 distiller_ = factory->CreateDistiller(); | 50 distiller_ = factory->CreateDistiller(); |
48 distiller_->DistillPage(url, | 51 distiller_->DistillPage(url, |
49 base::Bind(&TaskTracker::OnDistillerFinished, | 52 base::Bind(&TaskTracker::OnDistillerFinished, |
50 weak_ptr_factory_.GetWeakPtr()), | 53 weak_ptr_factory_.GetWeakPtr()), |
51 base::Bind(&TaskTracker::OnArticleDistillationUpdated, | 54 base::Bind(&TaskTracker::OnArticleDistillationUpdated, |
52 weak_ptr_factory_.GetWeakPtr())); | 55 weak_ptr_factory_.GetWeakPtr())); |
53 } | 56 } |
54 | 57 |
55 void TaskTracker::StartBlobFetcher() { | 58 void TaskTracker::StartBlobFetcher() { |
56 // TODO(cjhopman): There needs to be some local storage for the distilled | 59 if (content_store_) { |
57 // blob. When that happens, this should start some task to fetch the blob for | 60 content_store_->LoadContent(entry_, |
58 // |entry_| and asynchronously notify |this| when it is done. | 61 base::Bind(&TaskTracker::OnBlobFetched, |
62 weak_ptr_factory_.GetWeakPtr())); | |
63 } | |
59 } | 64 } |
60 | 65 |
61 void TaskTracker::AddSaveCallback(const SaveCallback& callback) { | 66 void TaskTracker::AddSaveCallback(const SaveCallback& callback) { |
62 DCHECK(!callback.is_null()); | 67 DCHECK(!callback.is_null()); |
63 save_callbacks_.push_back(callback); | 68 save_callbacks_.push_back(callback); |
64 if (content_ready_) { | 69 if (content_ready_) { |
65 // Distillation for this task has already completed, and so it can be | 70 // Distillation for this task has already completed, and so it can be |
66 // immediately saved. | 71 // immediately saved. |
67 ScheduleSaveCallbacks(true); | 72 ScheduleSaveCallbacks(true); |
68 } | 73 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 } | 144 } |
140 } | 145 } |
141 | 146 |
142 void TaskTracker::NotifyViewer(ViewRequestDelegate* delegate) { | 147 void TaskTracker::NotifyViewer(ViewRequestDelegate* delegate) { |
143 DCHECK(content_ready_); | 148 DCHECK(content_ready_); |
144 delegate->OnArticleReady(distilled_article_.get()); | 149 delegate->OnArticleReady(distilled_article_.get()); |
145 } | 150 } |
146 | 151 |
147 void TaskTracker::OnDistillerFinished( | 152 void TaskTracker::OnDistillerFinished( |
148 scoped_ptr<DistilledArticleProto> distilled_article) { | 153 scoped_ptr<DistilledArticleProto> distilled_article) { |
149 OnDistilledArticleReady(distilled_article.Pass()); | 154 if (!content_ready_) { |
155 OnDistilledArticleReady(distilled_article.Pass()); | |
156 } | |
157 } | |
158 | |
159 void TaskTracker::OnBlobFetched( | |
160 bool success, | |
161 scoped_ptr<DistilledArticleProto> distilled_article) { | |
162 if (!content_ready_ && success) { | |
163 OnDistilledArticleReady(distilled_article.Pass()); | |
164 } | |
150 } | 165 } |
151 | 166 |
152 void TaskTracker::OnDistilledArticleReady( | 167 void TaskTracker::OnDistilledArticleReady( |
153 scoped_ptr<DistilledArticleProto> distilled_article) { | 168 scoped_ptr<DistilledArticleProto> distilled_article) { |
169 DCHECK(!content_ready_); | |
170 content_ready_ = true; | |
154 distilled_article_ = distilled_article.Pass(); | 171 distilled_article_ = distilled_article.Pass(); |
155 bool distillation_successful = false; | 172 bool distillation_successful = false; |
nyquist
2014/04/10 19:00:13
how about bool distillation_successful = distilled
cjhopman
2014/04/11 22:37:21
Done.
| |
156 if (distilled_article_->pages_size() > 0) { | 173 if (distilled_article_->pages_size() > 0) { |
157 distillation_successful = true; | 174 distillation_successful = true; |
158 entry_.set_title(distilled_article_->title()); | 175 entry_.set_title(distilled_article_->title()); |
159 // Reset the pages. | 176 // Reset the pages. |
160 entry_.clear_pages(); | 177 entry_.clear_pages(); |
161 for (int i = 0; i < distilled_article_->pages_size(); ++i) { | 178 for (int i = 0; i < distilled_article_->pages_size(); ++i) { |
162 sync_pb::ArticlePage* page = entry_.add_pages(); | 179 sync_pb::ArticlePage* page = entry_.add_pages(); |
163 page->set_url(distilled_article_->pages(i).url()); | 180 page->set_url(distilled_article_->pages(i).url()); |
164 } | 181 } |
165 } | 182 } |
166 | 183 |
167 content_ready_ = true; | 184 if (distillation_successful) { |
shashi
2014/03/18 21:14:57
Can OnDistillerFinished determine if the distillat
nyquist
2014/04/10 19:00:13
Or how about just adding a param |store_if_valid|
cjhopman
2014/04/11 22:37:21
So this should be more correct now.
| |
185 // TODO(cjhopman): If this article came from the blob fetcher, there is no | |
186 // reason to store it back there. | |
187 AddDistilledContentToStore(); | |
188 } | |
168 | 189 |
169 for (size_t i = 0; i < viewers_.size(); ++i) { | 190 for (size_t i = 0; i < viewers_.size(); ++i) { |
170 NotifyViewer(viewers_[i]); | 191 NotifyViewer(viewers_[i]); |
171 } | 192 } |
172 | 193 |
173 // Already inside a callback run SaveCallbacks directly. | 194 // Already inside a callback run SaveCallbacks directly. |
174 DoSaveCallbacks(distillation_successful); | 195 DoSaveCallbacks(distillation_successful); |
175 } | 196 } |
176 | 197 |
177 void TaskTracker::OnArticleDistillationUpdated( | 198 void TaskTracker::OnArticleDistillationUpdated( |
178 const ArticleDistillationUpdate& article_update) { | 199 const ArticleDistillationUpdate& article_update) { |
179 for (size_t i = 0; i < viewers_.size(); ++i) { | 200 for (size_t i = 0; i < viewers_.size(); ++i) { |
180 viewers_[i]->OnArticleUpdated(article_update); | 201 viewers_[i]->OnArticleUpdated(article_update); |
181 } | 202 } |
182 } | 203 } |
183 | 204 |
205 void TaskTracker::AddDistilledContentToStore() { | |
206 if (content_store_) { | |
207 content_store_->SaveContent( | |
208 entry_, *distilled_article_, DistilledContentStore::SaveCallback()); | |
209 } | |
210 } | |
211 | |
212 | |
184 } // namespace dom_distiller | 213 } // namespace dom_distiller |
OLD | NEW |