OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/viewer.h" | 5 #include "components/dom_distiller/core/viewer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // Maps fontFamilies to CSS fontFamily classes. | 82 // Maps fontFamilies to CSS fontFamily classes. |
83 const std::string GetFontCssClass(DistilledPagePrefs::FontFamily font_family) { | 83 const std::string GetFontCssClass(DistilledPagePrefs::FontFamily font_family) { |
84 if (font_family == DistilledPagePrefs::SERIF) { | 84 if (font_family == DistilledPagePrefs::SERIF) { |
85 return kSerifCssClass; | 85 return kSerifCssClass; |
86 } else if (font_family == DistilledPagePrefs::MONOSPACE) { | 86 } else if (font_family == DistilledPagePrefs::MONOSPACE) { |
87 return kMonospaceCssClass; | 87 return kMonospaceCssClass; |
88 } | 88 } |
89 return kSansSerifCssClass; | 89 return kSansSerifCssClass; |
90 } | 90 } |
91 | 91 |
| 92 void EnsureNonEmptyTitleAndContent(std::string* title, std::string* content) { |
| 93 if (title->empty()) |
| 94 *title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
| 95 if (content->empty()) { |
| 96 *content = l10n_util::GetStringUTF8( |
| 97 IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 98 } |
| 99 } |
| 100 |
92 std::string ReplaceHtmlTemplateValues( | 101 std::string ReplaceHtmlTemplateValues( |
93 const std::string& title, | 102 const std::string& title, |
94 const std::string& content, | 103 const std::string& content, |
95 const std::string& loading_indicator_class, | 104 const std::string& loading_indicator_class, |
96 const std::string& original_url, | 105 const std::string& original_url, |
97 const DistilledPagePrefs::Theme theme, | 106 const DistilledPagePrefs::Theme theme, |
98 const DistilledPagePrefs::FontFamily font_family) { | 107 const DistilledPagePrefs::FontFamily font_family) { |
99 base::StringPiece html_template = | 108 base::StringPiece html_template = |
100 ResourceBundle::GetSharedInstance().GetRawDataResource( | 109 ResourceBundle::GetSharedInstance().GetRawDataResource( |
101 IDR_DOM_DISTILLER_VIEWER_HTML); | 110 IDR_DOM_DISTILLER_VIEWER_HTML); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 148 |
140 const std::string GetUnsafePartialArticleHtml( | 149 const std::string GetUnsafePartialArticleHtml( |
141 const DistilledPageProto* page_proto, | 150 const DistilledPageProto* page_proto, |
142 const DistilledPagePrefs::Theme theme, | 151 const DistilledPagePrefs::Theme theme, |
143 const DistilledPagePrefs::FontFamily font_family) { | 152 const DistilledPagePrefs::FontFamily font_family) { |
144 DCHECK(page_proto); | 153 DCHECK(page_proto); |
145 std::string title = net::EscapeForHTML(page_proto->title()); | 154 std::string title = net::EscapeForHTML(page_proto->title()); |
146 std::ostringstream unsafe_output_stream; | 155 std::ostringstream unsafe_output_stream; |
147 unsafe_output_stream << page_proto->html(); | 156 unsafe_output_stream << page_proto->html(); |
148 std::string unsafe_article_html = unsafe_output_stream.str(); | 157 std::string unsafe_article_html = unsafe_output_stream.str(); |
| 158 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); |
149 std::string original_url = page_proto->url(); | 159 std::string original_url = page_proto->url(); |
150 return ReplaceHtmlTemplateValues( | 160 return ReplaceHtmlTemplateValues( |
151 title, unsafe_article_html, "visible", original_url, theme, font_family); | 161 title, unsafe_article_html, "visible", original_url, theme, font_family); |
152 } | 162 } |
153 | 163 |
154 const std::string GetUnsafeArticleHtml( | 164 const std::string GetUnsafeArticleHtml( |
155 const DistilledArticleProto* article_proto, | 165 const DistilledArticleProto* article_proto, |
156 const DistilledPagePrefs::Theme theme, | 166 const DistilledPagePrefs::Theme theme, |
157 const DistilledPagePrefs::FontFamily font_family) { | 167 const DistilledPagePrefs::FontFamily font_family) { |
158 DCHECK(article_proto); | 168 DCHECK(article_proto); |
159 std::string title; | 169 std::string title; |
160 std::string unsafe_article_html; | 170 std::string unsafe_article_html; |
161 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 171 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
162 article_proto->pages(0).has_html()) { | 172 article_proto->pages(0).has_html()) { |
163 title = net::EscapeForHTML(article_proto->title()); | 173 title = net::EscapeForHTML(article_proto->title()); |
164 std::ostringstream unsafe_output_stream; | 174 std::ostringstream unsafe_output_stream; |
165 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 175 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
166 unsafe_output_stream << article_proto->pages(page_num).html(); | 176 unsafe_output_stream << article_proto->pages(page_num).html(); |
167 } | 177 } |
168 unsafe_article_html = unsafe_output_stream.str(); | 178 unsafe_article_html = unsafe_output_stream.str(); |
169 } else { | |
170 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | |
171 unsafe_article_html = | |
172 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | |
173 } | 179 } |
174 | 180 |
| 181 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); |
| 182 |
175 std::string original_url; | 183 std::string original_url; |
176 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 184 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
177 original_url = article_proto->pages(0).url(); | 185 original_url = article_proto->pages(0).url(); |
178 } | 186 } |
179 | 187 |
180 return ReplaceHtmlTemplateValues( | 188 return ReplaceHtmlTemplateValues( |
181 title, unsafe_article_html, "hidden", original_url, theme, font_family); | 189 title, unsafe_article_html, "hidden", original_url, theme, font_family); |
182 } | 190 } |
183 | 191 |
184 const std::string GetErrorPageHtml( | 192 const std::string GetErrorPageHtml( |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 } | 253 } |
246 | 254 |
247 const std::string GetDistilledPageFontFamilyJs( | 255 const std::string GetDistilledPageFontFamilyJs( |
248 DistilledPagePrefs::FontFamily font_family) { | 256 DistilledPagePrefs::FontFamily font_family) { |
249 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; | 257 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; |
250 } | 258 } |
251 | 259 |
252 } // namespace viewer | 260 } // namespace viewer |
253 | 261 |
254 } // namespace dom_distiller | 262 } // namespace dom_distiller |
OLD | NEW |