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

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: Bug fix & related cleanup Created 5 years, 9 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
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) {
111 base::StringPiece html_template = 113 base::StringPiece html_template =
112 ResourceBundle::GetSharedInstance().GetRawDataResource( 114 ResourceBundle::GetSharedInstance().GetRawDataResource(
113 IDR_DOM_DISTILLER_VIEWER_HTML); 115 IDR_DOM_DISTILLER_VIEWER_HTML);
114 std::vector<std::string> substitutions; 116 std::vector<std::string> substitutions;
115 substitutions.push_back(title); // $1 117 substitutions.push_back(title); // $1
116 substitutions.push_back(kViewerCssPath); // $2 118 substitutions.push_back(kViewerCssPath); // $2
117 substitutions.push_back(kViewerJsPath); // $3 119 substitutions.push_back(kViewerJsPath); // $3
118 substitutions.push_back(GetThemeCssClass(theme) + " " + 120 substitutions.push_back(GetThemeCssClass(theme) + " " +
119 GetFontCssClass(font_family)); // $4 121 GetFontCssClass(font_family)); // $4
120 substitutions.push_back(content); // $5 122 substitutions.push_back(loading_indicator_class); // $5
121 substitutions.push_back(loading_indicator_class); // $6 123 substitutions.push_back(original_url); // $6
122 substitutions.push_back(original_url); // $7
123 substitutions.push_back( 124 substitutions.push_back(
124 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $8 125 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $7
125 substitutions.push_back(textDirection); // $9 126 substitutions.push_back(textDirection); // $8
126 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 127 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
127 } 128 }
128 129
129 } // namespace 130 } // namespace
130 131
131 namespace viewer { 132 namespace viewer {
132 133
133 const std::string GetUnsafeIncrementalDistilledPageJs( 134 const std::string GetUnsafeIncrementalDistilledPageJs(
134 const DistilledPageProto* page_proto, 135 const DistilledPageProto* page_proto,
135 const bool is_last_page) { 136 const bool is_last_page) {
136 std::string output; 137 std::string output;
137 base::StringValue value(page_proto->html()); 138 base::StringValue value(page_proto->html());
138 base::JSONWriter::Write(&value, &output); 139 base::JSONWriter::Write(&value, &output);
140 EnsureNonEmptyContent(&output);
139 std::string page_update("addToPage("); 141 std::string page_update("addToPage(");
140 page_update += output + ");"; 142 page_update += output + ");";
141 return page_update + GetToggleLoadingIndicatorJs( 143 return page_update + GetToggleLoadingIndicatorJs(
142 is_last_page); 144 is_last_page);
143 145
144 } 146 }
145 147
148 const std::string GetErrorPageJs() {
149 base::StringValue value(l10n_util::GetStringUTF8(
150 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT));
151 std::string output;
152 base::JSONWriter::Write(&value, &output);
153 std::string page_update("addToPage(");
154 page_update += output + ");";
155 return page_update;
156 }
157
146 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { 158 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) {
147 if (is_last_page) 159 if (is_last_page)
148 return "showLoadingIndicator(true);"; 160 return "showLoadingIndicator(true);";
149 else 161 else
150 return "showLoadingIndicator(false);"; 162 return "showLoadingIndicator(false);";
151 } 163 }
152 164
153 const std::string GetUnsafePartialArticleHtml( 165 const std::string GetUnsafeArticleTemplateHtml(
154 const DistilledPageProto* page_proto, 166 const DistilledPageProto* page_proto,
155 const DistilledPagePrefs::Theme theme, 167 const DistilledPagePrefs::Theme theme,
156 const DistilledPagePrefs::FontFamily font_family) { 168 const DistilledPagePrefs::FontFamily font_family) {
157 DCHECK(page_proto); 169 DCHECK(page_proto);
170
158 std::string title = net::EscapeForHTML(page_proto->title()); 171 std::string title = net::EscapeForHTML(page_proto->title());
159 std::ostringstream unsafe_output_stream; 172
160 unsafe_output_stream << page_proto->html(); 173 EnsureNonEmptyTitle(&title);
161 std::string unsafe_article_html = unsafe_output_stream.str(); 174
162 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 175 std::string text_direction = page_proto->text_direction();
163 std::string original_url = page_proto->url(); 176 std::string original_url = page_proto->url();
164 return ReplaceHtmlTemplateValues( 177
165 title, page_proto->text_direction(), unsafe_article_html, "visible", 178 return ReplaceHtmlTemplateValues(title, text_direction, "hidden",
166 original_url, theme, font_family); 179 original_url, theme, font_family);
167 } 180 }
168 181
169 const std::string GetUnsafeArticleHtml( 182 const std::string GetUnsafeArticleContentJs(
170 const DistilledArticleProto* article_proto, 183 const DistilledArticleProto* article_proto) {
171 const DistilledPagePrefs::Theme theme,
172 const DistilledPagePrefs::FontFamily font_family) {
173 DCHECK(article_proto); 184 DCHECK(article_proto);
174 std::string title; 185 if (article_proto->pages_size() == 0 || !article_proto->pages(0).has_html()) {
175 std::string unsafe_article_html; 186 return "";
176 std::string text_direction = "";
177 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
178 article_proto->pages(0).has_html()) {
179 title = net::EscapeForHTML(article_proto->title());
180 std::ostringstream unsafe_output_stream;
181 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
182 unsafe_output_stream << article_proto->pages(page_num).html();
183 }
184 unsafe_article_html = unsafe_output_stream.str();
185 text_direction = article_proto->pages(0).text_direction();
186 } 187 }
187 188
188 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 189 std::ostringstream unsafe_output_stream;
189 190 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
190 std::string original_url; 191 unsafe_output_stream << article_proto->pages(page_num).html();
191 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
192 original_url = article_proto->pages(0).url();
193 } 192 }
194 193
195 return ReplaceHtmlTemplateValues( 194 std::string output;
196 title, text_direction, unsafe_article_html, "hidden", original_url, 195 base::StringValue value(unsafe_output_stream.str());
197 theme, font_family); 196 base::JSONWriter::Write(&value, &output);
197 EnsureNonEmptyContent(&output);
198 std::string page_update("addToPage(");
199 page_update += output + ");";
200 return page_update + GetToggleLoadingIndicatorJs(true);
198 } 201 }
199 202
200 const std::string GetErrorPageHtml( 203 const std::string GetErrorPageHtml(
201 const DistilledPagePrefs::Theme theme, 204 const DistilledPagePrefs::Theme theme,
202 const DistilledPagePrefs::FontFamily font_family) { 205 const DistilledPagePrefs::FontFamily font_family) {
203 std::string title = l10n_util::GetStringUTF8( 206 std::string title = l10n_util::GetStringUTF8(
204 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 207 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
205 std::string content = l10n_util::GetStringUTF8( 208 return ReplaceHtmlTemplateValues(title, "auto", "hidden", "", theme,
206 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 209 font_family);
207 return ReplaceHtmlTemplateValues(
208 title, "", content, "hidden", "", theme, font_family);
209 } 210 }
210 211
211 const std::string GetCss() { 212 const std::string GetCss() {
212 return ResourceBundle::GetSharedInstance().GetRawDataResource( 213 return ResourceBundle::GetSharedInstance().GetRawDataResource(
213 IDR_DISTILLER_CSS).as_string(); 214 IDR_DISTILLER_CSS).as_string();
214 } 215 }
215 216
216 const std::string GetJavaScript() { 217 const std::string GetJavaScript() {
217 return ResourceBundle::GetSharedInstance() 218 return ResourceBundle::GetSharedInstance()
218 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) 219 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 } 262 }
262 263
263 const std::string GetDistilledPageFontFamilyJs( 264 const std::string GetDistilledPageFontFamilyJs(
264 DistilledPagePrefs::FontFamily font_family) { 265 DistilledPagePrefs::FontFamily font_family) {
265 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; 266 return "useFontFamily('" + GetJsFontFamily(font_family) + "');";
266 } 267 }
267 268
268 } // namespace viewer 269 } // namespace viewer
269 270
270 } // namespace dom_distiller 271 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698