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/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/dom_distiller_resources.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 namespace dom_distiller { |
| 21 |
| 22 DistilledPageInfo::DistilledPageInfo() {} |
| 23 |
| 24 DistilledPageInfo::~DistilledPageInfo() {} |
| 25 |
| 26 PageDistiller::PageDistiller(const DistillerPageFactory& distiller_page_factory) |
| 27 : distiller_page_(distiller_page_factory.CreateDistillerPage(this).Pass()) { |
| 28 } |
| 29 |
| 30 PageDistiller::~PageDistiller() {} |
| 31 |
| 32 void PageDistiller::Init() { distiller_page_->Init(); } |
| 33 |
| 34 void PageDistiller::DistillPage(const GURL& url, |
| 35 const PageDistillerCallback& callback) { |
| 36 page_distiller_callback_ = callback; |
| 37 LoadURL(url); |
| 38 } |
| 39 |
| 40 void PageDistiller::LoadURL(const GURL& url) { distiller_page_->LoadURL(url); } |
| 41 |
| 42 void PageDistiller::OnLoadURLDone() { GetDistilledContent(); } |
| 43 |
| 44 void PageDistiller::GetDistilledContent() { |
| 45 std::string script = ResourceBundle::GetSharedInstance() |
| 46 .GetRawDataResource(IDR_DISTILLER_JS) |
| 47 .as_string(); |
| 48 distiller_page_->ExecuteJavaScript(script); |
| 49 } |
| 50 |
| 51 void PageDistiller::OnExecuteJavaScriptDone(const GURL& page_url, |
| 52 const base::Value* value) { |
| 53 scoped_ptr<DistilledPageInfo> page_info(new DistilledPageInfo()); |
| 54 std::string result; |
| 55 const base::ListValue* result_list = NULL; |
| 56 if (!value->GetAsList(&result_list)) { |
| 57 base::MessageLoop::current()->PostTask( |
| 58 FROM_HERE, |
| 59 base::Bind(page_distiller_callback_, base::Passed(&page_info), false)); |
| 60 } else { |
| 61 int i = 0; |
| 62 for (base::ListValue::const_iterator iter = result_list->begin(); |
| 63 iter != result_list->end(); |
| 64 ++iter, ++i) { |
| 65 std::string item; |
| 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. |
| 70 switch (i) { |
| 71 case 0: |
| 72 page_info->title = item; |
| 73 break; |
| 74 case 1: |
| 75 page_info->html = item; |
| 76 break; |
| 77 case 2: { |
| 78 page_info->next_page_url = item; |
| 79 break; |
| 80 } |
| 81 default: |
| 82 page_info->image_urls.push_back(item); |
| 83 } |
| 84 } |
| 85 base::MessageLoop::current()->PostTask( |
| 86 FROM_HERE, |
| 87 base::Bind(page_distiller_callback_, base::Passed(&page_info), true)); |
| 88 } |
| 89 } |
| 90 |
| 91 } // namespace dom_distiller |
OLD | NEW |