Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / content / distiller_page_web_contents.cc
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.
4
5 #include "components/dom_distiller/content/distiller_page_web_contents.h"
6
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"
16 #include "url/gurl.h"
17
18 namespace dom_distiller {
19
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_));
25 }
26
27 DistillerPageWebContents::DistillerPageWebContents(
28     DistillerPage::Delegate* delegate,
29     content::BrowserContext* browser_context)
30   : DistillerPage(delegate),
31     browser_context_(browser_context) {}
32
33 DistillerPageWebContents::~DistillerPageWebContents() {
34 }
35
36 void DistillerPageWebContents::InitImpl() {
37   DCHECK(browser_context_);
38   web_contents_.reset(
39       content::WebContents::Create(
40           content::WebContents::CreateParams(browser_context_)));
41 }
42
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);
48 }
49
50 void DistillerPageWebContents::ExecuteJavaScriptImpl(
51     const std::string& script) {
52   content::RenderViewHost* host = web_contents_->GetRenderViewHost();
53   DCHECK(host);
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()));
60 }
61
62 void DistillerPageWebContents::DidFinishLoad(int64 frame_id,
63                                              const GURL& validated_url,
64                                              bool is_main_frame,
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.
68   if (is_main_frame) {
69     content::WebContentsObserver::Observe(NULL);
70     OnLoadURLDone();
71   }
72 }
73
74 void DistillerPageWebContents::DidFailLoad(
75     int64 frame_id,
76     const GURL& validated_url,
77     bool is_main_frame,
78     int error_code,
79     const base::string16& error_description,
80     RenderViewHost* render_view_host) {
81   if (is_main_frame) {
82     content::WebContentsObserver::Observe(NULL);
83     OnLoadURLFailed();
84   }
85 }
86
87 }  // namespace dom_distiller