summaryrefslogtreecommitdiff
path: root/chromium/components/dom_distiller/content/browser/distiller_page_web_contents.cc
blob: 0ac8f7c7b8e3b87f2d1e5254453fdf9a422b7b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dom_distiller/content/browser/distiller_page_web_contents.h"

#include <memory>
#include <utility>

#include "base/callback.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
#include "components/dom_distiller/content/browser/web_contents_main_frame_observer.h"
#include "components/dom_distiller/core/distiller_page.h"
#include "components/dom_distiller/core/dom_distiller_constants.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "url/gurl.h"

namespace dom_distiller {

SourcePageHandleWebContents::SourcePageHandleWebContents(
    content::WebContents* web_contents,
    bool owned)
    : web_contents_(web_contents), owned_(owned) {
}

SourcePageHandleWebContents::~SourcePageHandleWebContents() {
  if (owned_) {
    delete web_contents_;
  }
}

std::unique_ptr<DistillerPage>
DistillerPageWebContentsFactory::CreateDistillerPage(
    const gfx::Size& render_view_size) const {
  DCHECK(browser_context_);
  return std::unique_ptr<DistillerPage>(new DistillerPageWebContents(
      browser_context_, render_view_size,
      std::unique_ptr<SourcePageHandleWebContents>()));
}

std::unique_ptr<DistillerPage>
DistillerPageWebContentsFactory::CreateDistillerPageWithHandle(
    std::unique_ptr<SourcePageHandle> handle) const {
  DCHECK(browser_context_);
  std::unique_ptr<SourcePageHandleWebContents> web_contents_handle =
      std::unique_ptr<SourcePageHandleWebContents>(
          static_cast<SourcePageHandleWebContents*>(handle.release()));
  return std::unique_ptr<DistillerPage>(new DistillerPageWebContents(
      browser_context_, gfx::Size(), std::move(web_contents_handle)));
}

DistillerPageWebContents::DistillerPageWebContents(
    content::BrowserContext* browser_context,
    const gfx::Size& render_view_size,
    std::unique_ptr<SourcePageHandleWebContents> optional_web_contents_handle)
    : state_(IDLE),
      source_page_handle_(nullptr),
      browser_context_(browser_context),
      render_view_size_(render_view_size),
      weak_factory_(this) {
  if (optional_web_contents_handle) {
    source_page_handle_ = std::move(optional_web_contents_handle);
    if (render_view_size.IsEmpty())
      render_view_size_ =
          source_page_handle_->web_contents()->GetContainerBounds().size();
  }
}

DistillerPageWebContents::~DistillerPageWebContents() {
}

bool DistillerPageWebContents::StringifyOutput() {
 return false;
}

void DistillerPageWebContents::DistillPageImpl(const GURL& url,
                                               const std::string& script) {
  DCHECK(browser_context_);
  DCHECK(state_ == IDLE);
  state_ = LOADING_PAGE;
  script_ = script;

  if (source_page_handle_ && source_page_handle_->web_contents() &&
      source_page_handle_->web_contents()->GetLastCommittedURL() == url) {
    WebContentsMainFrameObserver* main_frame_observer =
        WebContentsMainFrameObserver::FromWebContents(
            source_page_handle_->web_contents());
    if (main_frame_observer && main_frame_observer->is_initialized()) {
      if (main_frame_observer->is_document_loaded_in_main_frame()) {
        // Main frame has already loaded for the current WebContents, so execute
        // JavaScript immediately.
        ExecuteJavaScript();
      } else {
        // Main frame document has not loaded yet, so wait until it has before
        // executing JavaScript. It will trigger after DocumentLoadedInFrame is
        // called for the main frame.
        content::WebContentsObserver::Observe(
            source_page_handle_->web_contents());
      }
    } else {
      // The WebContentsMainFrameObserver has not been correctly initialized,
      // so fall back to creating a new WebContents.
      CreateNewWebContents(url);
    }
  } else {
    CreateNewWebContents(url);
  }
}

void DistillerPageWebContents::CreateNewWebContents(const GURL& url) {
  // Create new WebContents to use for distilling the content.
  content::WebContents::CreateParams create_params(browser_context_);
  create_params.initially_hidden = true;
  content::WebContents* web_contents =
      content::WebContents::Create(create_params);
  DCHECK(web_contents);

  web_contents->SetDelegate(this);

  // Start observing WebContents and load the requested URL.
  content::WebContentsObserver::Observe(web_contents);
  content::NavigationController::LoadURLParams params(url);
  web_contents->GetController().LoadURLWithParams(params);

  source_page_handle_.reset(
      new SourcePageHandleWebContents(web_contents, true));
}

gfx::Size DistillerPageWebContents::GetSizeForNewRenderView(
    content::WebContents* web_contents) const {
  gfx::Size size(render_view_size_);
  if (size.IsEmpty())
    size = web_contents->GetContainerBounds().size();
  // If size is still empty, set it to fullscreen so that document.offsetWidth
  // in the executed domdistiller.js won't be 0.
  if (size.IsEmpty()) {
    DVLOG(1) << "Using fullscreen as default RenderView size";
    size = display::Screen::GetScreen()->GetPrimaryDisplay().size();
  }
  return size;
}

void DistillerPageWebContents::DocumentLoadedInFrame(
    content::RenderFrameHost* render_frame_host) {
  if (render_frame_host ==
      source_page_handle_->web_contents()->GetMainFrame()) {
    ExecuteJavaScript();
  }
}

void DistillerPageWebContents::DidFailLoad(
    content::RenderFrameHost* render_frame_host,
    const GURL& validated_url,
    int error_code,
    const base::string16& error_description,
    bool was_ignored_by_handler) {
  if (!render_frame_host->GetParent()) {
    content::WebContentsObserver::Observe(NULL);
    DCHECK(state_ == LOADING_PAGE || state_ == EXECUTING_JAVASCRIPT);
    state_ = PAGELOAD_FAILED;
    auto empty = base::MakeUnique<base::Value>();
    OnWebContentsDistillationDone(GURL(), base::TimeTicks(), empty.get());
  }
}

void DistillerPageWebContents::ExecuteJavaScript() {
  content::RenderFrameHost* frame =
      source_page_handle_->web_contents()->GetMainFrame();
  DCHECK(frame);
  DCHECK_EQ(LOADING_PAGE, state_);
  state_ = EXECUTING_JAVASCRIPT;
  content::WebContentsObserver::Observe(NULL);
  // Stop any pending navigation since the intent is to distill the current
  // page.
  source_page_handle_->web_contents()->Stop();
  DVLOG(1) << "Beginning distillation";
  RunIsolatedJavaScript(
      frame, script_,
      base::Bind(&DistillerPageWebContents::OnWebContentsDistillationDone,
                 weak_factory_.GetWeakPtr(),
                 source_page_handle_->web_contents()->GetLastCommittedURL(),
                 base::TimeTicks::Now()));
}

void DistillerPageWebContents::OnWebContentsDistillationDone(
    const GURL& page_url,
    const base::TimeTicks& javascript_start,
    const base::Value* value) {
  DCHECK(state_ == IDLE || state_ == LOADING_PAGE ||  // TODO(nyquist): 493795.
         state_ == PAGELOAD_FAILED || state_ == EXECUTING_JAVASCRIPT);
  state_ = IDLE;

  if (!javascript_start.is_null()) {
    base::TimeDelta javascript_time = base::TimeTicks::Now() - javascript_start;
    UMA_HISTOGRAM_TIMES("DomDistiller.Time.RunJavaScript", javascript_time);
    DVLOG(1) << "DomDistiller.Time.RunJavaScript = " << javascript_time;
  }

  DistillerPage::OnDistillationDone(page_url, value);
}

}  // namespace dom_distiller