Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / dom_distiller / distiller_viewer.cc
blob9ec395286236817b1cdf90e60351197887721e52
1 // Copyright 2015 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 "ios/chrome/browser/dom_distiller/distiller_viewer.h"
7 #include <string>
9 #include "components/dom_distiller/core/distilled_page_prefs.h"
10 #include "components/dom_distiller/core/dom_distiller_request_view_base.h"
11 #include "components/dom_distiller/core/dom_distiller_service.h"
12 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
13 #include "components/dom_distiller/core/task_tracker.h"
14 #include "components/dom_distiller/core/viewer.h"
15 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h"
16 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
17 #include "ui/gfx/geometry/size.h"
19 namespace dom_distiller {
21 DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
22 const GURL& url,
23 const DistillationFinishedCallback& callback)
24 : DomDistillerRequestViewBase(
25 new DistilledPagePrefs(browser_state->GetPrefs())),
26 url_(url),
27 callback_(callback) {
28 DCHECK(browser_state);
29 DCHECK(url.is_valid());
30 dom_distiller::DomDistillerService* distillerService =
31 dom_distiller::DomDistillerServiceFactory::GetForBrowserState(
32 browser_state);
34 scoped_ptr<ViewerHandle> viewer_handle = distillerService->ViewUrl(
35 this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url);
37 TakeViewerHandle(viewer_handle.Pass());
40 DistillerViewer::~DistillerViewer() {
43 void DistillerViewer::OnArticleReady(
44 const dom_distiller::DistilledArticleProto* article_proto) {
45 DomDistillerRequestViewBase::OnArticleReady(article_proto);
47 const std::string html = viewer::GetUnsafeArticleTemplateHtml(
48 url_.spec(), distilled_page_prefs_->GetTheme(),
49 distilled_page_prefs_->GetFontFamily());
51 std::string html_and_script(html);
52 html_and_script +=
53 "<script> distiller_on_ios = true; " + js_buffer_ + "</script>";
54 callback_.Run(url_, html_and_script);
57 void DistillerViewer::SendJavaScript(const std::string& buffer) {
58 js_buffer_ += buffer;
61 } // namespace dom_distiller