Chromium Code Reviews| 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" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 SourcePageHandleWebContents::SourcePageHandleWebContents( | 24 SourcePageHandleWebContents::SourcePageHandleWebContents( |
| 25 scoped_ptr<content::WebContents> web_contents) | 25 scoped_ptr<content::WebContents> web_contents) |
| 26 : web_contents_(web_contents.Pass()) { | 26 : web_contents_(web_contents.Pass()) { |
| 27 DCHECK(web_contents_); | 27 DCHECK(web_contents_); |
| 28 } | 28 } |
| 29 | 29 |
| 30 SourcePageHandleWebContents::~SourcePageHandleWebContents() { | 30 SourcePageHandleWebContents::~SourcePageHandleWebContents() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { | 33 content::WebContents* SourcePageHandleWebContents::GetWebContents() const { |
|
nyquist
2015/05/13 07:56:38
I think you want to rebase this on top of https://
cjhopman
2015/05/20 02:00:02
I expect with that the changes to distiller_page_w
arjunpatel
2015/05/27 00:16:45
Yes, I think this will go away after the rebase.
arjunpatel
2015/05/27 00:16:45
Acknowledged.
| |
| 34 return web_contents_.Pass(); | 34 return web_contents_.get(); |
| 35 } | |
| 36 | |
| 37 SourcePageHandleWebContentsWeakPtr::SourcePageHandleWebContentsWeakPtr( | |
| 38 content::WebContents* web_contents) | |
| 39 : weak_factory_(web_contents), | |
| 40 web_contents_(weak_factory_.GetWeakPtr()) { | |
| 41 DCHECK(web_contents_); | |
| 42 } | |
| 43 | |
| 44 SourcePageHandleWebContentsWeakPtr::~SourcePageHandleWebContentsWeakPtr() { | |
| 45 } | |
| 46 | |
| 47 content::WebContents* SourcePageHandleWebContentsWeakPtr::GetWebContents() | |
| 48 const { | |
| 49 if (web_contents_) | |
| 50 return web_contents_.get(); | |
| 51 | |
| 52 return NULL; | |
| 35 } | 53 } |
| 36 | 54 |
| 37 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage( | 55 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage( |
| 38 const gfx::Size& render_view_size) const { | 56 const gfx::Size& render_view_size) const { |
| 39 DCHECK(browser_context_); | 57 DCHECK(browser_context_); |
| 40 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( | 58 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| 41 browser_context_, render_view_size, | 59 browser_context_, render_view_size)); |
| 42 scoped_ptr<SourcePageHandleWebContents>())); | |
| 43 } | 60 } |
| 44 | 61 |
| 45 scoped_ptr<DistillerPage> | 62 scoped_ptr<DistillerPage> |
| 46 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( | 63 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( |
| 47 scoped_ptr<SourcePageHandle> handle) const { | 64 scoped_ptr<SourcePageHandle> handle) const { |
| 48 DCHECK(browser_context_); | 65 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( | 66 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| 53 browser_context_, gfx::Size(), web_contents_handle.Pass())); | 67 browser_context_, gfx::Size(), handle.Pass())); |
| 54 } | 68 } |
| 55 | 69 |
| 56 DistillerPageWebContents::DistillerPageWebContents( | 70 DistillerPageWebContents::DistillerPageWebContents( |
| 57 content::BrowserContext* browser_context, | 71 content::BrowserContext* browser_context, |
| 58 const gfx::Size& render_view_size, | 72 const gfx::Size& render_view_size, |
| 59 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) | 73 scoped_ptr<SourcePageHandle> optional_web_contents_handle) |
| 60 : state_(IDLE), browser_context_(browser_context), | 74 : state_(IDLE), browser_context_(browser_context), |
| 61 render_view_size_(render_view_size) { | 75 render_view_size_(render_view_size), weak_factory_(this) { |
| 62 if (optional_web_contents_handle) { | 76 if (optional_web_contents_handle) { |
| 63 web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); | 77 web_contents_handle_ = optional_web_contents_handle.Pass(); |
| 64 if (render_view_size.IsEmpty()) | 78 if (render_view_size.IsEmpty()) |
| 65 render_view_size_ = web_contents_->GetContainerBounds().size(); | 79 render_view_size_ = |
| 80 web_contents_handle_->GetWebContents()->GetContainerBounds().size(); | |
| 66 } | 81 } |
| 67 } | 82 } |
| 68 | 83 |
| 84 DistillerPageWebContents::DistillerPageWebContents( | |
| 85 content::BrowserContext* browser_context, | |
| 86 const gfx::Size& render_view_size) | |
| 87 : state_(IDLE), browser_context_(browser_context), | |
| 88 render_view_size_(render_view_size), weak_factory_(this) { | |
| 89 } | |
| 90 | |
| 69 DistillerPageWebContents::~DistillerPageWebContents() { | 91 DistillerPageWebContents::~DistillerPageWebContents() { |
| 70 if (web_contents_) | |
| 71 web_contents_->SetDelegate(NULL); | |
| 72 } | 92 } |
| 73 | 93 |
| 74 bool DistillerPageWebContents::StringifyOutput() { | 94 bool DistillerPageWebContents::StringifyOutput() { |
| 75 return false; | 95 return false; |
| 76 } | 96 } |
| 77 | 97 |
| 78 bool DistillerPageWebContents::CreateNewContext() { | 98 bool DistillerPageWebContents::CreateNewContext() { |
| 79 return true; | 99 return true; |
| 80 } | 100 } |
| 81 | 101 |
| 82 void DistillerPageWebContents::DistillPageImpl(const GURL& url, | 102 void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| 83 const std::string& script) { | 103 const std::string& script) { |
| 84 DCHECK(browser_context_); | 104 DCHECK(browser_context_); |
| 85 DCHECK(state_ == IDLE); | 105 DCHECK(state_ == IDLE); |
| 86 state_ = LOADING_PAGE; | 106 state_ = LOADING_PAGE; |
| 87 script_ = script; | 107 script_ = script; |
| 88 | 108 |
| 89 if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { | 109 if (!web_contents_handle_) { |
| 110 CreateNewWebContents(url); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 content::WebContents* web_contents = web_contents_handle_->GetWebContents(); | |
| 115 | |
| 116 if (web_contents && web_contents->GetLastCommittedURL() == url) { | |
| 90 WebContentsMainFrameObserver* main_frame_observer = | 117 WebContentsMainFrameObserver* main_frame_observer = |
| 91 WebContentsMainFrameObserver::FromWebContents(web_contents_.get()); | 118 WebContentsMainFrameObserver::FromWebContents(web_contents); |
| 92 if (main_frame_observer && main_frame_observer->is_initialized()) { | 119 if (main_frame_observer && main_frame_observer->is_initialized()) { |
| 93 if (main_frame_observer->is_document_loaded_in_main_frame()) { | 120 if (main_frame_observer->is_document_loaded_in_main_frame()) { |
| 94 // Main frame has already loaded for the current WebContents, so execute | 121 // Main frame has already loaded for the current WebContents, so execute |
| 95 // JavaScript immediately. | 122 // JavaScript immediately. |
| 96 ExecuteJavaScript(); | 123 ExecuteJavaScript(); |
| 97 } else { | 124 } else { |
| 98 // Main frame document has not loaded yet, so wait until it has before | 125 // Main frame document has not loaded yet, so wait until it has before |
| 99 // executing JavaScript. It will trigger after DocumentLoadedInFrame is | 126 // executing JavaScript. It will trigger after DocumentLoadedInFrame is |
| 100 // called for the main frame. | 127 // called for the main frame. |
| 101 content::WebContentsObserver::Observe(web_contents_.get()); | 128 content::WebContentsObserver::Observe(web_contents); |
| 102 } | 129 } |
| 103 } else { | 130 } else { |
| 104 // The WebContentsMainFrameObserver has not been correctly initialized, | 131 // The WebContentsMainFrameObserver has not been correctly initialized, |
| 105 // so fall back to creating a new WebContents. | 132 // so fall back to creating a new WebContents. |
| 106 CreateNewWebContents(url); | 133 CreateNewWebContents(url); |
| 107 } | 134 } |
| 108 } else { | 135 } else { |
| 109 CreateNewWebContents(url); | 136 CreateNewWebContents(url); |
| 110 } | 137 } |
| 111 } | 138 } |
| 112 | 139 |
| 113 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { | 140 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { |
| 114 // Create new WebContents to use for distilling the content. | 141 // Create new WebContents to use for distilling the content. |
| 115 content::WebContents::CreateParams create_params(browser_context_); | 142 content::WebContents::CreateParams create_params(browser_context_); |
| 116 create_params.initially_hidden = true; | 143 create_params.initially_hidden = true; |
| 117 web_contents_.reset(content::WebContents::Create(create_params)); | 144 scoped_ptr<content::WebContents> web_contents( |
| 118 DCHECK(web_contents_.get()); | 145 content::WebContents::Create(create_params)); |
| 119 | 146 web_contents->SetDelegate(this); |
| 120 web_contents_->SetDelegate(this); | |
| 121 | 147 |
| 122 // Start observing WebContents and load the requested URL. | 148 // Start observing WebContents and load the requested URL. |
| 123 content::WebContentsObserver::Observe(web_contents_.get()); | 149 content::WebContentsObserver::Observe(web_contents.get()); |
| 124 content::NavigationController::LoadURLParams params(url); | 150 content::NavigationController::LoadURLParams params(url); |
| 125 web_contents_->GetController().LoadURLWithParams(params); | 151 web_contents->GetController().LoadURLWithParams(params); |
| 152 | |
| 153 web_contents_handle_.reset(new SourcePageHandleWebContents( | |
| 154 web_contents.Pass())); | |
| 155 DCHECK(web_contents_handle_.get()); | |
| 126 } | 156 } |
| 127 | 157 |
| 128 gfx::Size DistillerPageWebContents::GetSizeForNewRenderView( | 158 gfx::Size DistillerPageWebContents::GetSizeForNewRenderView( |
| 129 content::WebContents* web_contents) const { | 159 content::WebContents* web_contents) const { |
| 130 gfx::Size size(render_view_size_); | 160 gfx::Size size(render_view_size_); |
| 131 if (size.IsEmpty()) | 161 if (size.IsEmpty()) |
| 132 size = web_contents->GetContainerBounds().size(); | 162 size = web_contents->GetContainerBounds().size(); |
| 133 // If size is still empty, set it to fullscreen so that document.offsetWidth | 163 // If size is still empty, set it to fullscreen so that document.offsetWidth |
| 134 // in the executed domdistiller.js won't be 0. | 164 // in the executed domdistiller.js won't be 0. |
| 135 if (size.IsEmpty()) { | 165 if (size.IsEmpty()) { |
| 136 DVLOG(1) << "Using fullscreen as default RenderView size"; | 166 DVLOG(1) << "Using fullscreen as default RenderView size"; |
| 137 size = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size(); | 167 size = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().size(); |
| 138 } | 168 } |
| 139 return size; | 169 return size; |
| 140 } | 170 } |
| 141 | 171 |
| 142 void DistillerPageWebContents::DocumentLoadedInFrame( | 172 void DistillerPageWebContents::DocumentLoadedInFrame( |
| 143 content::RenderFrameHost* render_frame_host) { | 173 content::RenderFrameHost* render_frame_host) { |
| 144 if (render_frame_host == web_contents_->GetMainFrame()) { | 174 if (!web_contents_handle_) |
| 175 return; | |
| 176 | |
| 177 content::WebContents* web_contents = web_contents_handle_->GetWebContents(); | |
| 178 if (web_contents && render_frame_host == web_contents->GetMainFrame()) { | |
| 145 ExecuteJavaScript(); | 179 ExecuteJavaScript(); |
| 146 } | 180 } |
| 147 } | 181 } |
| 148 | 182 |
| 149 void DistillerPageWebContents::DidFailLoad( | 183 void DistillerPageWebContents::DidFailLoad( |
| 150 content::RenderFrameHost* render_frame_host, | 184 content::RenderFrameHost* render_frame_host, |
| 151 const GURL& validated_url, | 185 const GURL& validated_url, |
| 152 int error_code, | 186 int error_code, |
| 153 const base::string16& error_description) { | 187 const base::string16& error_description) { |
| 154 if (!render_frame_host->GetParent()) { | 188 if (!render_frame_host->GetParent()) { |
| 155 content::WebContentsObserver::Observe(NULL); | 189 content::WebContentsObserver::Observe(NULL); |
| 156 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); | 190 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); |
| 157 state_ = PAGELOAD_FAILED; | 191 state_ = PAGELOAD_FAILED; |
| 158 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); | 192 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); |
| 159 OnWebContentsDistillationDone(GURL(), empty.get()); | 193 OnWebContentsDistillationDone(GURL(), empty.get()); |
| 160 } | 194 } |
| 161 } | 195 } |
| 162 | 196 |
| 163 void DistillerPageWebContents::ExecuteJavaScript() { | 197 void DistillerPageWebContents::ExecuteJavaScript() { |
| 164 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); | 198 if (!web_contents_handle_) |
| 199 return; | |
| 200 | |
| 201 content::WebContents* web_contents = web_contents_handle_->GetWebContents(); | |
| 202 if (!web_contents) | |
| 203 return; | |
| 204 | |
| 205 content::RenderFrameHost* frame = web_contents->GetMainFrame(); | |
| 165 DCHECK(frame); | 206 DCHECK(frame); |
| 166 DCHECK_EQ(LOADING_PAGE, state_); | 207 DCHECK_EQ(LOADING_PAGE, state_); |
| 167 state_ = EXECUTING_JAVASCRIPT; | 208 state_ = EXECUTING_JAVASCRIPT; |
| 168 content::WebContentsObserver::Observe(NULL); | 209 content::WebContentsObserver::Observe(NULL); |
| 169 web_contents_->Stop(); | 210 web_contents->Stop(); |
| 170 DVLOG(1) << "Beginning distillation"; | 211 DVLOG(1) << "Beginning distillation"; |
| 171 frame->ExecuteJavaScript( | 212 frame->ExecuteJavaScript( |
| 172 base::UTF8ToUTF16(script_), | 213 base::UTF8ToUTF16(script_), |
| 173 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, | 214 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, |
| 174 base::Unretained(this), | 215 weak_factory_.GetWeakPtr(), |
| 175 web_contents_->GetLastCommittedURL())); | 216 web_contents->GetLastCommittedURL())); |
| 176 } | 217 } |
| 177 | 218 |
| 178 void DistillerPageWebContents::OnWebContentsDistillationDone( | 219 void DistillerPageWebContents::OnWebContentsDistillationDone( |
| 179 const GURL& page_url, | 220 const GURL& page_url, |
| 180 const base::Value* value) { | 221 const base::Value* value) { |
| 181 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); | 222 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); |
| 182 state_ = IDLE; | 223 state_ = IDLE; |
| 183 DistillerPage::OnDistillationDone(page_url, value); | 224 DistillerPage::OnDistillationDone(page_url, value); |
| 184 } | 225 } |
| 185 | 226 |
| 186 } // namespace dom_distiller | 227 } // namespace dom_distiller |
| OLD | NEW |