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