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 explicit SourcePageHandleWebContents(
22 scoped_ptr
<content::WebContents
> web_contents
);
23 virtual ~SourcePageHandleWebContents();
25 scoped_ptr
<content::WebContents
> GetWebContents();
28 // The WebContents this class owns.
29 scoped_ptr
<content::WebContents
> web_contents_
;
32 class DistillerPageWebContentsFactory
: public DistillerPageFactory
{
34 explicit DistillerPageWebContentsFactory(
35 content::BrowserContext
* browser_context
)
36 : DistillerPageFactory(), browser_context_(browser_context
) {}
37 virtual ~DistillerPageWebContentsFactory() {}
39 virtual scoped_ptr
<DistillerPage
> CreateDistillerPage(
40 const gfx::Size
& render_view_size
) const OVERRIDE
;
41 virtual scoped_ptr
<DistillerPage
> CreateDistillerPageWithHandle(
42 scoped_ptr
<SourcePageHandle
> handle
) const OVERRIDE
;
45 content::BrowserContext
* browser_context_
;
48 class DistillerPageWebContents
: public DistillerPage
,
49 public content::WebContentsDelegate
,
50 public content::WebContentsObserver
{
52 DistillerPageWebContents(
53 content::BrowserContext
* browser_context
,
54 const gfx::Size
& render_view_size
,
55 scoped_ptr
<SourcePageHandleWebContents
> optional_web_contents_handle
);
56 virtual ~DistillerPageWebContents();
58 // content::WebContentsDelegate implementation.
59 virtual gfx::Size
GetSizeForNewRenderView(
60 content::WebContents
* web_contents
) const OVERRIDE
;
62 // content::WebContentsObserver implementation.
63 virtual void DocumentLoadedInFrame(
64 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
66 virtual void DidFailLoad(content::RenderFrameHost
* render_frame_host
,
67 const GURL
& validated_url
,
69 const base::string16
& error_description
) OVERRIDE
;
72 virtual void DistillPageImpl(const GURL
& url
,
73 const std::string
& script
) OVERRIDE
;
76 friend class TestDistillerPageWebContents
;
79 // The page distiller is idle.
81 // A page is currently loading.
83 // There was an error processing the page.
85 // JavaScript is executing within the context of the page. When the
86 // JavaScript completes, the state will be returned to |IDLE|.
90 // Creates a new WebContents, adds |this| as an observer, and loads the
92 virtual void CreateNewWebContents(const GURL
& url
);
94 // Injects and executes JavaScript in the context of a loaded page. This
95 // must only be called after the page has successfully loaded.
96 void ExecuteJavaScript();
98 // Called when the distillation is done or if the page load failed.
99 void OnWebContentsDistillationDone(const GURL
& page_url
,
100 const base::Value
* value
);
102 // The current state of the |DistillerPage|, initially |IDLE|.
105 // The JavaScript to inject to extract content.
108 scoped_ptr
<content::WebContents
> web_contents_
;
109 content::BrowserContext
* browser_context_
;
110 gfx::Size render_view_size_
;
111 DISALLOW_COPY_AND_ASSIGN(DistillerPageWebContents
);
114 } // namespace dom_distiller
116 #endif // COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_