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