Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / content / distiller_page_web_contents.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_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_
6 #define COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "components/dom_distiller/core/distiller_page.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_delegate.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "url/gurl.h"
16
17 namespace dom_distiller {
18
19 class SourcePageHandleWebContents : public SourcePageHandle {
20  public:
21   explicit SourcePageHandleWebContents(
22       scoped_ptr<content::WebContents> web_contents);
23   ~SourcePageHandleWebContents() override;
24
25   scoped_ptr<content::WebContents> GetWebContents();
26
27  private:
28   // The WebContents this class owns.
29   scoped_ptr<content::WebContents> web_contents_;
30 };
31
32 class DistillerPageWebContentsFactory : public DistillerPageFactory {
33  public:
34   explicit DistillerPageWebContentsFactory(
35       content::BrowserContext* browser_context)
36       : DistillerPageFactory(), browser_context_(browser_context) {}
37   ~DistillerPageWebContentsFactory() override {}
38
39   scoped_ptr<DistillerPage> CreateDistillerPage(
40       const gfx::Size& render_view_size) const override;
41   scoped_ptr<DistillerPage> CreateDistillerPageWithHandle(
42       scoped_ptr<SourcePageHandle> handle) const override;
43
44  private:
45   content::BrowserContext* browser_context_;
46 };
47
48 class DistillerPageWebContents : public DistillerPage,
49                                  public content::WebContentsDelegate,
50                                  public content::WebContentsObserver {
51  public:
52   DistillerPageWebContents(
53       content::BrowserContext* browser_context,
54       const gfx::Size& render_view_size,
55       scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle);
56   ~DistillerPageWebContents() override;
57
58   // content::WebContentsDelegate implementation.
59   gfx::Size GetSizeForNewRenderView(
60       content::WebContents* web_contents) const override;
61
62   // content::WebContentsObserver implementation.
63   void DocumentLoadedInFrame(
64       content::RenderFrameHost* render_frame_host) override;
65
66   void DidFailLoad(content::RenderFrameHost* render_frame_host,
67                    const GURL& validated_url,
68                    int error_code,
69                    const base::string16& error_description) override;
70
71  protected:
72   void DistillPageImpl(const GURL& url, const std::string& script) override;
73
74  private:
75   friend class TestDistillerPageWebContents;
76
77   enum State {
78     // The page distiller is idle.
79     IDLE,
80     // A page is currently loading.
81     LOADING_PAGE,
82     // There was an error processing the page.
83     PAGELOAD_FAILED,
84     // JavaScript is executing within the context of the page. When the
85     // JavaScript completes, the state will be returned to |IDLE|.
86     EXECUTING_JAVASCRIPT
87   };
88
89   // Creates a new WebContents, adds |this| as an observer, and loads the
90   // |url|.
91   virtual void CreateNewWebContents(const GURL& url);
92
93   // Injects and executes JavaScript in the context of a loaded page. This
94   // must only be called after the page has successfully loaded.
95   void ExecuteJavaScript();
96
97   // Called when the distillation is done or if the page load failed.
98   void OnWebContentsDistillationDone(const GURL& page_url,
99                                      const base::Value* value);
100
101   // The current state of the |DistillerPage|, initially |IDLE|.
102   State state_;
103
104   // The JavaScript to inject to extract content.
105   std::string script_;
106
107   scoped_ptr<content::WebContents> web_contents_;
108   content::BrowserContext* browser_context_;
109   gfx::Size render_view_size_;
110   DISALLOW_COPY_AND_ASSIGN(DistillerPageWebContents);
111 };
112
113 }  // namespace dom_distiller
114
115 #endif  // COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_