Chromium Code Reviews| 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 <map> | |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "components/dom_distiller/core/article_distillation_update.h" | |
| 14 #include "components/dom_distiller/core/distiller_url_fetcher.h" | 17 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
| 15 #include "components/dom_distiller/core/page_distiller.h" | 18 #include "components/dom_distiller/core/page_distiller.h" |
| 16 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 19 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 18 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 19 | 22 |
| 20 namespace dom_distiller { | 23 namespace dom_distiller { |
| 21 | 24 |
| 22 class DistillerImpl; | 25 class DistillerImpl; |
| 23 | 26 |
| 24 class Distiller { | 27 class Distiller { |
| 25 public: | 28 public: |
| 26 typedef base::Callback<void(scoped_ptr<DistilledArticleProto>)> | 29 typedef base::Callback<void(scoped_ptr<DistilledArticleProto>)> |
| 27 DistillerCallback; | 30 DistillationFinishedCallback; |
| 31 typedef base::Callback<void(const ArticleDistillationUpdate&)> | |
| 32 DistillationUpdateCallback; | |
| 33 | |
| 28 virtual ~Distiller() {} | 34 virtual ~Distiller() {} |
| 29 | 35 |
| 30 // Distills a page, and asynchrounously returns the article HTML to the | 36 // Distills a page, and asynchronously returns the article HTML to the |
| 31 // supplied callback. | 37 // supplied |finished_cb| callback. |update_cb| is invoked whenever article |
| 38 // under distillation is updated with more data. | |
| 39 // E.g. when distilling a 2 page article, |update_cb| may be invoked each time | |
| 40 // a distilled page is added and |finished_cb| will be invoked once | |
| 41 // distillation is completed. | |
| 32 virtual void DistillPage(const GURL& url, | 42 virtual void DistillPage(const GURL& url, |
| 33 const DistillerCallback& callback) = 0; | 43 const DistillationFinishedCallback& finished_cb, |
| 44 const DistillationUpdateCallback& update_cb) = 0; | |
| 34 }; | 45 }; |
| 35 | 46 |
| 36 class DistillerFactory { | 47 class DistillerFactory { |
| 37 public: | 48 public: |
| 38 virtual scoped_ptr<Distiller> CreateDistiller() = 0; | 49 virtual scoped_ptr<Distiller> CreateDistiller() = 0; |
| 39 virtual ~DistillerFactory() {} | 50 virtual ~DistillerFactory() {} |
| 40 }; | 51 }; |
| 41 | 52 |
| 42 // Factory for creating a Distiller. | 53 // Factory for creating a Distiller. |
| 43 class DistillerFactoryImpl : public DistillerFactory { | 54 class DistillerFactoryImpl : public DistillerFactory { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 59 DistillerImpl( | 70 DistillerImpl( |
| 60 const DistillerPageFactory& distiller_page_factory, | 71 const DistillerPageFactory& distiller_page_factory, |
| 61 const DistillerURLFetcherFactory& distiller_url_fetcher_factory); | 72 const DistillerURLFetcherFactory& distiller_url_fetcher_factory); |
| 62 virtual ~DistillerImpl(); | 73 virtual ~DistillerImpl(); |
| 63 | 74 |
| 64 // Creates an execution context. This must be called once before any calls are | 75 // Creates an execution context. This must be called once before any calls are |
| 65 // made to distill the page. | 76 // made to distill the page. |
| 66 virtual void Init(); | 77 virtual void Init(); |
| 67 | 78 |
| 68 virtual void DistillPage(const GURL& url, | 79 virtual void DistillPage(const GURL& url, |
| 69 const DistillerCallback& callback) OVERRIDE; | 80 const DistillationFinishedCallback& finished_cb, |
| 81 const DistillationUpdateCallback& update_cb) | |
| 82 OVERRIDE; | |
| 70 | 83 |
| 71 void SetMaxNumPagesInArticle(size_t max_num_pages); | 84 void SetMaxNumPagesInArticle(size_t max_num_pages); |
| 72 | 85 |
| 73 private: | 86 private: |
| 74 // In case of multiple pages, the Distiller maintains state of multiple pages | 87 // In case of multiple pages, the Distiller maintains state of multiple pages |
| 75 // as page numbers relative to the page number where distillation started. | 88 // as page numbers relative to the page number where distillation started. |
| 76 // E.g. if distillation starts at page 2 for a 3 page article. The relative | 89 // E.g. if distillation starts at page 2 for a 3 page article. The relative |
| 77 // page numbers assigned to pages will be [-1,0,1]. | 90 // page numbers assigned to pages will be [-1,0,1]. |
| 78 | 91 |
| 79 // Class representing the state of a page under distillation. | 92 // Class representing the state of a page under distillation. |
| 80 struct DistilledPageData { | 93 struct DistilledPageData { |
| 81 DistilledPageData(); | 94 DistilledPageData(); |
| 82 virtual ~DistilledPageData(); | 95 virtual ~DistilledPageData(); |
| 83 // Relative page number of the page. | 96 // Relative page number of the page. |
| 84 int page_num; | 97 int page_num; |
| 85 std::string title; | 98 std::string title; |
| 86 ScopedVector<DistillerURLFetcher> image_fetchers_; | 99 ScopedVector<DistillerURLFetcher> image_fetchers_; |
| 87 scoped_ptr<DistilledPageProto> proto; | 100 scoped_refptr<base::RefCountedData<DistilledPageProto> > proto; |
| 88 | 101 |
| 89 private: | 102 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); | 103 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); |
| 91 }; | 104 }; |
| 92 | 105 |
| 93 void OnFetchImageDone(int page_num, | 106 void OnFetchImageDone(int page_num, |
| 94 DistillerURLFetcher* url_fetcher, | 107 DistillerURLFetcher* url_fetcher, |
| 95 const std::string& id, | 108 const std::string& id, |
| 96 const std::string& response); | 109 const std::string& response); |
| 97 | 110 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 115 // |page_num| is either under distillation or has already completed | 128 // |page_num| is either under distillation or has already completed |
| 116 // distillation. | 129 // distillation. |
| 117 bool IsPageNumberInUse(int page_num) const; | 130 bool IsPageNumberInUse(int page_num) const; |
| 118 | 131 |
| 119 bool AreAllPagesFinished() const; | 132 bool AreAllPagesFinished() const; |
| 120 | 133 |
| 121 // Total number of pages in the article that the distiller knows of, this | 134 // Total number of pages in the article that the distiller knows of, this |
| 122 // includes pages that are pending distillation. | 135 // includes pages that are pending distillation. |
| 123 size_t TotalPageCount() const; | 136 size_t TotalPageCount() const; |
| 124 | 137 |
| 125 // Runs |distillation_cb_| if all distillation callbacks and image fetches are | 138 // Runs |finished_cb_| if all distillation callbacks and image fetches are |
| 126 // complete. | 139 // complete. |
| 127 void RunDistillerCallbackIfDone(); | 140 void RunDistillerCallbackIfDone(); |
| 128 | 141 |
| 129 // Checks if page |distilled_page_data| has finished distillation, including | 142 // Checks if page |distilled_page_data| has finished distillation, including |
| 130 // all image fetches. | 143 // all image fetches. |
| 131 void AddPageIfDone(int page_num); | 144 void AddPageIfDone(int page_num); |
| 132 | 145 |
| 133 DistilledPageData* GetPageAtIndex(size_t index) const; | 146 DistilledPageData* GetPageAtIndex(size_t index) const; |
| 134 | 147 |
| 148 const ArticleDistillationUpdate CreateDistillationUpdate() const; | |
|
nyquist
2014/03/03 23:00:12
What does this do? Even though it's private a shor
shashi
2014/03/05 03:45:39
Done.
| |
| 149 | |
| 135 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; | 150 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; |
| 136 scoped_ptr<PageDistiller> page_distiller_; | 151 scoped_ptr<PageDistiller> page_distiller_; |
| 137 DistillerCallback distillation_cb_; | 152 DistillationFinishedCallback finished_cb_; |
| 153 DistillationUpdateCallback update_cb_; | |
| 138 | 154 |
| 139 // Set of pages that are under distillation or have finished distillation. | 155 // Set of pages that are under distillation or have finished distillation. |
| 140 // |started_pages_index_| and |finished_pages_index_| maintains the mapping | 156 // |started_pages_index_| and |finished_pages_index_| maintains the mapping |
| 141 // from page number to the indices in |pages_|. | 157 // from page number to the indices in |pages_|. |
| 142 ScopedVector<DistilledPageData> pages_; | 158 ScopedVector<DistilledPageData> pages_; |
| 143 | 159 |
| 144 // Maps page numbers of finished pages to the indices in |pages_|. | 160 // Maps page numbers of finished pages to the indices in |pages_|. |
| 145 std::map<int, size_t> finished_pages_index_; | 161 std::map<int, size_t> finished_pages_index_; |
| 146 | 162 |
| 147 // Maps page numbers of pages under distillation to the indices in |pages_|. | 163 // Maps page numbers of pages under distillation to the indices in |pages_|. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 159 base::hash_set<std::string> seen_urls_; | 175 base::hash_set<std::string> seen_urls_; |
| 160 | 176 |
| 161 size_t max_pages_in_article_; | 177 size_t max_pages_in_article_; |
| 162 | 178 |
| 163 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); | 179 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); |
| 164 }; | 180 }; |
| 165 | 181 |
| 166 } // namespace dom_distiller | 182 } // namespace dom_distiller |
| 167 | 183 |
| 168 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 184 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
| OLD | NEW |