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

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: Changed names and added 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.
33 const char kDarkJsTheme[] = "dark";
34 const char kLightJsTheme[] = "light";
35 const char kSepiaJsTheme[] = "sepia";
36
37 // Maps themes to CSS classes. Text returned must agree with themes in
38 // css/distilledpage.css.
39 const std::string GetJsTheme(DistilledPagePrefs::Theme theme) {
40 if (theme == DistilledPagePrefs::Theme::kDark) {
41 return kDarkJsTheme;
42 } else if (theme == DistilledPagePrefs::Theme::kSepia) {
43 return kSepiaJsTheme;
44 }
45 return kLightJsTheme;
46 }
47
31 std::string ReplaceHtmlTemplateValues( 48 std::string ReplaceHtmlTemplateValues(
32 const std::string& title, 49 const std::string& title,
33 const std::string& content, 50 const std::string& content,
34 const std::string& loading_indicator_class, 51 const std::string& loading_indicator_class,
35 const std::string& original_url) { 52 const std::string& original_url,
53 const DistilledPagePrefs::Theme theme) {
36 base::StringPiece html_template = 54 base::StringPiece html_template =
37 ResourceBundle::GetSharedInstance().GetRawDataResource( 55 ResourceBundle::GetSharedInstance().GetRawDataResource(
38 IDR_DOM_DISTILLER_VIEWER_HTML); 56 IDR_DOM_DISTILLER_VIEWER_HTML);
39 std::vector<std::string> substitutions; 57 std::vector<std::string> substitutions;
40 substitutions.push_back(title); // $1 58 substitutions.push_back(title); // $1
41 substitutions.push_back(kViewerCssPath); // $2 59 substitutions.push_back(kViewerCssPath); // $2
42 substitutions.push_back(kViewerJsPath); // $3 60 substitutions.push_back(kViewerJsPath); // $3
43 substitutions.push_back(title); // $4 61 substitutions.push_back(GetJsTheme(theme)); // $4
44 substitutions.push_back(content); // $5 62 substitutions.push_back(content); // $5
45 substitutions.push_back(loading_indicator_class); // $6 63 substitutions.push_back(loading_indicator_class); // $6
46 substitutions.push_back( 64 substitutions.push_back(
47 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 65 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7
48 substitutions.push_back(original_url); // $8 66 substitutions.push_back(original_url); // $8
49 substitutions.push_back( 67 substitutions.push_back(
50 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 68 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9
51 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 69 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
52 } 70 }
53 71
(...skipping 15 matching lines...) Expand all
69 } 87 }
70 88
71 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { 89 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) {
72 if (is_last_page) 90 if (is_last_page)
73 return "showLoadingIndicator(true);"; 91 return "showLoadingIndicator(true);";
74 else 92 else
75 return "showLoadingIndicator(false);"; 93 return "showLoadingIndicator(false);";
76 } 94 }
77 95
78 const std::string GetUnsafePartialArticleHtml( 96 const std::string GetUnsafePartialArticleHtml(
79 const DistilledPageProto* page_proto) { 97 const DistilledPageProto* page_proto,
98 const DistilledPagePrefs::Theme theme) {
80 DCHECK(page_proto); 99 DCHECK(page_proto);
81 std::string title = net::EscapeForHTML(page_proto->title()); 100 std::string title = net::EscapeForHTML(page_proto->title());
82 std::ostringstream unsafe_output_stream; 101 std::ostringstream unsafe_output_stream;
83 unsafe_output_stream << page_proto->html(); 102 unsafe_output_stream << page_proto->html();
84 std::string unsafe_article_html = unsafe_output_stream.str(); 103 std::string unsafe_article_html = unsafe_output_stream.str();
85 std::string original_url = page_proto->url(); 104 std::string original_url = page_proto->url();
86 return ReplaceHtmlTemplateValues(title, 105 return ReplaceHtmlTemplateValues(title,
87 unsafe_article_html, 106 unsafe_article_html,
88 "visible", 107 "visible",
89 original_url); 108 original_url,
109 theme);
90 } 110 }
91 111
92 const std::string GetUnsafeArticleHtml( 112 const std::string GetUnsafeArticleHtml(
93 const DistilledArticleProto* article_proto) { 113 const DistilledArticleProto* article_proto,
114 const DistilledPagePrefs::Theme theme) {
94 DCHECK(article_proto); 115 DCHECK(article_proto);
95 std::string title; 116 std::string title;
96 std::string unsafe_article_html; 117 std::string unsafe_article_html;
97 if (article_proto->has_title() && article_proto->pages_size() > 0 && 118 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
98 article_proto->pages(0).has_html()) { 119 article_proto->pages(0).has_html()) {
99 title = net::EscapeForHTML(article_proto->title()); 120 title = net::EscapeForHTML(article_proto->title());
100 std::ostringstream unsafe_output_stream; 121 std::ostringstream unsafe_output_stream;
101 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { 122 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
102 unsafe_output_stream << article_proto->pages(page_num).html(); 123 unsafe_output_stream << article_proto->pages(page_num).html();
103 } 124 }
104 unsafe_article_html = unsafe_output_stream.str(); 125 unsafe_article_html = unsafe_output_stream.str();
105 } else { 126 } else {
106 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); 127 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE);
107 unsafe_article_html = 128 unsafe_article_html =
108 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); 129 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT);
109 } 130 }
110 131
111 std::string original_url; 132 std::string original_url;
112 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { 133 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
113 original_url = article_proto->pages(0).url(); 134 original_url = article_proto->pages(0).url();
114 } 135 }
115 136
116 return ReplaceHtmlTemplateValues(title, 137 return ReplaceHtmlTemplateValues(title,
117 unsafe_article_html, 138 unsafe_article_html,
118 "hidden", 139 "hidden",
119 original_url); 140 original_url,
141 theme);
120 } 142 }
121 143
122 const std::string GetErrorPageHtml() { 144 const std::string GetErrorPageHtml(const DistilledPagePrefs::Theme theme) {
123 std::string title = l10n_util::GetStringUTF8( 145 std::string title = l10n_util::GetStringUTF8(
124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 146 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
125 std::string content = l10n_util::GetStringUTF8( 147 std::string content = l10n_util::GetStringUTF8(
126 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 148 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
127 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); 149 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme);
128 } 150 }
129 151
130 const std::string GetCss() { 152 const std::string GetCss() {
131 return ResourceBundle::GetSharedInstance() 153 return ResourceBundle::GetSharedInstance().GetRawDataResource(
132 .GetRawDataResource(IDR_DISTILLER_CSS) 154 IDR_DISTILLER_CSS).as_string();
133 .as_string();
134 } 155 }
135 156
136 const std::string GetJavaScript() { 157 const std::string GetJavaScript() {
137 return ResourceBundle::GetSharedInstance() 158 return ResourceBundle::GetSharedInstance()
138 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) 159 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS)
139 .as_string(); 160 .as_string();
140 } 161 }
141 162
142 scoped_ptr<ViewerHandle> CreateViewRequest( 163 scoped_ptr<ViewerHandle> CreateViewRequest(
143 DomDistillerServiceInterface* dom_distiller_service, 164 DomDistillerServiceInterface* dom_distiller_service,
(...skipping 24 matching lines...) Expand all
168 return dom_distiller_service->ViewUrl(view_request_delegate, 189 return dom_distiller_service->ViewUrl(view_request_delegate,
169 dom_distiller_service 190 dom_distiller_service
170 ->CreateDefaultDistillerPage(), 191 ->CreateDefaultDistillerPage(),
171 requested_url).Pass(); 192 requested_url).Pass();
172 } 193 }
173 194
174 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. 195 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|.
175 return scoped_ptr<ViewerHandle>(); 196 return scoped_ptr<ViewerHandle>();
176 } 197 }
177 198
199 const std::string GetDistilledPageThemeJs(DistilledPagePrefs::Theme theme) {
200 std::ostringstream js;
201 js << "document.body.className='" <<
nyquist 2014/07/07 19:12:49 Instead of doing this, could you instead call a Ja
smaslo 2014/07/08 19:58:11 Done.
202 GetJsTheme(theme) << "';";
203 return js.str();
204 }
205
178 } // namespace viewer 206 } // namespace viewer
179 207
180 } // namespace dom_distiller 208 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698