1 // Copyright 2014 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/page_distiller.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "components/dom_distiller/core/distiller_page.h"
14 #include "components/dom_distiller/core/distiller_url_fetcher.h"
15 #include "grit/component_resources.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "ui/base/resource/resource_bundle.h"
20 namespace dom_distiller {
22 DistilledPageInfo::DistilledPageInfo() {}
24 DistilledPageInfo::~DistilledPageInfo() {}
26 PageDistiller::PageDistiller(const DistillerPageFactory& distiller_page_factory)
27 : distiller_page_(distiller_page_factory.CreateDistillerPage(this).Pass()) {
30 PageDistiller::~PageDistiller() {}
32 void PageDistiller::Init() { distiller_page_->Init(); }
34 void PageDistiller::DistillPage(const GURL& url,
35 const PageDistillerCallback& callback) {
36 page_distiller_callback_ = callback;
40 void PageDistiller::LoadURL(const GURL& url) { distiller_page_->LoadURL(url); }
42 void PageDistiller::OnLoadURLDone() { GetDistilledContent(); }
44 void PageDistiller::GetDistilledContent() {
45 std::string script = ResourceBundle::GetSharedInstance()
46 .GetRawDataResource(IDR_DISTILLER_JS)
48 distiller_page_->ExecuteJavaScript(script);
51 void PageDistiller::OnExecuteJavaScriptDone(const GURL& page_url,
52 const base::Value* value) {
53 scoped_ptr<DistilledPageInfo> page_info(new DistilledPageInfo());
55 const base::ListValue* result_list = NULL;
56 if (!value->GetAsList(&result_list)) {
57 base::MessageLoop::current()->PostTask(
59 base::Bind(page_distiller_callback_, base::Passed(&page_info), false));
62 for (base::ListValue::const_iterator iter = result_list->begin();
63 iter != result_list->end();
66 (*iter)->GetAsString(&item);
67 // The JavaScript returns an array where the first element is the title,
68 // the second element is the article content HTML, and the remaining
69 // elements are image URLs referenced in the HTML.
72 page_info->title = item;
75 page_info->html = item;
78 page_info->next_page_url = item;
81 page_info->prev_page_url = item;
84 page_info->image_urls.push_back(item);
87 base::MessageLoop::current()->PostTask(
89 base::Bind(page_distiller_callback_, base::Passed(&page_info), true));
93 } // namespace dom_distiller