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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 9 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
8 | 10 |
9 namespace dom_distiller { | 11 namespace dom_distiller { |
10 | 12 |
11 ViewerHandle::ViewerHandle(CancelCallback callback) | 13 ViewerHandle::ViewerHandle(CancelCallback callback) |
12 : cancel_callback_(callback) {} | 14 : cancel_callback_(callback) {} |
13 | 15 |
14 ViewerHandle::~ViewerHandle() { | 16 ViewerHandle::~ViewerHandle() { |
15 if (!cancel_callback_.is_null()) { | 17 if (!cancel_callback_.is_null()) { |
16 cancel_callback_.Run(); | 18 cancel_callback_.Run(); |
17 } | 19 } |
18 } | 20 } |
19 | 21 |
20 TaskTracker::TaskTracker(const ArticleEntry& entry, CancelCallback callback) | 22 TaskTracker::TaskTracker(const ArticleEntry& entry, CancelCallback callback) |
21 : cancel_callback_(callback), | 23 : cancel_callback_(callback), |
22 entry_(entry), | 24 entry_(entry), |
23 distilled_page_(), | 25 distilled_article_(), |
| 26 distillation_complete_(false), |
24 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
25 | 28 |
26 TaskTracker::~TaskTracker() { DCHECK(viewers_.empty()); } | 29 TaskTracker::~TaskTracker() { DCHECK(viewers_.empty()); } |
27 | 30 |
28 void TaskTracker::StartDistiller(DistillerFactory* factory) { | 31 void TaskTracker::StartDistiller(DistillerFactory* factory) { |
29 if (distiller_) { | 32 if (distiller_) { |
30 return; | 33 return; |
31 } | 34 } |
32 if (entry_.pages_size() == 0) { | 35 if (entry_.pages_size() == 0) { |
33 return; | 36 return; |
(...skipping 10 matching lines...) Expand all Loading... |
44 | 47 |
45 void TaskTracker::StartBlobFetcher() { | 48 void TaskTracker::StartBlobFetcher() { |
46 // TODO(cjhopman): There needs to be some local storage for the distilled | 49 // TODO(cjhopman): There needs to be some local storage for the distilled |
47 // blob. When that happens, this should start some task to fetch the blob for | 50 // blob. When that happens, this should start some task to fetch the blob for |
48 // |entry_| and asynchronously notify |this| when it is done. | 51 // |entry_| and asynchronously notify |this| when it is done. |
49 } | 52 } |
50 | 53 |
51 void TaskTracker::AddSaveCallback(const SaveCallback& callback) { | 54 void TaskTracker::AddSaveCallback(const SaveCallback& callback) { |
52 DCHECK(!callback.is_null()); | 55 DCHECK(!callback.is_null()); |
53 save_callbacks_.push_back(callback); | 56 save_callbacks_.push_back(callback); |
54 if (distilled_page_) { | 57 if (distillation_complete_) { |
55 // Distillation for this task has already completed, and so it can be | 58 // Distillation for this task has already completed, and so it can be |
56 // immediately saved. | 59 // immediately saved. |
57 ScheduleSaveCallbacks(true); | 60 ScheduleSaveCallbacks(true); |
58 } | 61 } |
59 } | 62 } |
60 | 63 |
61 scoped_ptr<ViewerHandle> TaskTracker::AddViewer(ViewRequestDelegate* delegate) { | 64 scoped_ptr<ViewerHandle> TaskTracker::AddViewer(ViewRequestDelegate* delegate) { |
62 viewers_.push_back(delegate); | 65 viewers_.push_back(delegate); |
63 if (distilled_page_) { | 66 if (distillation_complete_) { |
64 // Distillation for this task has already completed, and so the delegate can | 67 // Distillation for this task has already completed, and so the delegate can |
65 // be immediately told of the result. | 68 // be immediately told of the result. |
66 base::MessageLoop::current()->PostTask( | 69 base::MessageLoop::current()->PostTask( |
67 FROM_HERE, | 70 FROM_HERE, |
68 base::Bind(&TaskTracker::NotifyViewer, | 71 base::Bind(&TaskTracker::NotifyViewer, |
69 weak_ptr_factory_.GetWeakPtr(), | 72 weak_ptr_factory_.GetWeakPtr(), |
70 delegate)); | 73 delegate)); |
71 } | 74 } |
72 return scoped_ptr<ViewerHandle>(new ViewerHandle(base::Bind( | 75 return scoped_ptr<ViewerHandle>(new ViewerHandle(base::Bind( |
73 &TaskTracker::RemoveViewer, weak_ptr_factory_.GetWeakPtr(), delegate))); | 76 &TaskTracker::RemoveViewer, weak_ptr_factory_.GetWeakPtr(), delegate))); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 void TaskTracker::ScheduleSaveCallbacks(bool distillation_succeeded) { | 116 void TaskTracker::ScheduleSaveCallbacks(bool distillation_succeeded) { |
114 base::MessageLoop::current()->PostTask( | 117 base::MessageLoop::current()->PostTask( |
115 FROM_HERE, | 118 FROM_HERE, |
116 base::Bind(&TaskTracker::DoSaveCallbacks, | 119 base::Bind(&TaskTracker::DoSaveCallbacks, |
117 weak_ptr_factory_.GetWeakPtr(), | 120 weak_ptr_factory_.GetWeakPtr(), |
118 distillation_succeeded)); | 121 distillation_succeeded)); |
119 } | 122 } |
120 | 123 |
121 void TaskTracker::DoSaveCallbacks(bool distillation_succeeded) { | 124 void TaskTracker::DoSaveCallbacks(bool distillation_succeeded) { |
122 if (!save_callbacks_.empty()) { | 125 if (!save_callbacks_.empty()) { |
123 DistilledPageProto* distilled_proto = | |
124 distillation_succeeded ? distilled_page_.get() : NULL; | |
125 | |
126 for (size_t i = 0; i < save_callbacks_.size(); ++i) { | 126 for (size_t i = 0; i < save_callbacks_.size(); ++i) { |
127 DCHECK(!save_callbacks_[i].is_null()); | 127 DCHECK(!save_callbacks_[i].is_null()); |
128 save_callbacks_[i].Run(entry_, distilled_proto, distillation_succeeded); | 128 save_callbacks_[i].Run( |
| 129 entry_, distilled_article_.get(), distillation_succeeded); |
129 } | 130 } |
130 | 131 |
131 save_callbacks_.clear(); | 132 save_callbacks_.clear(); |
132 MaybeCancel(); | 133 MaybeCancel(); |
133 } | 134 } |
134 } | 135 } |
135 | 136 |
136 void TaskTracker::NotifyViewer(ViewRequestDelegate* delegate) { | 137 void TaskTracker::NotifyViewer(ViewRequestDelegate* delegate) { |
137 DCHECK(distilled_page_); | 138 DCHECK(distillation_complete_); |
138 delegate->OnArticleReady(distilled_page_.get()); | 139 delegate->OnArticleReady(distilled_article_.get()); |
139 } | 140 } |
140 | 141 |
141 void TaskTracker::OnDistilledDataReady(scoped_ptr<DistilledPageProto> proto) { | 142 void TaskTracker::OnDistilledDataReady( |
142 distilled_page_ = proto.Pass(); | 143 scoped_ptr<DistilledArticleProto> distilled_article) { |
143 DCHECK(distilled_page_); | 144 distilled_article_ = distilled_article.Pass(); |
| 145 bool distillation_successful = false; |
| 146 if (distilled_article_->pages_size() > 0) { |
| 147 distillation_successful = true; |
| 148 entry_.set_title(distilled_article_->title()); |
| 149 // Reset the pages. |
| 150 entry_.clear_pages(); |
| 151 for (int i = 0; i < distilled_article_->pages_size(); ++i) { |
| 152 sync_pb::ArticlePage* page = entry_.add_pages(); |
| 153 page->set_url(distilled_article_->pages(i).url()); |
| 154 } |
| 155 } |
144 | 156 |
145 entry_.set_title(distilled_page_->title()); | 157 distillation_complete_ = true; |
| 158 |
146 for (size_t i = 0; i < viewers_.size(); ++i) { | 159 for (size_t i = 0; i < viewers_.size(); ++i) { |
147 NotifyViewer(viewers_[i]); | 160 NotifyViewer(viewers_[i]); |
148 } | 161 } |
149 | 162 |
150 // Already inside a callback run SaveCallbacks directly. | 163 // Already inside a callback run SaveCallbacks directly. |
151 DoSaveCallbacks(true); | 164 DoSaveCallbacks(distillation_successful); |
152 } | 165 } |
153 | 166 |
154 } // namespace dom_distiller | 167 } // namespace dom_distiller |
OLD | NEW |