Commit b592a04d authored by mdjones's avatar mdjones Committed by Commit bot

IOS distiller common code

This change is the IOS counterpart to
https://codereview.chromium.org/1101993003/

Code that is used for generating HTML and JavaScript have been moved
to a superclass; this changes the IOS code to use that.

BUG=472797

Review URL: https://codereview.chromium.org/1071413003

Cr-Commit-Position: refs/heads/master@{#330742}
parent 56bbc98e
......@@ -7,6 +7,7 @@
#include <string>
#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/dom_distiller/core/dom_distiller_request_view_base.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/task_tracker.h"
......@@ -17,12 +18,53 @@
namespace dom_distiller {
DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
namespace {
class IOSContentDataCallback : public DistillerDataCallback {
public:
IOSContentDataCallback(
const GURL& url,
const DistillationFinishedCallback& callback)
const DistillerViewer::DistillationFinishedCallback& callback,
DistillerViewer* distiller_viewer_handle);
~IOSContentDataCallback() override{};
void RunCallback(std::string& data) override;
private:
// Extra param needed by the callback specified below.
GURL url_;
// The callback to be run.
const DistillerViewer::DistillationFinishedCallback callback_;
// A handle to the DistillerViewer object.
DistillerViewer* distiller_viewer_handle_;
};
IOSContentDataCallback::IOSContentDataCallback(
const GURL& url,
const DistillerViewer::DistillationFinishedCallback& callback,
DistillerViewer* distiller_viewer_handle)
: url_(url),
callback_(callback),
distilled_page_prefs_(new DistilledPagePrefs(browser_state->GetPrefs())) {
distiller_viewer_handle_(distiller_viewer_handle) {
}
void IOSContentDataCallback::RunCallback(std::string& data) {
std::string htmlAndScript(data);
htmlAndScript += "<script>" +
distiller_viewer_handle_->GetJavaScriptBuffer() +
"</script>";
callback_.Run(url_, htmlAndScript);
}
} // namespace
DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
const GURL& url,
const DistillationFinishedCallback& callback)
: DomDistillerRequestViewBase(
scoped_ptr<DistillerDataCallback>(
new IOSContentDataCallback(url, callback, this)).Pass(),
new DistilledPagePrefs(browser_state->GetPrefs())) {
DCHECK(browser_state);
DCHECK(url.is_valid());
dom_distiller::DomDistillerService* distillerService =
......@@ -36,22 +78,12 @@ DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
DistillerViewer::~DistillerViewer() {
}
void DistillerViewer::OnArticleReady(
const DistilledArticleProto* article_proto) {
const std::string html = viewer::GetUnsafeArticleTemplateHtml(
&article_proto->pages(0), distilled_page_prefs_->GetTheme(),
distilled_page_prefs_->GetFontFamily());
std::string content_js = viewer::GetUnsafeArticleContentJs(article_proto);
// TODO(noyau): This can be done better with changes to the
// DistillationFinishedCallback. http://crbug.com/472805
std::string htmlAndScript(html);
htmlAndScript += "<script>" + content_js + "</script>";
callback_.Run(url_, htmlAndScript);
void DistillerViewer::SendJavaScript(const std::string& buffer) {
js_buffer_ += buffer;
}
// No need to hold on to the ViewerHandle now that distillation is complete.
viewer_handle_.reset();
std::string DistillerViewer::GetJavaScriptBuffer() {
return js_buffer_;
}
} // namespace dom_distiller
......@@ -5,7 +5,10 @@
#ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
#define IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
#include <string>
#include "base/memory/scoped_ptr.h"
#include "components/dom_distiller/core/dom_distiller_request_view_base.h"
#include "components/dom_distiller/core/task_tracker.h"
class GURL;
......@@ -21,7 +24,7 @@ class DistilledPagePrefs;
// A very simple and naive implementation of the dom_distiller
// ViewRequestDelegate: From an URL it builds an HTML string and notifies when
// finished.
class DistillerViewer : public dom_distiller::ViewRequestDelegate {
class DistillerViewer : public DomDistillerRequestViewBase {
public:
typedef base::Callback<void(const GURL&, const std::string&)>
DistillationFinishedCallback;
......@@ -31,20 +34,17 @@ class DistillerViewer : public dom_distiller::ViewRequestDelegate {
const DistillationFinishedCallback& callback);
~DistillerViewer() override;
// ViewRequestDelegate.
void OnArticleUpdated(
dom_distiller::ArticleDistillationUpdate article_update) override {}
void OnArticleReady(const DistilledArticleProto* article_proto) override;
void SendJavaScript(const std::string& buffer) override;
std::string GetJavaScriptBuffer();
private:
// The url of the distilled page.
const GURL url_;
// Callback to invoke when the page is finished.
DistillationFinishedCallback callback_;
// Interface for accessing preferences for distilled pages.
scoped_ptr<DistilledPagePrefs> distilled_page_prefs_;
// Keeps the distiller going until the view is released.
scoped_ptr<dom_distiller::ViewerHandle> viewer_handle_;
// JavaScript buffer.
std::string js_buffer_;
DISALLOW_COPY_AND_ASSIGN(DistillerViewer);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment