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 @@ ...@@ -7,6 +7,7 @@
#include <string> #include <string>
#include "components/dom_distiller/core/distilled_page_prefs.h" #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/dom_distiller_service.h"
#include "components/dom_distiller/core/proto/distilled_article.pb.h" #include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/task_tracker.h" #include "components/dom_distiller/core/task_tracker.h"
...@@ -17,12 +18,53 @@ ...@@ -17,12 +18,53 @@
namespace dom_distiller { namespace dom_distiller {
namespace {
class IOSContentDataCallback : public DistillerDataCallback {
public:
IOSContentDataCallback(
const GURL& url,
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),
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, DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
const GURL& url, const GURL& url,
const DistillationFinishedCallback& callback) const DistillationFinishedCallback& callback)
: url_(url), : DomDistillerRequestViewBase(
callback_(callback), scoped_ptr<DistillerDataCallback>(
distilled_page_prefs_(new DistilledPagePrefs(browser_state->GetPrefs())) { new IOSContentDataCallback(url, callback, this)).Pass(),
new DistilledPagePrefs(browser_state->GetPrefs())) {
DCHECK(browser_state); DCHECK(browser_state);
DCHECK(url.is_valid()); DCHECK(url.is_valid());
dom_distiller::DomDistillerService* distillerService = dom_distiller::DomDistillerService* distillerService =
...@@ -36,22 +78,12 @@ DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state, ...@@ -36,22 +78,12 @@ DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
DistillerViewer::~DistillerViewer() { DistillerViewer::~DistillerViewer() {
} }
void DistillerViewer::OnArticleReady( void DistillerViewer::SendJavaScript(const std::string& buffer) {
const DistilledArticleProto* article_proto) { js_buffer_ += buffer;
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);
// No need to hold on to the ViewerHandle now that distillation is complete. std::string DistillerViewer::GetJavaScriptBuffer() {
viewer_handle_.reset(); return js_buffer_;
} }
} // namespace dom_distiller } // namespace dom_distiller
...@@ -5,7 +5,10 @@ ...@@ -5,7 +5,10 @@
#ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_ #ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
#define 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 "base/memory/scoped_ptr.h"
#include "components/dom_distiller/core/dom_distiller_request_view_base.h"
#include "components/dom_distiller/core/task_tracker.h" #include "components/dom_distiller/core/task_tracker.h"
class GURL; class GURL;
...@@ -21,7 +24,7 @@ class DistilledPagePrefs; ...@@ -21,7 +24,7 @@ class DistilledPagePrefs;
// A very simple and naive implementation of the dom_distiller // A very simple and naive implementation of the dom_distiller
// ViewRequestDelegate: From an URL it builds an HTML string and notifies when // ViewRequestDelegate: From an URL it builds an HTML string and notifies when
// finished. // finished.
class DistillerViewer : public dom_distiller::ViewRequestDelegate { class DistillerViewer : public DomDistillerRequestViewBase {
public: public:
typedef base::Callback<void(const GURL&, const std::string&)> typedef base::Callback<void(const GURL&, const std::string&)>
DistillationFinishedCallback; DistillationFinishedCallback;
...@@ -31,20 +34,17 @@ class DistillerViewer : public dom_distiller::ViewRequestDelegate { ...@@ -31,20 +34,17 @@ class DistillerViewer : public dom_distiller::ViewRequestDelegate {
const DistillationFinishedCallback& callback); const DistillationFinishedCallback& callback);
~DistillerViewer() override; ~DistillerViewer() override;
// ViewRequestDelegate. void SendJavaScript(const std::string& buffer) override;
void OnArticleUpdated(
dom_distiller::ArticleDistillationUpdate article_update) override {} std::string GetJavaScriptBuffer();
void OnArticleReady(const DistilledArticleProto* article_proto) override;
private: private:
// The url of the distilled page. // The url of the distilled page.
const GURL url_; const GURL url_;
// Callback to invoke when the page is finished.
DistillationFinishedCallback callback_;
// Interface for accessing preferences for distilled pages. // Interface for accessing preferences for distilled pages.
scoped_ptr<DistilledPagePrefs> distilled_page_prefs_; scoped_ptr<DistilledPagePrefs> distilled_page_prefs_;
// Keeps the distiller going until the view is released. // JavaScript buffer.
scoped_ptr<dom_distiller::ViewerHandle> viewer_handle_; std::string js_buffer_;
DISALLOW_COPY_AND_ASSIGN(DistillerViewer); 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