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> > |
| 101 distilled_page_proto; |
88 | 102 |
89 private: | 103 private: |
90 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); | 104 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); |
91 }; | 105 }; |
92 | 106 |
93 void OnFetchImageDone(int page_num, | 107 void OnFetchImageDone(int page_num, |
94 DistillerURLFetcher* url_fetcher, | 108 DistillerURLFetcher* url_fetcher, |
95 const std::string& id, | 109 const std::string& id, |
96 const std::string& response); | 110 const std::string& response); |
97 | 111 |
(...skipping 17 matching lines...) Expand all Loading... |
115 // |page_num| is either under distillation or has already completed | 129 // |page_num| is either under distillation or has already completed |
116 // distillation. | 130 // distillation. |
117 bool IsPageNumberInUse(int page_num) const; | 131 bool IsPageNumberInUse(int page_num) const; |
118 | 132 |
119 bool AreAllPagesFinished() const; | 133 bool AreAllPagesFinished() const; |
120 | 134 |
121 // Total number of pages in the article that the distiller knows of, this | 135 // Total number of pages in the article that the distiller knows of, this |
122 // includes pages that are pending distillation. | 136 // includes pages that are pending distillation. |
123 size_t TotalPageCount() const; | 137 size_t TotalPageCount() const; |
124 | 138 |
125 // Runs |distillation_cb_| if all distillation callbacks and image fetches are | 139 // Runs |finished_cb_| if all distillation callbacks and image fetches are |
126 // complete. | 140 // complete. |
127 void RunDistillerCallbackIfDone(); | 141 void RunDistillerCallbackIfDone(); |
128 | 142 |
129 // Checks if page |distilled_page_data| has finished distillation, including | 143 // Checks if page |distilled_page_data| has finished distillation, including |
130 // all image fetches. | 144 // all image fetches. |
131 void AddPageIfDone(int page_num); | 145 void AddPageIfDone(int page_num); |
132 | 146 |
133 DistilledPageData* GetPageAtIndex(size_t index) const; | 147 DistilledPageData* GetPageAtIndex(size_t index) const; |
134 | 148 |
| 149 // Create an ArticleDistillationUpdate for the current distillation |
| 150 // state. |
| 151 const ArticleDistillationUpdate CreateDistillationUpdate() const; |
| 152 |
135 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; | 153 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; |
136 scoped_ptr<PageDistiller> page_distiller_; | 154 scoped_ptr<PageDistiller> page_distiller_; |
137 DistillerCallback distillation_cb_; | 155 DistillationFinishedCallback finished_cb_; |
| 156 DistillationUpdateCallback update_cb_; |
138 | 157 |
139 // Set of pages that are under distillation or have finished distillation. | 158 // Set of pages that are under distillation or have finished distillation. |
140 // |started_pages_index_| and |finished_pages_index_| maintains the mapping | 159 // |started_pages_index_| and |finished_pages_index_| maintains the mapping |
141 // from page number to the indices in |pages_|. | 160 // from page number to the indices in |pages_|. |
142 ScopedVector<DistilledPageData> pages_; | 161 ScopedVector<DistilledPageData> pages_; |
143 | 162 |
144 // Maps page numbers of finished pages to the indices in |pages_|. | 163 // Maps page numbers of finished pages to the indices in |pages_|. |
145 std::map<int, size_t> finished_pages_index_; | 164 std::map<int, size_t> finished_pages_index_; |
146 | 165 |
147 // Maps page numbers of pages under distillation to the indices in |pages_|. | 166 // 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_; | 178 base::hash_set<std::string> seen_urls_; |
160 | 179 |
161 size_t max_pages_in_article_; | 180 size_t max_pages_in_article_; |
162 | 181 |
163 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); | 182 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); |
164 }; | 183 }; |
165 | 184 |
166 } // namespace dom_distiller | 185 } // namespace dom_distiller |
167 | 186 |
168 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 187 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
OLD | NEW |