Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / distiller_page.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_PAGE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/values.h"
14 #include "third_party/dom_distiller_js/dom_distiller.pb.h"
15 #include "ui/gfx/size.h"
16 #include "url/gurl.h"
17
18 namespace dom_distiller {
19
20 struct DistilledPageInfo {
21   struct MarkupArticle {
22     std::string published_time;
23     std::string modified_time;
24     std::string expiration_time;
25     std::string section;
26     std::vector<std::string> authors;
27
28     MarkupArticle();
29     ~MarkupArticle();
30   };
31
32   struct MarkupImage {
33     std::string url;
34     std::string secure_url;
35     std::string type;
36     std::string caption;
37     int width;
38     int height;
39
40     MarkupImage();
41     ~MarkupImage();
42   };
43
44   struct MarkupInfo {
45     std::string title;
46     std::string type;
47     std::string url;
48     std::string description;
49     std::string publisher;
50     std::string copyright;
51     std::string author;
52     MarkupArticle article;
53     std::vector<MarkupImage> images;
54
55     MarkupInfo();
56     ~MarkupInfo();
57   };
58
59   std::string title;
60   std::string html;
61   std::string next_page_url;
62   std::string prev_page_url;
63   std::vector<std::string> image_urls;
64   MarkupInfo markup_info;
65
66   DistilledPageInfo();
67   ~DistilledPageInfo();
68
69  private:
70   DISALLOW_COPY_AND_ASSIGN(DistilledPageInfo);
71 };
72
73 class SourcePageHandle {
74  public:
75   virtual ~SourcePageHandle() {}
76 };
77
78 // Injects JavaScript into a page, and uses it to extract and return long-form
79 // content. The class can be reused to load and distill multiple pages,
80 // following the state transitions described along with the class's states.
81 // Constructing a DistillerPage should be cheap, as some of the instances can be
82 // thrown away without ever being used.
83 class DistillerPage {
84  public:
85   typedef base::Callback<void(scoped_ptr<DistilledPageInfo> distilled_page,
86                               bool distillation_successful)>
87       DistillerPageCallback;
88
89   DistillerPage();
90   virtual ~DistillerPage();
91
92   // Loads a URL. |OnDistillationDone| is called when the load completes or
93   // fails. May be called when the distiller is idle. Callers can assume that,
94   // for a given |url| and |options|, any DistillerPage implementation will
95   // extract the same content.
96   void DistillPage(const GURL& url,
97                    const dom_distiller::proto::DomDistillerOptions options,
98                    const DistillerPageCallback& callback);
99
100   // Called when the JavaScript execution completes. |page_url| is the url of
101   // the distilled page. |value| contains data returned by the script.
102   virtual void OnDistillationDone(const GURL& page_url,
103                                   const base::Value* value);
104
105  protected:
106   // Called by |DistillPage| to carry out platform-specific instructions to load
107   // and distill the |url| using the provided |script|. The extracted content
108   // should be the same regardless of the DistillerPage implementation.
109   virtual void DistillPageImpl(const GURL& url, const std::string& script) = 0;
110
111  private:
112   bool ready_;
113   DistillerPageCallback distiller_page_callback_;
114   DISALLOW_COPY_AND_ASSIGN(DistillerPage);
115 };
116
117 // Factory for generating a |DistillerPage|.
118 class DistillerPageFactory {
119  public:
120   virtual ~DistillerPageFactory();
121
122   // Constructs and returns a new DistillerPage. The implementation of this
123   // should be very cheap, since the pages can be thrown away without being
124   // used.
125   virtual scoped_ptr<DistillerPage> CreateDistillerPage(
126       const gfx::Size& render_view_size) const = 0;
127   virtual scoped_ptr<DistillerPage> CreateDistillerPageWithHandle(
128       scoped_ptr<SourcePageHandle> handle) const = 0;
129 };
130
131 }  // namespace dom_distiller
132
133 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_