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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_
11 #include "base/callback.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "components/dom_distiller/core/article_distillation_update.h"
18 #include "components/dom_distiller/core/distiller_page.h"
19 #include "components/dom_distiller/core/distiller_url_fetcher.h"
20 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
21 #include "net/url_request/url_request_context_getter.h"
24 namespace dom_distiller {
30 typedef base::Callback<void(scoped_ptr<DistilledArticleProto>)>
31 DistillationFinishedCallback;
32 typedef base::Callback<void(const ArticleDistillationUpdate&)>
33 DistillationUpdateCallback;
35 virtual ~Distiller() {}
37 // Distills a page, and asynchronously returns the article HTML to the
38 // supplied |finished_cb| callback. |update_cb| is invoked whenever article
39 // under distillation is updated with more data.
40 // E.g. when distilling a 2 page article, |update_cb| may be invoked each time
41 // a distilled page is added and |finished_cb| will be invoked once
42 // distillation is completed.
43 virtual void DistillPage(const GURL& url,
44 scoped_ptr<DistillerPage> distiller_page,
45 const DistillationFinishedCallback& finished_cb,
46 const DistillationUpdateCallback& update_cb) = 0;
49 class DistillerFactory {
51 virtual scoped_ptr<Distiller> CreateDistiller() = 0;
52 virtual ~DistillerFactory() {}
55 // Factory for creating a Distiller.
56 class DistillerFactoryImpl : public DistillerFactory {
59 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory);
60 virtual ~DistillerFactoryImpl();
61 virtual scoped_ptr<Distiller> CreateDistiller() OVERRIDE;
64 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory_;
67 // Distills a article from a page and associated pages.
68 class DistillerImpl : public Distiller {
71 const DistillerURLFetcherFactory& distiller_url_fetcher_factory);
72 virtual ~DistillerImpl();
74 virtual void DistillPage(
76 scoped_ptr<DistillerPage> distiller_page,
77 const DistillationFinishedCallback& finished_cb,
78 const DistillationUpdateCallback& update_cb) OVERRIDE;
80 void SetMaxNumPagesInArticle(size_t max_num_pages);
83 // In case of multiple pages, the Distiller maintains state of multiple pages
84 // as page numbers relative to the page number where distillation started.
85 // E.g. if distillation starts at page 2 for a 3 page article. The relative
86 // page numbers assigned to pages will be [-1,0,1].
88 // Class representing the state of a page under distillation.
89 struct DistilledPageData {
91 virtual ~DistilledPageData();
92 // Relative page number of the page.
95 ScopedVector<DistillerURLFetcher> image_fetchers_;
96 scoped_refptr<base::RefCountedData<DistilledPageProto> >
100 DISALLOW_COPY_AND_ASSIGN(DistilledPageData);
103 void OnFetchImageDone(int page_num,
104 DistillerURLFetcher* url_fetcher,
105 const std::string& id,
106 const std::string& response);
108 void OnPageDistillationFinished(int page_num,
109 const GURL& page_url,
110 scoped_ptr<DistilledPageInfo> distilled_page,
111 bool distillation_successful);
113 virtual void FetchImage(int page_num,
114 const std::string& image_id,
115 const std::string& item);
117 // Distills the next page.
118 void DistillNextPage();
120 // Adds the |url| to |pages_to_be_distilled| if |page_num| is a valid relative
121 // page number and |url| is valid. Ignores duplicate pages and urls.
122 void AddToDistillationQueue(int page_num, const GURL& url);
124 // Check if |page_num| is a valid relative page number, i.e. page with
125 // |page_num| is either under distillation or has already completed
127 bool IsPageNumberInUse(int page_num) const;
129 bool AreAllPagesFinished() const;
131 // Total number of pages in the article that the distiller knows of, this
132 // includes pages that are pending distillation.
133 size_t TotalPageCount() const;
135 // Runs |finished_cb_| if all distillation callbacks and image fetches are
137 void RunDistillerCallbackIfDone();
139 // Checks if page |distilled_page_data| has finished distillation, including
140 // all image fetches.
141 void AddPageIfDone(int page_num);
143 DistilledPageData* GetPageAtIndex(size_t index) const;
145 // Create an ArticleDistillationUpdate for the current distillation
147 const ArticleDistillationUpdate CreateDistillationUpdate() const;
149 const DistillerURLFetcherFactory& distiller_url_fetcher_factory_;
150 scoped_ptr<DistillerPage> distiller_page_;
151 DistillationFinishedCallback finished_cb_;
152 DistillationUpdateCallback update_cb_;
154 // Set of pages that are under distillation or have finished distillation.
155 // |started_pages_index_| and |finished_pages_index_| maintains the mapping
156 // from page number to the indices in |pages_|.
157 ScopedVector<DistilledPageData> pages_;
159 // Maps page numbers of finished pages to the indices in |pages_|.
160 std::map<int, size_t> finished_pages_index_;
162 // Maps page numbers of pages under distillation to the indices in |pages_|.
163 // If a page is |started_pages_| that means it is still waiting for an action
164 // (distillation or image fetch) to finish.
165 base::hash_map<int, size_t> started_pages_index_;
167 // The list of pages that are still waiting for distillation to start.
168 // This is a map, to make distiller prefer distilling lower page numbers
170 std::map<int, GURL> waiting_pages_;
172 // Set to keep track of which urls are already seen by the distiller. Used to
173 // prevent distiller from distilling the same url twice.
174 base::hash_set<std::string> seen_urls_;
176 size_t max_pages_in_article_;
178 bool destruction_allowed_;
180 base::WeakPtrFactory<DistillerImpl> weak_factory_;
182 DISALLOW_COPY_AND_ASSIGN(DistillerImpl);
185 } // namespace dom_distiller
187 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_