Don't preload rarely seen large images
[chromium-blink-merge.git] / components / dom_distiller / content / distiller_page_web_contents.cc
blob2c7493f6fd1dd7c1a21a1e600aa7974712c8e2ac
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 #include "components/dom_distiller/content/distiller_page_web_contents.h"
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/dom_distiller/content/distiller_javascript_utils.h"
12 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
13 #include "components/dom_distiller/core/distiller_page.h"
14 #include "components/dom_distiller/core/dom_distiller_constants.h"
15 #include "components/dom_distiller/core/dom_distiller_service.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "ui/gfx/screen.h"
23 #include "url/gurl.h"
25 namespace dom_distiller {
27 SourcePageHandleWebContents::SourcePageHandleWebContents(
28 content::WebContents* web_contents,
29 bool owned)
30 : web_contents_(web_contents), owned_(owned) {
33 SourcePageHandleWebContents::~SourcePageHandleWebContents() {
34 if (owned_) {
35 delete web_contents_;
39 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage(
40 const gfx::Size& render_view_size) const {
41 DCHECK(browser_context_);
42 return scoped_ptr<DistillerPage>(new DistillerPageWebContents(
43 browser_context_, render_view_size,
44 scoped_ptr<SourcePageHandleWebContents>()));
47 scoped_ptr<DistillerPage>
48 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle(
49 scoped_ptr<SourcePageHandle> handle) const {
50 DCHECK(browser_context_);
51 scoped_ptr<SourcePageHandleWebContents> web_contents_handle =
52 scoped_ptr<SourcePageHandleWebContents>(
53 static_cast<SourcePageHandleWebContents*>(handle.release()));
54 return scoped_ptr<DistillerPage>(new DistillerPageWebContents(
55 browser_context_, gfx::Size(), web_contents_handle.Pass()));
58 DistillerPageWebContents::DistillerPageWebContents(
59 content::BrowserContext* browser_context,
60 const gfx::Size& render_view_size,
61 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle)
62 : state_(IDLE),
63 source_page_handle_(nullptr),
64 browser_context_(browser_context),
65 render_view_size_(render_view_size),
66 weak_factory_(this) {
67 if (optional_web_contents_handle) {
68 source_page_handle_ = optional_web_contents_handle.Pass();
69 if (render_view_size.IsEmpty())
70 render_view_size_ =
71 source_page_handle_->web_contents()->GetContainerBounds().size();
75 DistillerPageWebContents::~DistillerPageWebContents() {
78 bool DistillerPageWebContents::StringifyOutput() {
79 return false;
82 bool DistillerPageWebContents::CreateNewContext() {
83 return true;
86 void DistillerPageWebContents::DistillPageImpl(const GURL& url,
87 const std::string& script) {
88 DCHECK(browser_context_);
89 DCHECK(state_ == IDLE);
90 state_ = LOADING_PAGE;
91 script_ = script;
93 if (source_page_handle_ && source_page_handle_->web_contents() &&
94 source_page_handle_->web_contents()->GetLastCommittedURL() == url) {
95 WebContentsMainFrameObserver* main_frame_observer =
96 WebContentsMainFrameObserver::FromWebContents(
97 source_page_handle_->web_contents());
98 if (main_frame_observer && main_frame_observer->is_initialized()) {
99 if (main_frame_observer->is_document_loaded_in_main_frame()) {
100 // Main frame has already loaded for the current WebContents, so execute
101 // JavaScript immediately.
102 ExecuteJavaScript();
103 } else {
104 // Main frame document has not loaded yet, so wait until it has before
105 // executing JavaScript. It will trigger after DocumentLoadedInFrame is
106 // called for the main frame.
107 content::WebContentsObserver::Observe(
108 source_page_handle_->web_contents());
110 } else {
111 // The WebContentsMainFrameObserver has not been correctly initialized,
112 // so fall back to creating a new WebContents.
113 CreateNewWebContents(url);
115 } else {
116 CreateNewWebContents(url);
120 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) {
121 // Create new WebContents to use for distilling the content.
122 content::WebContents::CreateParams create_params(browser_context_);
123 create_params.initially_hidden = true;
124 content::WebContents* web_contents =
125 content::WebContents::Create(create_params);
126 DCHECK(web_contents);
128 web_contents->SetDelegate(this);
130 // Start observing WebContents and load the requested URL.
131 content::WebContentsObserver::Observe(web_contents);
132 content::NavigationController::LoadURLParams params(url);
133 web_contents->GetController().LoadURLWithParams(params);
135 source_page_handle_.reset(
136 new SourcePageHandleWebContents(web_contents, true));
139 gfx::Size DistillerPageWebContents::GetSizeForNewRenderView(
140 content::WebContents* web_contents) const {
141 gfx::Size size(render_view_size_);
142 if (size.IsEmpty())
143 size = web_contents->GetContainerBounds().size();
144 // If size is still empty, set it to fullscreen so that document.offsetWidth
145 // in the executed domdistiller.js won't be 0.
146 if (size.IsEmpty()) {
147 DVLOG(1) << "Using fullscreen as default RenderView size";
148 size = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size();
150 return size;
153 void DistillerPageWebContents::DocumentLoadedInFrame(
154 content::RenderFrameHost* render_frame_host) {
155 if (render_frame_host ==
156 source_page_handle_->web_contents()->GetMainFrame()) {
157 ExecuteJavaScript();
161 void DistillerPageWebContents::DidFailLoad(
162 content::RenderFrameHost* render_frame_host,
163 const GURL& validated_url,
164 int error_code,
165 const base::string16& error_description,
166 bool was_ignored_by_handler) {
167 if (!render_frame_host->GetParent()) {
168 content::WebContentsObserver::Observe(NULL);
169 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT);
170 state_ = PAGELOAD_FAILED;
171 scoped_ptr<base::Value> empty = base::Value::CreateNullValue();
172 OnWebContentsDistillationDone(GURL(), base::TimeTicks(), empty.get());
176 void DistillerPageWebContents::ExecuteJavaScript() {
177 content::RenderFrameHost* frame =
178 source_page_handle_->web_contents()->GetMainFrame();
179 DCHECK(frame);
180 DCHECK_EQ(LOADING_PAGE, state_);
181 state_ = EXECUTING_JAVASCRIPT;
182 content::WebContentsObserver::Observe(NULL);
183 // Stop any pending navigation since the intent is to distill the current
184 // page.
185 source_page_handle_->web_contents()->Stop();
186 DVLOG(1) << "Beginning distillation";
187 RunIsolatedJavaScript(
188 frame, script_,
189 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone,
190 weak_factory_.GetWeakPtr(),
191 source_page_handle_->web_contents()->GetLastCommittedURL(),
192 base::TimeTicks::Now()));
195 void DistillerPageWebContents::OnWebContentsDistillationDone(
196 const GURL& page_url,
197 const base::TimeTicks& javascript_start,
198 const base::Value* value) {
199 DCHECK(state_ == IDLE || state_ == LOADING_PAGE || // TODO(nyquist): 493795.
200 state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT);
201 state_ = IDLE;
203 if (!javascript_start.is_null()) {
204 base::TimeDelta javascript_time = base::TimeTicks::Now() - javascript_start;
205 UMA_HISTOGRAM_TIMES("DomDistiller.Time.RunJavaScript", javascript_time);
206 DVLOG(1) << "DomDistiller.Time.RunJavaScript = " << javascript_time;
209 DistillerPage::OnDistillationDone(page_url, value);
212 } // namespace dom_distiller