| 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 <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "components/dom_distiller/core/distiller_page.h" | 22 #include "components/dom_distiller/core/distiller_page.h" |
| 22 #include "components/dom_distiller/core/distiller_url_fetcher.h" | 23 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
| 23 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 24 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 24 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 25 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 89 |
| 89 bool DistillerImpl::IsPageNumberInUse(int page_num) const { | 90 bool DistillerImpl::IsPageNumberInUse(int page_num) const { |
| 90 return waiting_pages_.find(page_num) != waiting_pages_.end() || | 91 return waiting_pages_.find(page_num) != waiting_pages_.end() || |
| 91 started_pages_index_.find(page_num) != started_pages_index_.end() || | 92 started_pages_index_.find(page_num) != started_pages_index_.end() || |
| 92 finished_pages_index_.find(page_num) != finished_pages_index_.end(); | 93 finished_pages_index_.find(page_num) != finished_pages_index_.end(); |
| 93 } | 94 } |
| 94 | 95 |
| 95 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index) | 96 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index) |
| 96 const { | 97 const { |
| 97 DCHECK_LT(index, pages_.size()); | 98 DCHECK_LT(index, pages_.size()); |
| 98 DistilledPageData* page_data = pages_[index]; | 99 DistilledPageData* page_data = pages_[index].get(); |
| 99 DCHECK(page_data); | 100 DCHECK(page_data); |
| 100 return page_data; | 101 return page_data; |
| 101 } | 102 } |
| 102 | 103 |
| 103 void DistillerImpl::DistillPage(const GURL& url, | 104 void DistillerImpl::DistillPage(const GURL& url, |
| 104 std::unique_ptr<DistillerPage> distiller_page, | 105 std::unique_ptr<DistillerPage> distiller_page, |
| 105 const DistillationFinishedCallback& finished_cb, | 106 const DistillationFinishedCallback& finished_cb, |
| 106 const DistillationUpdateCallback& update_cb) { | 107 const DistillationUpdateCallback& update_cb) { |
| 107 DCHECK(AreAllPagesFinished()); | 108 DCHECK(AreAllPagesFinished()); |
| 108 distiller_page_ = std::move(distiller_page); | 109 distiller_page_ = std::move(distiller_page); |
| 109 finished_cb_ = finished_cb; | 110 finished_cb_ = finished_cb; |
| 110 update_cb_ = update_cb; | 111 update_cb_ = update_cb; |
| 111 | 112 |
| 112 AddToDistillationQueue(0, url); | 113 AddToDistillationQueue(0, url); |
| 113 DistillNextPage(); | 114 DistillNextPage(); |
| 114 } | 115 } |
| 115 | 116 |
| 116 void DistillerImpl::DistillNextPage() { | 117 void DistillerImpl::DistillNextPage() { |
| 117 if (!waiting_pages_.empty()) { | 118 if (!waiting_pages_.empty()) { |
| 118 std::map<int, GURL>::iterator front = waiting_pages_.begin(); | 119 std::map<int, GURL>::iterator front = waiting_pages_.begin(); |
| 119 int page_num = front->first; | 120 int page_num = front->first; |
| 120 const GURL url = front->second; | 121 const GURL url = front->second; |
| 121 | 122 |
| 122 waiting_pages_.erase(front); | 123 waiting_pages_.erase(front); |
| 123 DCHECK(url.is_valid()); | 124 DCHECK(url.is_valid()); |
| 124 DCHECK(started_pages_index_.find(page_num) == started_pages_index_.end()); | 125 DCHECK(started_pages_index_.find(page_num) == started_pages_index_.end()); |
| 125 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end()); | 126 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end()); |
| 126 seen_urls_.insert(url.spec()); | 127 seen_urls_.insert(url.spec()); |
| 127 pages_.push_back(new DistilledPageData()); | 128 pages_.push_back(base::MakeUnique<DistilledPageData>()); |
| 128 started_pages_index_[page_num] = pages_.size() - 1; | 129 started_pages_index_[page_num] = pages_.size() - 1; |
| 129 distiller_page_->DistillPage( | 130 distiller_page_->DistillPage( |
| 130 url, | 131 url, |
| 131 dom_distiller_options_, | 132 dom_distiller_options_, |
| 132 base::Bind(&DistillerImpl::OnPageDistillationFinished, | 133 base::Bind(&DistillerImpl::OnPageDistillationFinished, |
| 133 weak_factory_.GetWeakPtr(), | 134 weak_factory_.GetWeakPtr(), |
| 134 page_num, | 135 page_num, |
| 135 url)); | 136 url)); |
| 136 } | 137 } |
| 137 } | 138 } |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 } | 285 } |
| 285 | 286 |
| 286 void DistillerImpl::FetchImage(int page_num, | 287 void DistillerImpl::FetchImage(int page_num, |
| 287 const std::string& image_id, | 288 const std::string& image_id, |
| 288 const std::string& image_url) { | 289 const std::string& image_url) { |
| 289 if (!GURL(image_url).is_valid()) return; | 290 if (!GURL(image_url).is_valid()) return; |
| 290 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); | 291 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); |
| 291 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); | 292 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); |
| 292 DistillerURLFetcher* fetcher = | 293 DistillerURLFetcher* fetcher = |
| 293 distiller_url_fetcher_factory_.CreateDistillerURLFetcher(); | 294 distiller_url_fetcher_factory_.CreateDistillerURLFetcher(); |
| 294 page_data->image_fetchers_.push_back(fetcher); | 295 page_data->image_fetchers_.push_back(base::WrapUnique(fetcher)); |
| 295 | 296 |
| 296 fetcher->FetchURL(image_url, | 297 fetcher->FetchURL(image_url, |
| 297 base::Bind(&DistillerImpl::OnFetchImageDone, | 298 base::Bind(&DistillerImpl::OnFetchImageDone, |
| 298 weak_factory_.GetWeakPtr(), | 299 weak_factory_.GetWeakPtr(), |
| 299 page_num, | 300 page_num, |
| 300 base::Unretained(fetcher), | 301 base::Unretained(fetcher), |
| 301 image_id, | 302 image_id, |
| 302 image_url)); | 303 image_url)); |
| 303 } | 304 } |
| 304 | 305 |
| 305 void DistillerImpl::OnFetchImageDone(int page_num, | 306 void DistillerImpl::OnFetchImageDone(int page_num, |
| 306 DistillerURLFetcher* url_fetcher, | 307 DistillerURLFetcher* url_fetcher, |
| 307 const std::string& id, | 308 const std::string& id, |
| 308 const std::string& original_url, | 309 const std::string& original_url, |
| 309 const std::string& response) { | 310 const std::string& response) { |
| 310 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); | 311 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); |
| 311 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); | 312 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); |
| 312 DCHECK(page_data->distilled_page_proto.get()); | 313 DCHECK(page_data->distilled_page_proto.get()); |
| 313 DCHECK(url_fetcher); | 314 DCHECK(url_fetcher); |
| 314 ScopedVector<DistillerURLFetcher>::iterator fetcher_it = | 315 auto fetcher_it = std::find_if( |
| 315 std::find(page_data->image_fetchers_.begin(), | 316 page_data->image_fetchers_.begin(), page_data->image_fetchers_.end(), |
| 316 page_data->image_fetchers_.end(), | 317 [url_fetcher](const std::unique_ptr<DistillerURLFetcher>& f) { |
| 317 url_fetcher); | 318 return url_fetcher == f.get(); |
| 319 }); |
| 318 | 320 |
| 319 DCHECK(fetcher_it != page_data->image_fetchers_.end()); | 321 DCHECK(fetcher_it != page_data->image_fetchers_.end()); |
| 320 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone | 322 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone |
| 321 // callback is invoked by the |url_fetcher|. | 323 // callback is invoked by the |url_fetcher|. |
| 322 page_data->image_fetchers_.weak_erase(fetcher_it); | 324 fetcher_it->release(); |
| 325 page_data->image_fetchers_.erase(fetcher_it); |
| 323 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, url_fetcher); | 326 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, url_fetcher); |
| 324 | 327 |
| 325 DistilledPageProto_Image* image = | 328 DistilledPageProto_Image* image = |
| 326 page_data->distilled_page_proto->data.add_image(); | 329 page_data->distilled_page_proto->data.add_image(); |
| 327 image->set_name(id); | 330 image->set_name(id); |
| 328 image->set_data(response); | 331 image->set_data(response); |
| 329 image->set_url(original_url); | 332 image->set_url(original_url); |
| 330 | 333 |
| 331 AddPageIfDone(page_num); | 334 AddPageIfDone(page_num); |
| 332 } | 335 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 DCHECK(finished_pages_index_.empty()); | 398 DCHECK(finished_pages_index_.empty()); |
| 396 | 399 |
| 397 base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_, | 400 base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_, |
| 398 false); | 401 false); |
| 399 finished_cb_.Run(std::move(article_proto)); | 402 finished_cb_.Run(std::move(article_proto)); |
| 400 finished_cb_.Reset(); | 403 finished_cb_.Reset(); |
| 401 } | 404 } |
| 402 } | 405 } |
| 403 | 406 |
| 404 } // namespace dom_distiller | 407 } // namespace dom_distiller |
| OLD | NEW |