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_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_
6 #define COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_
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"
17 namespace dom_distiller
{
19 class SourcePageHandleWebContents
: public SourcePageHandle
{
21 SourcePageHandleWebContents(content::WebContents
* web_contents
, bool owned
);
22 ~SourcePageHandleWebContents() override
;
24 // Retreives the WebContents. The SourcePageHandleWebContents keeps ownership.
25 content::WebContents
* web_contents() { return web_contents_
; }
28 // The WebContents this class holds.
29 content::WebContents
* web_contents_
;
30 // Whether this owns |web_contents_|.
34 class DistillerPageWebContentsFactory
: public DistillerPageFactory
{
36 explicit DistillerPageWebContentsFactory(
37 content::BrowserContext
* browser_context
)
38 : DistillerPageFactory(), browser_context_(browser_context
) {}
39 ~DistillerPageWebContentsFactory() override
{}
41 scoped_ptr
<DistillerPage
> CreateDistillerPage(
42 const gfx::Size
& render_view_size
) const override
;
43 scoped_ptr
<DistillerPage
> CreateDistillerPageWithHandle(
44 scoped_ptr
<SourcePageHandle
> handle
) const override
;
47 content::BrowserContext
* browser_context_
;
50 class DistillerPageWebContents
: public DistillerPage
,
51 public content::WebContentsDelegate
,
52 public content::WebContentsObserver
{
54 DistillerPageWebContents(
55 content::BrowserContext
* browser_context
,
56 const gfx::Size
& render_view_size
,
57 scoped_ptr
<SourcePageHandleWebContents
> optional_web_contents_handle
);
58 ~DistillerPageWebContents() override
;
60 // content::WebContentsDelegate implementation.
61 gfx::Size
GetSizeForNewRenderView(
62 content::WebContents
* web_contents
) const override
;
64 // content::WebContentsObserver implementation.
65 void DocumentLoadedInFrame(
66 content::RenderFrameHost
* render_frame_host
) override
;
68 void DidFailLoad(content::RenderFrameHost
* render_frame_host
,
69 const GURL
& validated_url
,
71 const base::string16
& error_description
) override
;
74 bool StringifyOutput() override
;
75 bool CreateNewContext() override
;
76 void DistillPageImpl(const GURL
& url
, const std::string
& script
) override
;
79 friend class TestDistillerPageWebContents
;
82 // The page distiller is idle.
84 // A page is currently loading.
86 // There was an error processing the page.
88 // JavaScript is executing within the context of the page. When the
89 // JavaScript completes, the state will be returned to |IDLE|.
93 // Creates a new WebContents, adds |this| as an observer, and loads the
95 virtual void CreateNewWebContents(const GURL
& url
);
97 // Injects and executes JavaScript in the context of a loaded page. This
98 // must only be called after the page has successfully loaded.
99 void ExecuteJavaScript();
101 // Called when the distillation is done or if the page load failed.
102 void OnWebContentsDistillationDone(const GURL
& page_url
,
103 const base::Value
* value
);
105 // The current state of the |DistillerPage|, initially |IDLE|.
108 // The JavaScript to inject to extract content.
111 scoped_ptr
<SourcePageHandleWebContents
> source_page_handle_
;
113 content::BrowserContext
* browser_context_
;
114 gfx::Size render_view_size_
;
115 DISALLOW_COPY_AND_ASSIGN(DistillerPageWebContents
);
118 } // namespace dom_distiller
120 #endif // COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_