| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/dom_distiller/distiller_viewer.h" | 5 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "components/dom_distiller/core/distilled_page_prefs.h" | 10 #include "components/dom_distiller/core/distilled_page_prefs.h" |
| 11 #include "components/dom_distiller/core/distiller.h" |
| 11 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 12 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
| 12 #include "components/dom_distiller/core/dom_distiller_service.h" | 13 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 13 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 14 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 14 #include "components/dom_distiller/core/task_tracker.h" | 15 #include "components/dom_distiller/core/task_tracker.h" |
| 15 #include "components/dom_distiller/core/viewer.h" | 16 #include "components/dom_distiller/core/viewer.h" |
| 16 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 17 | 18 |
| 18 namespace dom_distiller { | 19 namespace dom_distiller { |
| 19 | 20 |
| 20 DistillerViewer::DistillerViewer( | 21 DistillerViewer::DistillerViewer( |
| 21 dom_distiller::DomDistillerService* distillerService, | 22 dom_distiller::DomDistillerService* distillerService, |
| 22 PrefService* prefs, | 23 PrefService* prefs, |
| 23 const GURL& url, | 24 const GURL& url, |
| 24 const DistillationFinishedCallback& callback, | 25 const DistillationFinishedCallback& callback) |
| 25 std::unique_ptr<dom_distiller::DistillerPage> page) | 26 : DistillerViewerInterface(prefs), url_(url), callback_(callback) { |
| 26 : DistillerViewerInterface(distillerService, prefs), | |
| 27 url_(url), | |
| 28 callback_(callback) { | |
| 29 DCHECK(distillerService); | 27 DCHECK(distillerService); |
| 30 DCHECK(url.is_valid()); | 28 DCHECK(url.is_valid()); |
| 31 if (!page) { | 29 std::unique_ptr<dom_distiller::DistillerPage> page = |
| 32 page = distillerService->CreateDefaultDistillerPage(gfx::Size()); | 30 distillerService->CreateDefaultDistillerPage(gfx::Size()); |
| 33 } | |
| 34 | |
| 35 std::unique_ptr<ViewerHandle> viewer_handle = | 31 std::unique_ptr<ViewerHandle> viewer_handle = |
| 36 distillerService->ViewUrl(this, std::move(page), url); | 32 distillerService->ViewUrl(this, std::move(page), url); |
| 37 | 33 |
| 38 TakeViewerHandle(std::move(viewer_handle)); | 34 TakeViewerHandle(std::move(viewer_handle)); |
| 39 } | 35 } |
| 40 | 36 |
| 37 DistillerViewer::DistillerViewer( |
| 38 dom_distiller::DistillerFactory* distiller_factory, |
| 39 std::unique_ptr<dom_distiller::DistillerPage> page, |
| 40 PrefService* prefs, |
| 41 const GURL& url, |
| 42 const DistillationFinishedCallback& callback) |
| 43 : DistillerViewerInterface(prefs), url_(url), callback_(callback) { |
| 44 DCHECK(url.is_valid()); |
| 45 SendCommonJavaScript(); |
| 46 distiller_ = distiller_factory->CreateDistillerForUrl(url); |
| 47 distiller_->DistillPage( |
| 48 url, std::move(page), |
| 49 base::Bind(&DistillerViewer::OnDistillerFinished, base::Unretained(this)), |
| 50 base::Bind(&DistillerViewer::OnArticleDistillationUpdated, |
| 51 base::Unretained(this))); |
| 52 } |
| 53 |
| 41 DistillerViewer::~DistillerViewer() {} | 54 DistillerViewer::~DistillerViewer() {} |
| 42 | 55 |
| 56 void DistillerViewer::OnArticleDistillationUpdated( |
| 57 const dom_distiller::ArticleDistillationUpdate& article_update) {} |
| 58 |
| 59 void DistillerViewer::OnDistillerFinished( |
| 60 std::unique_ptr<dom_distiller::DistilledArticleProto> distilled_article) { |
| 61 OnArticleReady(distilled_article.get()); |
| 62 } |
| 63 |
| 43 void DistillerViewer::OnArticleReady( | 64 void DistillerViewer::OnArticleReady( |
| 44 const dom_distiller::DistilledArticleProto* article_proto) { | 65 const dom_distiller::DistilledArticleProto* article_proto) { |
| 45 DomDistillerRequestViewBase::OnArticleReady(article_proto); | 66 DomDistillerRequestViewBase::OnArticleReady(article_proto); |
| 46 bool is_empty = article_proto->pages_size() == 0 || | 67 bool is_empty = article_proto->pages_size() == 0 || |
| 47 article_proto->pages(0).html().empty(); | 68 article_proto->pages(0).html().empty(); |
| 48 if (!is_empty) { | 69 if (!is_empty) { |
| 49 std::vector<ImageInfo> images; | 70 std::vector<ImageInfo> images; |
| 50 for (int i = 0; i < article_proto->pages(0).image_size(); i++) { | 71 for (int i = 0; i < article_proto->pages(0).image_size(); i++) { |
| 51 auto image = article_proto->pages(0).image(i); | 72 auto image = article_proto->pages(0).image(i); |
| 52 images.push_back(ImageInfo{GURL(image.url()), image.data()}); | 73 images.push_back(ImageInfo{GURL(image.url()), image.data()}); |
| 53 } | 74 } |
| 54 const std::string html = viewer::GetUnsafeArticleTemplateHtml( | 75 const std::string html = viewer::GetUnsafeArticleTemplateHtml( |
| 55 url_.spec(), distilled_page_prefs_->GetTheme(), | 76 url_.spec(), distilled_page_prefs_->GetTheme(), |
| 56 distilled_page_prefs_->GetFontFamily()); | 77 distilled_page_prefs_->GetFontFamily()); |
| 57 | 78 |
| 58 std::string html_and_script(html); | 79 std::string html_and_script(html); |
| 59 html_and_script += | 80 html_and_script += |
| 60 "<script> distiller_on_ios = true; " + js_buffer_ + "</script>"; | 81 "<script> distiller_on_ios = true; " + js_buffer_ + "</script>"; |
| 61 callback_.Run(url_, html_and_script, images, article_proto->title()); | 82 callback_.Run(url_, html_and_script, images, article_proto->title()); |
| 62 } else { | 83 } else { |
| 63 callback_.Run(url_, std::string(), {}, std::string()); | 84 callback_.Run(url_, std::string(), {}, std::string()); |
| 64 } | 85 } |
| 65 } | 86 } |
| 66 | 87 |
| 67 void DistillerViewer::SendJavaScript(const std::string& buffer) { | 88 void DistillerViewer::SendJavaScript(const std::string& buffer) { |
| 68 js_buffer_ += buffer; | 89 js_buffer_ += buffer; |
| 69 } | 90 } |
| 70 | 91 |
| 71 } // namespace dom_distiller | 92 } // namespace dom_distiller |
| OLD | NEW |