| 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 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
| 6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "components/dom_distiller/core/article_distillation_update.h" | 20 #include "components/dom_distiller/core/article_distillation_update.h" |
| 21 #include "components/dom_distiller/core/distiller_page.h" | 21 #include "components/dom_distiller/core/distiller_page.h" |
| 22 #include "components/dom_distiller/core/distiller_url_fetcher.h" | 22 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
| 23 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 23 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 24 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 namespace dom_distiller { | 27 namespace dom_distiller { |
| 28 | 28 |
| 29 class DistillerImpl; | 29 class DistillerImpl; |
| 30 | 30 |
| 31 class Distiller { | 31 class Distiller { |
| 32 public: | 32 public: |
| 33 typedef base::Callback<void(scoped_ptr<DistilledArticleProto>)> | 33 typedef base::Callback<void(std::unique_ptr<DistilledArticleProto>)> |
| 34 DistillationFinishedCallback; | 34 DistillationFinishedCallback; |
| 35 typedef base::Callback<void(const ArticleDistillationUpdate&)> | 35 typedef base::Callback<void(const ArticleDistillationUpdate&)> |
| 36 DistillationUpdateCallback; | 36 DistillationUpdateCallback; |
| 37 | 37 |
| 38 virtual ~Distiller() {} | 38 virtual ~Distiller() {} |
| 39 | 39 |
| 40 // Distills a page, and asynchronously returns the article HTML to the | 40 // Distills a page, and asynchronously returns the article HTML to the |
| 41 // supplied |finished_cb| callback. |update_cb| is invoked whenever article | 41 // supplied |finished_cb| callback. |update_cb| is invoked whenever article |
| 42 // under distillation is updated with more data. | 42 // under distillation is updated with more data. |
| 43 // E.g. when distilling a 2 page article, |update_cb| may be invoked each time | 43 // E.g. when distilling a 2 page article, |update_cb| may be invoked each time |
| 44 // a distilled page is added and |finished_cb| will be invoked once | 44 // a distilled page is added and |finished_cb| will be invoked once |
| 45 // distillation is completed. | 45 // distillation is completed. |
| 46 virtual void DistillPage(const GURL& url, | 46 virtual void DistillPage(const GURL& url, |
| 47 scoped_ptr<DistillerPage> distiller_page, | 47 std::unique_ptr<DistillerPage> distiller_page, |
| 48 const DistillationFinishedCallback& finished_cb, | 48 const DistillationFinishedCallback& finished_cb, |
| 49 const DistillationUpdateCallback& update_cb) = 0; | 49 const DistillationUpdateCallback& update_cb) = 0; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class DistillerFactory { | 52 class DistillerFactory { |
| 53 public: | 53 public: |
| 54 virtual scoped_ptr<Distiller> CreateDistillerForUrl(const GURL& url) = 0; | 54 virtual std::unique_ptr<Distiller> CreateDistillerForUrl(const GURL& url) = 0; |
| 55 virtual ~DistillerFactory() {} | 55 virtual ~DistillerFactory() {} |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Factory for creating a Distiller. | 58 // Factory for creating a Distiller. |
| 59 class DistillerFactoryImpl : public DistillerFactory { | 59 class DistillerFactoryImpl : public DistillerFactory { |
| 60 public: | 60 public: |
| 61 DistillerFactoryImpl( | 61 DistillerFactoryImpl( |
| 62 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory, | 62 std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory, |
| 63 const dom_distiller::proto::DomDistillerOptions& dom_distiller_options); | 63 const dom_distiller::proto::DomDistillerOptions& dom_distiller_options); |
| 64 ~DistillerFactoryImpl() override; | 64 ~DistillerFactoryImpl() override; |
| 65 scoped_ptr<Distiller> CreateDistillerForUrl(const GURL& url) override; | 65 std::unique_ptr<Distiller> CreateDistillerForUrl(const GURL& url) override; |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory_; | 68 std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory_; |
| 69 dom_distiller::proto::DomDistillerOptions dom_distiller_options_; | 69 dom_distiller::proto::DomDistillerOptions dom_distiller_options_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // Distills a article from a page and associated pages. | 72 // Distills a article from a page and associated pages. |
| 73 class DistillerImpl : public Distiller { | 73 class DistillerImpl : public Distiller { |
| 74 public: | 74 public: |
| 75 DistillerImpl( | 75 DistillerImpl( |
| 76 const DistillerURLFetcherFactory& distiller_url_fetcher_factory, | 76 const DistillerURLFetcherFactory& distiller_url_fetcher_factory, |
| 77 const dom_distiller::proto::DomDistillerOptions& dom_distiller_options); | 77 const dom_distiller::proto::DomDistillerOptions& dom_distiller_options); |
| 78 ~DistillerImpl() override; | 78 ~DistillerImpl() override; |
| 79 | 79 |
| 80 void DistillPage(const GURL& url, | 80 void DistillPage(const GURL& url, |
| 81 scoped_ptr<DistillerPage> distiller_page, | 81 std::unique_ptr<DistillerPage> distiller_page, |
| 82 const DistillationFinishedCallback& finished_cb, | 82 const DistillationFinishedCallback& finished_cb, |
| 83 const DistillationUpdateCallback& update_cb) override; | 83 const DistillationUpdateCallback& update_cb) override; |
| 84 | 84 |
| 85 void SetMaxNumPagesInArticle(size_t max_num_pages); | 85 void SetMaxNumPagesInArticle(size_t max_num_pages); |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 // In case of multiple pages, the Distiller maintains state of multiple pages | 88 // In case of multiple pages, the Distiller maintains state of multiple pages |
| 89 // as page numbers relative to the page number where distillation started. | 89 // as page numbers relative to the page number where distillation started. |
| 90 // E.g. if distillation starts at page 2 for a 3 page article. The relative | 90 // E.g. if distillation starts at page 2 for a 3 page article. The relative |
| 91 // page numbers assigned to pages will be [-1,0,1]. | 91 // page numbers assigned to pages will be [-1,0,1]. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 106 | 106 |
| 107 void OnFetchImageDone(int page_num, | 107 void OnFetchImageDone(int page_num, |
| 108 DistillerURLFetcher* url_fetcher, | 108 DistillerURLFetcher* url_fetcher, |
| 109 const std::string& id, | 109 const std::string& id, |
| 110 const std::string& original_url, | 110 const std::string& original_url, |
| 111 const std::string& response); | 111 const std::string& response); |
| 112 | 112 |
| 113 void OnPageDistillationFinished( | 113 void OnPageDistillationFinished( |
| 114 int page_num, | 114 int page_num, |
| 115 const GURL& page_url, | 115 const GURL& page_url, |
| 116 scoped_ptr<proto::DomDistillerResult> distilled_page, | 116 std::unique_ptr<proto::DomDistillerResult> distilled_page, |
| 117 bool distillation_successful); | 117 bool distillation_successful); |
| 118 | 118 |
| 119 virtual void FetchImage(int page_num, | 119 virtual void FetchImage(int page_num, |
| 120 const std::string& image_id, | 120 const std::string& image_id, |
| 121 const std::string& image_url); | 121 const std::string& image_url); |
| 122 | 122 |
| 123 // Distills the next page. | 123 // Distills the next page. |
| 124 void DistillNextPage(); | 124 void DistillNextPage(); |
| 125 | 125 |
| 126 // Adds the |url| to |pages_to_be_distilled| if |page_num| is a valid relative | 126 // Adds the |url| to |pages_to_be_distilled| if |page_num| is a valid relative |
| (...skipping 19 matching lines...) Expand all Loading... |
| 146 // all image fetches. | 146 // all image fetches. |
| 147 void AddPageIfDone(int page_num); | 147 void AddPageIfDone(int page_num); |
| 148 | 148 |
| 149 DistilledPageData* GetPageAtIndex(size_t index) const; | 149 DistilledPageData* GetPageAtIndex(size_t index) const; |
| 150 | 150 |
| 151 // Create an ArticleDistillationUpdate for the current distillation | 151 // Create an ArticleDistillationUpdate for the current distillation |
| 152 // state. | 152 // state. |
| 153 const ArticleDistillationUpdate CreateDistillationUpdate() const; | 153 const ArticleDistillationUpdate CreateDistillationUpdate() const; |
| 154 | 154 |
| 155 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; | 155 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; |
| 156 scoped_ptr<DistillerPage> distiller_page_; | 156 std::unique_ptr<DistillerPage> distiller_page_; |
| 157 | 157 |
| 158 dom_distiller::proto::DomDistillerOptions dom_distiller_options_; | 158 dom_distiller::proto::DomDistillerOptions dom_distiller_options_; |
| 159 DistillationFinishedCallback finished_cb_; | 159 DistillationFinishedCallback finished_cb_; |
| 160 DistillationUpdateCallback update_cb_; | 160 DistillationUpdateCallback update_cb_; |
| 161 | 161 |
| 162 // Set of pages that are under distillation or have finished distillation. | 162 // Set of pages that are under distillation or have finished distillation. |
| 163 // |started_pages_index_| and |finished_pages_index_| maintains the mapping | 163 // |started_pages_index_| and |finished_pages_index_| maintains the mapping |
| 164 // from page number to the indices in |pages_|. | 164 // from page number to the indices in |pages_|. |
| 165 ScopedVector<DistilledPageData> pages_; | 165 ScopedVector<DistilledPageData> pages_; |
| 166 | 166 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 186 bool destruction_allowed_; | 186 bool destruction_allowed_; |
| 187 | 187 |
| 188 base::WeakPtrFactory<DistillerImpl> weak_factory_; | 188 base::WeakPtrFactory<DistillerImpl> weak_factory_; |
| 189 | 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); | 190 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); |
| 191 }; | 191 }; |
| 192 | 192 |
| 193 } // namespace dom_distiller | 193 } // namespace dom_distiller |
| 194 | 194 |
| 195 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 195 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
| OLD | NEW |