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

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: Added browsertest, changed to use CSP object-src instead of frame-src 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 scoped_ptr<RequestViewerHandle> request_viewer_handle( 128 scoped_ptr<RequestViewerHandle> request_viewer_handle(
118 new RequestViewerHandle(callback)); 129 new RequestViewerHandle(callback));
130
119 std::string entry_id = StringToUpperASCII(path); 131 std::string entry_id = StringToUpperASCII(path);
120 scoped_ptr<ViewerHandle> viewer_handle = 132 scoped_ptr<ViewerHandle> viewer_handle =
121 dom_distiller_service_->ViewEntry(request_viewer_handle.get(), entry_id); 133 dom_distiller_service_->ViewEntry(request_viewer_handle.get(), entry_id);
122 if (viewer_handle) { 134 if (viewer_handle) {
123 // The service returned a |ViewerHandle| and guarantees it will call 135 // The service returned a |ViewerHandle| and guarantees it will call
124 // the |RequestViewerHandle|, so passing ownership to it, to ensure the 136 // the |RequestViewerHandle|, so passing ownership to it, to ensure the
125 // request is not cancelled. The |RequestViewerHandle| will delete itself 137 // request is not cancelled. The |RequestViewerHandle| will delete itself
126 // after receiving the callback. 138 // after receiving the callback.
127 request_viewer_handle.get()->TakeViewerHandle(viewer_handle.Pass()); 139 request_viewer_handle.get()->TakeViewerHandle(viewer_handle.Pass());
128 (void) request_viewer_handle.release(); 140 (void)request_viewer_handle.release();
129 } else { 141 } else {
130 std::string title = l10n_util::GetStringUTF8( 142 std::string title = l10n_util::GetStringUTF8(
131 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); 143 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE);
132 std::string content = l10n_util::GetStringUTF8( 144 std::string content = l10n_util::GetStringUTF8(
133 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); 145 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT);
134 std::string html = ReplaceHtmlTemplateValues(title, content); 146 std::string html = ReplaceHtmlTemplateValues(title, content);
135 callback.Run(base::RefCountedString::TakeString(&html)); 147 callback.Run(base::RefCountedString::TakeString(&html));
136 } 148 }
137 }; 149 };
138 150
139 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) 151 std::string DomDistillerViewerSource::GetMimeType(const std::string& path)
140 const { 152 const {
153 if (path == kCssPath)
154 return "text/css";
141 return "text/html"; 155 return "text/html";
142 } 156 }
143 157
144 bool DomDistillerViewerSource::ShouldServiceRequest( 158 bool DomDistillerViewerSource::ShouldServiceRequest(
145 const net::URLRequest* request) const { 159 const net::URLRequest* request) const {
146 return request->url().SchemeIs(scheme_.c_str()); 160 return request->url().SchemeIs(scheme_.c_str());
147 } 161 }
148 162
149 void DomDistillerViewerSource::WillServiceRequest( 163 void DomDistillerViewerSource::WillServiceRequest(
150 const net::URLRequest* request, 164 const net::URLRequest* request,
151 std::string* path) const { 165 std::string* path) const {
152 // Since the full request is not available to StartDataRequest, replace the 166 if (*path != kCssPath) {
153 // path to contain the data needed. 167 // Since the full request is not available to StartDataRequest, replace the
154 *path = request->url().host(); 168 // path to contain the data needed.
169 *path = request->url().host();
170 }
155 }; 171 };
156 172
173 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc()
174 const {
175 return "object-src 'none'; style-src 'self'";
176 }
177
157 } // namespace dom_distiller 178 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698