OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/dom_distiller/core/page_distiller.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "components/dom_distiller/core/distiller_page.h" |
| 12 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
| 13 #include "grit/dom_distiller_resources.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace dom_distiller { |
| 19 |
| 20 DistilledPageInfo::DistilledPageInfo() {} |
| 21 |
| 22 DistilledPageInfo::~DistilledPageInfo() {} |
| 23 |
| 24 PageDistiller::PageDistiller(const DistillerPageFactory& distiller_page_factory) |
| 25 : distiller_page_(distiller_page_factory.CreateDistillerPage(this).Pass()) { |
| 26 } |
| 27 |
| 28 PageDistiller::~PageDistiller() {} |
| 29 |
| 30 void PageDistiller::Init() { distiller_page_->Init(); } |
| 31 |
| 32 void PageDistiller::DistillPage(const GURL& url, |
| 33 const PageDistillerCallback& callback) { |
| 34 page_distiller_callback_ = callback; |
| 35 LoadURL(url); |
| 36 } |
| 37 |
| 38 void PageDistiller::LoadURL(const GURL& url) { distiller_page_->LoadURL(url); } |
| 39 |
| 40 void PageDistiller::OnLoadURLDone() { GetDistilledContent(); } |
| 41 |
| 42 void PageDistiller::GetDistilledContent() { |
| 43 std::string script = ResourceBundle::GetSharedInstance() |
| 44 .GetRawDataResource(IDR_DISTILLER_JS) |
| 45 .as_string(); |
| 46 distiller_page_->ExecuteJavaScript(script); |
| 47 } |
| 48 |
| 49 void PageDistiller::OnExecuteJavaScriptDone(const GURL& page_url, |
| 50 const base::Value* value) { |
| 51 DistilledPageInfo page_info; |
| 52 std::string result; |
| 53 const base::ListValue* result_list = NULL; |
| 54 if (!value->GetAsList(&result_list)) { |
| 55 page_distiller_callback_.Run(page_info, false); |
| 56 } else { |
| 57 int i = 0; |
| 58 for (base::ListValue::const_iterator iter = result_list->begin(); |
| 59 iter != result_list->end(); |
| 60 ++iter, ++i) { |
| 61 std::string item; |
| 62 (*iter)->GetAsString(&item); |
| 63 // The JavaScript returns an array where the first element is the title, |
| 64 // the second element is the article content HTML, and the remaining |
| 65 // elements are image URLs referenced in the HTML. |
| 66 switch (i) { |
| 67 case 0: |
| 68 page_info.title = item; |
| 69 break; |
| 70 case 1: |
| 71 page_info.html = item; |
| 72 break; |
| 73 case 2: { |
| 74 page_info.next_page_url = item; |
| 75 break; |
| 76 } |
| 77 default: |
| 78 page_info.image_urls.push_back(item); |
| 79 } |
| 80 } |
| 81 page_distiller_callback_.Run(page_info, true); |
| 82 } |
| 83 } |
| 84 |
| 85 } // namespace dom_distiller |
OLD | NEW |