Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: components/dom_distiller/content/distiller_page_web_contents.cc

Issue 266073003: Add support for distilling current WebContents (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/core/distiller_page.h" 10 #include "components/dom_distiller/core/distiller_page.h"
11 #include "components/dom_distiller/core/dom_distiller_service.h"
11 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/navigation_controller.h" 13 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/render_frame_host.h" 14 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/browser/web_contents_observer.h"
17 #include "url/gurl.h" 18 #include "url/gurl.h"
18 19
19 namespace dom_distiller { 20 namespace dom_distiller {
20 21
22 SourcePageHandleWebContents::SourcePageHandleWebContents(
23 scoped_ptr<content::WebContents> web_contents)
24 : web_contents_(web_contents.Pass()) {
25 }
26
27 SourcePageHandleWebContents::~SourcePageHandleWebContents() {
28 }
29
30 scoped_ptr<content::WebContents> SourcePageHandleWebContents::GetWebContents() {
31 return web_contents_.Pass();
32 }
33
21 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage() 34 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage()
22 const { 35 const {
23 DCHECK(browser_context_); 36 DCHECK(browser_context_);
24 return scoped_ptr<DistillerPage>( 37 return scoped_ptr<DistillerPage>(new DistillerPageWebContents(
25 new DistillerPageWebContents(browser_context_)); 38 browser_context_, scoped_ptr<SourcePageHandleWebContents>()));
39 }
40
41 scoped_ptr<DistillerPage>
42 DistillerPageWebContentsFactory::CreateDistillerPageWithHandle(
43 scoped_ptr<SourcePageHandle> handle) const {
44 DCHECK(browser_context_);
45 scoped_ptr<SourcePageHandleWebContents> web_contents_handle =
46 scoped_ptr<SourcePageHandleWebContents>(
47 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.
48 return scoped_ptr<DistillerPage>(new DistillerPageWebContents(
49 browser_context_, web_contents_handle.Pass()));
26 } 50 }
27 51
28 DistillerPageWebContents::DistillerPageWebContents( 52 DistillerPageWebContents::DistillerPageWebContents(
29 content::BrowserContext* browser_context) 53 content::BrowserContext* browser_context,
30 : state_(IDLE), browser_context_(browser_context) {} 54 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle)
55 : state_(IDLE), browser_context_(browser_context) {
56 if (optional_web_contents_handle) {
57 web_contents_ = optional_web_contents_handle->GetWebContents().Pass();
58 }
59 }
31 60
32 DistillerPageWebContents::~DistillerPageWebContents() {} 61 DistillerPageWebContents::~DistillerPageWebContents() {
62 }
33 63
34 void DistillerPageWebContents::DistillPageImpl(const GURL& url, 64 void DistillerPageWebContents::DistillPageImpl(const GURL& url,
35 const std::string& script) { 65 const std::string& script) {
36 DCHECK(browser_context_); 66 DCHECK(browser_context_);
37 DCHECK(state_ == IDLE); 67 DCHECK(state_ == IDLE);
38 state_ = LOADING_PAGE; 68 state_ = LOADING_PAGE;
39 script_ = script; 69 script_ = script;
40 70
41 // Create new WebContents to use for distilling the content. 71 if (web_contents_ && web_contents_->GetLastCommittedURL() == url) {
42 content::WebContents::CreateParams create_params(browser_context_); 72 if (web_contents_->IsDocumentLoadedInMainFrame()) {
43 create_params.initially_hidden = true; 73 ExecuteJavaScript();
44 web_contents_.reset(content::WebContents::Create(create_params)); 74 } else {
45 DCHECK(web_contents_.get()); 75 // Main frame document has not loaded yet, so wait until it has before
76 // 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.
77 content::WebContentsObserver::Observe(web_contents_.get());
78 }
79 } else {
80 // Create new WebContents to use for distilling the content.
81 content::WebContents::CreateParams create_params(browser_context_);
82 create_params.initially_hidden = true;
83 web_contents_.reset(content::WebContents::Create(create_params));
84 DCHECK(web_contents_.get());
46 85
47 // Start observing WebContents and load the requested URL. 86 // Start observing WebContents and load the requested URL.
48 content::WebContentsObserver::Observe(web_contents_.get()); 87 content::WebContentsObserver::Observe(web_contents_.get());
49 content::NavigationController::LoadURLParams params(url); 88 content::NavigationController::LoadURLParams params(url);
50 web_contents_->GetController().LoadURLWithParams(params); 89 web_contents_->GetController().LoadURLWithParams(params);
90 }
51 } 91 }
52 92
53 void DistillerPageWebContents::DocumentLoadedInFrame( 93 void DistillerPageWebContents::DocumentLoadedInFrame(
54 int64 frame_id, 94 int64 frame_id,
55 RenderViewHost* render_view_host) { 95 RenderViewHost* render_view_host) {
56 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) { 96 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) {
57 content::WebContentsObserver::Observe(NULL);
58 web_contents_->Stop();
59 ExecuteJavaScript(); 97 ExecuteJavaScript();
60 } 98 }
61 } 99 }
62 100
63 void DistillerPageWebContents::DidFailLoad( 101 void DistillerPageWebContents::DidFailLoad(
64 int64 frame_id, 102 int64 frame_id,
65 const GURL& validated_url, 103 const GURL& validated_url,
66 bool is_main_frame, 104 bool is_main_frame,
67 int error_code, 105 int error_code,
68 const base::string16& error_description, 106 const base::string16& error_description,
69 RenderViewHost* render_view_host) { 107 RenderViewHost* render_view_host) {
70 if (is_main_frame) { 108 if (is_main_frame) {
71 content::WebContentsObserver::Observe(NULL); 109 content::WebContentsObserver::Observe(NULL);
72 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT); 110 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT);
73 state_ = PAGELOAD_FAILED; 111 state_ = PAGELOAD_FAILED;
74 scoped_ptr<base::Value> empty(base::Value::CreateNullValue()); 112 scoped_ptr<base::Value> empty(base::Value::CreateNullValue());
75 OnWebContentsDistillationDone(GURL(), empty.get()); 113 OnWebContentsDistillationDone(GURL(), empty.get());
76 } 114 }
77 } 115 }
78 116
79 void DistillerPageWebContents::ExecuteJavaScript() { 117 void DistillerPageWebContents::ExecuteJavaScript() {
80 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); 118 content::RenderFrameHost* frame = web_contents_->GetMainFrame();
81 DCHECK(frame); 119 DCHECK(frame);
82 DCHECK_EQ(LOADING_PAGE, state_); 120 DCHECK_EQ(LOADING_PAGE, state_);
83 state_ = EXECUTING_JAVASCRIPT; 121 state_ = EXECUTING_JAVASCRIPT;
122 content::WebContentsObserver::Observe(NULL);
123 web_contents_->Stop();
84 DVLOG(1) << "Beginning distillation"; 124 DVLOG(1) << "Beginning distillation";
85 frame->ExecuteJavaScript( 125 frame->ExecuteJavaScript(
86 base::UTF8ToUTF16(script_), 126 base::UTF8ToUTF16(script_),
87 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone, 127 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone,
88 base::Unretained(this), 128 base::Unretained(this),
89 web_contents_->GetLastCommittedURL())); 129 web_contents_->GetLastCommittedURL()));
90 } 130 }
91 131
92 void DistillerPageWebContents::OnWebContentsDistillationDone( 132 void DistillerPageWebContents::OnWebContentsDistillationDone(
93 const GURL& page_url, 133 const GURL& page_url,
94 const base::Value* value) { 134 const base::Value* value) {
95 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT); 135 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT);
96 state_ = IDLE; 136 state_ = IDLE;
97 DistillerPage::OnDistillationDone(page_url, value); 137 DistillerPage::OnDistillationDone(page_url, value);
98 } 138 }
99 139
100 } // namespace dom_distiller 140 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698