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

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

Issue 341563002: Theme Preferences for Distilled Pages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor changes to tests Created 6 years, 5 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 | Annotate | Revision Log
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"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "components/dom_distiller/core/distilled_page_prefs.h"
13 #include "components/dom_distiller/core/dom_distiller_service.h" 14 #include "components/dom_distiller/core/dom_distiller_service.h"
14 #include "components/dom_distiller/core/proto/distilled_article.pb.h" 15 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" 16 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
16 #include "components/dom_distiller/core/task_tracker.h" 17 #include "components/dom_distiller/core/task_tracker.h"
17 #include "components/dom_distiller/core/url_constants.h" 18 #include "components/dom_distiller/core/url_constants.h"
18 #include "components/dom_distiller/core/url_utils.h" 19 #include "components/dom_distiller/core/url_utils.h"
19 #include "grit/component_resources.h" 20 #include "grit/component_resources.h"
20 #include "grit/components_strings.h" 21 #include "grit/components_strings.h"
21 #include "net/base/escape.h" 22 #include "net/base/escape.h"
22 #include "net/url_request/url_request.h" 23 #include "net/url_request/url_request.h"
23 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
25 #include "url/gurl.h" 26 #include "url/gurl.h"
26 27
27 namespace dom_distiller { 28 namespace dom_distiller {
28 29
29 namespace { 30 namespace {
30 31
32 // JS Themes. Must agree with useTheme() in dom_distiller_viewer.js
nyquist 2014/07/08 22:26:48 Nit: "JS themes" and add a period at the end of th
smaslo 2014/07/09 19:42:25 Done.
33 const char kDarkJsTheme[] = "dark";
34 const char kLightJsTheme[] = "light";
35 const char kSepiaJsTheme[] = "sepia";
36
37 // CSS classes. Must agree with classes in distilledpage.css.
38 const char kDarkCssClass[] = "dark";
39 const char kLightCssClass[] = "light";
40 const char kSepiaCssClass[] = "sepia";
41
42 // Maps themes to JS themes.
43 const std::string GetJsTheme(DistilledPagePrefs::Theme theme) {
44 if (theme == DistilledPagePrefs::Theme::kDark) {
45 return kDarkJsTheme;
46 } else if (theme == DistilledPagePrefs::Theme::kSepia) {
47 return kSepiaJsTheme;
48 }
49 return kLightJsTheme;
50 }
51
52 // Maps themes to CSS classes.
53 const std::string GetCssClass(DistilledPagePrefs::Theme theme) {
54 if (theme == DistilledPagePrefs::Theme::kDark) {
55 return kDarkCssClass;
56 } else if (theme == DistilledPagePrefs::Theme::kSepia) {
57 return kSepiaCssClass;
58 }
59 return kLightCssClass;
60 }
61
31 std::string ReplaceHtmlTemplateValues( 62 std::string ReplaceHtmlTemplateValues(
32 const std::string& title, 63 const std::string& title,
33 const std::string& content, 64 const std::string& content,
34 const std::string& loading_indicator_class, 65 const std::string& loading_indicator_class,
35 const std::string& original_url) { 66 const std::string& original_url,
67 const DistilledPagePrefs::Theme theme) {
36 base::StringPiece html_template = 68 base::StringPiece html_template =
37 ResourceBundle::GetSharedInstance().GetRawDataResource( 69 ResourceBundle::GetSharedInstance().GetRawDataResource(
38 IDR_DOM_DISTILLER_VIEWER_HTML); 70 IDR_DOM_DISTILLER_VIEWER_HTML);
39 std::vector<std::string> substitutions; 71 std::vector<std::string> substitutions;
40 substitutions.push_back(title); // $1 72 substitutions.push_back(title); // $1
41 substitutions.push_back(kViewerCssPath); // $2 73 substitutions.push_back(kViewerCssPath); // $2
42 substitutions.push_back(kViewerJsPath); // $3 74 substitutions.push_back(kViewerJsPath); // $3
43 substitutions.push_back(title); // $4 75 substitutions.push_back(GetCssClass(theme)); // $4
44 substitutions.push_back(content); // $5 76 substitutions.push_back(content); // $5
45 substitutions.push_back(loading_indicator_class); // $6 77 substitutions.push_back(loading_indicator_class); // $6
46 substitutions.push_back( 78 substitutions.push_back(
47 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 79 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7
48 substitutions.push_back(original_url); // $8 80 substitutions.push_back(original_url); // $8
49 substitutions.push_back( 81 substitutions.push_back(
50 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 82 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9
51 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 83 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
52 } 84 }
53 85
(...skipping 15 matching lines...) Expand all
69 } 101 }
70 102
71 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { 103 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) {
72 if (is_last_page) 104 if (is_last_page)
73 return "showLoadingIndicator(true);"; 105 return "showLoadingIndicator(true);";
74 else 106 else
75 return "showLoadingIndicator(false);"; 107 return "showLoadingIndicator(false);";
76 } 108 }
77 109
78 const std::string GetUnsafePartialArticleHtml( 110 const std::string GetUnsafePartialArticleHtml(
79 const DistilledPageProto* page_proto) { 111 const DistilledPageProto* page_proto,
112 const DistilledPagePrefs::Theme theme) {
80 DCHECK(page_proto); 113 DCHECK(page_proto);
81 std::string title = net::EscapeForHTML(page_proto->title()); 114 std::string title = net::EscapeForHTML(page_proto->title());
82 std::ostringstream unsafe_output_stream; 115 std::ostringstream unsafe_output_stream;
83 unsafe_output_stream << page_proto->html(); 116 unsafe_output_stream << page_proto->html();
84 std::string unsafe_article_html = unsafe_output_stream.str(); 117 std::string unsafe_article_html = unsafe_output_stream.str();
85 std::string original_url = page_proto->url(); 118 std::string original_url = page_proto->url();
86 return ReplaceHtmlTemplateValues(title, 119 return ReplaceHtmlTemplateValues(title,
87 unsafe_article_html, 120 unsafe_article_html,
88 "visible", 121 "visible",
89 original_url); 122 original_url,
123 theme);
90 } 124 }
91 125
92 const std::string GetUnsafeArticleHtml( 126 const std::string GetUnsafeArticleHtml(
93 const DistilledArticleProto* article_proto) { 127 const DistilledArticleProto* article_proto,
128 const DistilledPagePrefs::Theme theme) {
94 DCHECK(article_proto); 129 DCHECK(article_proto);
95 std::string title; 130 std::string title;
96 std::string unsafe_article_html; 131 std::string unsafe_article_html;
97 if (article_proto->has_title() && article_proto->pages_size() > 0 && 132 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
98 article_proto->pages(0).has_html()) { 133 article_proto->pages(0).has_html()) {
99 title = net::EscapeForHTML(article_proto->title()); 134 title = net::EscapeForHTML(article_proto->title());
100 std::ostringstream unsafe_output_stream; 135 std::ostringstream unsafe_output_stream;
101 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { 136 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
102 unsafe_output_stream << article_proto->pages(page_num).html(); 137 unsafe_output_stream << article_proto->pages(page_num).html();
103 } 138 }
104 unsafe_article_html = unsafe_output_stream.str(); 139 unsafe_article_html = unsafe_output_stream.str();
105 } else { 140 } else {
106 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); 141 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE);
107 unsafe_article_html = 142 unsafe_article_html =
108 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); 143 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT);
109 } 144 }
110 145
111 std::string original_url; 146 std::string original_url;
112 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { 147 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
113 original_url = article_proto->pages(0).url(); 148 original_url = article_proto->pages(0).url();
114 } 149 }
115 150
116 return ReplaceHtmlTemplateValues(title, 151 return ReplaceHtmlTemplateValues(title,
117 unsafe_article_html, 152 unsafe_article_html,
118 "hidden", 153 "hidden",
119 original_url); 154 original_url,
155 theme);
120 } 156 }
121 157
122 const std::string GetErrorPageHtml() { 158 const std::string GetErrorPageHtml(const DistilledPagePrefs::Theme theme) {
123 std::string title = l10n_util::GetStringUTF8( 159 std::string title = l10n_util::GetStringUTF8(
124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 160 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
125 std::string content = l10n_util::GetStringUTF8( 161 std::string content = l10n_util::GetStringUTF8(
126 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 162 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
127 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); 163 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme);
128 } 164 }
129 165
130 const std::string GetCss() { 166 const std::string GetCss() {
131 return ResourceBundle::GetSharedInstance() 167 return ResourceBundle::GetSharedInstance().GetRawDataResource(
132 .GetRawDataResource(IDR_DISTILLER_CSS) 168 IDR_DISTILLER_CSS).as_string();
133 .as_string();
134 } 169 }
135 170
136 const std::string GetJavaScript() { 171 const std::string GetJavaScript() {
137 return ResourceBundle::GetSharedInstance() 172 return ResourceBundle::GetSharedInstance()
138 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) 173 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS)
139 .as_string(); 174 .as_string();
140 } 175 }
141 176
142 scoped_ptr<ViewerHandle> CreateViewRequest( 177 scoped_ptr<ViewerHandle> CreateViewRequest(
143 DomDistillerServiceInterface* dom_distiller_service, 178 DomDistillerServiceInterface* dom_distiller_service,
(...skipping 24 matching lines...) Expand all
168 return dom_distiller_service->ViewUrl(view_request_delegate, 203 return dom_distiller_service->ViewUrl(view_request_delegate,
169 dom_distiller_service 204 dom_distiller_service
170 ->CreateDefaultDistillerPage(), 205 ->CreateDefaultDistillerPage(),
171 requested_url).Pass(); 206 requested_url).Pass();
172 } 207 }
173 208
174 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. 209 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|.
175 return scoped_ptr<ViewerHandle>(); 210 return scoped_ptr<ViewerHandle>();
176 } 211 }
177 212
213 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) {
214 std::string str = "useTheme('" + GetJsTheme(theme) + "');";
nyquist 2014/07/08 22:26:48 inline the return
smaslo 2014/07/09 19:42:25 Done.
215 return str;
216 }
217
178 } // namespace viewer 218 } // namespace viewer
179 219
180 } // namespace dom_distiller 220 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698