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 scoped_ptr
<content::WebContents
> web_contents
)
26 : web_contents_(web_contents
.Pass()) {
27 DCHECK(web_contents_
);
30 SourcePageHandleWebContents::~SourcePageHandleWebContents() {
33 scoped_ptr
<content::WebContents
> SourcePageHandleWebContents::GetWebContents() {
34 return web_contents_
.Pass();
37 scoped_ptr
<DistillerPage
> DistillerPageWebContentsFactory::CreateDistillerPage(
38 const gfx::Size
& render_view_size
) const {
39 DCHECK(browser_context_
);
40 return scoped_ptr
<DistillerPage
>(new DistillerPageWebContents(
41 browser_context_
, render_view_size
,
42 scoped_ptr
<SourcePageHandleWebContents
>()));
45 scoped_ptr
<DistillerPage
>
46 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle(
47 scoped_ptr
<SourcePageHandle
> handle
) const {
48 DCHECK(browser_context_
);
49 scoped_ptr
<SourcePageHandleWebContents
> web_contents_handle
=
50 scoped_ptr
<SourcePageHandleWebContents
>(
51 static_cast<SourcePageHandleWebContents
*>(handle
.release()));
52 return scoped_ptr
<DistillerPage
>(new DistillerPageWebContents(
53 browser_context_
, gfx::Size(), web_contents_handle
.Pass()));
56 DistillerPageWebContents::DistillerPageWebContents(
57 content::BrowserContext
* browser_context
,
58 const gfx::Size
& render_view_size
,
59 scoped_ptr
<SourcePageHandleWebContents
> optional_web_contents_handle
)
60 : state_(IDLE
), browser_context_(browser_context
),
61 render_view_size_(render_view_size
) {
62 if (optional_web_contents_handle
) {
63 web_contents_
= optional_web_contents_handle
->GetWebContents().Pass();
64 if (render_view_size
.IsEmpty())
65 render_view_size_
= web_contents_
->GetContainerBounds().size();
69 DistillerPageWebContents::~DistillerPageWebContents() {
71 web_contents_
->SetDelegate(NULL
);
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 (web_contents_
&& web_contents_
->GetLastCommittedURL() == url
) {
90 WebContentsMainFrameObserver
* main_frame_observer
=
91 WebContentsMainFrameObserver::FromWebContents(web_contents_
.get());
92 if (main_frame_observer
&& main_frame_observer
->is_initialized()) {
93 if (main_frame_observer
->is_document_loaded_in_main_frame()) {
94 // Main frame has already loaded for the current WebContents, so execute
95 // JavaScript immediately.
98 // Main frame document has not loaded yet, so wait until it has before
99 // executing JavaScript. It will trigger after DocumentLoadedInFrame is
100 // called for the main frame.
101 content::WebContentsObserver::Observe(web_contents_
.get());
104 // The WebContentsMainFrameObserver has not been correctly initialized,
105 // so fall back to creating a new WebContents.
106 CreateNewWebContents(url
);
109 CreateNewWebContents(url
);
113 void DistillerPageWebContents::CreateNewWebContents(const GURL
& url
) {
114 // Create new WebContents to use for distilling the content.
115 content::WebContents::CreateParams
create_params(browser_context_
);
116 create_params
.initially_hidden
= true;
117 web_contents_
.reset(content::WebContents::Create(create_params
));
118 DCHECK(web_contents_
.get());
120 web_contents_
->SetDelegate(this);
122 // Start observing WebContents and load the requested URL.
123 content::WebContentsObserver::Observe(web_contents_
.get());
124 content::NavigationController::LoadURLParams
params(url
);
125 web_contents_
->GetController().LoadURLWithParams(params
);
128 gfx::Size
DistillerPageWebContents::GetSizeForNewRenderView(
129 content::WebContents
* web_contents
) const {
130 gfx::Size
size(render_view_size_
);
132 size
= web_contents
->GetContainerBounds().size();
133 // If size is still empty, set it to fullscreen so that document.offsetWidth
134 // in the executed domdistiller.js won't be 0.
135 if (size
.IsEmpty()) {
136 DVLOG(1) << "Using fullscreen as default RenderView size";
137 size
= gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size();
142 void DistillerPageWebContents::DocumentLoadedInFrame(
143 content::RenderFrameHost
* render_frame_host
) {
144 if (render_frame_host
== web_contents_
->GetMainFrame()) {
149 void DistillerPageWebContents::DidFailLoad(
150 content::RenderFrameHost
* render_frame_host
,
151 const GURL
& validated_url
,
153 const base::string16
& error_description
) {
154 if (!render_frame_host
->GetParent()) {
155 content::WebContentsObserver::Observe(NULL
);
156 DCHECK(state_
== LOADING_PAGE
|| state_
== EXECUTING_JAVASCRIPT
);
157 state_
= PAGELOAD_FAILED
;
158 scoped_ptr
<base::Value
> empty(base::Value::CreateNullValue());
159 OnWebContentsDistillationDone(GURL(), empty
.get());
163 void DistillerPageWebContents::ExecuteJavaScript() {
164 content::RenderFrameHost
* frame
= web_contents_
->GetMainFrame();
166 DCHECK_EQ(LOADING_PAGE
, state_
);
167 state_
= EXECUTING_JAVASCRIPT
;
168 content::WebContentsObserver::Observe(NULL
);
169 web_contents_
->Stop();
170 DVLOG(1) << "Beginning distillation";
171 frame
->ExecuteJavaScript(
172 base::UTF8ToUTF16(script_
),
173 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone
,
174 base::Unretained(this),
175 web_contents_
->GetLastCommittedURL()));
178 void DistillerPageWebContents::OnWebContentsDistillationDone(
179 const GURL
& page_url
,
180 const base::Value
* value
) {
181 DCHECK(state_
== PAGELOAD_FAILED
|| state_
== EXECUTING_JAVASCRIPT
);
183 DistillerPage::OnDistillationDone(page_url
, value
);
186 } // namespace dom_distiller