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/core/distiller.h"
10 #include "base/callback.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "components/dom_distiller/core/distiller_page.h"
15 #include "components/dom_distiller/core/distiller_url_fetcher.h"
16 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
17 #include "grit/dom_distiller_resources.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "ui/base/resource/resource_bundle.h"
22 namespace dom_distiller {
24 DistillerFactoryImpl::DistillerFactoryImpl(
25 scoped_ptr<DistillerPageFactory> distiller_page_factory,
26 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory)
27 : distiller_page_factory_(distiller_page_factory.Pass()),
28 distiller_url_fetcher_factory_(distiller_url_fetcher_factory.Pass()) {}
30 DistillerFactoryImpl::~DistillerFactoryImpl() {}
32 scoped_ptr<Distiller> DistillerFactoryImpl::CreateDistiller() {
33 scoped_ptr<DistillerImpl> distiller(new DistillerImpl(
34 *distiller_page_factory_, *distiller_url_fetcher_factory_));
36 return distiller.PassAs<Distiller>();
39 DistillerImpl::DistillerImpl(
40 const DistillerPageFactory& distiller_page_factory,
41 const DistillerURLFetcherFactory& distiller_url_fetcher_factory)
42 : distiller_page_factory_(distiller_page_factory),
43 distiller_url_fetcher_factory_(distiller_url_fetcher_factory) {
44 distiller_page_ = distiller_page_factory_.CreateDistillerPage(this).Pass();
47 DistillerImpl::~DistillerImpl() {
50 void DistillerImpl::Init() {
51 distiller_page_->Init();
54 void DistillerImpl::DistillPage(const GURL& url,
55 const DistillerCallback& distillation_cb) {
56 distillation_cb_ = distillation_cb;
57 proto_.reset(new DistilledPageProto());
58 proto_->set_url(url.spec());
62 void DistillerImpl::LoadURL(const GURL& url) {
63 distiller_page_->LoadURL(url);
66 void DistillerImpl::OnLoadURLDone() {
67 GetDistilledContent();
70 void DistillerImpl::GetDistilledContent() {
72 ResourceBundle::GetSharedInstance().GetRawDataResource(
73 IDR_DISTILLER_JS).as_string();
74 distiller_page_->ExecuteJavaScript(script);
77 void DistillerImpl::OnExecuteJavaScriptDone(const base::Value* value) {
79 bool fetched_image = false;
80 const base::ListValue* result_list = NULL;
81 if (!value->GetAsList(&result_list)) {
83 distillation_cb_.Run(proto_.Pass());
87 for (base::ListValue::const_iterator iter = result_list->begin();
88 iter != result_list->end(); ++iter, ++i) {
90 (*iter)->GetAsString(&item);
91 // The JavaScript returns an array where the first element is the title,
92 // the second element is the article content HTML, and the remaining
93 // elements are image URLs referenced in the HTML.
96 proto_->set_title(item);
99 proto_->set_html(item);
102 int image_number = i - 2;
103 std::string image_id = base::StringPrintf("%d", image_number);
104 FetchImage(image_id, item);
105 fetched_image = true;
109 distillation_cb_.Run(proto_.Pass());
112 void DistillerImpl::FetchImage(const std::string& image_id,
113 const std::string& item) {
114 DistillerURLFetcher* fetcher =
115 distiller_url_fetcher_factory_.CreateDistillerURLFetcher();
116 image_fetchers_[image_id] = fetcher;
117 fetcher->FetchURL(item,
118 base::Bind(&DistillerImpl::OnFetchImageDone,
119 base::Unretained(this), image_id));
122 void DistillerImpl::OnFetchImageDone(const std::string& id,
123 const std::string& response) {
125 DistilledPageProto_Image* image = proto_->add_image();
127 image->set_data(response);
128 DCHECK(image_fetchers_.end() != image_fetchers_.find(id));
129 DistillerURLFetcher* fetcher = image_fetchers_[id];
130 int result = image_fetchers_.erase(id);
132 DCHECK_EQ(1, result);
133 if (image_fetchers_.empty()) {
134 distillation_cb_.Run(proto_.Pass());
138 } // namespace dom_distiller