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/distiller.h" | 5 #include "components/dom_distiller/core/distiller.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 #include <vector> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "components/dom_distiller/core/distiller_page.h" | 17 #include "components/dom_distiller/core/distiller_page.h" |
17 #include "components/dom_distiller/core/distiller_url_fetcher.h" | 18 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 90 |
90 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index) | 91 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index) |
91 const { | 92 const { |
92 DCHECK_LT(index, pages_.size()); | 93 DCHECK_LT(index, pages_.size()); |
93 DistilledPageData* page_data = pages_[index]; | 94 DistilledPageData* page_data = pages_[index]; |
94 DCHECK(page_data); | 95 DCHECK(page_data); |
95 return page_data; | 96 return page_data; |
96 } | 97 } |
97 | 98 |
98 void DistillerImpl::DistillPage(const GURL& url, | 99 void DistillerImpl::DistillPage(const GURL& url, |
99 const DistillerCallback& distillation_cb) { | 100 const DistillationFinishedCallback& finished_cb, |
| 101 const DistillationUpdateCallback& update_cb) { |
100 DCHECK(AreAllPagesFinished()); | 102 DCHECK(AreAllPagesFinished()); |
101 distillation_cb_ = distillation_cb; | 103 finished_cb_ = finished_cb; |
| 104 update_cb_ = update_cb; |
102 | 105 |
103 AddToDistillationQueue(0, url); | 106 AddToDistillationQueue(0, url); |
104 DistillNextPage(); | 107 DistillNextPage(); |
105 } | 108 } |
106 | 109 |
107 void DistillerImpl::DistillNextPage() { | 110 void DistillerImpl::DistillNextPage() { |
108 if (!waiting_pages_.empty()) { | 111 if (!waiting_pages_.empty()) { |
109 std::map<int, GURL>::iterator front = waiting_pages_.begin(); | 112 std::map<int, GURL>::iterator front = waiting_pages_.begin(); |
110 int page_num = front->first; | 113 int page_num = front->first; |
111 const GURL url = front->second; | 114 const GURL url = front->second; |
(...skipping 17 matching lines...) Expand all Loading... |
129 void DistillerImpl::OnPageDistillationFinished( | 132 void DistillerImpl::OnPageDistillationFinished( |
130 int page_num, | 133 int page_num, |
131 const GURL& page_url, | 134 const GURL& page_url, |
132 scoped_ptr<DistilledPageInfo> distilled_page, | 135 scoped_ptr<DistilledPageInfo> distilled_page, |
133 bool distillation_successful) { | 136 bool distillation_successful) { |
134 DCHECK(distilled_page.get()); | 137 DCHECK(distilled_page.get()); |
135 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); | 138 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); |
136 if (distillation_successful) { | 139 if (distillation_successful) { |
137 DistilledPageData* page_data = | 140 DistilledPageData* page_data = |
138 GetPageAtIndex(started_pages_index_[page_num]); | 141 GetPageAtIndex(started_pages_index_[page_num]); |
139 DistilledPageProto* current_page = new DistilledPageProto(); | 142 page_data->proto = new base::RefCountedData<DistilledPageProto>(); |
140 page_data->proto.reset(current_page); | |
141 page_data->page_num = page_num; | 143 page_data->page_num = page_num; |
142 page_data->title = distilled_page->title; | 144 page_data->title = distilled_page->title; |
143 | 145 |
144 current_page->set_url(page_url.spec()); | 146 page_data->proto->data.set_url(page_url.spec()); |
145 current_page->set_html(distilled_page->html); | 147 page_data->proto->data.set_html(distilled_page->html); |
146 | 148 |
147 GURL next_page_url(distilled_page->next_page_url); | 149 GURL next_page_url(distilled_page->next_page_url); |
148 if (next_page_url.is_valid()) { | 150 if (next_page_url.is_valid()) { |
149 // The pages should be in same origin. | 151 // The pages should be in same origin. |
150 DCHECK_EQ(next_page_url.GetOrigin(), page_url.GetOrigin()); | 152 DCHECK_EQ(next_page_url.GetOrigin(), page_url.GetOrigin()); |
151 AddToDistillationQueue(page_num + 1, next_page_url); | 153 AddToDistillationQueue(page_num + 1, next_page_url); |
152 } | 154 } |
153 | 155 |
154 GURL prev_page_url(distilled_page->prev_page_url); | 156 GURL prev_page_url(distilled_page->prev_page_url); |
155 if (prev_page_url.is_valid()) { | 157 if (prev_page_url.is_valid()) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 std::find(page_data->image_fetchers_.begin(), | 203 std::find(page_data->image_fetchers_.begin(), |
202 page_data->image_fetchers_.end(), | 204 page_data->image_fetchers_.end(), |
203 url_fetcher); | 205 url_fetcher); |
204 | 206 |
205 DCHECK(fetcher_it != page_data->image_fetchers_.end()); | 207 DCHECK(fetcher_it != page_data->image_fetchers_.end()); |
206 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone | 208 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone |
207 // callback is invoked by the |url_fetcher|. | 209 // callback is invoked by the |url_fetcher|. |
208 page_data->image_fetchers_.weak_erase(fetcher_it); | 210 page_data->image_fetchers_.weak_erase(fetcher_it); |
209 base::MessageLoop::current()->DeleteSoon(FROM_HERE, url_fetcher); | 211 base::MessageLoop::current()->DeleteSoon(FROM_HERE, url_fetcher); |
210 | 212 |
211 DistilledPageProto_Image* image = page_data->proto->add_image(); | 213 DistilledPageProto_Image* image = page_data->proto->data.add_image(); |
212 image->set_name(id); | 214 image->set_name(id); |
213 image->set_data(response); | 215 image->set_data(response); |
214 | 216 |
215 AddPageIfDone(page_num); | 217 AddPageIfDone(page_num); |
216 } | 218 } |
217 | 219 |
218 void DistillerImpl::AddPageIfDone(int page_num) { | 220 void DistillerImpl::AddPageIfDone(int page_num) { |
219 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); | 221 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); |
220 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end()); | 222 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end()); |
221 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); | 223 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); |
222 if (page_data->image_fetchers_.empty()) { | 224 if (page_data->image_fetchers_.empty()) { |
223 finished_pages_index_[page_num] = started_pages_index_[page_num]; | 225 finished_pages_index_[page_num] = started_pages_index_[page_num]; |
224 started_pages_index_.erase(page_num); | 226 started_pages_index_.erase(page_num); |
| 227 const ArticleDistillationUpdate& article_update = |
| 228 CreateDistillationUpdate(); |
| 229 DCHECK_EQ(article_update.GetPagesSize(), finished_pages_index_.size()); |
| 230 update_cb_.Run(article_update); |
225 RunDistillerCallbackIfDone(); | 231 RunDistillerCallbackIfDone(); |
226 } | 232 } |
227 } | 233 } |
228 | 234 |
| 235 const ArticleDistillationUpdate DistillerImpl::CreateDistillationUpdate() |
| 236 const { |
| 237 bool has_prev_page = false; |
| 238 bool has_next_page = false; |
| 239 if (!finished_pages_index_.empty()) { |
| 240 int prev_page_num = finished_pages_index_.begin()->first - 1; |
| 241 int next_page_num = finished_pages_index_.rbegin()->first + 1; |
| 242 has_prev_page = IsPageNumberInUse(prev_page_num); |
| 243 has_next_page = IsPageNumberInUse(next_page_num); |
| 244 } |
| 245 |
| 246 std::vector<ArticleDistillationUpdate::RefPtrToDistilledPageProto> |
| 247 update_pages; |
| 248 for (std::map<int, size_t>::const_iterator it = finished_pages_index_.begin(); |
| 249 it != finished_pages_index_.end(); |
| 250 ++it) { |
| 251 update_pages.push_back(pages_[it->second]->proto); |
| 252 } |
| 253 return ArticleDistillationUpdate(update_pages, has_next_page, has_prev_page); |
| 254 } |
| 255 |
229 void DistillerImpl::RunDistillerCallbackIfDone() { | 256 void DistillerImpl::RunDistillerCallbackIfDone() { |
230 DCHECK(!distillation_cb_.is_null()); | 257 DCHECK(!finished_cb_.is_null()); |
231 if (AreAllPagesFinished()) { | 258 if (AreAllPagesFinished()) { |
232 bool first_page = true; | 259 bool first_page = true; |
233 scoped_ptr<DistilledArticleProto> article_proto( | 260 scoped_ptr<DistilledArticleProto> article_proto( |
234 new DistilledArticleProto()); | 261 new DistilledArticleProto()); |
235 // Stitch the pages back into the article. | 262 // Stitch the pages back into the article. |
236 for (std::map<int, size_t>::iterator it = finished_pages_index_.begin(); | 263 for (std::map<int, size_t>::iterator it = finished_pages_index_.begin(); |
237 it != finished_pages_index_.end();) { | 264 it != finished_pages_index_.end();) { |
238 DistilledPageData* page_data = GetPageAtIndex(it->second); | 265 DistilledPageData* page_data = GetPageAtIndex(it->second); |
239 *(article_proto->add_pages()) = *(page_data->proto); | 266 *(article_proto->add_pages()) = page_data->proto.get()->data; |
240 | 267 |
241 if (first_page) { | 268 if (first_page) { |
242 article_proto->set_title(page_data->title); | 269 article_proto->set_title(page_data->title); |
243 first_page = false; | 270 first_page = false; |
244 } | 271 } |
245 | 272 |
246 finished_pages_index_.erase(it++); | 273 finished_pages_index_.erase(it++); |
247 } | 274 } |
248 | 275 |
249 pages_.clear(); | 276 pages_.clear(); |
250 DCHECK_LE(static_cast<size_t>(article_proto->pages_size()), | 277 DCHECK_LE(static_cast<size_t>(article_proto->pages_size()), |
251 max_pages_in_article_); | 278 max_pages_in_article_); |
252 | 279 |
253 DCHECK(pages_.empty()); | 280 DCHECK(pages_.empty()); |
254 DCHECK(finished_pages_index_.empty()); | 281 DCHECK(finished_pages_index_.empty()); |
255 distillation_cb_.Run(article_proto.Pass()); | 282 finished_cb_.Run(article_proto.Pass()); |
256 distillation_cb_.Reset(); | 283 finished_cb_.Reset(); |
257 } | 284 } |
258 } | 285 } |
259 | 286 |
260 } // namespace dom_distiller | 287 } // namespace dom_distiller |
OLD | NEW |