Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: components/dom_distiller/core/viewer.cc

Issue 1015463004: Consistent content placement method for dom-distiller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert ios code Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/dom_distiller/core/viewer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // Maps fontFamilies to CSS fontFamily classes. 83 // Maps fontFamilies to CSS fontFamily classes.
84 const std::string GetFontCssClass(DistilledPagePrefs::FontFamily font_family) { 84 const std::string GetFontCssClass(DistilledPagePrefs::FontFamily font_family) {
85 if (font_family == DistilledPagePrefs::SERIF) { 85 if (font_family == DistilledPagePrefs::SERIF) {
86 return kSerifCssClass; 86 return kSerifCssClass;
87 } else if (font_family == DistilledPagePrefs::MONOSPACE) { 87 } else if (font_family == DistilledPagePrefs::MONOSPACE) {
88 return kMonospaceCssClass; 88 return kMonospaceCssClass;
89 } 89 }
90 return kSansSerifCssClass; 90 return kSansSerifCssClass;
91 } 91 }
92 92
93 void EnsureNonEmptyTitleAndContent(std::string* title, std::string* content) { 93 void EnsureNonEmptyTitle(std::string* title) {
94 if (title->empty()) 94 if (title->empty())
95 *title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); 95 *title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE);
96 }
97
98 void EnsureNonEmptyContent(std::string* content) {
96 UMA_HISTOGRAM_BOOLEAN("DomDistiller.PageHasDistilledData", !content->empty()); 99 UMA_HISTOGRAM_BOOLEAN("DomDistiller.PageHasDistilledData", !content->empty());
97 if (content->empty()) { 100 if (content->empty()) {
98 *content = l10n_util::GetStringUTF8( 101 *content = l10n_util::GetStringUTF8(
99 IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); 102 IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT);
100 } 103 }
101 } 104 }
102 105
103 std::string ReplaceHtmlTemplateValues( 106 std::string ReplaceHtmlTemplateValues(
104 const std::string& title, 107 const std::string& title,
105 const std::string& textDirection, 108 const std::string& textDirection,
106 const std::string& content,
107 const std::string& loading_indicator_class, 109 const std::string& loading_indicator_class,
108 const std::string& original_url, 110 const std::string& original_url,
109 const DistilledPagePrefs::Theme theme, 111 const DistilledPagePrefs::Theme theme,
110 const DistilledPagePrefs::FontFamily font_family) { 112 const DistilledPagePrefs::FontFamily font_family,
113 const std::string& htmlContent) {
111 base::StringPiece html_template = 114 base::StringPiece html_template =
112 ResourceBundle::GetSharedInstance().GetRawDataResource( 115 ResourceBundle::GetSharedInstance().GetRawDataResource(
113 IDR_DOM_DISTILLER_VIEWER_HTML); 116 IDR_DOM_DISTILLER_VIEWER_HTML);
114 std::vector<std::string> substitutions; 117 std::vector<std::string> substitutions;
115 substitutions.push_back(title); // $1 118 substitutions.push_back(title); // $1
116 119
117 std::ostringstream css; 120 std::ostringstream css;
118 std::ostringstream script; 121 std::ostringstream script;
119 #if defined(OS_IOS) 122 #if defined(OS_IOS)
120 // On iOS the content is inlined as there is no API to detect those requests 123 // On iOS the content is inlined as there is no API to detect those requests
121 // and return the local data once a page is loaded. 124 // and return the local data once a page is loaded.
122 css << "<style>" << viewer::GetCss() << "</style>"; 125 css << "<style>" << viewer::GetCss() << "</style>";
123 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>"; 126 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>";
124 #else 127 #else
125 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">"; 128 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">";
126 script << "<script src=\"" << kViewerJsPath << "\"></script>"; 129 script << "<script src=\"" << kViewerJsPath << "\"></script>";
127 #endif // defined(OS_IOS) 130 #endif // defined(OS_IOS)
128 substitutions.push_back(css.str()); // $2 131 substitutions.push_back(css.str()); // $2
129 substitutions.push_back(script.str()); // $3 132 substitutions.push_back(script.str()); // $3
130 133
131 substitutions.push_back(GetThemeCssClass(theme) + " " + 134 substitutions.push_back(GetThemeCssClass(theme) + " " +
132 GetFontCssClass(font_family)); // $4 135 GetFontCssClass(font_family)); // $4
133 substitutions.push_back(content); // $5 136 substitutions.push_back(loading_indicator_class); // $5
134 substitutions.push_back(loading_indicator_class); // $6 137 substitutions.push_back(original_url); // $6
135 substitutions.push_back(original_url); // $7
136 substitutions.push_back( 138 substitutions.push_back(
137 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $8 139 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $7
138 substitutions.push_back(textDirection); // $9 140 substitutions.push_back(textDirection); // $8
141 substitutions.push_back(htmlContent); // $9
139 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 142 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
140 } 143 }
141 144
142 } // namespace 145 } // namespace
143 146
144 namespace viewer { 147 namespace viewer {
145 148
146 const std::string GetUnsafeIncrementalDistilledPageJs( 149 const std::string GetUnsafeIncrementalDistilledPageJs(
147 const DistilledPageProto* page_proto, 150 const DistilledPageProto* page_proto,
148 const bool is_last_page) { 151 const bool is_last_page) {
149 std::string output; 152 std::string output;
150 base::StringValue value(page_proto->html()); 153 base::StringValue value(page_proto->html());
151 base::JSONWriter::Write(&value, &output); 154 base::JSONWriter::Write(&value, &output);
155 EnsureNonEmptyContent(&output);
152 std::string page_update("addToPage("); 156 std::string page_update("addToPage(");
153 page_update += output + ");"; 157 page_update += output + ");";
154 return page_update + GetToggleLoadingIndicatorJs( 158 return page_update + GetToggleLoadingIndicatorJs(
155 is_last_page); 159 is_last_page);
156 160
157 } 161 }
158 162
163 const std::string GetErrorPageJs() {
164 base::StringValue value(l10n_util::GetStringUTF8(
165 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT));
166 std::string output;
167 base::JSONWriter::Write(&value, &output);
168 std::string page_update("addToPage(");
169 page_update += output + ");";
170 return page_update;
171 }
172
159 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { 173 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) {
160 if (is_last_page) 174 if (is_last_page)
161 return "showLoadingIndicator(true);"; 175 return "showLoadingIndicator(true);";
162 else 176 else
163 return "showLoadingIndicator(false);"; 177 return "showLoadingIndicator(false);";
164 } 178 }
165 179
166 const std::string GetUnsafePartialArticleHtml( 180 const std::string GetUnsafeArticleTemplateHtml(
167 const DistilledPageProto* page_proto, 181 const DistilledPageProto* page_proto,
168 const DistilledPagePrefs::Theme theme, 182 const DistilledPagePrefs::Theme theme,
169 const DistilledPagePrefs::FontFamily font_family) { 183 const DistilledPagePrefs::FontFamily font_family) {
170 DCHECK(page_proto); 184 DCHECK(page_proto);
185
171 std::string title = net::EscapeForHTML(page_proto->title()); 186 std::string title = net::EscapeForHTML(page_proto->title());
187
188 EnsureNonEmptyTitle(&title);
189
190 std::string text_direction = page_proto->text_direction();
191 std::string original_url = page_proto->url();
192
193 return ReplaceHtmlTemplateValues(title, text_direction, "hidden",
194 original_url, theme, font_family, "");
195 }
196
197 const std::string GetUnsafeArticleContentJs(
198 const DistilledArticleProto* article_proto) {
199 DCHECK(article_proto);
200 if (article_proto->pages_size() == 0 || !article_proto->pages(0).has_html()) {
201 return "";
202 }
203
172 std::ostringstream unsafe_output_stream; 204 std::ostringstream unsafe_output_stream;
173 unsafe_output_stream << page_proto->html(); 205 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
174 std::string unsafe_article_html = unsafe_output_stream.str(); 206 unsafe_output_stream << article_proto->pages(page_num).html();
175 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 207 }
176 std::string original_url = page_proto->url(); 208
177 return ReplaceHtmlTemplateValues( 209 std::string output;
178 title, page_proto->text_direction(), unsafe_article_html, "visible", 210 base::StringValue value(unsafe_output_stream.str());
179 original_url, theme, font_family); 211 base::JSONWriter::Write(&value, &output);
212 EnsureNonEmptyContent(&output);
213 std::string page_update("addToPage(");
214 page_update += output + ");";
215 return page_update + GetToggleLoadingIndicatorJs(true);
216 }
217
218 const std::string GetErrorPageHtml(
219 const DistilledPagePrefs::Theme theme,
220 const DistilledPagePrefs::FontFamily font_family) {
221 std::string title = l10n_util::GetStringUTF8(
222 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
223 return ReplaceHtmlTemplateValues(title, "auto", "hidden", "", theme,
224 font_family, "");
180 } 225 }
181 226
182 const std::string GetUnsafeArticleHtml( 227 const std::string GetUnsafeArticleHtml(
183 const DistilledArticleProto* article_proto, 228 const DistilledArticleProto* article_proto,
184 const DistilledPagePrefs::Theme theme, 229 const DistilledPagePrefs::Theme theme,
185 const DistilledPagePrefs::FontFamily font_family) { 230 const DistilledPagePrefs::FontFamily font_family) {
186 DCHECK(article_proto); 231 DCHECK(article_proto);
187 std::string title; 232 std::string title;
188 std::string unsafe_article_html; 233 std::string unsafe_article_html;
189 std::string text_direction = ""; 234 std::string text_direction = "";
190 if (article_proto->has_title() && article_proto->pages_size() > 0 && 235 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
191 article_proto->pages(0).has_html()) { 236 article_proto->pages(0).has_html()) {
192 title = net::EscapeForHTML(article_proto->title()); 237 title = net::EscapeForHTML(article_proto->title());
193 std::ostringstream unsafe_output_stream; 238 std::ostringstream unsafe_output_stream;
194 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { 239 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
195 unsafe_output_stream << article_proto->pages(page_num).html(); 240 unsafe_output_stream << article_proto->pages(page_num).html();
196 } 241 }
197 unsafe_article_html = unsafe_output_stream.str(); 242 unsafe_article_html = unsafe_output_stream.str();
198 text_direction = article_proto->pages(0).text_direction(); 243 text_direction = article_proto->pages(0).text_direction();
199 } 244 }
200 245
201 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 246 EnsureNonEmptyTitle(&title);
247 EnsureNonEmptyContent(&unsafe_article_html);
202 248
203 std::string original_url; 249 std::string original_url;
204 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { 250 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
205 original_url = article_proto->pages(0).url(); 251 original_url = article_proto->pages(0).url();
206 } 252 }
207 253
208 return ReplaceHtmlTemplateValues( 254 return ReplaceHtmlTemplateValues(
209 title, text_direction, unsafe_article_html, "hidden", original_url, 255 title, text_direction, "hidden", original_url, theme, font_family,
210 theme, font_family); 256 unsafe_article_html);
211 }
212
213 const std::string GetErrorPageHtml(
214 const DistilledPagePrefs::Theme theme,
215 const DistilledPagePrefs::FontFamily font_family) {
216 std::string title = l10n_util::GetStringUTF8(
217 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
218 std::string content = l10n_util::GetStringUTF8(
219 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
220 return ReplaceHtmlTemplateValues(
221 title, "", content, "hidden", "", theme, font_family);
222 } 257 }
223 258
224 const std::string GetCss() { 259 const std::string GetCss() {
225 return ResourceBundle::GetSharedInstance().GetRawDataResource( 260 return ResourceBundle::GetSharedInstance().GetRawDataResource(
226 IDR_DISTILLER_CSS).as_string(); 261 IDR_DISTILLER_CSS).as_string();
227 } 262 }
228 263
229 const std::string GetJavaScript() { 264 const std::string GetJavaScript() {
230 return ResourceBundle::GetSharedInstance() 265 return ResourceBundle::GetSharedInstance()
231 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) 266 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 309 }
275 310
276 const std::string GetDistilledPageFontFamilyJs( 311 const std::string GetDistilledPageFontFamilyJs(
277 DistilledPagePrefs::FontFamily font_family) { 312 DistilledPagePrefs::FontFamily font_family) {
278 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; 313 return "useFontFamily('" + GetJsFontFamily(font_family) + "');";
279 } 314 }
280 315
281 } // namespace viewer 316 } // namespace viewer
282 317
283 } // namespace dom_distiller 318 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/core/viewer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698