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

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

Issue 235833003: Move helper utilities for the DOM Distiller Viewer to core. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed iOS grit whitelist Created 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/dom_distiller/core/viewer.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "components/dom_distiller/core/dom_distiller_service.h"
13 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
14 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
15 #include "components/dom_distiller/core/task_tracker.h"
16 #include "components/dom_distiller/core/url_constants.h"
17 #include "components/dom_distiller/core/url_utils.h"
18 #include "grit/component_resources.h"
19 #include "grit/component_strings.h"
20 #include "net/base/escape.h"
21 #include "net/url_request/url_request.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "url/gurl.h"
25
26 namespace dom_distiller {
27
28 namespace {
29
30 std::string ReplaceHtmlTemplateValues(const std::string& title,
31 const std::string& content,
32 const std::string& original_url) {
33 base::StringPiece html_template =
34 ResourceBundle::GetSharedInstance().GetRawDataResource(
35 IDR_DOM_DISTILLER_VIEWER_HTML);
36 std::vector<std::string> substitutions;
37 substitutions.push_back(title); // $1
38 substitutions.push_back(kCssPath); // $2
39 substitutions.push_back(title); // $3
40 substitutions.push_back(content); // $4
41 substitutions.push_back(original_url); // $5
42 substitutions.push_back(
43 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $6
44 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
45 }
46
47 } // namespace
48
49 namespace viewer {
50
51 const std::string GetUnsafeHtml(const DistilledArticleProto* article_proto) {
52 DCHECK(article_proto);
53 std::string title;
54 std::string unsafe_article_html;
55 if (article_proto->has_title() && article_proto->pages_size() > 0 &&
56 article_proto->pages(0).has_html()) {
57 title = net::EscapeForHTML(article_proto->title());
58 // TODO(shashishekhar): Add support for correcting displaying multiple pages
59 // after discussing the right way to display them.
60 std::ostringstream unsafe_output_stream;
61 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) {
62 unsafe_output_stream << article_proto->pages(page_num).html();
63 }
64 unsafe_article_html = unsafe_output_stream.str();
65 } else {
66 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE);
67 unsafe_article_html =
68 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT);
69 }
70
71 std::string original_url;
72 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) {
73 original_url = article_proto->pages(0).url();
74 }
75
76 return ReplaceHtmlTemplateValues(title, unsafe_article_html, original_url);
77 }
78
79 const std::string GetErrorPageHtml() {
80 std::string title = l10n_util::GetStringUTF8(
81 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
82 std::string content = l10n_util::GetStringUTF8(
83 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
84 return ReplaceHtmlTemplateValues(title, content, "");
85 }
86
87 const std::string GetCss() {
88 return ResourceBundle::GetSharedInstance()
89 .GetRawDataResource(IDR_DISTILLER_CSS)
90 .as_string();
91 }
92
93 scoped_ptr<ViewerHandle> CreateViewRequest(
94 DomDistillerServiceInterface* dom_distiller_service,
95 const std::string& path,
96 ViewRequestDelegate* view_request_delegate) {
97 std::string entry_id =
98 url_utils::GetValueForKeyInUrlPathQuery(path, kEntryIdKey);
99 bool has_valid_entry_id = !entry_id.empty();
100 entry_id = StringToUpperASCII(entry_id);
101
102 std::string requested_url_str =
103 url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey);
104 GURL requested_url(requested_url_str);
105 bool has_valid_url = url_utils::IsUrlDistillable(requested_url);
106
107 if (has_valid_entry_id && has_valid_url) {
108 // It is invalid to specify a query param for both |kEntryIdKey| and
109 // |kUrlKey|.
110 return scoped_ptr<ViewerHandle>();
111 }
112
113 if (has_valid_entry_id) {
114 return dom_distiller_service->ViewEntry(view_request_delegate, entry_id)
115 .Pass();
116 } else if (has_valid_url) {
117 return dom_distiller_service->ViewUrl(view_request_delegate, requested_url)
118 .Pass();
119 }
120
121 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|.
122 return scoped_ptr<ViewerHandle>();
123 }
124
125 } // namespace viewer
126
127 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/core/viewer.h ('k') | components/dom_distiller/core/viewer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698