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/core/distiller_page.h" | 11 #include "components/dom_distiller/core/distiller_page.h" |
| 12 #include "components/dom_distiller/core/dom_distiller_service.h" | |
| 11 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 18 | 20 |
| 19 namespace dom_distiller { | 21 namespace dom_distiller { |
| 20 | 22 |
| 23 SourcePageHandleWebContents::SourcePageHandleWebContents( | |
| 24 scoped_ptr<content::WebContents> web_contents) | |
| 25 : web_contents_(web_contents.Pass()) { | |
| 26 DCHECK(web_contents_); | |
| 27 } | |
| 28 | |
| 29 SourcePageHandleWebContents::~SourcePageHandleWebContents() { | |
| 30 } | |
| 31 | |
| 32 scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() { | |
| 33 return web_contents_.Pass(); | |
| 34 } | |
| 35 | |
| 21 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage() | 36 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage() |
| 22 const { | 37 const { |
| 23 DCHECK(browser_context_); | 38 DCHECK(browser_context_); |
| 24 return scoped_ptr<DistillerPage>( | 39 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( |
| 25 new DistillerPageWebContents(browser_context_)); | 40 browser_context_, scoped_ptr<SourcePageHandleWebContents>())); |
| 41 } | |
| 42 | |
| 43 scoped_ptr<DistillerPage> | |
| 44 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle( | |
| 45 scoped_ptr<SourcePageHandle> handle) const { | |
| 46 DCHECK(browser_context_); | |
| 47 scoped_ptr<SourcePageHandleWebContents> web_contents_handle = | |
| 48 scoped_ptr<SourcePageHandleWebContents>( | |
| 49 static_cast<SourcePageHandleWebContents*>(handle.release())); | |
| 50 return scoped_ptr<DistillerPage>(new DistillerPageWebContents( | |
| 51 browser_context_, web_contents_handle.Pass())); | |
| 26 } | 52 } |
| 27 | 53 |
| 28 DistillerPageWebContents::DistillerPageWebContents( | 54 DistillerPageWebContents::DistillerPageWebContents( |
| 29 content::BrowserContext* browser_context) | 55 content::BrowserContext* browser_context, |
| 30 : state_(IDLE), browser_context_(browser_context) {} | 56 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle) |
| 57 : state_(IDLE), browser_context_(browser_context) { | |
| 58 if (optional_web_contents_handle) { | |
| 59 web_contents_ = optional_web_contents_handle->GetWebContents().Pass(); | |
| 60 } | |
| 61 } | |
| 31 | 62 |
| 32 DistillerPageWebContents::~DistillerPageWebContents() {} | 63 DistillerPageWebContents::~DistillerPageWebContents() { |
| 64 } | |
| 33 | 65 |
| 34 void DistillerPageWebContents::DistillPageImpl(const GURL& url, | 66 void DistillerPageWebContents::DistillPageImpl(const GURL& url, |
| 35 const std::string& script) { | 67 const std::string& script) { |
| 36 DCHECK(browser_context_); | 68 DCHECK(browser_context_); |
| 37 DCHECK(state_ == IDLE); | 69 DCHECK(state_ == IDLE); |
| 38 state_ = LOADING_PAGE; | 70 state_ = LOADING_PAGE; |
| 39 script_ = script; | 71 script_ = script; |
| 40 | 72 |
| 73 if (web_contents_ && web_contents_->GetLastCommittedURL() == url) { | |
| 74 WebContentsMainFrameObserver* main_frame_observer = | |
| 75 WebContentsMainFrameObserver::FromWebContents(web_contents_.get()); | |
| 76 if (main_frame_observer && main_frame_observer->is_initialized()) { | |
| 77 if (main_frame_observer->is_document_loaded_in_main_frame()) { | |
| 78 // Main frame has already loaded for the current WebContents, so execute | |
| 79 // JavaScript immediately. | |
| 80 ExecuteJavaScript(); | |
| 81 } else { | |
| 82 // Main frame document has not loaded yet, so wait until it has before | |
| 83 // executing JavaScript. It will trigger after DocumentLoadedInFrame is | |
| 84 // called for the main frame. | |
| 85 content::WebContentsObserver::Observe(web_contents_.get()); | |
| 86 web_contents_->Stop(); | |
|
Yaron
2014/05/16 02:02:49
You don't want to stop here, right? You're waiting
nyquist
2014/05/20 19:58:37
Done.
| |
| 87 } | |
| 88 } else { | |
| 89 // The WebContentsMainFrameObserver has not been correctly initialized, | |
| 90 // so fall back to creating a new WebContents. | |
| 91 CreateNewWebContents(url); | |
| 92 } | |
| 93 } else { | |
| 94 CreateNewWebContents(url); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { | |
| 41 // Create new WebContents to use for distilling the content. | 99 // Create new WebContents to use for distilling the content. |
| 42 content::WebContents::CreateParams create_params(browser_context_); | 100 content::WebContents::CreateParams create_params(browser_context_); |
| 43 create_params.initially_hidden = true; | 101 create_params.initially_hidden = true; |
| 44 web_contents_.reset(content::WebContents::Create(create_params)); | 102 web_contents_.reset(content::WebContents::Create(create_params)); |
| 45 DCHECK(web_contents_.get()); | 103 DCHECK(web_contents_.get()); |
| 46 | 104 |
| 47 // Start observing WebContents and load the requested URL. | 105 // Start observing WebContents and load the requested URL. |
| 48 content::WebContentsObserver::Observe(web_contents_.get()); | 106 content::WebContentsObserver::Observe(web_contents_.get()); |
| 49 content::NavigationController::LoadURLParams params(url); | 107 content::NavigationController::LoadURLParams params(url); |
| 50 web_contents_->GetController().LoadURLWithParams(params); | 108 web_contents_->GetController().LoadURLWithParams(params); |
| 51 } | 109 } |
| 52 | 110 |
| 53 void DistillerPageWebContents::DocumentLoadedInFrame( | 111 void DistillerPageWebContents::DocumentLoadedInFrame( |
| 54 int64 frame_id, | 112 int64 frame_id, |
| 55 RenderViewHost* render_view_host) { | 113 RenderViewHost* render_view_host) { |
| 56 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { | 114 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { |
| 57 content::WebContentsObserver::Observe(NULL); | |
| 58 web_contents_->Stop(); | |
| 59 ExecuteJavaScript(); | 115 ExecuteJavaScript(); |
| 60 } | 116 } |
| 61 } | 117 } |
| 62 | 118 |
| 63 void DistillerPageWebContents::DidFailLoad( | 119 void DistillerPageWebContents::DidFailLoad( |
| 64 int64 frame_id, | 120 int64 frame_id, |
| 65 const GURL& validated_url, | 121 const GURL& validated_url, |
| 66 bool is_main_frame, | 122 bool is_main_frame, |
| 67 int error_code, | 123 int error_code, |
| 68 const base::string16& error_description, | 124 const base::string16& error_description, |
| 69 RenderViewHost* render_view_host) { | 125 RenderViewHost* render_view_host) { |
| 70 if (is_main_frame) { | 126 if (is_main_frame) { |
| 71 content::WebContentsObserver::Observe(NULL); | 127 content::WebContentsObserver::Observe(NULL); |
| 72 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); | 128 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); |
| 73 state_ = PAGELOAD_FAILED; | 129 state_ = PAGELOAD_FAILED; |
| 74 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); | 130 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); |
| 75 OnWebContentsDistillationDone(GURL(), empty.get()); | 131 OnWebContentsDistillationDone(GURL(), empty.get()); |
| 76 } | 132 } |
| 77 } | 133 } |
| 78 | 134 |
| 79 void DistillerPageWebContents::ExecuteJavaScript() { | 135 void DistillerPageWebContents::ExecuteJavaScript() { |
| 80 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); | 136 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); |
| 81 DCHECK(frame); | 137 DCHECK(frame); |
| 82 DCHECK_EQ(LOADING_PAGE, state_); | 138 DCHECK_EQ(LOADING_PAGE, state_); |
| 83 state_ = EXECUTING_JAVASCRIPT; | 139 state_ = EXECUTING_JAVASCRIPT; |
| 140 content::WebContentsObserver::Observe(NULL); | |
| 141 web_contents_->Stop(); | |
| 84 DVLOG(1) << "Beginning distillation"; | 142 DVLOG(1) << "Beginning distillation"; |
| 85 frame->ExecuteJavaScript( | 143 frame->ExecuteJavaScript( |
| 86 base::UTF8ToUTF16(script_), | 144 base::UTF8ToUTF16(script_), |
| 87 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, | 145 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, |
| 88 base::Unretained(this), | 146 base::Unretained(this), |
| 89 web_contents_->GetLastCommittedURL())); | 147 web_contents_->GetLastCommittedURL())); |
| 90 } | 148 } |
| 91 | 149 |
| 92 void DistillerPageWebContents::OnWebContentsDistillationDone( | 150 void DistillerPageWebContents::OnWebContentsDistillationDone( |
| 93 const GURL& page_url, | 151 const GURL& page_url, |
| 94 const base::Value* value) { | 152 const base::Value* value) { |
| 95 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); | 153 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); |
| 96 state_ = IDLE; | 154 state_ = IDLE; |
| 97 DistillerPage::OnDistillationDone(page_url, value); | 155 DistillerPage::OnDistillationDone(page_url, value); |
| 98 } | 156 } |
| 99 | 157 |
| 100 } // namespace dom_distiller | 158 } // namespace dom_distiller |
| OLD | NEW |