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_service.h" | 11 #include "components/dom_distiller/core/dom_distiller_service.h" |
11 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 12 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
12 #include "components/dom_distiller/core/task_tracker.h" | 13 #include "components/dom_distiller/core/task_tracker.h" |
13 #include "components/dom_distiller/core/viewer.h" | 14 #include "components/dom_distiller/core/viewer.h" |
14 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 15 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
15 #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" |
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 |
| 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 += |
| 53 "<script>" + distiller_viewer_handle_->getJsBuffer() + "</script>"; |
| 54 |
| 55 callback_.Run(url_, htmlAndScript); |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
20 DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state, | 60 DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state, |
21 const GURL& url, | 61 const GURL& url, |
22 const DistillationFinishedCallback& callback) | 62 const DistillationFinishedCallback& callback) |
23 : url_(url), | 63 : DomDistillerRequestViewBase( |
24 callback_(callback), | 64 scoped_ptr<DistillerDataCallback>( |
25 distilled_page_prefs_(new DistilledPagePrefs(browser_state->GetPrefs())) { | 65 new IOSContentDataCallback(url, callback, this)).Pass(), |
| 66 new DistilledPagePrefs(browser_state->GetPrefs())) { |
26 DCHECK(browser_state); | 67 DCHECK(browser_state); |
27 DCHECK(url.is_valid()); | 68 DCHECK(url.is_valid()); |
28 dom_distiller::DomDistillerService* distillerService = | 69 dom_distiller::DomDistillerService* distillerService = |
29 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( | 70 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( |
30 browser_state); | 71 browser_state); |
31 | 72 |
32 viewer_handle_ = distillerService->ViewUrl( | 73 viewer_handle_ = distillerService->ViewUrl( |
33 this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url); | 74 this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url); |
34 } | 75 } |
35 | 76 |
36 DistillerViewer::~DistillerViewer() { | 77 DistillerViewer::~DistillerViewer() { |
37 } | 78 } |
38 | 79 |
39 void DistillerViewer::OnArticleReady( | 80 void DistillerViewer::SendJavaScript(const std::string& buffer) { |
40 const DistilledArticleProto* article_proto) { | 81 js_buffer_ += buffer; |
41 const std::string html = viewer::GetUnsafeArticleTemplateHtml( | 82 } |
42 &article_proto->pages(0), distilled_page_prefs_->GetTheme(), | |
43 distilled_page_prefs_->GetFontFamily()); | |
44 | 83 |
45 std::string content_js = viewer::GetUnsafeArticleContentJs(article_proto); | 84 std::string DistillerViewer::getJsBuffer() { |
46 // TODO(noyau): This can be done better with changes to the | 85 return js_buffer_; |
47 // DistillationFinishedCallback. http://crbug.com/472805 | |
48 std::string htmlAndScript(html); | |
49 htmlAndScript += "<script>" + content_js + "</script>"; | |
50 | |
51 callback_.Run(url_, htmlAndScript); | |
52 | |
53 // No need to hold on to the ViewerHandle now that distillation is complete. | |
54 viewer_handle_.reset(); | |
55 } | 86 } |
56 | 87 |
57 } // namespace dom_distiller | 88 } // namespace dom_distiller |
OLD | NEW |