OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/core/distiller_page.h" | 5 #include "components/dom_distiller/core/distiller_page.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 DCHECK_NE(std::string::npos, options_offset); | 41 DCHECK_NE(std::string::npos, options_offset); |
42 DCHECK_EQ(std::string::npos, | 42 DCHECK_EQ(std::string::npos, |
43 script.find(kOptionsPlaceholder, options_offset + 1)); | 43 script.find(kOptionsPlaceholder, options_offset + 1)); |
44 script = | 44 script = |
45 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); | 45 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); |
46 return script; | 46 return script; |
47 } | 47 } |
48 | 48 |
49 } | 49 } |
50 | 50 |
51 DistilledPageInfo::DistilledPageInfo() {} | |
52 | |
53 DistilledPageInfo::~DistilledPageInfo() {} | |
54 | |
55 DistilledPageInfo::MarkupArticle::MarkupArticle() {} | |
56 | |
57 DistilledPageInfo::MarkupArticle::~MarkupArticle() {} | |
58 | |
59 DistilledPageInfo::MarkupImage::MarkupImage() {} | |
60 | |
61 DistilledPageInfo::MarkupImage::~MarkupImage() {} | |
62 | |
63 DistilledPageInfo::MarkupInfo::MarkupInfo() {} | |
64 | |
65 DistilledPageInfo::MarkupInfo::~MarkupInfo() {} | |
66 | |
67 DistillerPageFactory::~DistillerPageFactory() {} | 51 DistillerPageFactory::~DistillerPageFactory() {} |
68 | 52 |
69 DistillerPage::DistillerPage() : ready_(true) {} | 53 DistillerPage::DistillerPage() : ready_(true) {} |
70 | 54 |
71 DistillerPage::~DistillerPage() {} | 55 DistillerPage::~DistillerPage() {} |
72 | 56 |
73 void DistillerPage::DistillPage( | 57 void DistillerPage::DistillPage( |
74 const GURL& gurl, | 58 const GURL& gurl, |
75 const dom_distiller::proto::DomDistillerOptions options, | 59 const dom_distiller::proto::DomDistillerOptions options, |
76 const DistillerPageCallback& callback) { | 60 const DistillerPageCallback& callback) { |
77 DCHECK(ready_); | 61 DCHECK(ready_); |
78 // It is only possible to distill one page at a time. |ready_| is reset when | 62 // It is only possible to distill one page at a time. |ready_| is reset when |
79 // the callback to OnDistillationDone happens. | 63 // the callback to OnDistillationDone happens. |
80 ready_ = false; | 64 ready_ = false; |
81 distiller_page_callback_ = callback; | 65 distiller_page_callback_ = callback; |
82 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); | 66 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); |
83 } | 67 } |
84 | 68 |
85 void DistillerPage::OnDistillationDone(const GURL& page_url, | 69 void DistillerPage::OnDistillationDone(const GURL& page_url, |
86 const base::Value* value) { | 70 const base::Value* value) { |
87 DCHECK(!ready_); | 71 DCHECK(!ready_); |
88 ready_ = true; | 72 ready_ = true; |
89 | 73 |
90 scoped_ptr<DistilledPageInfo> page_info(new DistilledPageInfo()); | 74 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( |
91 bool found_content = !value->IsType(base::Value::TYPE_NULL); | 75 new dom_distiller::proto::DomDistillerResult()); |
92 if (found_content) { | 76 bool found_content; |
93 dom_distiller::proto::DomDistillerResult distiller_result = | 77 if (value->IsType(base::Value::TYPE_NULL)) { |
94 dom_distiller::proto::json::DomDistillerResult::ReadFromValue(value); | 78 found_content = false; |
95 | 79 } else { |
96 page_info->title = distiller_result.title(); | 80 found_content = |
97 page_info->html = distiller_result.distilled_content().html(); | 81 dom_distiller::proto::json::DomDistillerResult::ReadFromValue( |
98 page_info->next_page_url = distiller_result.pagination_info().next_page(); | 82 value, distiller_result.get()); |
cjhopman
2014/08/28 21:58:53
If we just use the old version of ReadFromValue he
nyquist
2014/08/28 22:09:15
Done.
| |
99 page_info->prev_page_url = distiller_result.pagination_info().prev_page(); | 83 if (!found_content) |
100 for (int i = 0; i < distiller_result.image_urls_size(); ++i) { | 84 DVLOG(1) << "Unable to parse DomDistillerResult."; |
101 const std::string image_url = distiller_result.image_urls(i); | |
102 if (GURL(image_url).is_valid()) { | |
103 page_info->image_urls.push_back(image_url); | |
104 } | |
105 } | |
106 const dom_distiller::proto::MarkupInfo& src_markup_info = | |
107 distiller_result.markup_info(); | |
108 DistilledPageInfo::MarkupInfo& dst_markup_info = page_info->markup_info; | |
109 dst_markup_info.title = src_markup_info.title(); | |
110 dst_markup_info.type = src_markup_info.type(); | |
111 dst_markup_info.url = src_markup_info.url(); | |
112 dst_markup_info.description = src_markup_info.description(); | |
113 dst_markup_info.publisher = src_markup_info.publisher(); | |
114 dst_markup_info.copyright = src_markup_info.copyright(); | |
115 dst_markup_info.author = src_markup_info.author(); | |
116 | |
117 const dom_distiller::proto::MarkupArticle& src_article = | |
118 src_markup_info.article(); | |
119 DistilledPageInfo::MarkupArticle& dst_article = dst_markup_info.article; | |
120 dst_article.published_time = src_article.published_time(); | |
121 dst_article.modified_time = src_article.modified_time(); | |
122 dst_article.expiration_time = src_article.expiration_time(); | |
123 dst_article.section = src_article.section(); | |
124 for (int i = 0; i < src_article.authors_size(); ++i) { | |
125 dst_article.authors.push_back(src_article.authors(i)); | |
126 } | |
127 | |
128 for (int i = 0; i < src_markup_info.images_size(); ++i) { | |
129 const dom_distiller::proto::MarkupImage& src_image = | |
130 src_markup_info.images(i); | |
131 DistilledPageInfo::MarkupImage dst_image; | |
132 dst_image.url = src_image.url(); | |
133 dst_image.secure_url = src_image.secure_url(); | |
134 dst_image.type = src_image.type(); | |
135 dst_image.caption = src_image.caption(); | |
136 dst_image.width = src_image.width(); | |
137 dst_image.height = src_image.height(); | |
138 dst_markup_info.images.push_back(dst_image); | |
139 } | |
140 } | 85 } |
141 | 86 |
142 base::MessageLoop::current()->PostTask( | 87 base::MessageLoop::current()->PostTask( |
143 FROM_HERE, | 88 FROM_HERE, |
144 base::Bind( | 89 base::Bind(distiller_page_callback_, |
145 distiller_page_callback_, base::Passed(&page_info), found_content)); | 90 base::Passed(&distiller_result), |
91 found_content)); | |
146 } | 92 } |
147 | 93 |
148 } // namespace dom_distiller | 94 } // namespace dom_distiller |
OLD | NEW |