| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 void EnsureNonEmptyContent(std::string* content) { | 98 void EnsureNonEmptyContent(std::string* content) { |
| 99 UMA_HISTOGRAM_BOOLEAN("DomDistiller.PageHasDistilledData", !content->empty()); | 99 UMA_HISTOGRAM_BOOLEAN("DomDistiller.PageHasDistilledData", !content->empty()); |
| 100 if (content->empty()) { | 100 if (content->empty()) { |
| 101 *content = l10n_util::GetStringUTF8( | 101 *content = l10n_util::GetStringUTF8( |
| 102 IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 102 IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 std::string ReplaceHtmlTemplateValues( | 106 std::string ReplaceHtmlTemplateValues( |
| 107 const std::string& title, | |
| 108 const std::string& textDirection, | |
| 109 const std::string& loading_indicator_class, | |
| 110 const std::string& original_url, | 107 const std::string& original_url, |
| 111 const DistilledPagePrefs::Theme theme, | 108 const DistilledPagePrefs::Theme theme, |
| 112 const DistilledPagePrefs::FontFamily font_family) { | 109 const DistilledPagePrefs::FontFamily font_family) { |
| 113 base::StringPiece html_template = | 110 base::StringPiece html_template = |
| 114 ResourceBundle::GetSharedInstance().GetRawDataResource( | 111 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 115 IDR_DOM_DISTILLER_VIEWER_HTML); | 112 IDR_DOM_DISTILLER_VIEWER_HTML); |
| 116 // TODO(mdjones): Many or all of these substitutions can be placed on the | 113 // TODO(mdjones): Many or all of these substitutions can be placed on the |
| 117 // page via JavaScript. | 114 // page via JavaScript. |
| 118 std::vector<std::string> substitutions; | 115 std::vector<std::string> substitutions; |
| 119 substitutions.push_back(title); // $1 | |
| 120 | 116 |
| 121 std::ostringstream css; | 117 std::ostringstream css; |
| 122 std::ostringstream script; | 118 std::ostringstream script; |
| 123 #if defined(OS_IOS) | 119 #if defined(OS_IOS) |
| 124 // On iOS the content is inlined as there is no API to detect those requests | 120 // On iOS the content is inlined as there is no API to detect those requests |
| 125 // and return the local data once a page is loaded. | 121 // and return the local data once a page is loaded. |
| 126 css << "<style>" << viewer::GetCss() << "</style>"; | 122 css << "<style>" << viewer::GetCss() << "</style>"; |
| 127 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>"; | 123 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>"; |
| 128 #else | 124 #else |
| 129 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">"; | 125 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">"; |
| 130 script << "<script src=\"" << kViewerJsPath << "\"></script>"; | 126 script << "<script src=\"" << kViewerJsPath << "\"></script>"; |
| 131 #endif // defined(OS_IOS) | 127 #endif // defined(OS_IOS) |
| 132 substitutions.push_back(css.str()); // $2 | 128 substitutions.push_back(css.str()); // $1 |
| 133 substitutions.push_back(script.str()); // $3 | 129 substitutions.push_back(script.str()); // $2 |
| 134 | 130 |
| 135 substitutions.push_back(GetThemeCssClass(theme) + " " + | 131 substitutions.push_back(GetThemeCssClass(theme) + " " + |
| 136 GetFontCssClass(font_family)); // $4 | 132 GetFontCssClass(font_family)); // $3 |
| 137 substitutions.push_back(loading_indicator_class); // $5 | 133 substitutions.push_back(original_url); // $4 |
| 138 substitutions.push_back(original_url); // $6 | |
| 139 substitutions.push_back( | 134 substitutions.push_back( |
| 140 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $7 | 135 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $5 |
| 141 substitutions.push_back(textDirection); // $8 | |
| 142 | 136 |
| 143 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 137 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
| 144 } | 138 } |
| 145 | 139 |
| 146 } // namespace | 140 } // namespace |
| 147 | 141 |
| 148 namespace viewer { | 142 namespace viewer { |
| 149 | 143 |
| 150 const std::string GetShowFeedbackFormJs() { | 144 const std::string GetShowFeedbackFormJs() { |
| 151 base::StringValue question_val( | 145 base::StringValue question_val( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 const std::string GetErrorPageJs() { | 177 const std::string GetErrorPageJs() { |
| 184 base::StringValue value(l10n_util::GetStringUTF8( | 178 base::StringValue value(l10n_util::GetStringUTF8( |
| 185 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)); | 179 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)); |
| 186 std::string output; | 180 std::string output; |
| 187 base::JSONWriter::Write(&value, &output); | 181 base::JSONWriter::Write(&value, &output); |
| 188 std::string page_update("addToPage("); | 182 std::string page_update("addToPage("); |
| 189 page_update += output + ");"; | 183 page_update += output + ");"; |
| 190 return page_update; | 184 return page_update; |
| 191 } | 185 } |
| 192 | 186 |
| 187 const std::string GetSetTitleJs(std::string& title) { |
| 188 EnsureNonEmptyTitle(&title); |
| 189 base::StringValue value(title); |
| 190 std::string output; |
| 191 base::JSONWriter::Write(&value, &output); |
| 192 return "setTitle(" + output + ");"; |
| 193 } |
| 194 |
| 195 const std::string GetSetTextDirectionJs(const std::string& direction) { |
| 196 base::StringValue value(direction); |
| 197 std::string output; |
| 198 base::JSONWriter::Write(&value, &output); |
| 199 return "setTextDirection(" + output + ");"; |
| 200 } |
| 201 |
| 193 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { | 202 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { |
| 194 if (is_last_page) | 203 if (is_last_page) |
| 195 return "showLoadingIndicator(true);"; | 204 return "showLoadingIndicator(true);"; |
| 196 else | 205 else |
| 197 return "showLoadingIndicator(false);"; | 206 return "showLoadingIndicator(false);"; |
| 198 } | 207 } |
| 199 | 208 |
| 200 const std::string GetUnsafeArticleTemplateHtml( | 209 const std::string GetUnsafeArticleTemplateHtml( |
| 201 const DistilledPageProto* page_proto, | 210 const std::string original_url, |
| 202 const DistilledPagePrefs::Theme theme, | 211 const DistilledPagePrefs::Theme theme, |
| 203 const DistilledPagePrefs::FontFamily font_family) { | 212 const DistilledPagePrefs::FontFamily font_family) { |
| 204 DCHECK(page_proto); | 213 return ReplaceHtmlTemplateValues(original_url, theme, font_family); |
| 205 | |
| 206 std::string title = net::EscapeForHTML(page_proto->title()); | |
| 207 | |
| 208 EnsureNonEmptyTitle(&title); | |
| 209 | |
| 210 std::string text_direction = page_proto->text_direction(); | |
| 211 std::string original_url = page_proto->url(); | |
| 212 | |
| 213 return ReplaceHtmlTemplateValues(title, text_direction, "hidden", | |
| 214 original_url, theme, font_family); | |
| 215 } | 214 } |
| 216 | 215 |
| 217 const std::string GetUnsafeArticleContentJs( | 216 const std::string GetUnsafeArticleContentJs( |
| 218 const DistilledArticleProto* article_proto) { | 217 const DistilledArticleProto* article_proto) { |
| 219 DCHECK(article_proto); | 218 DCHECK(article_proto); |
| 220 std::ostringstream unsafe_output_stream; | 219 std::ostringstream unsafe_output_stream; |
| 221 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_html()) { | 220 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_html()) { |
| 222 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 221 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
| 223 unsafe_output_stream << article_proto->pages(page_num).html(); | 222 unsafe_output_stream << article_proto->pages(page_num).html(); |
| 224 } | 223 } |
| 225 } | 224 } |
| 226 | 225 |
| 227 std::string output(unsafe_output_stream.str()); | 226 std::string output(unsafe_output_stream.str()); |
| 228 EnsureNonEmptyContent(&output); | 227 EnsureNonEmptyContent(&output); |
| 229 base::StringValue value(output); | 228 base::StringValue value(output); |
| 230 base::JSONWriter::Write(&value, &output); | 229 base::JSONWriter::Write(&value, &output); |
| 231 std::string page_update("addToPage("); | 230 std::string page_update("addToPage("); |
| 232 page_update += output + ");"; | 231 page_update += output + ");"; |
| 233 return page_update + GetToggleLoadingIndicatorJs(true); | 232 return page_update + GetToggleLoadingIndicatorJs(true); |
| 234 } | 233 } |
| 235 | 234 |
| 236 const std::string GetErrorPageHtml( | |
| 237 const DistilledPagePrefs::Theme theme, | |
| 238 const DistilledPagePrefs::FontFamily font_family) { | |
| 239 std::string title = l10n_util::GetStringUTF8( | |
| 240 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | |
| 241 return ReplaceHtmlTemplateValues(title, "auto", "hidden", "", theme, | |
| 242 font_family); | |
| 243 } | |
| 244 | |
| 245 const std::string GetCss() { | 235 const std::string GetCss() { |
| 246 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 236 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 247 IDR_DISTILLER_CSS).as_string(); | 237 IDR_DISTILLER_CSS).as_string(); |
| 248 } | 238 } |
| 249 | 239 |
| 250 const std::string GetJavaScript() { | 240 const std::string GetJavaScript() { |
| 251 return ResourceBundle::GetSharedInstance() | 241 return ResourceBundle::GetSharedInstance() |
| 252 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 242 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
| 253 .as_string(); | 243 .as_string(); |
| 254 } | 244 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 285 } |
| 296 | 286 |
| 297 const std::string GetDistilledPageFontFamilyJs( | 287 const std::string GetDistilledPageFontFamilyJs( |
| 298 DistilledPagePrefs::FontFamily font_family) { | 288 DistilledPagePrefs::FontFamily font_family) { |
| 299 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; | 289 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; |
| 300 } | 290 } |
| 301 | 291 |
| 302 } // namespace viewer | 292 } // namespace viewer |
| 303 | 293 |
| 304 } // namespace dom_distiller | 294 } // namespace dom_distiller |
| OLD | NEW |