Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / distiller.h
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.
4
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_
7
8 #include <map>
9 #include <string>
10
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"
22 #include "url/gurl.h"
23
24 namespace dom_distiller {
25
26 class DistillerImpl;
27
28 class Distiller {
29  public:
30   typedef base::Callback<void(scoped_ptr<DistilledArticleProto>)>
31       DistillationFinishedCallback;
32   typedef base::Callback<void(const ArticleDistillationUpdate&)>
33       DistillationUpdateCallback;
34
35   virtual ~Distiller() {}
36
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;
47 };
48
49 class DistillerFactory {
50  public:
51   virtual scoped_ptr<Distiller> CreateDistiller() = 0;
52   virtual ~DistillerFactory() {}
53 };
54
55 // Factory for creating a Distiller.
56 class DistillerFactoryImpl : public DistillerFactory {
57  public:
58   DistillerFactoryImpl(
59       scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory);
60   virtual ~DistillerFactoryImpl();
61   virtual scoped_ptr<Distiller> CreateDistiller() OVERRIDE;
62
63  private:
64   scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory_;
65 };
66
67 // Distills a article from a page and associated pages.
68 class DistillerImpl : public Distiller {
69  public:
70   DistillerImpl(
71       const DistillerURLFetcherFactory& distiller_url_fetcher_factory);
72   virtual ~DistillerImpl();
73
74   virtual void DistillPage(
75       const GURL& url,
76       scoped_ptr<DistillerPage> distiller_page,
77       const DistillationFinishedCallback& finished_cb,
78       const DistillationUpdateCallback& update_cb) OVERRIDE;
79
80   void SetMaxNumPagesInArticle(size_t max_num_pages);
81
82  private:
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].
87
88   // Class representing the state of a page under distillation.
89   struct DistilledPageData {
90     DistilledPageData();
91     virtual ~DistilledPageData();
92     // Relative page number of the page.
93     int page_num;
94     std::string title;
95     ScopedVector<DistillerURLFetcher> image_fetchers_;
96     scoped_refptr<base::RefCountedData<DistilledPageProto> >
97         distilled_page_proto;
98
99    private:
100     DISALLOW_COPY_AND_ASSIGN(DistilledPageData);
101   };
102
103   void OnFetchImageDone(int page_num,
104                         DistillerURLFetcher* url_fetcher,
105                         const std::string& id,
106                         const std::string& response);
107
108   void OnPageDistillationFinished(int page_num,
109                                   const GURL& page_url,
110                                   scoped_ptr<DistilledPageInfo> distilled_page,
111                                   bool distillation_successful);
112
113   virtual void FetchImage(int page_num,
114                           const std::string& image_id,
115                           const std::string& item);
116
117   // Distills the next page.
118   void DistillNextPage();
119
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);
123
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
126   // distillation.
127   bool IsPageNumberInUse(int page_num) const;
128
129   bool AreAllPagesFinished() const;
130
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;
134
135   // Runs |finished_cb_| if all distillation callbacks and image fetches are
136   // complete.
137   void RunDistillerCallbackIfDone();
138
139   // Checks if page |distilled_page_data| has finished distillation, including
140   // all image fetches.
141   void AddPageIfDone(int page_num);
142
143   DistilledPageData* GetPageAtIndex(size_t index) const;
144
145   // Create an ArticleDistillationUpdate for the current distillation
146   // state.
147   const ArticleDistillationUpdate CreateDistillationUpdate() const;
148
149   const DistillerURLFetcherFactory& distiller_url_fetcher_factory_;
150   scoped_ptr<DistillerPage> distiller_page_;
151   DistillationFinishedCallback finished_cb_;
152   DistillationUpdateCallback update_cb_;
153
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_;
158
159   // Maps page numbers of finished pages to the indices in |pages_|.
160   std::map<int, size_t> finished_pages_index_;
161
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_;
166
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
169   // first.
170   std::map<int, GURL> waiting_pages_;
171
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_;
175
176   size_t max_pages_in_article_;
177
178   bool destruction_allowed_;
179
180   base::WeakPtrFactory<DistillerImpl> weak_factory_;
181
182   DISALLOW_COPY_AND_ASSIGN(DistillerImpl);
183 };
184
185 }  // namespace dom_distiller
186
187 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_