Don't preload rarely seen large images
[chromium-blink-merge.git] / components / dom_distiller / content / distiller_page_web_contents.h
blobfcf368f9cf2f9eb5d209bd5a4eafb17c219d134a
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,
72 bool was_ignored_by_handler) override;
74 protected:
75 bool StringifyOutput() override;
76 bool CreateNewContext() override;
77 void DistillPageImpl(const GURL& url, const std::string& script) override;
79 private:
80 friend class TestDistillerPageWebContents;
82 enum State {
83 // The page distiller is idle.
84 IDLE,
85 // A page is currently loading.
86 LOADING_PAGE,
87 // There was an error processing the page.
88 PAGELOAD_FAILED,
89 // JavaScript is executing within the context of the page. When the
90 // JavaScript completes, the state will be returned to |IDLE|.
91 EXECUTING_JAVASCRIPT
94 // Creates a new WebContents, adds |this| as an observer, and loads the
95 // |url|.
96 virtual void CreateNewWebContents(const GURL& url);
98 // Injects and executes JavaScript in the context of a loaded page. This
99 // must only be called after the page has successfully loaded.
100 void ExecuteJavaScript();
102 // Called when the distillation is done or if the page load failed.
103 void OnWebContentsDistillationDone(const GURL& page_url,
104 const base::TimeTicks& javascript_start,
105 const base::Value* value);
107 // The current state of the |DistillerPage|, initially |IDLE|.
108 State state_;
110 // The JavaScript to inject to extract content.
111 std::string script_;
113 scoped_ptr<SourcePageHandleWebContents> source_page_handle_;
115 content::BrowserContext* browser_context_;
116 gfx::Size render_view_size_;
117 base::WeakPtrFactory<DistillerPageWebContents> weak_factory_;
118 DISALLOW_COPY_AND_ASSIGN(DistillerPageWebContents);
121 } // namespace dom_distiller
123 #endif // COMPONENTS_DOM_DISTILLER_CONTENT_DISTILLER_PAGE_WEB_CONTENTS_H_