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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "components/dom_distiller/core/dom_distiller_service.h" | 13 #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_article.pb.h" |
14 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 15 #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/task_tracker.h" |
16 #include "components/dom_distiller/core/url_constants.h" | 17 #include "components/dom_distiller/core/url_constants.h" |
17 #include "components/dom_distiller/core/url_utils.h" | 18 #include "components/dom_distiller/core/url_utils.h" |
18 #include "grit/component_resources.h" | 19 #include "grit/component_resources.h" |
19 #include "grit/component_strings.h" | 20 #include "grit/component_strings.h" |
20 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
26 namespace dom_distiller { | 27 namespace dom_distiller { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 std::string ReplaceHtmlTemplateValues(const std::string& title, | 31 std::string ReplaceHtmlTemplateValues( |
31 const std::string& content, | 32 const std::string& title, |
32 const std::string& original_url) { | 33 const std::string& content, |
| 34 const std::string& loading_indicator_class, |
| 35 const std::string& original_url) { |
33 base::StringPiece html_template = | 36 base::StringPiece html_template = |
34 ResourceBundle::GetSharedInstance().GetRawDataResource( | 37 ResourceBundle::GetSharedInstance().GetRawDataResource( |
35 IDR_DOM_DISTILLER_VIEWER_HTML); | 38 IDR_DOM_DISTILLER_VIEWER_HTML); |
36 std::vector<std::string> substitutions; | 39 std::vector<std::string> substitutions; |
37 substitutions.push_back(title); // $1 | 40 substitutions.push_back(title); // $1 |
38 substitutions.push_back(kCssPath); // $2 | 41 substitutions.push_back(kViewerCssPath); // $2 |
39 substitutions.push_back(title); // $3 | 42 substitutions.push_back(kViewerJsPath); // $3 |
40 substitutions.push_back(content); // $4 | 43 substitutions.push_back(title); // $4 |
41 substitutions.push_back(original_url); // $5 | 44 substitutions.push_back(content); // $5 |
| 45 substitutions.push_back(loading_indicator_class); // $6 |
42 substitutions.push_back( | 46 substitutions.push_back( |
43 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $6 | 47 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_LOADING_STRING)); // $7 |
| 48 substitutions.push_back(original_url); // $8 |
| 49 substitutions.push_back( |
| 50 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $9 |
44 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 51 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
45 } | 52 } |
46 | 53 |
47 } // namespace | 54 } // namespace |
48 | 55 |
49 namespace viewer { | 56 namespace viewer { |
50 | 57 |
51 const std::string GetUnsafeHtml(const DistilledArticleProto* article_proto) { | 58 const std::string GetUnsafeIncrementalDistilledPageJs( |
| 59 const DistilledPageProto* page_proto, |
| 60 const bool is_last_page) { |
| 61 std::string output; |
| 62 base::StringValue value(page_proto->html()); |
| 63 base::JSONWriter::Write(&value, &output); |
| 64 std::string page_update("addToPage("); |
| 65 page_update += output + ");"; |
| 66 return page_update + GetToggleLoadingIndicatorJs( |
| 67 is_last_page); |
| 68 |
| 69 } |
| 70 |
| 71 const std::string GetToggleLoadingIndicatorJs(const bool is_last_page) { |
| 72 if (is_last_page) |
| 73 return "showLoadingIndicator(true);"; |
| 74 else |
| 75 return "showLoadingIndicator(false);"; |
| 76 } |
| 77 |
| 78 const std::string GetUnsafePartialArticleHtml( |
| 79 const DistilledPageProto* page_proto) { |
| 80 DCHECK(page_proto); |
| 81 std::string title = net::EscapeForHTML(page_proto->title()); |
| 82 std::ostringstream unsafe_output_stream; |
| 83 unsafe_output_stream << page_proto->html(); |
| 84 std::string unsafe_article_html = unsafe_output_stream.str(); |
| 85 std::string original_url = page_proto->url(); |
| 86 return ReplaceHtmlTemplateValues(title, |
| 87 unsafe_article_html, |
| 88 "visible", |
| 89 original_url); |
| 90 } |
| 91 |
| 92 const std::string GetUnsafeArticleHtml( |
| 93 const DistilledArticleProto* article_proto) { |
52 DCHECK(article_proto); | 94 DCHECK(article_proto); |
53 std::string title; | 95 std::string title; |
54 std::string unsafe_article_html; | 96 std::string unsafe_article_html; |
55 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 97 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
56 article_proto->pages(0).has_html()) { | 98 article_proto->pages(0).has_html()) { |
57 title = net::EscapeForHTML(article_proto->title()); | 99 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; | 100 std::ostringstream unsafe_output_stream; |
61 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 101 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
62 unsafe_output_stream << article_proto->pages(page_num).html(); | 102 unsafe_output_stream << article_proto->pages(page_num).html(); |
63 } | 103 } |
64 unsafe_article_html = unsafe_output_stream.str(); | 104 unsafe_article_html = unsafe_output_stream.str(); |
65 } else { | 105 } else { |
66 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 106 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
67 unsafe_article_html = | 107 unsafe_article_html = |
68 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 108 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
69 } | 109 } |
70 | 110 |
71 std::string original_url; | 111 std::string original_url; |
72 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 112 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
73 original_url = article_proto->pages(0).url(); | 113 original_url = article_proto->pages(0).url(); |
74 } | 114 } |
75 | 115 |
76 return ReplaceHtmlTemplateValues(title, unsafe_article_html, original_url); | 116 return ReplaceHtmlTemplateValues(title, |
| 117 unsafe_article_html, |
| 118 "hidden", |
| 119 original_url); |
77 } | 120 } |
78 | 121 |
79 const std::string GetErrorPageHtml() { | 122 const std::string GetErrorPageHtml() { |
80 std::string title = l10n_util::GetStringUTF8( | 123 std::string title = l10n_util::GetStringUTF8( |
81 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
82 std::string content = l10n_util::GetStringUTF8( | 125 std::string content = l10n_util::GetStringUTF8( |
83 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 126 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
84 return ReplaceHtmlTemplateValues(title, content, ""); | 127 return ReplaceHtmlTemplateValues(title, content, "hidden", ""); |
85 } | 128 } |
86 | 129 |
87 const std::string GetCss() { | 130 const std::string GetCss() { |
88 return ResourceBundle::GetSharedInstance() | 131 return ResourceBundle::GetSharedInstance() |
89 .GetRawDataResource(IDR_DISTILLER_CSS) | 132 .GetRawDataResource(IDR_DISTILLER_CSS) |
90 .as_string(); | 133 .as_string(); |
91 } | 134 } |
92 | 135 |
| 136 const std::string GetJavaScript() { |
| 137 return ResourceBundle::GetSharedInstance() |
| 138 .GetRawDataResource(IDR_DOM_DISTILLER_VIEWER_JS) |
| 139 .as_string(); |
| 140 } |
| 141 |
93 scoped_ptr<ViewerHandle> CreateViewRequest( | 142 scoped_ptr<ViewerHandle> CreateViewRequest( |
94 DomDistillerServiceInterface* dom_distiller_service, | 143 DomDistillerServiceInterface* dom_distiller_service, |
95 const std::string& path, | 144 const std::string& path, |
96 ViewRequestDelegate* view_request_delegate) { | 145 ViewRequestDelegate* view_request_delegate) { |
97 std::string entry_id = | 146 std::string entry_id = |
98 url_utils::GetValueForKeyInUrlPathQuery(path, kEntryIdKey); | 147 url_utils::GetValueForKeyInUrlPathQuery(path, kEntryIdKey); |
99 bool has_valid_entry_id = !entry_id.empty(); | 148 bool has_valid_entry_id = !entry_id.empty(); |
100 entry_id = StringToUpperASCII(entry_id); | 149 entry_id = StringToUpperASCII(entry_id); |
101 | 150 |
102 std::string requested_url_str = | 151 std::string requested_url_str = |
(...skipping 19 matching lines...) Expand all Loading... |
122 requested_url).Pass(); | 171 requested_url).Pass(); |
123 } | 172 } |
124 | 173 |
125 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 174 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
126 return scoped_ptr<ViewerHandle>(); | 175 return scoped_ptr<ViewerHandle>(); |
127 } | 176 } |
128 | 177 |
129 } // namespace viewer | 178 } // namespace viewer |
130 | 179 |
131 } // namespace dom_distiller | 180 } // namespace dom_distiller |
OLD | NEW |