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 ArticleDistillationCallback; |
31 typedef base::Callback<void(const ArticleDistillationUpdate&)> | |
32 PageDistillationCallback; | |
cjhopman
2014/02/28 02:38:14
How about:
DistillationFinishedCallback
and
Disti
cjhopman
2014/02/28 02:38:14
Consider making a new class for these callbacks (i
shashi
2014/03/02 02:53:40
Done.
shashi
2014/03/02 02:53:40
Done.
| |
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 asynchrounously returns the article HTML to the |
cjhopman
2014/02/28 02:38:14
s/asynchrounously/asynchronously
shashi
2014/03/02 02:53:40
Done.
| |
31 // supplied callback. | 37 // supplied callback. |
cjhopman
2014/02/28 02:38:14
Update this comment to reflect these changes.
shashi
2014/03/02 02:53:40
Done.
| |
32 virtual void DistillPage(const GURL& url, | 38 virtual void DistillPage(const GURL& url, |
33 const DistillerCallback& callback) = 0; | 39 const ArticleDistillationCallback& article_cb, |
40 const PageDistillationCallback& page_cb) = 0; | |
34 }; | 41 }; |
35 | 42 |
36 class DistillerFactory { | 43 class DistillerFactory { |
37 public: | 44 public: |
38 virtual scoped_ptr<Distiller> CreateDistiller() = 0; | 45 virtual scoped_ptr<Distiller> CreateDistiller() = 0; |
39 virtual ~DistillerFactory() {} | 46 virtual ~DistillerFactory() {} |
40 }; | 47 }; |
41 | 48 |
42 // Factory for creating a Distiller. | 49 // Factory for creating a Distiller. |
43 class DistillerFactoryImpl : public DistillerFactory { | 50 class DistillerFactoryImpl : public DistillerFactory { |
(...skipping 15 matching lines...) Expand all Loading... | |
59 DistillerImpl( | 66 DistillerImpl( |
60 const DistillerPageFactory& distiller_page_factory, | 67 const DistillerPageFactory& distiller_page_factory, |
61 const DistillerURLFetcherFactory& distiller_url_fetcher_factory); | 68 const DistillerURLFetcherFactory& distiller_url_fetcher_factory); |
62 virtual ~DistillerImpl(); | 69 virtual ~DistillerImpl(); |
63 | 70 |
64 // Creates an execution context. This must be called once before any calls are | 71 // Creates an execution context. This must be called once before any calls are |
65 // made to distill the page. | 72 // made to distill the page. |
66 virtual void Init(); | 73 virtual void Init(); |
67 | 74 |
68 virtual void DistillPage(const GURL& url, | 75 virtual void DistillPage(const GURL& url, |
69 const DistillerCallback& callback) OVERRIDE; | 76 const ArticleDistillationCallback& article_cb, |
77 const PageDistillationCallback& page_cb) OVERRIDE; | |
70 | 78 |
71 void SetMaxNumPagesInArticle(size_t max_num_pages); | 79 void SetMaxNumPagesInArticle(size_t max_num_pages); |
72 | 80 |
73 private: | 81 private: |
74 // In case of multiple pages, the Distiller maintains state of multiple pages | 82 // In case of multiple pages, the Distiller maintains state of multiple pages |
75 // as page numbers relative to the page number where distillation started. | 83 // 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 | 84 // 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]. | 85 // page numbers assigned to pages will be [-1,0,1]. |
78 | 86 |
79 // Class representing the state of a page under distillation. | 87 // Class representing the state of a page under distillation. |
80 struct DistilledPageData { | 88 struct DistilledPageData { |
81 DistilledPageData(); | 89 DistilledPageData(); |
82 virtual ~DistilledPageData(); | 90 virtual ~DistilledPageData(); |
83 // Relative page number of the page. | 91 // Relative page number of the page. |
84 int page_num; | 92 int page_num; |
85 std::string title; | 93 std::string title; |
86 ScopedVector<DistillerURLFetcher> image_fetchers_; | 94 ScopedVector<DistillerURLFetcher> image_fetchers_; |
87 scoped_ptr<DistilledPageProto> proto; | 95 scoped_refptr<base::RefCountedData<DistilledPageProto> > proto; |
88 | 96 |
89 private: | 97 private: |
90 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); | 98 DISALLOW_COPY_AND_ASSIGN(DistilledPageData); |
91 }; | 99 }; |
92 | 100 |
93 void OnFetchImageDone(int page_num, | 101 void OnFetchImageDone(int page_num, |
94 DistillerURLFetcher* url_fetcher, | 102 DistillerURLFetcher* url_fetcher, |
95 const std::string& id, | 103 const std::string& id, |
96 const std::string& response); | 104 const std::string& response); |
97 | 105 |
(...skipping 17 matching lines...) Expand all Loading... | |
115 // |page_num| is either under distillation or has already completed | 123 // |page_num| is either under distillation or has already completed |
116 // distillation. | 124 // distillation. |
117 bool IsPageNumberInUse(int page_num) const; | 125 bool IsPageNumberInUse(int page_num) const; |
118 | 126 |
119 bool AreAllPagesFinished() const; | 127 bool AreAllPagesFinished() const; |
120 | 128 |
121 // Total number of pages in the article that the distiller knows of, this | 129 // Total number of pages in the article that the distiller knows of, this |
122 // includes pages that are pending distillation. | 130 // includes pages that are pending distillation. |
123 size_t TotalPageCount() const; | 131 size_t TotalPageCount() const; |
124 | 132 |
125 // Runs |distillation_cb_| if all distillation callbacks and image fetches are | 133 // Runs |article_cb_| if all distillation callbacks and image fetches are |
126 // complete. | 134 // complete. |
127 void RunDistillerCallbackIfDone(); | 135 void RunDistillerCallbackIfDone(); |
128 | 136 |
129 // Checks if page |distilled_page_data| has finished distillation, including | 137 // Checks if page |distilled_page_data| has finished distillation, including |
130 // all image fetches. | 138 // all image fetches. |
131 void AddPageIfDone(int page_num); | 139 void AddPageIfDone(int page_num); |
132 | 140 |
133 DistilledPageData* GetPageAtIndex(size_t index) const; | 141 DistilledPageData* GetPageAtIndex(size_t index) const; |
134 | 142 |
135 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; | 143 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_; |
136 scoped_ptr<PageDistiller> page_distiller_; | 144 scoped_ptr<PageDistiller> page_distiller_; |
137 DistillerCallback distillation_cb_; | 145 ArticleDistillationCallback article_cb_; |
146 PageDistillationCallback page_cb_; | |
138 | 147 |
139 // Set of pages that are under distillation or have finished distillation. | 148 // Set of pages that are under distillation or have finished distillation. |
140 // |started_pages_index_| and |finished_pages_index_| maintains the mapping | 149 // |started_pages_index_| and |finished_pages_index_| maintains the mapping |
141 // from page number to the indices in |pages_|. | 150 // from page number to the indices in |pages_|. |
142 ScopedVector<DistilledPageData> pages_; | 151 ScopedVector<DistilledPageData> pages_; |
143 | 152 |
144 // Maps page numbers of finished pages to the indices in |pages_|. | 153 // Maps page numbers of finished pages to the indices in |pages_|. |
145 std::map<int, size_t> finished_pages_index_; | 154 std::map<int, size_t> finished_pages_index_; |
146 | 155 |
147 // Maps page numbers of pages under distillation to the indices in |pages_|. | 156 // 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_; | 168 base::hash_set<std::string> seen_urls_; |
160 | 169 |
161 size_t max_pages_in_article_; | 170 size_t max_pages_in_article_; |
162 | 171 |
163 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); | 172 DISALLOW_COPY_AND_ASSIGN(DistillerImpl); |
164 }; | 173 }; |
165 | 174 |
166 } // namespace dom_distiller | 175 } // namespace dom_distiller |
167 | 176 |
168 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ | 177 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_ |
OLD | NEW |