Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / components / dom_distiller / content / distiller_page_web_contents.cc
blobbaa9977a9f167f2f27d7cdd1c0c004f69af8b68a
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_frame_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "url/gurl.h"
19 namespace dom_distiller {
21 scoped_ptr<DistillerPage> DistillerPageWebContentsFactory::CreateDistillerPage()
22 const {
23 DCHECK(browser_context_);
24 return scoped_ptr<DistillerPage>(
25 new DistillerPageWebContents(browser_context_));
28 DistillerPageWebContents::DistillerPageWebContents(
29 content::BrowserContext* browser_context)
30 : state_(IDLE), browser_context_(browser_context) {}
32 DistillerPageWebContents::~DistillerPageWebContents() {}
34 void DistillerPageWebContents::DistillPageImpl(const GURL& url,
35 const std::string& script) {
36 DCHECK(browser_context_);
37 DCHECK(state_ == IDLE);
38 state_ = LOADING_PAGE;
39 script_ = script;
41 // Create new WebContents to use for distilling the content.
42 content::WebContents::CreateParams create_params(browser_context_);
43 create_params.initially_hidden = true;
44 web_contents_.reset(content::WebContents::Create(create_params));
45 DCHECK(web_contents_.get());
47 // Start observing WebContents and load the requested URL.
48 content::WebContentsObserver::Observe(web_contents_.get());
49 content::NavigationController::LoadURLParams params(url);
50 web_contents_->GetController().LoadURLWithParams(params);
53 void DistillerPageWebContents::DocumentLoadedInFrame(
54 int64 frame_id,
55 RenderViewHost* render_view_host) {
56 if (frame_id == web_contents_->GetMainFrame()->GetRoutingID()) {
57 content::WebContentsObserver::Observe(NULL);
58 web_contents_->Stop();
59 ExecuteJavaScript();
63 void DistillerPageWebContents::DidFailLoad(
64 int64 frame_id,
65 const GURL& validated_url,
66 bool is_main_frame,
67 int error_code,
68 const base::string16& error_description,
69 RenderViewHost* render_view_host) {
70 if (is_main_frame) {
71 content::WebContentsObserver::Observe(NULL);
72 DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT);
73 state_ = PAGELOAD_FAILED;
74 scoped_ptr<base::Value> empty(base::Value::CreateNullValue());
75 OnWebContentsDistillationDone(GURL(), empty.get());
79 void DistillerPageWebContents::ExecuteJavaScript() {
80 content::RenderFrameHost* frame = web_contents_->GetMainFrame();
81 DCHECK(frame);
82 DCHECK_EQ(LOADING_PAGE, state_);
83 state_ = EXECUTING_JAVASCRIPT;
84 DVLOG(1) << "Beginning distillation";
85 frame->ExecuteJavaScript(
86 base::UTF8ToUTF16(script_),
87 base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone,
88 base::Unretained(this),
89 web_contents_->GetLastCommittedURL()));
92 void DistillerPageWebContents::OnWebContentsDistillationDone(
93 const GURL& page_url,
94 const base::Value* value) {
95 DCHECK(state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT);
96 state_ = IDLE;
97 DistillerPage::OnDistillationDone(page_url, value);
100 } // namespace dom_distiller