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( |
120 unsafe_article_html, | 153 title, unsafe_article_html, "visible", original_url, theme, font_family); |
robliao
2014/08/10 02:43:45
Adding just font_family at the end should be okay.
sunangel
2014/08/11 21:51:33
Done but contradicts git cl format. Also done for
| |
121 "visible", | |
122 original_url, | |
123 theme); | |
124 } | 154 } |
125 | 155 |
126 const std::string GetUnsafeArticleHtml( | 156 const std::string GetUnsafeArticleHtml( |
127 const DistilledArticleProto* article_proto, | 157 const DistilledArticleProto* article_proto, |
128 const DistilledPagePrefs::Theme theme) { | 158 const DistilledPagePrefs::Theme theme, |
159 const DistilledPagePrefs::FontFamily font_family) { | |
129 DCHECK(article_proto); | 160 DCHECK(article_proto); |
130 std::string title; | 161 std::string title; |
131 std::string unsafe_article_html; | 162 std::string unsafe_article_html; |
132 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 163 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
133 article_proto->pages(0).has_html()) { | 164 article_proto->pages(0).has_html()) { |
134 title = net::EscapeForHTML(article_proto->title()); | 165 title = net::EscapeForHTML(article_proto->title()); |
135 std::ostringstream unsafe_output_stream; | 166 std::ostringstream unsafe_output_stream; |
136 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 167 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
137 unsafe_output_stream << article_proto->pages(page_num).html(); | 168 unsafe_output_stream << article_proto->pages(page_num).html(); |
138 } | 169 } |
139 unsafe_article_html = unsafe_output_stream.str(); | 170 unsafe_article_html = unsafe_output_stream.str(); |
140 } else { | 171 } else { |
141 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 172 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
142 unsafe_article_html = | 173 unsafe_article_html = |
143 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 174 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
144 } | 175 } |
145 | 176 |
146 std::string original_url; | 177 std::string original_url; |
147 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 178 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
148 original_url = article_proto->pages(0).url(); | 179 original_url = article_proto->pages(0).url(); |
149 } | 180 } |
150 | 181 |
151 return ReplaceHtmlTemplateValues(title, | 182 return ReplaceHtmlTemplateValues( |
152 unsafe_article_html, | 183 title, unsafe_article_html, "hidden", original_url, theme, font_family); |
153 "hidden", | |
154 original_url, | |
155 theme); | |
156 } | 184 } |
157 | 185 |
158 const std::string GetErrorPageHtml(const DistilledPagePrefs::Theme theme) { | 186 const std::string GetErrorPageHtml( |
187 const DistilledPagePrefs::Theme theme, | |
188 const DistilledPagePrefs::FontFamily font_family) { | |
159 std::string title = l10n_util::GetStringUTF8( | 189 std::string title = l10n_util::GetStringUTF8( |
160 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 190 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
161 std::string content = l10n_util::GetStringUTF8( | 191 std::string content = l10n_util::GetStringUTF8( |
162 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 192 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
163 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme); | 193 return ReplaceHtmlTemplateValues( |
194 title, content, "hidden", "", theme, font_family); | |
164 } | 195 } |
165 | 196 |
166 const std::string GetCss() { | 197 const std::string GetCss() { |
167 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 198 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
168 IDR_DISTILLER_CSS).as_string(); | 199 IDR_DISTILLER_CSS).as_string(); |
169 } | 200 } |
170 | 201 |
171 const std::string GetJavaScript() { | 202 const std::string GetJavaScript() { |
172 return ResourceBundle::GetSharedInstance() | 203 return ResourceBundle::GetSharedInstance() |
173 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 204 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 } | 239 } |
209 | 240 |
210 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 241 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
211 return scoped_ptr<ViewerHandle>(); | 242 return scoped_ptr<ViewerHandle>(); |
212 } | 243 } |
213 | 244 |
214 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { | 245 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) { |
215 return "useTheme('" + GetJsTheme(theme) + "');"; | 246 return "useTheme('" + GetJsTheme(theme) + "');"; |
216 } | 247 } |
217 | 248 |
249 const std::string GetDistilledPageFontFamilyJs( | |
250 DistilledPagePrefs::FontFamily font_family) { | |
251 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; | |
252 } | |
253 | |
218 } // namespace viewer | 254 } // namespace viewer |
219 | 255 |
220 } // namespace dom_distiller | 256 } // namespace dom_distiller |
OLD | NEW |