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

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: Fix IOS version of 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
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 118
117 std::ostringstream css; 119 std::ostringstream css;
118 std::ostringstream script; 120 std::ostringstream script;
119 #if defined(OS_IOS) 121 #if defined(OS_IOS)
120 // On iOS the content is inlined as there is no API to detect those requests 122 // 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. 123 // and return the local data once a page is loaded.
122 css << "<style>" << viewer::GetCss() << "</style>"; 124 css << "<style>" << viewer::GetCss() << "</style>";
123 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>"; 125 script << "<script>\n" << viewer::GetJavaScript() << "\n</script>";
124 #else 126 #else
125 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">"; 127 css << "<link rel=\"stylesheet\" href=\"/" << kViewerCssPath << "\">";
126 script << "<script src=\"" << kViewerJsPath << "\"></script>"; 128 script << "<script src=\"" << kViewerJsPath << "\"></script>";
127 #endif // defined(OS_IOS) 129 #endif // defined(OS_IOS)
128 substitutions.push_back(css.str()); // $2 130 substitutions.push_back(css.str()); // $2
129 substitutions.push_back(script.str()); // $3 131 substitutions.push_back(script.str()); // $3
130 132
131 substitutions.push_back(GetThemeCssClass(theme) + " " + 133 substitutions.push_back(GetThemeCssClass(theme) + " " +
132 GetFontCssClass(font_family)); // $4 134 GetFontCssClass(font_family)); // $4
133 substitutions.push_back(content); // $5 135 substitutions.push_back(loading_indicator_class); // $5
134 substitutions.push_back(loading_indicator_class); // $6 136 substitutions.push_back(original_url); // $6
135 substitutions.push_back(original_url); // $7
136 substitutions.push_back( 137 substitutions.push_back(
137 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $8 138 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $7
138 substitutions.push_back(textDirection); // $9 139 substitutions.push_back(textDirection); // $8
139 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 140 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
140 } 141 }
141 142
142 } // namespace 143 } // namespace
143 144
144 namespace viewer { 145 namespace viewer {
145 146
146 const std::string GetUnsafeIncrementalDistilledPageJs( 147 const std::string GetUnsafeIncrementalDistilledPageJs(
147 const DistilledPageProto* page_proto, 148 const DistilledPageProto* page_proto,
148 const bool is_last_page) { 149 const bool is_last_page) {
149 std::string output; 150 std::string output;
150 base::StringValue value(page_proto->html()); 151 base::StringValue value(page_proto->html());
151 base::JSONWriter::Write(&value, &output); 152 base::JSONWriter::Write(&value, &output);
153 EnsureNonEmptyContent(&output);
152 std::string page_update("addToPage("); 154 std::string page_update("addToPage(");
153 page_update += output + ");"; 155 page_update += output + ");";
154 return page_update + GetToggleLoadingIndicatorJs( 156 return page_update + GetToggleLoadingIndicatorJs(
155 is_last_page); 157 is_last_page);
156 158
157 } 159 }
158 160
161 const std::string GetErrorPageJs() {
162 base::StringValue value(l10n_util::GetStringUTF8(
163 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT));
164 std::string output;
165 base::JSONWriter::Write(&value, &output);
166 std::string page_update("addToPage(");
167 page_update += output + ");";
168 return page_update;
169 }
170
159 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { 171 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) {
160 if (is_last_page) 172 if (is_last_page)
161 return "showLoadingIndicator(true);"; 173 return "showLoadingIndicator(true);";
162 else 174 else
163 return "showLoadingIndicator(false);"; 175 return "showLoadingIndicator(false);";
164 } 176 }
165 177
166 const std::string GetUnsafePartialArticleHtml( 178 const std::string GetUnsafeArticleTemplateHtml(
167 const DistilledPageProto* page_proto, 179 const DistilledPageProto* page_proto,
168 const DistilledPagePrefs::Theme theme, 180 const DistilledPagePrefs::Theme theme,
169 const DistilledPagePrefs::FontFamily font_family) { 181 const DistilledPagePrefs::FontFamily font_family) {
170 DCHECK(page_proto); 182 DCHECK(page_proto);
183
171 std::string title = net::EscapeForHTML(page_proto->title()); 184 std::string title = net::EscapeForHTML(page_proto->title());
172 std::ostringstream unsafe_output_stream; 185
173 unsafe_output_stream << page_proto->html(); 186 EnsureNonEmptyTitle(&title);
174 std::string unsafe_article_html = unsafe_output_stream.str(); 187
175 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 188 std::string text_direction = page_proto->text_direction();
176 std::string original_url = page_proto->url(); 189 std::string original_url = page_proto->url();
177 return ReplaceHtmlTemplateValues( 190
178 title, page_proto->text_direction(), unsafe_article_html, "visible", 191 return ReplaceHtmlTemplateValues(title, text_direction, "hidden",
179 original_url, theme, font_family); 192 original_url, theme, font_family);
180 } 193 }
181 194
182 const std::string GetUnsafeArticleHtml( 195 const std::string GetUnsafeArticleContentJs(
183 const DistilledArticleProto* article_proto, 196 const DistilledArticleProto* article_proto) {
184 const DistilledPagePrefs::Theme theme,
185 const DistilledPagePrefs::FontFamily font_family) {
186 DCHECK(article_proto); 197 DCHECK(article_proto);
187 std::string title; 198 if (article_proto->pages_size() == 0 || !article_proto->pages(0).has_html()) {
188 std::string unsafe_article_html; 199 return "";
189 std::string text_direction = "";
190 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
191 article_proto->pages(0).has_html()) {
192 title = net::EscapeForHTML(article_proto->title());
193 std::ostringstream unsafe_output_stream;
194 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
195 unsafe_output_stream << article_proto->pages(page_num).html();
196 }
197 unsafe_article_html = unsafe_output_stream.str();
198 text_direction = article_proto->pages(0).text_direction();
199 } 200 }
200 201
201 EnsureNonEmptyTitleAndContent(&title, &unsafe_article_html); 202 std::ostringstream unsafe_output_stream;
202 203 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
203 std::string original_url; 204 unsafe_output_stream << article_proto->pages(page_num).html();
204 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
205 original_url = article_proto->pages(0).url();
206 } 205 }
207 206
208 return ReplaceHtmlTemplateValues( 207 std::string output;
209 title, text_direction, unsafe_article_html, "hidden", original_url, 208 base::StringValue value(unsafe_output_stream.str());
210 theme, font_family); 209 base::JSONWriter::Write(&value, &output);
210 EnsureNonEmptyContent(&output);
211 std::string page_update("addToPage(");
212 page_update += output + ");";
213 return page_update + GetToggleLoadingIndicatorJs(true);
211 } 214 }
212 215
213 const std::string GetErrorPageHtml( 216 const std::string GetErrorPageHtml(
214 const DistilledPagePrefs::Theme theme, 217 const DistilledPagePrefs::Theme theme,
215 const DistilledPagePrefs::FontFamily font_family) { 218 const DistilledPagePrefs::FontFamily font_family) {
216 std::string title = l10n_util::GetStringUTF8( 219 std::string title = l10n_util::GetStringUTF8(
217 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 220 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
218 std::string content = l10n_util::GetStringUTF8( 221 return ReplaceHtmlTemplateValues(title, "auto", "hidden", "", theme,
219 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 222 font_family);
220 return ReplaceHtmlTemplateValues(
221 title, "", content, "hidden", "", theme, font_family);
222 } 223 }
223 224
224 const std::string GetCss() { 225 const std::string GetCss() {
225 return ResourceBundle::GetSharedInstance().GetRawDataResource( 226 return ResourceBundle::GetSharedInstance().GetRawDataResource(
226 IDR_DISTILLER_CSS).as_string(); 227 IDR_DISTILLER_CSS).as_string();
227 } 228 }
228 229
229 const std::string GetJavaScript() { 230 const std::string GetJavaScript() {
230 return ResourceBundle::GetSharedInstance() 231 return ResourceBundle::GetSharedInstance()
231 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) 232 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 275 }
275 276
276 const std::string GetDistilledPageFontFamilyJs( 277 const std::string GetDistilledPageFontFamilyJs(
277 DistilledPagePrefs::FontFamily font_family) { 278 DistilledPagePrefs::FontFamily font_family) {
278 return "useFontFamily('" + GetJsFontFamily(font_family) + "');"; 279 return "useFontFamily('" + GetJsFontFamily(font_family) + "');";
279 } 280 }
280 281
281 } // namespace viewer 282 } // namespace viewer
282 283
283 } // namespace dom_distiller 284 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698