| 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 | 8 |
| 9 #include "components/dom_distiller/core/distilled_page_prefs.h" | 9 #include "components/dom_distiller/core/distilled_page_prefs.h" |
| 10 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 10 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
| 11 #include "components/dom_distiller/core/dom_distiller_service.h" | 11 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 12 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 12 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 13 #include "components/dom_distiller/core/task_tracker.h" | 13 #include "components/dom_distiller/core/task_tracker.h" |
| 14 #include "components/dom_distiller/core/viewer.h" | 14 #include "components/dom_distiller/core/viewer.h" |
| 15 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 15 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
| 16 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.
h" | 16 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.
h" |
| 17 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 18 | 18 |
| 19 namespace dom_distiller { | 19 namespace dom_distiller { |
| 20 | 20 |
| 21 namespace { | |
| 22 | |
| 23 class IOSContentDataCallback : public DistillerDataCallback { | |
| 24 public: | |
| 25 IOSContentDataCallback( | |
| 26 const GURL& url, | |
| 27 const DistillerViewer::DistillationFinishedCallback& callback, | |
| 28 DistillerViewer* distiller_viewer_handle); | |
| 29 ~IOSContentDataCallback() override{}; | |
| 30 void RunCallback(std::string& data) override; | |
| 31 | |
| 32 private: | |
| 33 // Extra param needed by the callback specified below. | |
| 34 GURL url_; | |
| 35 // The callback to be run. | |
| 36 const DistillerViewer::DistillationFinishedCallback callback_; | |
| 37 // A handle to the DistillerViewer object. | |
| 38 DistillerViewer* distiller_viewer_handle_; | |
| 39 }; | |
| 40 | |
| 41 IOSContentDataCallback::IOSContentDataCallback( | |
| 42 const GURL& url, | |
| 43 const DistillerViewer::DistillationFinishedCallback& callback, | |
| 44 DistillerViewer* distiller_viewer_handle) | |
| 45 : url_(url), | |
| 46 callback_(callback), | |
| 47 distiller_viewer_handle_(distiller_viewer_handle) { | |
| 48 } | |
| 49 | |
| 50 void IOSContentDataCallback::RunCallback(std::string& data) { | |
| 51 std::string htmlAndScript(data); | |
| 52 htmlAndScript += "<script>" + | |
| 53 distiller_viewer_handle_->GetJavaScriptBuffer() + | |
| 54 "</script>"; | |
| 55 | |
| 56 callback_.Run(url_, htmlAndScript); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state, | 21 DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state, |
| 62 const GURL& url, | 22 const GURL& url, |
| 63 const DistillationFinishedCallback& callback) | 23 const DistillationFinishedCallback& callback) |
| 64 : DomDistillerRequestViewBase( | 24 : DomDistillerRequestViewBase( |
| 65 scoped_ptr<DistillerDataCallback>( | 25 new DistilledPagePrefs(browser_state->GetPrefs())), |
| 66 new IOSContentDataCallback(url, callback, this)).Pass(), | 26 url_(url), |
| 67 new DistilledPagePrefs(browser_state->GetPrefs())) { | 27 callback_(callback) { |
| 68 DCHECK(browser_state); | 28 DCHECK(browser_state); |
| 69 DCHECK(url.is_valid()); | 29 DCHECK(url.is_valid()); |
| 70 dom_distiller::DomDistillerService* distillerService = | 30 dom_distiller::DomDistillerService* distillerService = |
| 71 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( | 31 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( |
| 72 browser_state); | 32 browser_state); |
| 73 | 33 |
| 74 viewer_handle_ = distillerService->ViewUrl( | 34 scoped_ptr<ViewerHandle> viewer_handle = distillerService->ViewUrl( |
| 75 this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url); | 35 this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url); |
| 36 |
| 37 TakeViewerHandle(viewer_handle.Pass()); |
| 76 } | 38 } |
| 77 | 39 |
| 78 DistillerViewer::~DistillerViewer() { | 40 DistillerViewer::~DistillerViewer() { |
| 79 } | 41 } |
| 80 | 42 |
| 43 void DistillerViewer::OnArticleReady( |
| 44 const dom_distiller::DistilledArticleProto* article_proto) { |
| 45 DomDistillerRequestViewBase::OnArticleReady(article_proto); |
| 46 |
| 47 const std::string html = viewer::GetUnsafeArticleTemplateHtml( |
| 48 url_.spec(), distilled_page_prefs_->GetTheme(), |
| 49 distilled_page_prefs_->GetFontFamily()); |
| 50 |
| 51 std::string html_and_script(html); |
| 52 std::string hide_feedback = |
| 53 "document.getElementById('feedbackContainer').style.display = 'none';"; |
| 54 html_and_script += "<script>" + js_buffer_ + hide_feedback + "</script>"; |
| 55 callback_.Run(url_, html_and_script); |
| 56 } |
| 57 |
| 81 void DistillerViewer::SendJavaScript(const std::string& buffer) { | 58 void DistillerViewer::SendJavaScript(const std::string& buffer) { |
| 82 js_buffer_ += buffer; | 59 js_buffer_ += buffer; |
| 83 } | 60 } |
| 84 | 61 |
| 85 std::string DistillerViewer::GetJavaScriptBuffer() { | |
| 86 return js_buffer_; | |
| 87 } | |
| 88 | |
| 89 } // namespace dom_distiller | 62 } // namespace dom_distiller |
| OLD | NEW |