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(original_url); // $7 | 110 substitutions.push_back(original_url); // $7 |
79 substitutions.push_back( | 111 substitutions.push_back( |
80 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $8 | 112 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $8 |
81 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 113 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
82 } | 114 } |
83 | 115 |
84 } // namespace | 116 } // namespace |
85 | 117 |
(...skipping 14 matching lines...) Expand all Loading... |
100 | 132 |
101 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { | 133 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { |
102 if (is_last_page) | 134 if (is_last_page) |
103 return "showLoadingIndicator(true);"; | 135 return "showLoadingIndicator(true);"; |
104 else | 136 else |
105 return "showLoadingIndicator(false);"; | 137 return "showLoadingIndicator(false);"; |
106 } | 138 } |
107 | 139 |
108 const std::string GetUnsafePartialArticleHtml( | 140 const std::string GetUnsafePartialArticleHtml( |
109 const DistilledPageProto* page_proto, | 141 const DistilledPageProto* page_proto, |
110 const DistilledPagePrefs::Theme theme) { | 142 const DistilledPagePrefs::Theme theme, |
| 143 const DistilledPagePrefs::FontFamily font_family) { |
111 DCHECK(page_proto); | 144 DCHECK(page_proto); |
112 std::string title = net::EscapeForHTML(page_proto->title()); | 145 std::string title = net::EscapeForHTML(page_proto->title()); |
113 std::ostringstream unsafe_output_stream; | 146 std::ostringstream unsafe_output_stream; |
114 unsafe_output_stream << page_proto->html(); | 147 unsafe_output_stream << page_proto->html(); |
115 std::string unsafe_article_html = unsafe_output_stream.str(); | 148 std::string unsafe_article_html = unsafe_output_stream.str(); |
116 std::string original_url = page_proto->url(); | 149 std::string original_url = page_proto->url(); |
117 return ReplaceHtmlTemplateValues(title, | 150 return ReplaceHtmlTemplateValues( |
118 unsafe_article_html, | 151 title, unsafe_article_html, "visible", original_url, theme, font_family); |
119 "visible", | |
120 original_url, | |
121 theme); | |
122 } | 152 } |
123 | 153 |
124 const std::string GetUnsafeArticleHtml( | 154 const std::string GetUnsafeArticleHtml( |
125 const DistilledArticleProto* article_proto, | 155 const DistilledArticleProto* article_proto, |
126 const DistilledPagePrefs::Theme theme) { | 156 const DistilledPagePrefs::Theme theme, |
| 157 const DistilledPagePrefs::FontFamily font_family) { |
127 DCHECK(article_proto); | 158 DCHECK(article_proto); |
128 std::string title; | 159 std::string title; |
129 std::string unsafe_article_html; | 160 std::string unsafe_article_html; |
130 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 161 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
131 article_proto->pages(0).has_html()) { | 162 article_proto->pages(0).has_html()) { |
132 title = net::EscapeForHTML(article_proto->title()); | 163 title = net::EscapeForHTML(article_proto->title()); |
133 std::ostringstream unsafe_output_stream; | 164 std::ostringstream unsafe_output_stream; |
134 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 165 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
135 unsafe_output_stream << article_proto->pages(page_num).html(); | 166 unsafe_output_stream << article_proto->pages(page_num).html(); |
136 } | 167 } |
137 unsafe_article_html = unsafe_output_stream.str(); | 168 unsafe_article_html = unsafe_output_stream.str(); |
138 } else { | 169 } else { |
139 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 170 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
140 unsafe_article_html = | 171 unsafe_article_html = |
141 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 172 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
142 } | 173 } |
143 | 174 |
144 std::string original_url; | 175 std::string original_url; |
145 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 176 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
146 original_url = article_proto->pages(0).url(); | 177 original_url = article_proto->pages(0).url(); |
147 } | 178 } |
148 | 179 |
149 return ReplaceHtmlTemplateValues(title, | 180 return ReplaceHtmlTemplateValues( |
150 unsafe_article_html, | 181 title, unsafe_article_html, "hidden", original_url, theme, font_family); |
151 "hidden", | |
152 original_url, | |
153 theme); | |
154 } | 182 } |
155 | 183 |
156 const std::string GetErrorPageHtml(const DistilledPagePrefs::Theme theme) { | 184 const std::string GetErrorPageHtml( |
| 185 const DistilledPagePrefs::Theme theme, |
| 186 const DistilledPagePrefs::FontFamily font_family) { |
157 std::string title = l10n_util::GetStringUTF8( | 187 std::string title = l10n_util::GetStringUTF8( |
158 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 188 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
159 std::string content = l10n_util::GetStringUTF8( | 189 std::string content = l10n_util::GetStringUTF8( |
160 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 190 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
161 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme); | 191 return ReplaceHtmlTemplateValues( |
| 192 title, content, "hidden", "", theme, font_family); |
162 } | 193 } |
163 | 194 |
164 const std::string GetCss() { | 195 const std::string GetCss() { |
165 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 196 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
166 IDR_DISTILLER_CSS).as_string(); | 197 IDR_DISTILLER_CSS).as_string(); |
167 } | 198 } |
168 | 199 |
169 const std::string GetJavaScript() { | 200 const std::string GetJavaScript() { |
170 return ResourceBundle::GetSharedInstance() | 201 return ResourceBundle::GetSharedInstance() |
171 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 202 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 237 } |
207 | 238 |
208 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 239 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
209 return scoped_ptr<ViewerHandle>(); | 240 return scoped_ptr<ViewerHandle>(); |
210 } | 241 } |
211 | 242 |
212 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { | 243 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { |
213 return "useTheme('" + GetJsTheme(theme) + "');"; | 244 return "useTheme('" + GetJsTheme(theme) + "');"; |
214 } | 245 } |
215 | 246 |
| 247 const std::string GetDistilledPageFontFamilyJs( |
| 248 DistilledPagePrefs::FontFamily font_family) { |
| 249 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; |
| 250 } |
| 251 |
216 } // namespace viewer | 252 } // namespace viewer |
217 | 253 |
218 } // namespace dom_distiller | 254 } // namespace dom_distiller |
OLD | NEW |