Switch TestFrameNavigationObserver to DidCommitProvisionalLoadForFrame.
[chromium-blink-merge.git] / components / dom_distiller / content / distiller_page_web_contents.h
blob82abe8d530c69155b91bee19b31b41d537a743c8
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_
8 #include <string>
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"
17 namespace dom_distiller {
19 class SourcePageHandleWebContents : public SourcePageHandle {
20 public:
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_; }
27 private:
28 // The WebContents this class holds.
29 content::WebContents* web_contents_;
30 // Whether this owns |web_contents_|.
31 bool owned_;
34 class DistillerPageWebContentsFactory : public DistillerPageFactory {
35 public:
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;
46 private:
47 content::BrowserContext* browser_context_;
50 class DistillerPageWebContents : public DistillerPage,
51 public content::WebContentsDelegate,
52 public content::WebContentsObserver {
53 public:
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,
70 int error_code,
71 const base::string16& error_description) override;
73 protected:
74 bool StringifyOutput() override;
75 bool CreateNewContext() override;
76 void DistillPageImpl(const GURL& url, const std::string& script) override;
78 private:
79 friend class TestDistillerPageWebContents;
81 enum State {
82 // The page distiller is idle.
83 IDLE,
84 // A page is currently loading.
85 LOADING_PAGE,
86 // There was an error processing the page.
87 PAGELOAD_FAILED,
88 // JavaScript is executing within the context of the page. When the
89 // JavaScript completes, the state will be returned to |IDLE|.
90 EXECUTING_JAVASCRIPT
93 // Creates a new WebContents, adds |this| as an observer, and loads the
94 // |url|.
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|.
106 State state_;
108 // The JavaScript to inject to extract content.
109 std::string script_;
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_