Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: components/dom_distiller/content/dom_distiller_viewer_source.cc

Issue 138923002: Add support for showing CSS with distilled articles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed nit from cjhopman@ Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_page.pb.h" 15 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
16 #include "components/dom_distiller/core/task_tracker.h" 16 #include "components/dom_distiller/core/task_tracker.h"
17 #include "content/public/browser/render_frame_host.h" 17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_view_host.h" 18 #include "content/public/browser/render_view_host.h"
19 #include "grit/component_strings.h" 19 #include "grit/component_strings.h"
20 #include "grit/dom_distiller_resources.h" 20 #include "grit/dom_distiller_resources.h"
21 #include "net/base/escape.h" 21 #include "net/base/escape.h"
22 #include "net/url_request/url_request.h" 22 #include "net/url_request/url_request.h"
23 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/resource/resource_bundle.h"
25 #include "url/gurl.h" 25 #include "url/gurl.h"
26 26
27 namespace { 27 namespace {
28 28
29 const char kCssPath[] = "readability.css";
30
29 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) { 31 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) {
30 base::StringPiece html_template = 32 base::StringPiece html_template =
31 ResourceBundle::GetSharedInstance().GetRawDataResource( 33 ResourceBundle::GetSharedInstance().GetRawDataResource(
32 IDR_DOM_DISTILLER_VIEWER_HTML); 34 IDR_DOM_DISTILLER_VIEWER_HTML);
33 std::vector<std::string> substitutions; 35 std::vector<std::string> substitutions;
34 substitutions.push_back(title); // $1 36 substitutions.push_back(title); // $1
35 substitutions.push_back(title); // $2 37 substitutions.push_back(kCssPath); // $2
36 substitutions.push_back(content); // $3 38 substitutions.push_back(title); // $3
39 substitutions.push_back(content); // $4
37 return ReplaceStringPlaceholders(html_template, substitutions, NULL); 40 return ReplaceStringPlaceholders(html_template, substitutions, NULL);
38 } 41 }
39 42
40 } // namespace 43 } // namespace
41 44
42 namespace dom_distiller { 45 namespace dom_distiller {
43 46
44 // Handles receiving data asynchronously for a specific entry, and passing 47 // Handles receiving data asynchronously for a specific entry, and passing
45 // it along to the data callback for the data source. 48 // it along to the data callback for the data source.
46 class RequestViewerHandle : public ViewRequestDelegate { 49 class RequestViewerHandle : public ViewRequestDelegate {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 int render_frame_id, 110 int render_frame_id,
108 const content::URLDataSource::GotDataCallback& callback) { 111 const content::URLDataSource::GotDataCallback& callback) {
109 content::RenderFrameHost* render_frame_host = 112 content::RenderFrameHost* render_frame_host =
110 content::RenderFrameHost::FromID(render_process_id, render_frame_id); 113 content::RenderFrameHost::FromID(render_process_id, render_frame_id);
111 DCHECK(render_frame_host); 114 DCHECK(render_frame_host);
112 content::RenderViewHost* render_view_host = 115 content::RenderViewHost* render_view_host =
113 render_frame_host->GetRenderViewHost(); 116 render_frame_host->GetRenderViewHost();
114 DCHECK(render_view_host); 117 DCHECK(render_view_host);
115 CHECK_EQ(0, render_view_host->GetEnabledBindings()); 118 CHECK_EQ(0, render_view_host->GetEnabledBindings());
116 119
120 if (kCssPath == path) {
121 std::string css = ResourceBundle::GetSharedInstance()
122 .GetRawDataResource(IDR_DISTILLER_CSS)
123 .as_string();
124 callback.Run(base::RefCountedString::TakeString(&css));
125 return;
126 }
127
117 RequestViewerHandle* request_viewer_handle = 128 RequestViewerHandle* request_viewer_handle =
118 new RequestViewerHandle(callback); 129 new RequestViewerHandle(callback);
119 std::string entry_id = StringToUpperASCII(path); 130 std::string entry_id = StringToUpperASCII(path);
120 scoped_ptr<ViewerHandle> viewer_handle = 131 scoped_ptr<ViewerHandle> viewer_handle =
121 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id); 132 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id);
122 if (viewer_handle) { 133 if (viewer_handle) {
123 // The service returned a |ViewerHandle| and guarantees it will call 134 // The service returned a |ViewerHandle| and guarantees it will call
124 // the |RequestViewerHandle|, so passing ownership to it, to ensure the 135 // the |RequestViewerHandle|, so passing ownership to it, to ensure the
125 // request is not cancelled. The |RequestViewerHandle| will delete itself 136 // request is not cancelled. The |RequestViewerHandle| will delete itself
126 // after receiving the callback. 137 // after receiving the callback.
127 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); 138 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass());
128 } else { 139 } else {
129 // The service did not return a |ViewerHandle|, which means the 140 // The service did not return a |ViewerHandle|, which means the
130 // |RequestViewerHandle| will never be called, so clean up now. 141 // |RequestViewerHandle| will never be called, so clean up now.
131 delete request_viewer_handle; 142 delete request_viewer_handle;
132 143
133 std::string title = l10n_util::GetStringUTF8( 144 std::string title = l10n_util::GetStringUTF8(
134 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 145 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
135 std::string content = l10n_util::GetStringUTF8( 146 std::string content = l10n_util::GetStringUTF8(
136 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 147 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
137 std::string html = ReplaceHtmlTemplateValues(title, content); 148 std::string html = ReplaceHtmlTemplateValues(title, content);
138 callback.Run(base::RefCountedString::TakeString(&html)); 149 callback.Run(base::RefCountedString::TakeString(&html));
139 } 150 }
140 }; 151 };
141 152
142 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) 153 std::string DomDistillerViewerSource::GetMimeType(const std::string& path)
143 const { 154 const {
155 if (path == kCssPath)
156 return "text/css";
144 return "text/html"; 157 return "text/html";
145 } 158 }
146 159
147 bool DomDistillerViewerSource::ShouldServiceRequest( 160 bool DomDistillerViewerSource::ShouldServiceRequest(
148 const net::URLRequest* request) const { 161 const net::URLRequest* request) const {
149 return request->url().SchemeIs(scheme_.c_str()); 162 return request->url().SchemeIs(scheme_.c_str());
150 } 163 }
151 164
152 void DomDistillerViewerSource::WillServiceRequest( 165 void DomDistillerViewerSource::WillServiceRequest(
153 const net::URLRequest* request, 166 const net::URLRequest* request,
154 std::string* path) const { 167 std::string* path) const {
155 // Since the full request is not available to StartDataRequest, replace the 168 if (*path != kCssPath) {
156 // path to contain the data needed. 169 // Since the full request is not available to StartDataRequest, replace the
157 *path = request->url().host(); 170 // path to contain the data needed.
171 *path = request->url().host();
172 }
158 }; 173 };
159 174
175 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc()
176 const {
177 return "object-src 'none'; style-src 'self'";
178 }
179
160 } // namespace dom_distiller 180 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698