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" |
| 13 #include "base/strings/utf_string_conversions.h" |
12 #include "components/dom_distiller/core/dom_distiller_service.h" | 14 #include "components/dom_distiller/core/dom_distiller_service.h" |
13 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 15 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
14 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 16 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
15 #include "components/dom_distiller/core/task_tracker.h" | 17 #include "components/dom_distiller/core/task_tracker.h" |
16 #include "components/dom_distiller/core/url_constants.h" | 18 #include "components/dom_distiller/core/url_constants.h" |
17 #include "components/dom_distiller/core/url_utils.h" | 19 #include "components/dom_distiller/core/url_utils.h" |
18 #include "grit/component_resources.h" | 20 #include "grit/component_resources.h" |
19 #include "grit/component_strings.h" | 21 #include "grit/component_strings.h" |
20 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
21 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
22 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
25 | 27 |
26 namespace dom_distiller { | 28 namespace dom_distiller { |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 std::string ReplaceHtmlTemplateValues(const std::string& title, | 32 std::string ReplaceHtmlTemplateValues( |
31 const std::string& content, | 33 const std::string& title, |
32 const std::string& original_url) { | 34 const std::string& content, |
| 35 const std::string& original_url, |
| 36 const std::string& loading_indicator_style) { |
33 base::StringPiece html_template = | 37 base::StringPiece html_template = |
34 ResourceBundle::GetSharedInstance().GetRawDataResource( | 38 ResourceBundle::GetSharedInstance().GetRawDataResource( |
35 IDR_DOM_DISTILLER_VIEWER_HTML); | 39 IDR_DOM_DISTILLER_VIEWER_HTML); |
36 std::vector<std::string> substitutions; | 40 std::vector<std::string> substitutions; |
37 substitutions.push_back(title); // $1 | 41 substitutions.push_back(title); // $1 |
38 substitutions.push_back(kCssPath); // $2 | 42 substitutions.push_back(kCssPath); // $2 |
39 substitutions.push_back(title); // $3 | 43 substitutions.push_back(title); // $3 |
40 substitutions.push_back(content); // $4 | 44 substitutions.push_back(content); // $4 |
41 substitutions.push_back(original_url); // $5 | 45 substitutions.push_back(loading_indicator_style); // $5 |
| 46 substitutions.push_back(original_url); // $6 |
42 substitutions.push_back( | 47 substitutions.push_back( |
43 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $6 | 48 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_VIEW_ORIGINAL)); // $7 |
44 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 49 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
45 } | 50 } |
46 | 51 |
47 } // namespace | 52 } // namespace |
48 | 53 |
49 namespace viewer { | 54 namespace viewer { |
50 | 55 |
51 const std::string GetUnsafeHtml(const DistilledArticleProto* article_proto) { | 56 const base::string16 GetUnsafeIncrementalDistilledPageJs( |
| 57 const DistilledPageProto* page_proto, |
| 58 const bool is_last_page) { |
| 59 std::string output; |
| 60 base::StringValue value(page_proto->html()); |
| 61 base::JSONWriter::Write(&value, &output); |
| 62 std::string page_update("addToPage("); |
| 63 page_update += output + ");"; |
| 64 return base::UTF8ToUTF16(page_update) + GetToggleLoadingIndicatorJs( |
| 65 is_last_page); |
| 66 |
| 67 } |
| 68 |
| 69 const base::string16 GetToggleLoadingIndicatorJs(const bool is_last_page) { |
| 70 if (is_last_page) |
| 71 return base::UTF8ToUTF16("showLoadingIndicator(true);"); |
| 72 else |
| 73 return base::UTF8ToUTF16("showLoadingIndicator(false);"); |
| 74 } |
| 75 |
| 76 const std::string GetUnsafePartialArticleHtml( |
| 77 const DistilledPageProto* page_proto) { |
| 78 DCHECK(page_proto); |
| 79 std::string title = net::EscapeForHTML(page_proto->title()); |
| 80 std::string loading_indicator_style = "block"; |
| 81 std::ostringstream unsafe_output_stream; |
| 82 unsafe_output_stream << page_proto->html(); |
| 83 std::string unsafe_article_html = unsafe_output_stream.str(); |
| 84 std::string original_url = page_proto->url(); |
| 85 return ReplaceHtmlTemplateValues(title, |
| 86 unsafe_article_html, |
| 87 original_url, |
| 88 loading_indicator_style); |
| 89 } |
| 90 |
| 91 const std::string GetUnsafeArticleHtml( |
| 92 const DistilledArticleProto* article_proto) { |
52 DCHECK(article_proto); | 93 DCHECK(article_proto); |
53 std::string title; | 94 std::string title; |
54 std::string unsafe_article_html; | 95 std::string unsafe_article_html; |
55 if (article_proto->has_title() && article_proto->pages_size() > 0 && | 96 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
56 article_proto->pages(0).has_html()) { | 97 article_proto->pages(0).has_html()) { |
57 title = net::EscapeForHTML(article_proto->title()); | 98 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; | 99 std::ostringstream unsafe_output_stream; |
61 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { | 100 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
62 unsafe_output_stream << article_proto->pages(page_num).html(); | 101 unsafe_output_stream << article_proto->pages(page_num).html(); |
63 } | 102 } |
64 unsafe_article_html = unsafe_output_stream.str(); | 103 unsafe_article_html = unsafe_output_stream.str(); |
65 } else { | 104 } else { |
66 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 105 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
67 unsafe_article_html = | 106 unsafe_article_html = |
68 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 107 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
69 } | 108 } |
70 | 109 |
71 std::string original_url; | 110 std::string original_url; |
72 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { | 111 if (article_proto->pages_size() > 0 && article_proto->pages(0).has_url()) { |
73 original_url = article_proto->pages(0).url(); | 112 original_url = article_proto->pages(0).url(); |
74 } | 113 } |
75 | 114 |
76 return ReplaceHtmlTemplateValues(title, unsafe_article_html, original_url); | 115 std::string loading_indicator_style = "none"; |
| 116 return ReplaceHtmlTemplateValues(title, |
| 117 unsafe_article_html, |
| 118 original_url, |
| 119 loading_indicator_style); |
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, "", "none"); |
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 15 matching lines...) Expand all Loading... |
118 .Pass(); | 167 .Pass(); |
119 } | 168 } |
120 | 169 |
121 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. | 170 // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|. |
122 return scoped_ptr<ViewerHandle>(); | 171 return scoped_ptr<ViewerHandle>(); |
123 } | 172 } |
124 | 173 |
125 } // namespace viewer | 174 } // namespace viewer |
126 | 175 |
127 } // namespace dom_distiller | 176 } // namespace dom_distiller |
OLD | NEW |