Chromium Code Reviews| 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 | 9 |
| 9 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 #include "components/dom_distiller/core/dom_distiller_service.h" | |
| 15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | |
| 16 #include "components/dom_distiller/core/task_tracker.h" | |
| 11 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "grit/component_strings.h" | |
| 19 #include "grit/dom_distiller_resources.h" | |
| 12 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 #include "ui/base/resource/resource_bundle.h" | |
| 13 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 14 | 24 |
| 25 namespace { | |
| 26 | |
| 27 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) { | |
| 28 base::StringPiece html_template = | |
| 29 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 30 IDR_DOM_DISTILLER_VIEWER_HTML); | |
| 31 std::vector<std::string> substitutions; | |
| 32 substitutions.push_back(title); // $1 | |
| 33 substitutions.push_back(title); // $2 | |
| 34 substitutions.push_back(content); // $3 | |
| 35 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | |
| 36 } | |
| 37 } | |
|
Nico
2014/01/15 05:31:48
nit: newline above, this add " // namespace"
nyquist
2014/01/15 22:21:43
Done.
| |
| 38 | |
| 15 namespace dom_distiller { | 39 namespace dom_distiller { |
| 16 | 40 |
| 17 DomDistillerViewerSource::DomDistillerViewerSource() {} | 41 // Handles receiving data asynchronously for a specific entry, and passing |
| 42 // it along to the data callback for the data source. | |
| 43 class RequestViewerHandle : public ViewRequestDelegate { | |
| 44 public: | |
| 45 explicit RequestViewerHandle( | |
| 46 const content::URLDataSource::GotDataCallback& callback); | |
| 47 virtual ~RequestViewerHandle(); | |
| 48 | |
| 49 // ViewRequestDelegate implementation. | |
| 50 virtual void OnArticleReady(DistilledPageProto* proto) OVERRIDE; | |
| 51 | |
| 52 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | |
| 53 | |
| 54 private: | |
| 55 // The handle to the view request towards the DomDistillerService. It | |
| 56 // needs to be kept around to ensure the distillation request finishes. | |
| 57 scoped_ptr<ViewerHandle> viewer_handle_; | |
| 58 | |
| 59 // This holds the callback to where the data retreived is sent back. | |
| 60 content::URLDataSource::GotDataCallback callback_; | |
| 61 }; | |
| 62 | |
| 63 RequestViewerHandle::RequestViewerHandle( | |
| 64 const content::URLDataSource::GotDataCallback& callback) | |
| 65 : callback_(callback) {} | |
| 66 | |
| 67 RequestViewerHandle::~RequestViewerHandle() {} | |
| 68 | |
| 69 void RequestViewerHandle::OnArticleReady(DistilledPageProto* proto) { | |
| 70 DCHECK(proto); | |
| 71 std::string title; | |
|
Tom Sepez
2014/01/14 22:25:56
Do we know that title doesn't itself contain marku
nyquist
2014/01/14 22:26:53
No, we don't. It should be sanitized.
nyquist
2014/01/15 22:21:43
Done.
| |
| 72 std::string article_html; | |
| 73 if (proto->has_title() && proto->has_html()) { | |
| 74 title = proto->title(); | |
| 75 article_html = proto->html(); | |
| 76 } else { | |
| 77 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | |
| 78 article_html = | |
| 79 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | |
| 80 } | |
| 81 std::string html = ReplaceHtmlTemplateValues(title, article_html); | |
| 82 callback_.Run(base::RefCountedString::TakeString(&html)); | |
| 83 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 84 } | |
| 85 | |
| 86 void RequestViewerHandle::TakeViewerHandle( | |
| 87 scoped_ptr<ViewerHandle> viewer_handle) { | |
| 88 viewer_handle_ = viewer_handle.Pass(); | |
| 89 } | |
| 90 | |
| 91 DomDistillerViewerSource::DomDistillerViewerSource( | |
| 92 DomDistillerService* dom_distiller_service) | |
| 93 : dom_distiller_service_(dom_distiller_service) {} | |
| 18 | 94 |
| 19 DomDistillerViewerSource::~DomDistillerViewerSource() {} | 95 DomDistillerViewerSource::~DomDistillerViewerSource() {} |
| 20 | 96 |
| 21 std::string DomDistillerViewerSource::GetSource() const { | 97 std::string DomDistillerViewerSource::GetSource() const { |
| 22 return chrome::kDomDistillerScheme; | 98 return chrome::kDomDistillerScheme; |
| 23 } | 99 } |
| 24 | 100 |
| 25 void DomDistillerViewerSource::StartDataRequest( | 101 void DomDistillerViewerSource::StartDataRequest( |
| 26 const std::string& path, | 102 const std::string& path, |
| 27 int render_process_id, | 103 int render_process_id, |
| 28 int render_view_id, | 104 int render_view_id, |
| 29 const content::URLDataSource::GotDataCallback& callback) { | 105 const content::URLDataSource::GotDataCallback& callback) { |
| 30 std::string page_template = "Aloha!"; | 106 RequestViewerHandle* request_viewer_handle = |
| 31 callback.Run(base::RefCountedString::TakeString(&page_template)); | 107 new RequestViewerHandle(callback); |
| 108 std::string entry_id = StringToUpperASCII(path); | |
| 109 scoped_ptr<ViewerHandle> viewer_handle = | |
| 110 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id); | |
| 111 if (viewer_handle) { | |
| 112 // The service returned a |ViewerHandle| and guarantees it will call | |
| 113 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | |
| 114 // request is not cancelled. | |
| 115 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | |
| 116 } else { | |
| 117 // The service did not return a |ViewerHandle|, which means the | |
| 118 // |RequestViewerHandle| will never be called, so cleanup now. | |
|
Nico
2014/01/15 05:31:48
nit "clean up now"
nyquist
2014/01/15 22:21:43
Done.
| |
| 119 delete request_viewer_handle; | |
| 120 | |
| 121 std::string title = l10n_util::GetStringUTF8( | |
| 122 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | |
| 123 std::string content = l10n_util::GetStringUTF8( | |
| 124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | |
| 125 std::string html = ReplaceHtmlTemplateValues(title, content); | |
| 126 callback.Run(base::RefCountedString::TakeString(&html)); | |
| 127 } | |
| 32 }; | 128 }; |
| 33 | 129 |
| 34 std::string DomDistillerViewerSource::GetMimeType( | 130 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) |
| 35 const std::string& path) const { | 131 const { |
| 36 return "text/html"; | 132 return "text/html"; |
| 37 } | 133 } |
| 38 | 134 |
| 39 bool DomDistillerViewerSource::ShouldServiceRequest( | 135 bool DomDistillerViewerSource::ShouldServiceRequest( |
| 40 const net::URLRequest* request) const { | 136 const net::URLRequest* request) const { |
| 41 return request->url().SchemeIs(chrome::kDomDistillerScheme); | 137 return request->url().SchemeIs(chrome::kDomDistillerScheme); |
| 42 } | 138 } |
| 43 | 139 |
| 140 void DomDistillerViewerSource::WillServiceRequest( | |
| 141 const net::URLRequest* request, | |
| 142 std::string* path) const { | |
| 143 // Since the full request is not available to StartDataRequest, replace the | |
| 144 // path to contain the data needed. | |
| 145 path->assign(request->url().host()); | |
|
Nico
2014/01/15 05:31:48
nit: imho `*path = ...` is way more readable
nyquist
2014/01/15 22:21:43
Done.
| |
| 146 }; | |
| 147 | |
| 44 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { | 148 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { |
| 45 return "frame-src none;"; | 149 return "frame-src none;"; |
| 46 } | 150 } |
| 47 | 151 |
| 48 } // namespace dom_distiller | 152 } // namespace dom_distiller |
| OLD | NEW |