| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace dom_distiller { | 28 namespace dom_distiller { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // JS Themes. Must agree with useTheme() in dom_distiller_viewer.js. | 32 // JS Themes. Must agree with useTheme() in dom_distiller_viewer.js. |
| 33 const char kDarkJsTheme[] = "dark"; | 33 const char kDarkJsTheme[] = "dark"; |
| 34 const char kLightJsTheme[] = "light"; | 34 const char kLightJsTheme[] = "light"; |
| 35 const char kSepiaJsTheme[] = "sepia"; | 35 const char kSepiaJsTheme[] = "sepia"; |
| 36 | 36 |
| 37 // CSS classes. Must agree with classes in distilledpage.css. | 37 // CSS Theme classes. Must agree with classes in distilledpage.css. |
| 38 const char kDarkCssClass[] = "dark"; | 38 const char kDarkCssClass[] = "dark"; |
| 39 const char kLightCssClass[] = "light"; | 39 const char kLightCssClass[] = "light"; |
| 40 const char kSepiaCssClass[] = "sepia"; | 40 const char kSepiaCssClass[] = "sepia"; |
| 41 | 41 |
| 42 // JS FontFamilies. Must agree with useFontFamily() in dom_distiller_viewer.js. |
| 43 const char kSerifJsFontFamily[] = "serif"; |
| 44 const char kSansSerifJsFontFamily[] = "sans-serif"; |
| 45 const char kMonospaceJsFontFamily[] = "monospace"; |
| 46 |
| 47 // CSS FontFamily classes. Must agree with classes in distilledpage.css. |
| 48 const char kSerifCssClass[] = "serif"; |
| 49 const char kSansSerifCssClass[] = "sans-serif"; |
| 50 const char kMonospaceCssClass[] = "monospace"; |
| 51 |
| 42 // Maps themes to JS themes. | 52 // Maps themes to JS themes. |
| 43 const std::string GetJsTheme(DistilledPagePrefs::Theme theme) { | 53 const std::string GetJsTheme(DistilledPagePrefs::Theme theme) { |
| 44 if (theme == DistilledPagePrefs::DARK) { | 54 if (theme == DistilledPagePrefs::DARK) { |
| 45 return kDarkJsTheme; | 55 return kDarkJsTheme; |
| 46 } else if (theme == DistilledPagePrefs::SEPIA) { | 56 } else if (theme == DistilledPagePrefs::SEPIA) { |
| 47 return kSepiaJsTheme; | 57 return kSepiaJsTheme; |
| 48 } | 58 } |
| 49 return kLightJsTheme; | 59 return kLightJsTheme; |
| 50 } | 60 } |
| 51 | 61 |
| 52 // Maps themes to CSS classes. | 62 // Maps themes to CSS classes. |
| 53 const std::string GetCssClass(DistilledPagePrefs::Theme theme) { | 63 const std::string GetThemeCssClass(DistilledPagePrefs::Theme theme) { |
| 54 if (theme == DistilledPagePrefs::DARK) { | 64 if (theme == DistilledPagePrefs::DARK) { |
| 55 return kDarkCssClass; | 65 return kDarkCssClass; |
| 56 } else if (theme == DistilledPagePrefs::SEPIA) { | 66 } else if (theme == DistilledPagePrefs::SEPIA) { |
| 57 return kSepiaCssClass; | 67 return kSepiaCssClass; |
| 58 } | 68 } |
| 59 return kLightCssClass; | 69 return kLightCssClass; |
| 60 } | 70 } |
| 61 | 71 |
| 72 // Maps font families to JS font families. |
| 73 const std::string GetJsFontFamily(DistilledPagePrefs::FontFamily font_family) { |
| 74 if (font_family == DistilledPagePrefs::SERIF) { |
| 75 return kSerifJsFontFamily; |
| 76 } else if (font_family == DistilledPagePrefs::MONOSPACE) { |
| 77 return kMonospaceJsFontFamily; |
| 78 } |
| 79 return kSansSerifJsFontFamily; |
| 80 } |
| 81 |
| 82 // Maps fontFamilies to CSS fontFamily classes. |
| 83 const std::string GetFontCssClass(DistilledPagePrefs::FontFamily font_family) { |
| 84 if (font_family == DistilledPagePrefs::SERIF) { |
| 85 return kSerifCssClass; |
| 86 } else if (font_family == DistilledPagePrefs::MONOSPACE) { |
| 87 return kMonospaceCssClass; |
| 88 } |
| 89 return kSansSerifCssClass; |
| 90 } |
| 91 |
| 62 std::string ReplaceHtmlTemplateValues( | 92 std::string ReplaceHtmlTemplateValues( |
| 63 const std::string& title, | 93 const std::string& title, |
| 64 const std::string& content, | 94 const std::string& content, |
| 65 const std::string& loading_indicator_class, | 95 const std::string& loading_indicator_class, |
| 66 const std::string& original_url, | 96 const std::string& original_url, |
| 67 const DistilledPagePrefs::Theme theme) { | 97 const DistilledPagePrefs::Theme theme, |
| 98 const DistilledPagePrefs::FontFamily font_family) { |
| 68 base::StringPiece html_template = | 99 base::StringPiece html_template = |
| 69 ResourceBundle::GetSharedInstance().GetRawDataResource( | 100 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 70 IDR_DOM_DISTILLER_VIEWER_HTML); | 101 IDR_DOM_DISTILLER_VIEWER_HTML); |
| 71 std::vector<std::string> substitutions; | 102 std::vector<std::string> substitutions; |
| 72 substitutions.push_back(title); // $1 | 103 substitutions.push_back(title); // $1 |
| 73 substitutions.push_back(kViewerCssPath); // $2 | 104 substitutions.push_back(kViewerCssPath); // $2 |
| 74 substitutions.push_back(kViewerJsPath); // $3 | 105 substitutions.push_back(kViewerJsPath); // $3 |
| 75 substitutions.push_back(GetCssClass(theme)); // $4 | 106 substitutions.push_back(GetThemeCssClass(theme) + " " + |
| 107 GetFontCssClass(font_family)); // $4 |
| 76 substitutions.push_back(content); // $5 | 108 substitutions.push_back(content); // $5 |
| 77 substitutions.push_back(loading_indicator_class); // $6 | 109 substitutions.push_back(loading_indicator_class); // $6 |
| 78 substitutions.push_back( | 110 substitutions.push_back( |
| 79 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 | 111 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 |
| 80 substitutions.push_back(original_url); // $8 | 112 substitutions.push_back(original_url); // $8 |
| 81 substitutions.push_back( | 113 substitutions.push_back( |
| 82 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 | 114 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 |
| 83 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 115 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
| 84 } | 116 } |
| 85 | 117 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 102 | 134 |
| 103 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { | 135 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { |
| 104 if (is_last_page) | 136 if (is_last_page) |
| 105 return "showLoadingIndicator(true);"; | 137 return "showLoadingIndicator(true);"; |
| 106 else | 138 else |
| 107 return "showLoadingIndicator(false);"; | 139 return "showLoadingIndicator(false);"; |
| 108 } | 140 } |
| 109 | 141 |
| 110 const std::string GetUnsafePartialArticleHtml( | 142 const std::string GetUnsafePartialArticleHtml( |
| 111 const DistilledPageProto* page_proto, | 143 const DistilledPageProto* page_proto, |
| 112 const DistilledPagePrefs::Theme theme) { | 144 const DistilledPagePrefs::Theme theme, |
| 145 const DistilledPagePrefs::FontFamily font_family) { |
| 113 DCHECK(page_proto); | 146 DCHECK(page_proto); |
| 114 std::string title = net::EscapeForHTML(page_proto->title()); | 147 std::string title = net::EscapeForHTML(page_proto->title()); |
| 115 std::ostringstream unsafe_output_stream; | 148 std::ostringstream unsafe_output_stream; |
| 116 unsafe_output_stream << page_proto->html(); | 149 unsafe_output_stream << page_proto->html(); |
| 117 std::string unsafe_article_html = unsafe_output_stream.str(); | 150 std::string unsafe_article_html = unsafe_output_stream.str(); |
| 118 std::string original_url = page_proto->url(); | 151 std::string original_url = page_proto->url(); |
| 119 return ReplaceHtmlTemplateValues(title, | 152 return ReplaceHtmlTemplateValues(title, |
| 120 unsafe_article_html, | 153 unsafe_article_html, |
| 121 "visible", | 154 "visible", |
| 122 original_url, | 155 original_url, |
| 123 theme); | 156 theme, |
| 157 font_family); |
| 124 } | 158 } |
| 125 | 159 |
| 126 const std::string GetUnsafeArticleHtml( | 160 const std::string GetUnsafeArticleHtml( |
| 127 const DistilledArticleProto* article_proto, | 161 const DistilledArticleProto* article_proto, |
| 128 const DistilledPagePrefs::Theme theme) { | 162 const DistilledPagePrefs::Theme theme, |
| 163 const DistilledPagePrefs::FontFamily font_family) { |
| 129 DCHECK(article_proto); | 164 DCHECK(article_proto); |
| 130 std::string title; | 165 std::string title; |
| 131 std::string unsafe_article_html; | 166 std::string unsafe_article_html; |
| 132 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 167 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
| 133 article_proto->pages(0).has_html()) { | 168 article_proto->pages(0).has_html()) { |
| 134 title = net::EscapeForHTML(article_proto->title()); | 169 title = net::EscapeForHTML(article_proto->title()); |
| 135 std::ostringstream unsafe_output_stream; | 170 std::ostringstream unsafe_output_stream; |
| 136 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 171 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
| 137 unsafe_output_stream << article_proto->pages(page_num).html(); | 172 unsafe_output_stream << article_proto->pages(page_num).html(); |
| 138 } | 173 } |
| 139 unsafe_article_html = unsafe_output_stream.str(); | 174 unsafe_article_html = unsafe_output_stream.str(); |
| 140 } else { | 175 } else { |
| 141 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 176 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
| 142 unsafe_article_html = | 177 unsafe_article_html = |
| 143 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 178 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 144 } | 179 } |
| 145 | 180 |
| 146 std::string original_url; | 181 std::string original_url; |
| 147 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 182 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
| 148 original_url = article_proto->pages(0).url(); | 183 original_url = article_proto->pages(0).url(); |
| 149 } | 184 } |
| 150 | 185 |
| 151 return ReplaceHtmlTemplateValues(title, | 186 return ReplaceHtmlTemplateValues(title, |
| 152 unsafe_article_html, | 187 unsafe_article_html, |
| 153 "hidden", | 188 "hidden", |
| 154 original_url, | 189 original_url, |
| 155 theme); | 190 theme, |
| 191 font_family); |
| 156 } | 192 } |
| 157 | 193 |
| 158 const std::string GetErrorPageHtml(const DistilledPagePrefs::Theme theme) { | 194 const std::string GetErrorPageHtml( |
| 195 const DistilledPagePrefs::Theme theme, |
| 196 const DistilledPagePrefs::FontFamily font_family) { |
| 159 std::string title = l10n_util::GetStringUTF8( | 197 std::string title = l10n_util::GetStringUTF8( |
| 160 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 198 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
| 161 std::string content = l10n_util::GetStringUTF8( | 199 std::string content = l10n_util::GetStringUTF8( |
| 162 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 200 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
| 163 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme); | 201 return ReplaceHtmlTemplateValues( |
| 202 title, content, "hidden", "", theme, font_family); |
| 164 } | 203 } |
| 165 | 204 |
| 166 const std::string GetCss() { | 205 const std::string GetCss() { |
| 167 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 206 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 168 IDR_DISTILLER_CSS).as_string(); | 207 IDR_DISTILLER_CSS).as_string(); |
| 169 } | 208 } |
| 170 | 209 |
| 171 const std::string GetJavaScript() { | 210 const std::string GetJavaScript() { |
| 172 return ResourceBundle::GetSharedInstance() | 211 return ResourceBundle::GetSharedInstance() |
| 173 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 212 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 } | 247 } |
| 209 | 248 |
| 210 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 249 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
| 211 return scoped_ptr<ViewerHandle>(); | 250 return scoped_ptr<ViewerHandle>(); |
| 212 } | 251 } |
| 213 | 252 |
| 214 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { | 253 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { |
| 215 return "useTheme('" + GetJsTheme(theme) + "');"; | 254 return "useTheme('" + GetJsTheme(theme) + "');"; |
| 216 } | 255 } |
| 217 | 256 |
| 257 const std::string GetDistilledPageFontFamilyJs( |
| 258 DistilledPagePrefs::FontFamily font_family) { |
| 259 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; |
| 260 } |
| 261 |
| 218 } // namespace viewer | 262 } // namespace viewer |
| 219 | 263 |
| 220 } // namespace dom_distiller | 264 } // namespace dom_distiller |
| OLD | NEW |