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 } |
| 87 } else { |
| 88 // The WebContentsMainFrameObserver has not been correctly initialized, |
| 89 // so fall back to creating a new WebContents. |
| 90 CreateNewWebContents(url); |
| 91 } |
| 92 } else { |
| 93 CreateNewWebContents(url); |
| 94 } |
| 95 } |
| 96 |
| 97 void DistillerPageWebContents::CreateNewWebContents(const GURL& url) { |
41 // Create new WebContents to use for distilling the content. | 98 // Create new WebContents to use for distilling the content. |
42 content::WebContents::CreateParams create_params(browser_context_); | 99 content::WebContents::CreateParams create_params(browser_context_); |
43 create_params.initially_hidden = true; | 100 create_params.initially_hidden = true; |
44 web_contents_.reset(content::WebContents::Create(create_params)); | 101 web_contents_.reset(content::WebContents::Create(create_params)); |
45 DCHECK(web_contents_.get()); | 102 DCHECK(web_contents_.get()); |
46 | 103 |
47 // Start observing WebContents and load the requested URL. | 104 // Start observing WebContents and load the requested URL. |
48 content::WebContentsObserver::Observe(web_contents_.get()); | 105 content::WebContentsObserver::Observe(web_contents_.get()); |
49 content::NavigationController::LoadURLParams params(url); | 106 content::NavigationController::LoadURLParams params(url); |
50 web_contents_->GetController().LoadURLWithParams(params); | 107 web_contents_->GetController().LoadURLWithParams(params); |
51 } | 108 } |
52 | 109 |
53 void DistillerPageWebContents::DocumentLoadedInFrame( | 110 void DistillerPageWebContents::DocumentLoadedInFrame( |
54 int64 frame_id, | 111 int64 frame_id, |
55 RenderViewHost* render_view_host) { | 112 RenderViewHost* render_view_host) { |
56 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { | 113 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { |
57 content::WebContentsObserver::Observe(NULL); | |
58 web_contents_->Stop(); | |
59 ExecuteJavaScript(); | 114 ExecuteJavaScript(); |
60 } | 115 } |
61 } | 116 } |
62 | 117 |
63 void DistillerPageWebContents::DidFailLoad( | 118 void DistillerPageWebContents::DidFailLoad( |
64 int64 frame_id, | 119 int64 frame_id, |
65 const GURL& validated_url, | 120 const GURL& validated_url, |
66 bool is_main_frame, | 121 bool is_main_frame, |
67 int error_code, | 122 int error_code, |
68 const base::string16& error_description, | 123 const base::string16& error_description, |
69 RenderViewHost* render_view_host) { | 124 RenderViewHost* render_view_host) { |
70 if (is_main_frame) { | 125 if (is_main_frame) { |
71 content::WebContentsObserver::Observe(NULL); | 126 content::WebContentsObserver::Observe(NULL); |
72 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); | 127 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); |
73 state_ = PAGELOAD_FAILED; | 128 state_ = PAGELOAD_FAILED; |
74 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); | 129 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); |
75 OnWebContentsDistillationDone(GURL(), empty.get()); | 130 OnWebContentsDistillationDone(GURL(), empty.get()); |
76 } | 131 } |
77 } | 132 } |
78 | 133 |
79 void DistillerPageWebContents::ExecuteJavaScript() { | 134 void DistillerPageWebContents::ExecuteJavaScript() { |
80 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); | 135 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); |
81 DCHECK(frame); | 136 DCHECK(frame); |
82 DCHECK_EQ(LOADING_PAGE, state_); | 137 DCHECK_EQ(LOADING_PAGE, state_); |
83 state_ = EXECUTING_JAVASCRIPT; | 138 state_ = EXECUTING_JAVASCRIPT; |
| 139 content::WebContentsObserver::Observe(NULL); |
| 140 web_contents_->Stop(); |
84 DVLOG(1) << "Beginning distillation"; | 141 DVLOG(1) << "Beginning distillation"; |
85 frame->ExecuteJavaScript( | 142 frame->ExecuteJavaScript( |
86 base::UTF8ToUTF16(script_), | 143 base::UTF8ToUTF16(script_), |
87 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, | 144 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, |
88 base::Unretained(this), | 145 base::Unretained(this), |
89 web_contents_->GetLastCommittedURL())); | 146 web_contents_->GetLastCommittedURL())); |
90 } | 147 } |
91 | 148 |
92 void DistillerPageWebContents::OnWebContentsDistillationDone( | 149 void DistillerPageWebContents::OnWebContentsDistillationDone( |
93 const GURL& page_url, | 150 const GURL& page_url, |
94 const base::Value* value) { | 151 const base::Value* value) { |
95 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); | 152 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); |
96 state_ = IDLE; | 153 state_ = IDLE; |
97 DistillerPage::OnDistillationDone(page_url, value); | 154 DistillerPage::OnDistillationDone(page_url, value); |
98 } | 155 } |
99 | 156 |
100 } // namespace dom_distiller | 157 } // namespace dom_distiller |
OLD | NEW |