OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/content/distiller_page_web_contents.h" | 5 #include "components/dom_distiller/content/distiller_page_web_contents.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" | 10 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" |
11 #include "components/dom_distiller/core/distiller_page.h" | 11 #include "components/dom_distiller/core/distiller_page.h" |
12 #include "components/dom_distiller/core/dom_distiller_service.h" | 12 #include "components/dom_distiller/core/dom_distiller_service.h" |
13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
14 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "ui/gfx/screen.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace dom_distiller { | 22 namespace dom_distiller { |
22 | 23 |
23 SourcePageHandleWebContents::SourcePageHandleWebContents( | 24 SourcePageHandleWebContents::SourcePageHandleWebContents( |
24 scoped_ptr<content::WebContents> web_contents) | 25 scoped_ptr<content::WebContents> web_contents) |
25 : web_contents_(web_contents.Pass()) { | 26 : web_contents_(web_contents.Pass()) { |
26 DCHECK(web_contents_); | 27 DCHECK(web_contents_); |
27 } | 28 } |
28 | 29 |
29 SourcePageHandleWebContents::~SourcePageHandleWebContents() { | 30 SourcePageHandleWebContents::~SourcePageHandleWebContents() { |
30 } | 31 } |
31 | 32 |
32 scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { | 33 scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { |
33 return web_contents_.Pass(); | 34 return web_contents_.Pass(); |
34 } | 35 } |
35 | 36 |
36 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage() | 37 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage( |
37 const { | 38 const gfx::Size& render_view_size) const { |
38 DCHECK(browser_context_); | 39 DCHECK(browser_context_); |
39 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( | 40 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
40 browser_context_, scoped_ptr<SourcePageHandleWebContents>())); | 41 browser_context_, render_view_size, |
| 42 scoped_ptr<SourcePageHandleWebContents>())); |
41 } | 43 } |
42 | 44 |
43 scoped_ptr<DistillerPage> | 45 scoped_ptr<DistillerPage> |
44 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( | 46 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( |
45 scoped_ptr<SourcePageHandle> handle) const { | 47 scoped_ptr<SourcePageHandle> handle) const { |
46 DCHECK(browser_context_); | 48 DCHECK(browser_context_); |
47 scoped_ptr<SourcePageHandleWebContents> web_contents_handle = | 49 scoped_ptr<SourcePageHandleWebContents> web_contents_handle = |
48 scoped_ptr<SourcePageHandleWebContents>( | 50 scoped_ptr<SourcePageHandleWebContents>( |
49 static_cast<SourcePageHandleWebContents*>(handle.release())); | 51 static_cast<SourcePageHandleWebContents*>(handle.release())); |
50 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( | 52 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
51 browser_context_, web_contents_handle.Pass())); | 53 browser_context_, gfx::Size(), web_contents_handle.Pass())); |
52 } | 54 } |
53 | 55 |
54 DistillerPageWebContents::DistillerPageWebContents( | 56 DistillerPageWebContents::DistillerPageWebContents( |
55 content::BrowserContext* browser_context, | 57 content::BrowserContext* browser_context, |
| 58 const gfx::Size& render_view_size, |
56 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) | 59 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) |
57 : state_(IDLE), browser_context_(browser_context) { | 60 : state_(IDLE), browser_context_(browser_context), |
| 61 render_view_size_(render_view_size) { |
58 if (optional_web_contents_handle) { | 62 if (optional_web_contents_handle) { |
59 web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); | 63 web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); |
| 64 if (render_view_size.IsEmpty()) |
| 65 render_view_size_ = web_contents_->GetContainerBounds().size(); |
60 } | 66 } |
61 } | 67 } |
62 | 68 |
63 DistillerPageWebContents::~DistillerPageWebContents() { | 69 DistillerPageWebContents::~DistillerPageWebContents() { |
| 70 if (web_contents_) |
| 71 web_contents_->SetDelegate(NULL); |
64 } | 72 } |
65 | 73 |
66 void DistillerPageWebContents::DistillPageImpl(const GURL& url, | 74 void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
67 const std::string& script) { | 75 const std::string& script) { |
68 DCHECK(browser_context_); | 76 DCHECK(browser_context_); |
69 DCHECK(state_ == IDLE); | 77 DCHECK(state_ == IDLE); |
70 state_ = LOADING_PAGE; | 78 state_ = LOADING_PAGE; |
71 script_ = script; | 79 script_ = script; |
72 | 80 |
73 if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { | 81 if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { |
(...skipping 20 matching lines...) Expand all Loading... |
94 } | 102 } |
95 } | 103 } |
96 | 104 |
97 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { | 105 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { |
98 // Create new WebContents to use for distilling the content. | 106 // Create new WebContents to use for distilling the content. |
99 content::WebContents::CreateParams create_params(browser_context_); | 107 content::WebContents::CreateParams create_params(browser_context_); |
100 create_params.initially_hidden = true; | 108 create_params.initially_hidden = true; |
101 web_contents_.reset(content::WebContents::Create(create_params)); | 109 web_contents_.reset(content::WebContents::Create(create_params)); |
102 DCHECK(web_contents_.get()); | 110 DCHECK(web_contents_.get()); |
103 | 111 |
| 112 web_contents_->SetDelegate(this); |
| 113 |
104 // Start observing WebContents and load the requested URL. | 114 // Start observing WebContents and load the requested URL. |
105 content::WebContentsObserver::Observe(web_contents_.get()); | 115 content::WebContentsObserver::Observe(web_contents_.get()); |
106 content::NavigationController::LoadURLParams params(url); | 116 content::NavigationController::LoadURLParams params(url); |
107 web_contents_->GetController().LoadURLWithParams(params); | 117 web_contents_->GetController().LoadURLWithParams(params); |
108 } | 118 } |
109 | 119 |
| 120 gfx::Size DistillerPageWebContents::GetSizeForNewRenderView( |
| 121 content::WebContents* web_contents) const { |
| 122 gfx::Size size(render_view_size_); |
| 123 if (size.IsEmpty()) |
| 124 size = web_contents->GetContainerBounds().size(); |
| 125 // If size is still empty, set it to fullscreen so that document.offsetWidth |
| 126 // in the executed domdistiller.js won't be 0. |
| 127 if (size.IsEmpty()) { |
| 128 DVLOG(1) << "Using fullscreen as default RenderView size"; |
| 129 size = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size(); |
| 130 } |
| 131 return size; |
| 132 } |
| 133 |
110 void DistillerPageWebContents::DocumentLoadedInFrame( | 134 void DistillerPageWebContents::DocumentLoadedInFrame( |
111 content::RenderFrameHost* render_frame_host) { | 135 content::RenderFrameHost* render_frame_host) { |
112 if (render_frame_host == web_contents_->GetMainFrame()) { | 136 if (render_frame_host == web_contents_->GetMainFrame()) { |
113 ExecuteJavaScript(); | 137 ExecuteJavaScript(); |
114 } | 138 } |
115 } | 139 } |
116 | 140 |
117 void DistillerPageWebContents::DidFailLoad( | 141 void DistillerPageWebContents::DidFailLoad( |
118 content::RenderFrameHost* render_frame_host, | 142 content::RenderFrameHost* render_frame_host, |
119 const GURL& validated_url, | 143 const GURL& validated_url, |
(...skipping 25 matching lines...) Expand all Loading... |
145 | 169 |
146 void DistillerPageWebContents::OnWebContentsDistillationDone( | 170 void DistillerPageWebContents::OnWebContentsDistillationDone( |
147 const GURL& page_url, | 171 const GURL& page_url, |
148 const base::Value* value) { | 172 const base::Value* value) { |
149 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); | 173 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); |
150 state_ = IDLE; | 174 state_ = IDLE; |
151 DistillerPage::OnDistillationDone(page_url, value); | 175 DistillerPage::OnDistillationDone(page_url, value); |
152 } | 176 } |
153 | 177 |
154 } // namespace dom_distiller | 178 } // namespace dom_distiller |
OLD | NEW |