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/content/dom_distiller_viewer_source.h" | 5 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "components/dom_distiller/core/dom_distiller_service.h" | 14 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 15 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 16 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
16 #include "components/dom_distiller/core/task_tracker.h" | 17 #include "components/dom_distiller/core/task_tracker.h" |
17 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
18 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
19 #include "grit/component_strings.h" | 20 #include "grit/component_strings.h" |
20 #include "grit/dom_distiller_resources.h" | 21 #include "grit/dom_distiller_resources.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" |
(...skipping 21 matching lines...) Expand all Loading... |
46 | 47 |
47 // Handles receiving data asynchronously for a specific entry, and passing | 48 // Handles receiving data asynchronously for a specific entry, and passing |
48 // it along to the data callback for the data source. | 49 // it along to the data callback for the data source. |
49 class RequestViewerHandle : public ViewRequestDelegate { | 50 class RequestViewerHandle : public ViewRequestDelegate { |
50 public: | 51 public: |
51 explicit RequestViewerHandle( | 52 explicit RequestViewerHandle( |
52 const content::URLDataSource::GotDataCallback& callback); | 53 const content::URLDataSource::GotDataCallback& callback); |
53 virtual ~RequestViewerHandle(); | 54 virtual ~RequestViewerHandle(); |
54 | 55 |
55 // ViewRequestDelegate implementation. | 56 // ViewRequestDelegate implementation. |
56 virtual void OnArticleReady(DistilledPageProto* proto) OVERRIDE; | 57 virtual void OnArticleReady(const DistilledArticleProto* article_proto) |
| 58 OVERRIDE; |
57 | 59 |
58 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | 60 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
59 | 61 |
60 private: | 62 private: |
61 // The handle to the view request towards the DomDistillerService. It | 63 // The handle to the view request towards the DomDistillerService. It |
62 // needs to be kept around to ensure the distillation request finishes. | 64 // needs to be kept around to ensure the distillation request finishes. |
63 scoped_ptr<ViewerHandle> viewer_handle_; | 65 scoped_ptr<ViewerHandle> viewer_handle_; |
64 | 66 |
65 // This holds the callback to where the data retrieved is sent back. | 67 // This holds the callback to where the data retrieved is sent back. |
66 content::URLDataSource::GotDataCallback callback_; | 68 content::URLDataSource::GotDataCallback callback_; |
67 }; | 69 }; |
68 | 70 |
69 RequestViewerHandle::RequestViewerHandle( | 71 RequestViewerHandle::RequestViewerHandle( |
70 const content::URLDataSource::GotDataCallback& callback) | 72 const content::URLDataSource::GotDataCallback& callback) |
71 : callback_(callback) {} | 73 : callback_(callback) {} |
72 | 74 |
73 RequestViewerHandle::~RequestViewerHandle() {} | 75 RequestViewerHandle::~RequestViewerHandle() {} |
74 | 76 |
75 void RequestViewerHandle::OnArticleReady(DistilledPageProto* proto) { | 77 void RequestViewerHandle::OnArticleReady( |
76 DCHECK(proto); | 78 const DistilledArticleProto* article_proto) { |
| 79 DCHECK(article_proto); |
77 std::string title; | 80 std::string title; |
78 std::string unsafe_article_html; | 81 std::string unsafe_article_html; |
79 if (proto->has_title() && proto->has_html()) { | 82 if (article_proto->has_title() && article_proto->pages_size() > 0 && |
80 title = net::EscapeForHTML(proto->title()); | 83 article_proto->pages(0).has_html()) { |
81 unsafe_article_html = proto->html(); | 84 title = net::EscapeForHTML(article_proto->title()); |
| 85 // TODO(shashishekhar): Add support for displaying multiple pages after |
| 86 // discussing |
| 87 // the right way to display them. |
| 88 std::stringstream unsafe_output_stream; |
| 89 for (int page_num = 0; page_num < article_proto->pages_size(); ++page_num) { |
| 90 unsafe_output_stream << article_proto->pages(page_num).html(); |
| 91 } |
| 92 unsafe_article_html = unsafe_output_stream.str(); |
82 } else { | 93 } else { |
83 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | 94 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
84 unsafe_article_html = | 95 unsafe_article_html = |
85 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | 96 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
86 } | 97 } |
87 std::string unsafe_page_html = | 98 std::string unsafe_page_html = |
88 ReplaceHtmlTemplateValues(title, unsafe_article_html); | 99 ReplaceHtmlTemplateValues(title, unsafe_article_html); |
89 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 100 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
90 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 101 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
91 } | 102 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 *path = request->url().host(); | 182 *path = request->url().host(); |
172 } | 183 } |
173 }; | 184 }; |
174 | 185 |
175 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 186 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
176 const { | 187 const { |
177 return "object-src 'none'; style-src 'self'"; | 188 return "object-src 'none'; style-src 'self'"; |
178 } | 189 } |
179 | 190 |
180 } // namespace dom_distiller | 191 } // namespace dom_distiller |
OLD | NEW |