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 baa9977a9f167f2f27d7cdd1c0c004f69af8b68a..9566f29fd0ddd2c392b8ff142bce5f1589110cf7 100644 |
| --- a/components/dom_distiller/content/distiller_page_web_contents.cc |
| +++ b/components/dom_distiller/content/distiller_page_web_contents.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "components/dom_distiller/core/distiller_page.h" |
| +#include "components/dom_distiller/core/dom_distiller_service.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/navigation_controller.h" |
| #include "content/public/browser/render_frame_host.h" |
| @@ -18,18 +19,47 @@ |
| namespace dom_distiller { |
| +SourcePageHandleWebContents::SourcePageHandleWebContents( |
| + scoped_ptr<content::WebContents> web_contents) |
| + : web_contents_(web_contents.Pass()) { |
| +} |
| + |
| +SourcePageHandleWebContents::~SourcePageHandleWebContents() { |
| +} |
| + |
| +scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { |
| + return web_contents_.Pass(); |
| +} |
| + |
| scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage() |
| const { |
| DCHECK(browser_context_); |
| - return scoped_ptr<DistillerPage>( |
| - new DistillerPageWebContents(browser_context_)); |
| + return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| + browser_context_, scoped_ptr<SourcePageHandleWebContents>())); |
| +} |
| + |
| +scoped_ptr<DistillerPage> |
| +DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( |
| + scoped_ptr<SourcePageHandle> handle) const { |
| + DCHECK(browser_context_); |
| + scoped_ptr<SourcePageHandleWebContents> web_contents_handle = |
| + scoped_ptr<SourcePageHandleWebContents>( |
| + reinterpret_cast<SourcePageHandleWebContents*>(handle.release())); |
|
Yaron
2014/05/06 00:23:20
static_cast
nyquist
2014/05/06 03:52:27
Sadly, no. This cast makes the type more specific,
nyquist
2014/05/15 10:15:35
Done.
|
| + return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| + browser_context_, web_contents_handle.Pass())); |
| } |
| DistillerPageWebContents::DistillerPageWebContents( |
| - content::BrowserContext* browser_context) |
| - : state_(IDLE), browser_context_(browser_context) {} |
| + content::BrowserContext* browser_context, |
| + scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) |
| + : state_(IDLE), browser_context_(browser_context) { |
| + if (optional_web_contents_handle) { |
| + web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); |
| + } |
| +} |
| -DistillerPageWebContents::~DistillerPageWebContents() {} |
| +DistillerPageWebContents::~DistillerPageWebContents() { |
| +} |
| void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| const std::string& script) { |
| @@ -38,24 +68,32 @@ void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| state_ = LOADING_PAGE; |
| script_ = script; |
| - // 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()); |
| + if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { |
| + if (web_contents_->IsDocumentLoadedInMainFrame()) { |
| + ExecuteJavaScript(); |
| + } else { |
| + // Main frame document has not loaded yet, so wait until it has before |
| + // executing JavaScript. |
|
Yaron
2014/05/06 00:23:20
In the attach case, I wonder if we should stop the
nyquist
2014/05/06 03:52:27
Yeah, I think we could stop the WebContents here.
nyquist
2014/05/15 10:15:35
Done.
|
| + content::WebContentsObserver::Observe(web_contents_.get()); |
| + } |
| + } else { |
| + // 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()); |
| - // Start observing WebContents and load the requested URL. |
| - content::WebContentsObserver::Observe(web_contents_.get()); |
| - content::NavigationController::LoadURLParams params(url); |
| - web_contents_->GetController().LoadURLWithParams(params); |
| + // Start observing WebContents and load the requested URL. |
| + content::WebContentsObserver::Observe(web_contents_.get()); |
| + content::NavigationController::LoadURLParams params(url); |
| + web_contents_->GetController().LoadURLWithParams(params); |
| + } |
| } |
| void DistillerPageWebContents::DocumentLoadedInFrame( |
| int64 frame_id, |
| RenderViewHost* render_view_host) { |
| if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { |
| - content::WebContentsObserver::Observe(NULL); |
| - web_contents_->Stop(); |
| ExecuteJavaScript(); |
| } |
| } |
| @@ -81,6 +119,8 @@ void DistillerPageWebContents::ExecuteJavaScript() { |
| DCHECK(frame); |
| DCHECK_EQ(LOADING_PAGE, state_); |
| state_ = EXECUTING_JAVASCRIPT; |
| + content::WebContentsObserver::Observe(NULL); |
| + web_contents_->Stop(); |
| DVLOG(1) << "Beginning distillation"; |
| frame->ExecuteJavaScript( |
| base::UTF8ToUTF16(script_), |