Chromium Code Reviews| Index: components/dom_distiller/content/distiller_page_web_contents.cc |
| diff --git a/components/dom_distiller/content/distiller_page_web_contents.cc b/components/dom_distiller/content/distiller_page_web_contents.cc |
| index 9bee2ecdb4641da0d1726c75f7a806645f0c838d..b33bf7e3e9f9a013369a9d9cbe6b1d358ea5f0b1 100644 |
| --- a/components/dom_distiller/content/distiller_page_web_contents.cc |
| +++ b/components/dom_distiller/content/distiller_page_web_contents.cc |
| @@ -30,45 +30,65 @@ SourcePageHandleWebContents::SourcePageHandleWebContents( |
| SourcePageHandleWebContents::~SourcePageHandleWebContents() { |
| } |
| -scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { |
| - return web_contents_.Pass(); |
| +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.
|
| + return web_contents_.get(); |
| +} |
| + |
| +SourcePageHandleWebContentsWeakPtr::SourcePageHandleWebContentsWeakPtr( |
| + content::WebContents* web_contents) |
| + : weak_factory_(web_contents), |
| + web_contents_(weak_factory_.GetWeakPtr()) { |
| + DCHECK(web_contents_); |
| +} |
| + |
| +SourcePageHandleWebContentsWeakPtr::~SourcePageHandleWebContentsWeakPtr() { |
| +} |
| + |
| +content::WebContents* SourcePageHandleWebContentsWeakPtr::GetWebContents() |
| + const { |
| + if (web_contents_) |
| + return web_contents_.get(); |
| + |
| + return NULL; |
| } |
| scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage( |
| const gfx::Size& render_view_size) const { |
| DCHECK(browser_context_); |
| return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| - browser_context_, render_view_size, |
| - scoped_ptr<SourcePageHandleWebContents>())); |
| + browser_context_, render_view_size)); |
| } |
| scoped_ptr<DistillerPage> |
| DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( |
| scoped_ptr<SourcePageHandle> handle) const { |
| DCHECK(browser_context_); |
| - scoped_ptr<SourcePageHandleWebContents> web_contents_handle = |
| - scoped_ptr<SourcePageHandleWebContents>( |
| - static_cast<SourcePageHandleWebContents*>(handle.release())); |
| return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| - browser_context_, gfx::Size(), web_contents_handle.Pass())); |
| + browser_context_, gfx::Size(), handle.Pass())); |
| } |
| DistillerPageWebContents::DistillerPageWebContents( |
| content::BrowserContext* browser_context, |
| const gfx::Size& render_view_size, |
| - scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) |
| + scoped_ptr<SourcePageHandle> optional_web_contents_handle) |
| : state_(IDLE), browser_context_(browser_context), |
| - render_view_size_(render_view_size) { |
| + render_view_size_(render_view_size), weak_factory_(this) { |
| if (optional_web_contents_handle) { |
| - web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); |
| + web_contents_handle_ = optional_web_contents_handle.Pass(); |
| if (render_view_size.IsEmpty()) |
| - render_view_size_ = web_contents_->GetContainerBounds().size(); |
| + render_view_size_ = |
| + web_contents_handle_->GetWebContents()->GetContainerBounds().size(); |
| } |
| } |
| +DistillerPageWebContents::DistillerPageWebContents( |
| + content::BrowserContext* browser_context, |
| + const gfx::Size& render_view_size) |
| + : state_(IDLE), browser_context_(browser_context), |
| + render_view_size_(render_view_size), weak_factory_(this) { |
| +} |
| + |
| DistillerPageWebContents::~DistillerPageWebContents() { |
| - if (web_contents_) |
| - web_contents_->SetDelegate(NULL); |
| } |
| bool DistillerPageWebContents::StringifyOutput() { |
| @@ -86,9 +106,16 @@ void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| state_ = LOADING_PAGE; |
| script_ = script; |
| - if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { |
| + if (!web_contents_handle_) { |
| + CreateNewWebContents(url); |
| + return; |
| + } |
| + |
| + content::WebContents* web_contents = web_contents_handle_->GetWebContents(); |
| + |
| + if (web_contents && web_contents->GetLastCommittedURL() == url) { |
| WebContentsMainFrameObserver* main_frame_observer = |
| - WebContentsMainFrameObserver::FromWebContents(web_contents_.get()); |
| + WebContentsMainFrameObserver::FromWebContents(web_contents); |
| if (main_frame_observer && main_frame_observer->is_initialized()) { |
| if (main_frame_observer->is_document_loaded_in_main_frame()) { |
| // Main frame has already loaded for the current WebContents, so execute |
| @@ -98,7 +125,7 @@ void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| // Main frame document has not loaded yet, so wait until it has before |
| // executing JavaScript. It will trigger after DocumentLoadedInFrame is |
| // called for the main frame. |
| - content::WebContentsObserver::Observe(web_contents_.get()); |
| + content::WebContentsObserver::Observe(web_contents); |
| } |
| } else { |
| // The WebContentsMainFrameObserver has not been correctly initialized, |
| @@ -114,15 +141,18 @@ void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { |
| // Create new WebContents to use for distilling the content. |
| content::WebContents::CreateParams create_params(browser_context_); |
| create_params.initially_hidden = true; |
| - web_contents_.reset(content::WebContents::Create(create_params)); |
| - DCHECK(web_contents_.get()); |
| - |
| - web_contents_->SetDelegate(this); |
| + scoped_ptr<content::WebContents> web_contents( |
| + content::WebContents::Create(create_params)); |
| + web_contents->SetDelegate(this); |
| // Start observing WebContents and load the requested URL. |
| - content::WebContentsObserver::Observe(web_contents_.get()); |
| + content::WebContentsObserver::Observe(web_contents.get()); |
| content::NavigationController::LoadURLParams params(url); |
| - web_contents_->GetController().LoadURLWithParams(params); |
| + web_contents->GetController().LoadURLWithParams(params); |
| + |
| + web_contents_handle_.reset(new SourcePageHandleWebContents( |
| + web_contents.Pass())); |
| + DCHECK(web_contents_handle_.get()); |
| } |
| gfx::Size DistillerPageWebContents::GetSizeForNewRenderView( |
| @@ -141,7 +171,11 @@ gfx::Size DistillerPageWebContents::GetSizeForNewRenderView( |
| void DistillerPageWebContents::DocumentLoadedInFrame( |
| content::RenderFrameHost* render_frame_host) { |
| - if (render_frame_host == web_contents_->GetMainFrame()) { |
| + if (!web_contents_handle_) |
| + return; |
| + |
| + content::WebContents* web_contents = web_contents_handle_->GetWebContents(); |
| + if (web_contents && render_frame_host == web_contents->GetMainFrame()) { |
| ExecuteJavaScript(); |
| } |
| } |
| @@ -161,18 +195,25 @@ void DistillerPageWebContents::DidFailLoad( |
| } |
| void DistillerPageWebContents::ExecuteJavaScript() { |
| - content::RenderFrameHost* frame = web_contents_->GetMainFrame(); |
| + if (!web_contents_handle_) |
| + return; |
| + |
| + content::WebContents* web_contents = web_contents_handle_->GetWebContents(); |
| + if (!web_contents) |
| + return; |
| + |
| + content::RenderFrameHost* frame = web_contents->GetMainFrame(); |
| DCHECK(frame); |
| DCHECK_EQ(LOADING_PAGE, state_); |
| state_ = EXECUTING_JAVASCRIPT; |
| content::WebContentsObserver::Observe(NULL); |
| - web_contents_->Stop(); |
| + web_contents->Stop(); |
| DVLOG(1) << "Beginning distillation"; |
| frame->ExecuteJavaScript( |
| base::UTF8ToUTF16(script_), |
| base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, |
| - base::Unretained(this), |
| - web_contents_->GetLastCommittedURL())); |
| + weak_factory_.GetWeakPtr(), |
| + web_contents->GetLastCommittedURL())); |
| } |
| void DistillerPageWebContents::OnWebContentsDistillationDone( |