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/strings/utf_string_conversions.h"
10 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
11 #include "components/dom_distiller/core/distiller_page.h"
12 #include "components/dom_distiller/core/dom_distiller_service.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_observer.h"
19 #include "ui/gfx/screen.h"
22 namespace dom_distiller
{
24 SourcePageHandleWebContents::SourcePageHandleWebContents(
25 content::WebContents
* web_contents
,
27 : web_contents_(web_contents
), owned_(owned
) {
30 SourcePageHandleWebContents::~SourcePageHandleWebContents() {
36 scoped_ptr
<DistillerPage
> DistillerPageWebContentsFactory::CreateDistillerPage(
37 const gfx::Size
& render_view_size
) const {
38 DCHECK(browser_context_
);
39 return scoped_ptr
<DistillerPage
>(new DistillerPageWebContents(
40 browser_context_
, render_view_size
,
41 scoped_ptr
<SourcePageHandleWebContents
>()));
44 scoped_ptr
<DistillerPage
>
45 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle(
46 scoped_ptr
<SourcePageHandle
> handle
) const {
47 DCHECK(browser_context_
);
48 scoped_ptr
<SourcePageHandleWebContents
> web_contents_handle
=
49 scoped_ptr
<SourcePageHandleWebContents
>(
50 static_cast<SourcePageHandleWebContents
*>(handle
.release()));
51 return scoped_ptr
<DistillerPage
>(new DistillerPageWebContents(
52 browser_context_
, gfx::Size(), web_contents_handle
.Pass()));
55 DistillerPageWebContents::DistillerPageWebContents(
56 content::BrowserContext
* browser_context
,
57 const gfx::Size
& render_view_size
,
58 scoped_ptr
<SourcePageHandleWebContents
> optional_web_contents_handle
)
60 source_page_handle_(nullptr),
61 browser_context_(browser_context
),
62 render_view_size_(render_view_size
) {
63 if (optional_web_contents_handle
) {
64 source_page_handle_
= optional_web_contents_handle
.Pass();
65 if (render_view_size
.IsEmpty())
67 source_page_handle_
->web_contents()->GetContainerBounds().size();
71 DistillerPageWebContents::~DistillerPageWebContents() {
74 bool DistillerPageWebContents::StringifyOutput() {
78 bool DistillerPageWebContents::CreateNewContext() {
82 void DistillerPageWebContents::DistillPageImpl(const GURL
& url
,
83 const std::string
& script
) {
84 DCHECK(browser_context_
);
85 DCHECK(state_
== IDLE
);
86 state_
= LOADING_PAGE
;
89 if (source_page_handle_
&& source_page_handle_
->web_contents() &&
90 source_page_handle_
->web_contents()->GetLastCommittedURL() == url
) {
91 WebContentsMainFrameObserver
* main_frame_observer
=
92 WebContentsMainFrameObserver::FromWebContents(
93 source_page_handle_
->web_contents());
94 if (main_frame_observer
&& main_frame_observer
->is_initialized()) {
95 if (main_frame_observer
->is_document_loaded_in_main_frame()) {
96 // Main frame has already loaded for the current WebContents, so execute
97 // JavaScript immediately.
100 // Main frame document has not loaded yet, so wait until it has before
101 // executing JavaScript. It will trigger after DocumentLoadedInFrame is
102 // called for the main frame.
103 content::WebContentsObserver::Observe(
104 source_page_handle_
->web_contents());
107 // The WebContentsMainFrameObserver has not been correctly initialized,
108 // so fall back to creating a new WebContents.
109 CreateNewWebContents(url
);
112 CreateNewWebContents(url
);
116 void DistillerPageWebContents::CreateNewWebContents(const GURL
& url
) {
117 // Create new WebContents to use for distilling the content.
118 content::WebContents::CreateParams
create_params(browser_context_
);
119 create_params
.initially_hidden
= true;
120 content::WebContents
* web_contents
=
121 content::WebContents::Create(create_params
);
122 DCHECK(web_contents
);
124 web_contents
->SetDelegate(this);
126 // Start observing WebContents and load the requested URL.
127 content::WebContentsObserver::Observe(web_contents
);
128 content::NavigationController::LoadURLParams
params(url
);
129 web_contents
->GetController().LoadURLWithParams(params
);
131 source_page_handle_
.reset(
132 new SourcePageHandleWebContents(web_contents
, true));
135 gfx::Size
DistillerPageWebContents::GetSizeForNewRenderView(
136 content::WebContents
* web_contents
) const {
137 gfx::Size
size(render_view_size_
);
139 size
= web_contents
->GetContainerBounds().size();
140 // If size is still empty, set it to fullscreen so that document.offsetWidth
141 // in the executed domdistiller.js won't be 0.
142 if (size
.IsEmpty()) {
143 DVLOG(1) << "Using fullscreen as default RenderView size";
144 size
= gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size();
149 void DistillerPageWebContents::DocumentLoadedInFrame(
150 content::RenderFrameHost
* render_frame_host
) {
151 if (render_frame_host
==
152 source_page_handle_
->web_contents()->GetMainFrame()) {
157 void DistillerPageWebContents::DidFailLoad(
158 content::RenderFrameHost
* render_frame_host
,
159 const GURL
& validated_url
,
161 const base::string16
& error_description
) {
162 if (!render_frame_host
->GetParent()) {
163 content::WebContentsObserver::Observe(NULL
);
164 DCHECK(state_
== LOADING_PAGE
|| state_
== EXECUTING_JAVASCRIPT
);
165 state_
= PAGELOAD_FAILED
;
166 scoped_ptr
<base::Value
> empty
= base::Value::CreateNullValue();
167 OnWebContentsDistillationDone(GURL(), empty
.get());
171 void DistillerPageWebContents::ExecuteJavaScript() {
172 content::RenderFrameHost
* frame
=
173 source_page_handle_
->web_contents()->GetMainFrame();
175 DCHECK_EQ(LOADING_PAGE
, state_
);
176 state_
= EXECUTING_JAVASCRIPT
;
177 content::WebContentsObserver::Observe(NULL
);
178 // Stop any pending navigation since the intent is to distill the current
180 source_page_handle_
->web_contents()->Stop();
181 DVLOG(1) << "Beginning distillation";
182 frame
->ExecuteJavaScript(
183 base::UTF8ToUTF16(script_
),
184 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone
,
185 base::Unretained(this),
186 source_page_handle_
->web_contents()->GetLastCommittedURL()));
189 void DistillerPageWebContents::OnWebContentsDistillationDone(
190 const GURL
& page_url
,
191 const base::Value
* value
) {
192 DCHECK(state_
== PAGELOAD_FAILED
|| state_
== EXECUTING_JAVASCRIPT
);
194 DistillerPage::OnDistillationDone(page_url
, value
);
197 } // namespace dom_distiller