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/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 19 #include "grit/component_strings.h" | |
| 20 #include "grit/dom_distiller_resources.h" | |
| 21 #include "net/base/escape.h" | |
| 13 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/resource/resource_bundle.h" | |
| 14 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 15 | 26 |
| 27 namespace { | |
| 28 | |
| 29 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) { | |
| 30 base::StringPiece html_template = | |
| 31 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 32 IDR_DOM_DISTILLER_VIEWER_HTML); | |
| 33 std::vector<std::string> substitutions; | |
| 34 substitutions.push_back(title); // $1 | |
| 35 substitutions.push_back(title); // $2 | |
| 36 substitutions.push_back(content); // $3 | |
| 37 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 16 namespace dom_distiller { | 42 namespace dom_distiller { |
| 17 | 43 |
| 18 DomDistillerViewerSource::DomDistillerViewerSource(const std::string& scheme) | 44 // Handles receiving data asynchronously for a specific entry, and passing |
| 19 : scheme_(scheme) {} | 45 // it along to the data callback for the data source. |
| 46 class RequestViewerHandle : public ViewRequestDelegate { | |
| 47 public: | |
| 48 explicit RequestViewerHandle( | |
| 49 const content::URLDataSource::GotDataCallback& callback); | |
| 50 virtual ~RequestViewerHandle(); | |
| 51 | |
| 52 // ViewRequestDelegate implementation. | |
| 53 virtual void OnArticleReady(DistilledPageProto* proto) OVERRIDE; | |
| 54 | |
| 55 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | |
| 56 | |
| 57 private: | |
| 58 // The handle to the view request towards the DomDistillerService. It | |
| 59 // needs to be kept around to ensure the distillation request finishes. | |
| 60 scoped_ptr<ViewerHandle> viewer_handle_; | |
| 61 | |
| 62 // This holds the callback to where the data retreived is sent back. | |
|
cjhopman
2014/01/28 02:56:56
s/retreived/retrieved/
nyquist
2014/01/28 18:16:12
Done.
| |
| 63 content::URLDataSource::GotDataCallback callback_; | |
| 64 }; | |
| 65 | |
| 66 RequestViewerHandle::RequestViewerHandle( | |
| 67 const content::URLDataSource::GotDataCallback& callback) | |
| 68 : callback_(callback) {} | |
| 69 | |
| 70 RequestViewerHandle::~RequestViewerHandle() {} | |
| 71 | |
| 72 void RequestViewerHandle::OnArticleReady(DistilledPageProto* proto) { | |
| 73 DCHECK(proto); | |
| 74 std::string title; | |
| 75 std::string unsafe_article_html; | |
| 76 if (proto->has_title() && proto->has_html()) { | |
| 77 title = net::EscapeForHTML(proto->title()); | |
| 78 unsafe_article_html = proto->html(); | |
| 79 } else { | |
| 80 title = l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); | |
| 81 unsafe_article_html = | |
| 82 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); | |
| 83 } | |
| 84 std::string unsafe_page_html = | |
| 85 ReplaceHtmlTemplateValues(title, unsafe_article_html); | |
| 86 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | |
| 87 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 88 } | |
| 89 | |
| 90 void RequestViewerHandle::TakeViewerHandle( | |
| 91 scoped_ptr<ViewerHandle> viewer_handle) { | |
| 92 viewer_handle_ = viewer_handle.Pass(); | |
| 93 } | |
| 94 | |
| 95 DomDistillerViewerSource::DomDistillerViewerSource( | |
| 96 DomDistillerService* dom_distiller_service, const std::string& scheme) | |
| 97 : scheme_(scheme), | |
| 98 dom_distiller_service_(dom_distiller_service) {} | |
| 20 | 99 |
| 21 DomDistillerViewerSource::~DomDistillerViewerSource() {} | 100 DomDistillerViewerSource::~DomDistillerViewerSource() {} |
| 22 | 101 |
| 23 std::string DomDistillerViewerSource::GetSource() const { return scheme_; } | 102 std::string DomDistillerViewerSource::GetSource() const { return scheme_; } |
| 24 | 103 |
| 25 void DomDistillerViewerSource::StartDataRequest( | 104 void DomDistillerViewerSource::StartDataRequest( |
| 26 const std::string& path, | 105 const std::string& path, |
| 27 int render_process_id, | 106 int render_process_id, |
| 28 int render_frame_id, | 107 int render_frame_id, |
| 29 const content::URLDataSource::GotDataCallback& callback) { | 108 const content::URLDataSource::GotDataCallback& callback) { |
| 30 content::RenderFrameHost* render_frame_host = | 109 content::RenderFrameHost* render_frame_host = |
| 31 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | 110 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 32 DCHECK(render_frame_host); | 111 DCHECK(render_frame_host); |
| 33 content::RenderViewHost* render_view_host = | 112 content::RenderViewHost* render_view_host = |
| 34 render_frame_host->GetRenderViewHost(); | 113 render_frame_host->GetRenderViewHost(); |
| 35 DCHECK(render_view_host); | 114 DCHECK(render_view_host); |
| 36 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 115 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
| 37 | 116 |
| 38 std::string page_template = "Aloha!"; | 117 RequestViewerHandle* request_viewer_handle = |
|
cjhopman
2014/01/28 02:56:56
What do you think about making this a scoped_ptr,
nyquist
2014/01/28 18:16:12
Done.
| |
| 39 callback.Run(base::RefCountedString::TakeString(&page_template)); | 118 new RequestViewerHandle(callback); |
| 119 std::string entry_id = StringToUpperASCII(path); | |
| 120 scoped_ptr<ViewerHandle> viewer_handle = | |
| 121 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id); | |
| 122 if (viewer_handle) { | |
| 123 // The service returned a |ViewerHandle| and guarantees it will call | |
| 124 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | |
| 125 // request is not cancelled. | |
| 126 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | |
| 127 } else { | |
| 128 // The service did not return a |ViewerHandle|, which means the | |
| 129 // |RequestViewerHandle| will never be called, so clean up now. | |
| 130 delete request_viewer_handle; | |
| 131 | |
| 132 std::string title = l10n_util::GetStringUTF8( | |
| 133 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | |
| 134 std::string content = l10n_util::GetStringUTF8( | |
| 135 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | |
| 136 std::string html = ReplaceHtmlTemplateValues(title, content); | |
| 137 callback.Run(base::RefCountedString::TakeString(&html)); | |
| 138 } | |
| 40 }; | 139 }; |
| 41 | 140 |
| 42 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) | 141 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) |
| 43 const { | 142 const { |
| 44 return "text/plain"; | 143 return "text/html"; |
| 45 } | 144 } |
| 46 | 145 |
| 47 bool DomDistillerViewerSource::ShouldServiceRequest( | 146 bool DomDistillerViewerSource::ShouldServiceRequest( |
| 48 const net::URLRequest* request) const { | 147 const net::URLRequest* request) const { |
| 49 return request->url().SchemeIs(scheme_.c_str()); | 148 return request->url().SchemeIs(scheme_.c_str()); |
| 50 } | 149 } |
| 51 | 150 |
| 151 void DomDistillerViewerSource::WillServiceRequest( | |
| 152 const net::URLRequest* request, | |
| 153 std::string* path) const { | |
| 154 // Since the full request is not available to StartDataRequest, replace the | |
| 155 // path to contain the data needed. | |
| 156 *path = request->url().host(); | |
| 157 }; | |
| 158 | |
| 52 } // namespace dom_distiller | 159 } // namespace dom_distiller |
| OLD | NEW |