Chromium Code Reviews| 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 // CSS class names - must agree with css/distilledpage.css. | |
| 33 const char kDarkClassName[] = "dark"; | |
| 34 const char kLightClassName[] = "light"; | |
| 35 const char kSepiaClassName[] = "sepia"; | |
| 36 | |
| 31 std::string ReplaceHtmlTemplateValues( | 37 std::string ReplaceHtmlTemplateValues( |
| 32 const std::string& title, | 38 const std::string& title, |
| 33 const std::string& content, | 39 const std::string& content, |
| 34 const std::string& loading_indicator_class, | 40 const std::string& loading_indicator_class, |
| 35 const std::string& original_url) { | 41 const std::string& original_url, |
| 42 const ReaderModePrefs::Theme theme) { | |
| 36 base::StringPiece html_template = | 43 base::StringPiece html_template = |
| 37 ResourceBundle::GetSharedInstance().GetRawDataResource( | 44 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 38 IDR_DOM_DISTILLER_VIEWER_HTML); | 45 IDR_DOM_DISTILLER_VIEWER_HTML); |
| 39 std::vector<std::string> substitutions; | 46 std::vector<std::string> substitutions; |
| 40 substitutions.push_back(title); // $1 | 47 substitutions.push_back(title); // $1 |
| 41 substitutions.push_back(kViewerCssPath); // $2 | 48 substitutions.push_back(kViewerCssPath); // $2 |
| 42 substitutions.push_back(kViewerJsPath); // $3 | 49 substitutions.push_back(kViewerJsPath); // $3 |
| 43 substitutions.push_back(title); // $4 | 50 substitutions.push_back(viewer::GetBodyCssClass(theme)); // $4 |
| 44 substitutions.push_back(content); // $5 | 51 substitutions.push_back(content); // $5 |
| 45 substitutions.push_back(loading_indicator_class); // $6 | 52 substitutions.push_back(loading_indicator_class); // $6 |
| 46 substitutions.push_back( | 53 substitutions.push_back( |
| 47 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 | 54 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 |
| 48 substitutions.push_back(original_url); // $8 | 55 substitutions.push_back(original_url); // $8 |
| 49 substitutions.push_back( | 56 substitutions.push_back( |
| 50 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 | 57 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 |
| 51 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 58 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
| 52 } | 59 } |
| 53 | 60 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 69 } | 76 } |
| 70 | 77 |
| 71 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { | 78 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { |
| 72 if (is_last_page) | 79 if (is_last_page) |
| 73 return "showLoadingIndicator(true);"; | 80 return "showLoadingIndicator(true);"; |
| 74 else | 81 else |
| 75 return "showLoadingIndicator(false);"; | 82 return "showLoadingIndicator(false);"; |
| 76 } | 83 } |
| 77 | 84 |
| 78 const std::string GetUnsafePartialArticleHtml( | 85 const std::string GetUnsafePartialArticleHtml( |
| 79 const DistilledPageProto* page_proto) { | 86 const DistilledPageProto* page_proto, |
| 87 const ReaderModePrefs::Theme theme) { | |
| 80 DCHECK(page_proto); | 88 DCHECK(page_proto); |
| 81 std::string title = net::EscapeForHTML(page_proto->title()); | 89 std::string title = net::EscapeForHTML(page_proto->title()); |
| 82 std::ostringstream unsafe_output_stream; | 90 std::ostringstream unsafe_output_stream; |
| 83 unsafe_output_stream << page_proto->html(); | 91 unsafe_output_stream << page_proto->html(); |
| 84 std::string unsafe_article_html = unsafe_output_stream.str(); | 92 std::string unsafe_article_html = unsafe_output_stream.str(); |
| 85 std::string original_url = page_proto->url(); | 93 std::string original_url = page_proto->url(); |
| 86 return ReplaceHtmlTemplateValues(title, | 94 return ReplaceHtmlTemplateValues(title, |
| 87 unsafe_article_html, | 95 unsafe_article_html, |
| 88 "visible", | 96 "visible", |
| 89 original_url); | 97 original_url, |
| 98 theme); | |
| 90 } | 99 } |
| 91 | 100 |
| 92 const std::string GetUnsafeArticleHtml( | 101 const std::string GetUnsafeArticleHtml( |
| 93 const DistilledArticleProto* article_proto) { | 102 const DistilledArticleProto* article_proto, |
| 103 const ReaderModePrefs::Theme theme) { | |
| 94 DCHECK(article_proto); | 104 DCHECK(article_proto); |
| 95 std::string title; | 105 std::string title; |
| 96 std::string unsafe_article_html; | 106 std::string unsafe_article_html; |
| 97 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 107 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
| 98 article_proto->pages(0).has_html()) { | 108 article_proto->pages(0).has_html()) { |
| 99 title = net::EscapeForHTML(article_proto->title()); | 109 title = net::EscapeForHTML(article_proto->title()); |
| 100 std::ostringstream unsafe_output_stream; | 110 std::ostringstream unsafe_output_stream; |
| 101 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 111 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
| 102 unsafe_output_stream << article_proto->pages(page_num).html(); | 112 unsafe_output_stream << article_proto->pages(page_num).html(); |
| 103 } | 113 } |
| 104 unsafe_article_html = unsafe_output_stream.str(); | 114 unsafe_article_html = unsafe_output_stream.str(); |
| 105 } else { | 115 } else { |
| 106 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 116 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
| 107 unsafe_article_html = | 117 unsafe_article_html = |
| 108 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 118 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 109 } | 119 } |
| 110 | 120 |
| 111 std::string original_url; | 121 std::string original_url; |
| 112 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 122 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
| 113 original_url = article_proto->pages(0).url(); | 123 original_url = article_proto->pages(0).url(); |
| 114 } | 124 } |
| 115 | 125 |
| 116 return ReplaceHtmlTemplateValues(title, | 126 return ReplaceHtmlTemplateValues(title, |
| 117 unsafe_article_html, | 127 unsafe_article_html, |
| 118 "hidden", | 128 "hidden", |
| 119 original_url); | 129 original_url, |
| 130 theme); | |
| 120 } | 131 } |
| 121 | 132 |
| 122 const std::string GetErrorPageHtml() { | 133 const std::string GetErrorPageHtml(const ReaderModePrefs::Theme theme) { |
| 123 std::string title = l10n_util::GetStringUTF8( | 134 std::string title = l10n_util::GetStringUTF8( |
| 124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 135 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
| 125 std::string content = l10n_util::GetStringUTF8( | 136 std::string content = l10n_util::GetStringUTF8( |
| 126 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 137 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
| 127 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); | 138 return ReplaceHtmlTemplateValues(title, content, "hidden", "", theme); |
| 128 } | 139 } |
| 129 | 140 |
| 130 const std::string GetCss() { | 141 const std::string GetCss() { |
| 131 return ResourceBundle::GetSharedInstance() | 142 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 132 .GetRawDataResource(IDR_DISTILLER_CSS) | 143 IDR_DISTILLER_CSS).as_string(); |
| 133 .as_string(); | |
| 134 } | 144 } |
| 135 | 145 |
| 136 const std::string GetJavaScript() { | 146 const std::string GetJavaScript() { |
| 137 return ResourceBundle::GetSharedInstance() | 147 return ResourceBundle::GetSharedInstance() |
| 138 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) | 148 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
| 139 .as_string(); | 149 .as_string(); |
| 140 } | 150 } |
| 141 | 151 |
| 142 scoped_ptr<ViewerHandle> CreateViewRequest( | 152 scoped_ptr<ViewerHandle> CreateViewRequest( |
| 143 DomDistillerServiceInterface* dom_distiller_service, | 153 DomDistillerServiceInterface* dom_distiller_service, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 168 return dom_distiller_service->ViewUrl(view_request_delegate, | 178 return dom_distiller_service->ViewUrl(view_request_delegate, |
| 169 dom_distiller_service | 179 dom_distiller_service |
| 170 ->CreateDefaultDistillerPage(), | 180 ->CreateDefaultDistillerPage(), |
| 171 requested_url).Pass(); | 181 requested_url).Pass(); |
| 172 } | 182 } |
| 173 | 183 |
| 174 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 184 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
| 175 return scoped_ptr<ViewerHandle>(); | 185 return scoped_ptr<ViewerHandle>(); |
| 176 } | 186 } |
| 177 | 187 |
| 188 const char* GetBodyCssClass(ReaderModePrefs::Theme theme) { | |
|
nyquist
2014/06/27 20:48:27
I guess this could be added a unittest for?
smaslo
2014/06/30 17:57:15
Done.
| |
| 189 if (theme == ReaderModePrefs::Theme::kDark) { | |
| 190 return kDarkClassName; | |
| 191 } else if (theme == ReaderModePrefs::Theme::kSepia) { | |
| 192 return kSepiaClassName; | |
| 193 } else { | |
| 194 return kLightClassName; | |
|
nyquist
2014/06/27 20:48:27
Remove the else block and just return kLightClassN
smaslo
2014/06/30 17:57:15
Done.
| |
| 195 } | |
| 196 } | |
| 197 | |
| 178 } // namespace viewer | 198 } // namespace viewer |
| 179 | 199 |
| 180 } // namespace dom_distiller | 200 } // namespace dom_distiller |
| OLD | NEW |