OLD | NEW |
---|---|
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/dom_distiller_service.h" | 13 #include "components/dom_distiller/core/dom_distiller_service.h" |
14 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 14 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
16 #include "components/dom_distiller/core/reader_mode_preferences.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 const char kWhite[] = "#FFF"; | |
nyquist
2014/06/23 16:03:06
Nit: I think these two can go away if you follow b
smaslo
2014/06/23 22:14:33
Done.
| |
33 const char kBlack[] = "#000"; | |
34 | |
35 std::string GetBodyInsertionText(ReaderModePrefs* reader_mode_prefs) { | |
nyquist
2014/06/23 16:03:06
Is there a way of writing a test to ensure we use
smaslo
2014/06/23 22:14:33
The only way I've heard so far is doing a string t
| |
36 if (reader_mode_prefs->GetHighContrastPref()) { | |
37 std::string body_insertion_text = "background-color: "; | |
38 body_insertion_text += kBlack; | |
39 body_insertion_text += ";\ncolor: "; | |
40 body_insertion_text += kWhite; | |
41 body_insertion_text += ";"; | |
42 return body_insertion_text; | |
43 } | |
44 else { | |
45 std::string body_insertion_text = "background-color: "; | |
46 body_insertion_text += kWhite; | |
47 body_insertion_text += ";\ncolor: "; | |
48 body_insertion_text += kBlack; | |
49 body_insertion_text += ";"; | |
50 return body_insertion_text; | |
51 } | |
battre
2014/06/23 08:37:23
Opt:
Wouldn't this be more readable if you phrase
smaslo
2014/06/23 22:14:33
Done.
| |
52 } | |
53 | |
31 std::string ReplaceHtmlTemplateValues( | 54 std::string ReplaceHtmlTemplateValues( |
32 const std::string& title, | 55 const std::string& title, |
33 const std::string& content, | 56 const std::string& content, |
34 const std::string& loading_indicator_class, | 57 const std::string& loading_indicator_class, |
35 const std::string& original_url) { | 58 const std::string& original_url) { |
36 base::StringPiece html_template = | 59 base::StringPiece html_template = |
37 ResourceBundle::GetSharedInstance().GetRawDataResource( | 60 ResourceBundle::GetSharedInstance().GetRawDataResource( |
38 IDR_DOM_DISTILLER_VIEWER_HTML); | 61 IDR_DOM_DISTILLER_VIEWER_HTML); |
39 std::vector<std::string> substitutions; | 62 std::vector<std::string> substitutions; |
40 substitutions.push_back(title); // $1 | 63 substitutions.push_back(title); // $1 |
41 substitutions.push_back(kViewerCssPath); // $2 | 64 substitutions.push_back(kViewerCssPath); // $2 |
42 substitutions.push_back(kViewerJsPath); // $3 | 65 substitutions.push_back(kViewerJsPath); // $3 |
43 substitutions.push_back(title); // $4 | 66 substitutions.push_back(title); // $4 |
44 substitutions.push_back(content); // $5 | 67 substitutions.push_back(content); // $5 |
45 substitutions.push_back(loading_indicator_class); // $6 | 68 substitutions.push_back(loading_indicator_class); // $6 |
46 substitutions.push_back( | 69 substitutions.push_back( |
47 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 | 70 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 |
48 substitutions.push_back(original_url); // $8 | 71 substitutions.push_back(original_url); // $8 |
49 substitutions.push_back( | 72 substitutions.push_back( |
50 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 | 73 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 |
51 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 74 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
52 } | 75 } |
53 | 76 |
77 std::string ReplaceCSSTemplateValues(ReaderModePrefs* reader_mode_prefs) { | |
78 std::vector<std::string> substitutions; | |
79 substitutions.push_back(GetBodyInsertionText(reader_mode_prefs)); | |
80 base::StringPiece css_template = | |
81 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
82 IDR_DISTILLER_CSS); | |
83 return ReplaceStringPlaceholders(css_template, substitutions, NULL); | |
84 } | |
85 | |
54 } // namespace | 86 } // namespace |
55 | 87 |
56 namespace viewer { | 88 namespace viewer { |
57 | 89 |
58 const std::string GetUnsafeIncrementalDistilledPageJs( | 90 const std::string GetUnsafeIncrementalDistilledPageJs( |
59 const DistilledPageProto* page_proto, | 91 const DistilledPageProto* page_proto, |
60 const bool is_last_page) { | 92 const bool is_last_page) { |
61 std::string output; | 93 std::string output; |
62 base::StringValue value(page_proto->html()); | 94 base::StringValue value(page_proto->html()); |
63 base::JSONWriter::Write(&value, &output); | 95 base::JSONWriter::Write(&value, &output); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 } | 152 } |
121 | 153 |
122 const std::string GetErrorPageHtml() { | 154 const std::string GetErrorPageHtml() { |
123 std::string title = l10n_util::GetStringUTF8( | 155 std::string title = l10n_util::GetStringUTF8( |
124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 156 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
125 std::string content = l10n_util::GetStringUTF8( | 157 std::string content = l10n_util::GetStringUTF8( |
126 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 158 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
127 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); | 159 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); |
128 } | 160 } |
129 | 161 |
130 const std::string GetCss() { | 162 const std::string GetCss(ReaderModePrefs* reader_mode_prefs) { |
131 return ResourceBundle::GetSharedInstance() | 163 return ReplaceCSSTemplateValues(reader_mode_prefs); |
nyquist
2014/06/23 16:03:06
This is only used once, so maybe you could inline
smaslo
2014/06/23 22:14:34
Done.
| |
132 .GetRawDataResource(IDR_DISTILLER_CSS) | |
133 .as_string(); | |
134 } | 164 } |
135 | 165 |
136 const std::string GetJavaScript() { | 166 const std::string GetJavaScript() { |
137 return ResourceBundle::GetSharedInstance() | 167 return ResourceBundle::GetSharedInstance() |
138 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 168 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
139 .as_string(); | 169 .as_string(); |
140 } | 170 } |
141 | 171 |
142 scoped_ptr<ViewerHandle> CreateViewRequest( | 172 scoped_ptr<ViewerHandle> CreateViewRequest( |
143 DomDistillerServiceInterface* dom_distiller_service, | 173 DomDistillerServiceInterface* dom_distiller_service, |
(...skipping 27 matching lines...) Expand all Loading... | |
171 requested_url).Pass(); | 201 requested_url).Pass(); |
172 } | 202 } |
173 | 203 |
174 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 204 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
175 return scoped_ptr<ViewerHandle>(); | 205 return scoped_ptr<ViewerHandle>(); |
176 } | 206 } |
177 | 207 |
178 } // namespace viewer | 208 } // namespace viewer |
179 | 209 |
180 } // namespace dom_distiller | 210 } // namespace dom_distiller |
OLD | NEW |