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
3 // found in the LICENSE file.
5 #include "components/dom_distiller/content/distiller_page_web_contents.h"
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/dom_distiller/core/distiller_page.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
18 namespace dom_distiller {
20 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage(
21 DistillerPage::Delegate* delegate) const {
22 DCHECK(browser_context_);
23 return scoped_ptr<DistillerPage>(
24 new DistillerPageWebContents(delegate, browser_context_));
27 DistillerPageWebContents::DistillerPageWebContents(
28 DistillerPage::Delegate* delegate,
29 content::BrowserContext* browser_context)
30 : DistillerPage(delegate),
31 browser_context_(browser_context) {}
33 DistillerPageWebContents::~DistillerPageWebContents() {
36 void DistillerPageWebContents::InitImpl() {
37 DCHECK(browser_context_);
39 content::WebContents::Create(
40 content::WebContents::CreateParams(browser_context_)));
43 void DistillerPageWebContents::LoadURLImpl(const GURL& gurl) {
44 DCHECK(web_contents_.get());
45 content::WebContentsObserver::Observe(web_contents_.get());
46 content::NavigationController::LoadURLParams params(gurl);
47 web_contents_->GetController().LoadURLWithParams(params);
50 void DistillerPageWebContents::ExecuteJavaScriptImpl(
51 const std::string& script) {
52 content::RenderViewHost* host = web_contents_->GetRenderViewHost();
54 host->ExecuteJavascriptInWebFrameCallbackResult(
55 base::string16(), // frame_xpath
56 base::UTF8ToUTF16(script),
57 base::Bind(&DistillerPage::OnExecuteJavaScriptDone,
58 base::Unretained(this),
59 web_contents_->GetLastCommittedURL()));
62 void DistillerPageWebContents::DidFinishLoad(int64 frame_id,
63 const GURL& validated_url,
65 RenderViewHost* render_view_host) {
66 // TODO(shashishekhar): Find a better way to detect when it is safe to run the
67 // distillation script. Waiting for the entire page to load is really slow.
69 content::WebContentsObserver::Observe(NULL);
74 void DistillerPageWebContents::DidFailLoad(
76 const GURL& validated_url,
79 const base::string16& error_description,
80 RenderViewHost* render_view_host) {
82 content::WebContentsObserver::Observe(NULL);
87 } // namespace dom_distiller