| 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 #ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ | 5 #ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ |
| 6 #define IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ | 6 #define IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 12 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
| 13 #include "components/dom_distiller/core/task_tracker.h" | 13 #include "components/dom_distiller/core/task_tracker.h" |
| 14 | 14 |
| 15 class GURL; | 15 class GURL; |
| 16 | 16 |
| 17 namespace dom_distiller { | 17 namespace dom_distiller { |
| 18 | 18 |
| 19 class DistilledPagePrefs; | 19 class DistilledPagePrefs; |
| 20 class Distiller; |
| 20 | 21 |
| 21 // An interface for a dom_distiller ViewRequestDelegate that distills a URL and | 22 // An interface for a dom_distiller ViewRequestDelegate that distills a URL and |
| 22 // calls the given callback with the distilled HTML string and the images it | 23 // calls the given callback with the distilled HTML string and the images it |
| 23 // contains. | 24 // contains. |
| 24 class DistillerViewerInterface : public DomDistillerRequestViewBase { | 25 class DistillerViewerInterface : public DomDistillerRequestViewBase { |
| 25 public: | 26 public: |
| 26 struct ImageInfo { | 27 struct ImageInfo { |
| 27 // The url of the image. | 28 // The url of the image. |
| 28 GURL url; | 29 GURL url; |
| 29 // The image data as a string. | 30 // The image data as a string. |
| 30 std::string data; | 31 std::string data; |
| 31 }; | 32 }; |
| 32 typedef base::Callback<void(const GURL& url, | 33 typedef base::Callback<void(const GURL& url, |
| 33 const std::string& html, | 34 const std::string& html, |
| 34 const std::vector<ImageInfo>& images, | 35 const std::vector<ImageInfo>& images, |
| 35 const std::string& title)> | 36 const std::string& title)> |
| 36 DistillationFinishedCallback; | 37 DistillationFinishedCallback; |
| 37 | 38 |
| 38 DistillerViewerInterface(dom_distiller::DomDistillerService* distillerService, | 39 DistillerViewerInterface(PrefService* prefs) |
| 39 PrefService* prefs) | |
| 40 : DomDistillerRequestViewBase(new DistilledPagePrefs(prefs)) {} | 40 : DomDistillerRequestViewBase(new DistilledPagePrefs(prefs)) {} |
| 41 ~DistillerViewerInterface() override {} | 41 ~DistillerViewerInterface() override {} |
| 42 | 42 |
| 43 void OnArticleReady( | 43 void OnArticleReady( |
| 44 const dom_distiller::DistilledArticleProto* article_proto) override = 0; | 44 const dom_distiller::DistilledArticleProto* article_proto) override = 0; |
| 45 | 45 |
| 46 void SendJavaScript(const std::string& buffer) override = 0; | 46 void SendJavaScript(const std::string& buffer) override = 0; |
| 47 | 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(DistillerViewerInterface); | 48 DISALLOW_COPY_AND_ASSIGN(DistillerViewerInterface); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // A very simple and naive implementation of the DistillerViewer. | 51 // A very simple and naive implementation of the DistillerViewer. |
| 52 class DistillerViewer : public DistillerViewerInterface { | 52 class DistillerViewer : public DistillerViewerInterface { |
| 53 public: | 53 public: |
| 54 // Creates a |DistillerView| that will be used to distill |url|. | 54 // Creates a |DistillerView| that will be used to distill |url|. |
| 55 // If |page| is not null, it will be used to load |url| and inject the page. | |
| 56 // If |page| is null, the default factory of |distillerService| will be used. | |
| 57 // |callback| is called when distillation is finished with the protobuf | 55 // |callback| is called when distillation is finished with the protobuf |
| 58 // containing the distilled page. | 56 // containing the distilled page. |
| 59 DistillerViewer(dom_distiller::DomDistillerService* distillerService, | 57 DistillerViewer(dom_distiller::DomDistillerService* distillerService, |
| 60 PrefService* prefs, | 58 PrefService* prefs, |
| 61 const GURL& url, | 59 const GURL& url, |
| 62 const DistillationFinishedCallback& callback, | 60 const DistillationFinishedCallback& callback); |
| 63 std::unique_ptr<dom_distiller::DistillerPage> page); | 61 |
| 62 // Creates a |DistillerView| without depending on the DomDistillerService. |
| 63 // Caller must provide |distiller_factory| and |page| which cannot be null. |
| 64 // |callback| is called when distillation is finished with the protobuf |
| 65 // containing the distilled page. |
| 66 DistillerViewer(dom_distiller::DistillerFactory* distiller_factory, |
| 67 std::unique_ptr<dom_distiller::DistillerPage> page, |
| 68 PrefService* prefs, |
| 69 const GURL& url, |
| 70 const DistillationFinishedCallback& callback); |
| 64 ~DistillerViewer() override; | 71 ~DistillerViewer() override; |
| 65 | 72 |
| 73 // DistillerViewerInterface implementation |
| 74 // Called by the distiller service when article is ready. |
| 66 void OnArticleReady( | 75 void OnArticleReady( |
| 67 const dom_distiller::DistilledArticleProto* article_proto) override; | 76 const dom_distiller::DistilledArticleProto* article_proto) override; |
| 68 | 77 |
| 69 void SendJavaScript(const std::string& buffer) override; | 78 void SendJavaScript(const std::string& buffer) override; |
| 70 | 79 |
| 71 private: | 80 private: |
| 81 // Called by the distiller when article is ready. |
| 82 void OnDistillerFinished( |
| 83 std::unique_ptr<dom_distiller::DistilledArticleProto> distilled_article); |
| 84 |
| 85 // Called by the distiller when article is updated. |
| 86 void OnArticleDistillationUpdated( |
| 87 const dom_distiller::ArticleDistillationUpdate& article_update); |
| 88 |
| 72 // The url of the distilled page. | 89 // The url of the distilled page. |
| 73 const GURL url_; | 90 const GURL url_; |
| 74 // JavaScript buffer. | 91 // JavaScript buffer. |
| 75 std::string js_buffer_; | 92 std::string js_buffer_; |
| 76 // Callback to run once distillation is complete. | 93 // Callback to run once distillation is complete. |
| 77 const DistillationFinishedCallback callback_; | 94 const DistillationFinishedCallback callback_; |
| 95 // Keep reference of the distiller_ during distillation. |
| 96 std::unique_ptr<Distiller> distiller_; |
| 78 | 97 |
| 79 DISALLOW_COPY_AND_ASSIGN(DistillerViewer); | 98 DISALLOW_COPY_AND_ASSIGN(DistillerViewer); |
| 80 }; | 99 }; |
| 81 | 100 |
| 82 } // namespace dom_distiller | 101 } // namespace dom_distiller |
| 83 | 102 |
| 84 #endif // IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ | 103 #endif // IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ |
| OLD | NEW |