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 <sstream> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 73 |
74 // content::WebContentsObserver implementation: | 74 // content::WebContentsObserver implementation: |
75 void DidNavigateMainFrame( | 75 void DidNavigateMainFrame( |
76 const content::LoadCommittedDetails& details, | 76 const content::LoadCommittedDetails& details, |
77 const content::FrameNavigateParams& params) override; | 77 const content::FrameNavigateParams& params) override; |
78 void RenderProcessGone(base::TerminationStatus status) override; | 78 void RenderProcessGone(base::TerminationStatus status) override; |
79 void WebContentsDestroyed() override; | 79 void WebContentsDestroyed() override; |
80 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 80 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
81 const GURL& validated_url) override; | 81 const GURL& validated_url) override; |
82 | 82 |
83 private: | 83 protected: |
84 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | 84 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't |
85 // ready. | 85 // ready. |
86 void SendJavaScript(const std::string& buffer) override; | 86 void SendJavaScript(const std::string& buffer) override; |
87 | 87 |
88 // Cancels the current view request. Once called, no updates will be | 88 // Cancels the current view request. Once called, no updates will be |
89 // propagated to the view, and the request to DomDistillerService will be | 89 // propagated to the view, and the request to DomDistillerService will be |
90 // cancelled. | 90 // cancelled. |
91 void Cancel(); | 91 void Cancel(); |
92 | 92 |
93 // The scheme hosting the current view request; | 93 // The scheme hosting the current view request; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 return; | 182 return; |
183 } | 183 } |
184 waiting_for_page_ready_ = false; | 184 waiting_for_page_ready_ = false; |
185 if (buffer_.empty()) { | 185 if (buffer_.empty()) { |
186 return; | 186 return; |
187 } | 187 } |
188 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); | 188 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); |
189 buffer_.clear(); | 189 buffer_.clear(); |
190 } | 190 } |
191 | 191 |
| 192 class DomDistillerViewerSource::PrintPreviewRequestViewerHandle |
| 193 : public DomDistillerViewerSource::RequestViewerHandle { |
| 194 public: |
| 195 explicit PrintPreviewRequestViewerHandle( |
| 196 content::WebContents* web_contents, |
| 197 const std::string& expected_scheme, |
| 198 const std::string& expected_request_path, |
| 199 scoped_ptr<ContentDataCallback> callback, |
| 200 DistilledPagePrefs* distilled_page_prefs); |
| 201 ~PrintPreviewRequestViewerHandle() override; |
| 202 |
| 203 // ViewRequestDelegate implementation: |
| 204 void OnArticleReady(const DistilledArticleProto* article_proto) override; |
| 205 void OnArticleUpdated(ArticleDistillationUpdate article_update) override; |
| 206 |
| 207 private: |
| 208 // DistilledPagePrefs::Observer implementation: |
| 209 void OnChangeFontFamily( |
| 210 DistilledPagePrefs::FontFamily new_font_family) override; |
| 211 void OnChangeTheme(DistilledPagePrefs::Theme new_theme) override; |
| 212 }; |
| 213 |
| 214 DomDistillerViewerSource:: |
| 215 PrintPreviewRequestViewerHandle::PrintPreviewRequestViewerHandle( |
| 216 content::WebContents* web_contents, |
| 217 const std::string& expected_scheme, |
| 218 const std::string& expected_request_path, |
| 219 scoped_ptr<ContentDataCallback> callback, |
| 220 DistilledPagePrefs* distilled_page_prefs) |
| 221 : DomDistillerViewerSource::RequestViewerHandle( |
| 222 web_contents, |
| 223 expected_scheme, |
| 224 expected_request_path, |
| 225 callback.Pass(), |
| 226 distilled_page_prefs) { |
| 227 } |
| 228 |
| 229 DomDistillerViewerSource:: |
| 230 PrintPreviewRequestViewerHandle::~PrintPreviewRequestViewerHandle() { |
| 231 } |
| 232 |
| 233 void DomDistillerViewerSource::PrintPreviewRequestViewerHandle::OnArticleReady( |
| 234 const DistilledArticleProto* article_proto) { |
| 235 std::string content = dom_distiller::viewer::GetUnsafePrintPreviewHtml( |
| 236 article_proto, |
| 237 distilled_page_prefs_->GetTheme(), |
| 238 distilled_page_prefs_->GetFontFamily()); |
| 239 callback_->RunCallback(content); |
| 240 |
| 241 // No need to hold on to the ViewerHandle now that distillation is complete. |
| 242 viewer_handle_.reset(); |
| 243 } |
| 244 |
| 245 void DomDistillerViewerSource:: |
| 246 PrintPreviewRequestViewerHandle::OnArticleUpdated( |
| 247 ArticleDistillationUpdate article_update) { |
| 248 } |
| 249 |
| 250 void DomDistillerViewerSource::PrintPreviewRequestViewerHandle::OnChangeTheme( |
| 251 DistilledPagePrefs::Theme new_theme) { |
| 252 } |
| 253 |
| 254 void DomDistillerViewerSource:: |
| 255 PrintPreviewRequestViewerHandle::OnChangeFontFamily( |
| 256 DistilledPagePrefs::FontFamily new_font) { |
| 257 } |
| 258 |
192 DomDistillerViewerSource::DomDistillerViewerSource( | 259 DomDistillerViewerSource::DomDistillerViewerSource( |
193 DomDistillerServiceInterface* dom_distiller_service, | 260 DomDistillerServiceInterface* dom_distiller_service, |
194 const std::string& scheme, | 261 const std::string& scheme, |
195 scoped_ptr<ExternalFeedbackReporter> external_reporter) | 262 scoped_ptr<ExternalFeedbackReporter> external_reporter) |
196 : scheme_(scheme), | 263 : scheme_(scheme), |
197 dom_distiller_service_(dom_distiller_service), | 264 dom_distiller_service_(dom_distiller_service), |
198 external_feedback_reporter_(external_reporter.Pass()) { | 265 external_feedback_reporter_(external_reporter.Pass()) { |
199 } | 266 } |
200 | 267 |
201 DomDistillerViewerSource::~DomDistillerViewerSource() { | 268 DomDistillerViewerSource::~DomDistillerViewerSource() { |
(...skipping 13 matching lines...) Expand all Loading... |
215 if (!render_frame_host) return; | 282 if (!render_frame_host) return; |
216 content::RenderViewHost* render_view_host = | 283 content::RenderViewHost* render_view_host = |
217 render_frame_host->GetRenderViewHost(); | 284 render_frame_host->GetRenderViewHost(); |
218 DCHECK(render_view_host); | 285 DCHECK(render_view_host); |
219 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 286 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
220 | 287 |
221 if (kViewerCssPath == path) { | 288 if (kViewerCssPath == path) { |
222 std::string css = viewer::GetCss(); | 289 std::string css = viewer::GetCss(); |
223 callback.Run(base::RefCountedString::TakeString(&css)); | 290 callback.Run(base::RefCountedString::TakeString(&css)); |
224 return; | 291 return; |
| 292 } else if (kPrintPreviewViewerCssPath == path) { |
| 293 std::string css = viewer::GetPrintPreviewCss(); |
| 294 callback.Run(base::RefCountedString::TakeString(&css)); |
| 295 return; |
225 } else if (kViewerJsPath == path) { | 296 } else if (kViewerJsPath == path) { |
226 std::string js = viewer::GetJavaScript(); | 297 std::string js = viewer::GetJavaScript(); |
227 callback.Run(base::RefCountedString::TakeString(&js)); | 298 callback.Run(base::RefCountedString::TakeString(&js)); |
228 return; | 299 return; |
229 } else if (kViewerViewOriginalPath == path) { | 300 } else if (kViewerViewOriginalPath == path) { |
230 content::RecordAction(base::UserMetricsAction("DomDistiller_ViewOriginal")); | 301 content::RecordAction(base::UserMetricsAction("DomDistiller_ViewOriginal")); |
231 callback.Run(NULL); | 302 callback.Run(NULL); |
232 return; | 303 return; |
233 } else if (kFeedbackBad == path) { | 304 } else if (kFeedbackBad == path) { |
234 FeedbackReporter::ReportQuality(false); | 305 FeedbackReporter::ReportQuality(false); |
235 callback.Run(NULL); | 306 callback.Run(NULL); |
236 if (!external_feedback_reporter_) | 307 if (!external_feedback_reporter_) |
237 return; | 308 return; |
238 content::WebContents* contents = | 309 content::WebContents* contents = |
239 content::WebContents::FromRenderFrameHost(render_frame_host); | 310 content::WebContents::FromRenderFrameHost(render_frame_host); |
240 external_feedback_reporter_->ReportExternalFeedback( | 311 external_feedback_reporter_->ReportExternalFeedback( |
241 contents, contents->GetURL(), false); | 312 contents, contents->GetURL(), false); |
242 return; | 313 return; |
243 } else if (kFeedbackGood == path) { | 314 } else if (kFeedbackGood == path) { |
244 FeedbackReporter::ReportQuality(true); | 315 FeedbackReporter::ReportQuality(true); |
245 callback.Run(NULL); | 316 callback.Run(NULL); |
246 return; | 317 return; |
247 } | 318 } |
248 content::WebContents* web_contents = | 319 content::WebContents* web_contents = |
249 content::WebContents::FromRenderFrameHost(render_frame_host); | 320 content::WebContents::FromRenderFrameHost(render_frame_host); |
250 DCHECK(web_contents); | 321 DCHECK(web_contents); |
251 // An empty |path| is invalid, but guard against it. If not empty, assume | 322 |
252 // |path| starts with '?', which is stripped away. | |
253 scoped_ptr<ContentDataCallback> data_callback( | 323 scoped_ptr<ContentDataCallback> data_callback( |
254 new ContentDataCallback(callback)); | 324 new ContentDataCallback(callback)); |
255 const std::string path_after_query_separator = | 325 RequestViewerHandle* request_viewer_handle = NULL; |
256 path.size() > 0 ? path.substr(1) : ""; | 326 std::string print_prefix = "printing/"; |
257 RequestViewerHandle* request_viewer_handle = new RequestViewerHandle( | 327 if (path.compare(0, print_prefix.length(), print_prefix) == 0) { |
| 328 const std::string path_after_query_separator = |
| 329 path.substr(print_prefix.length() + 1); |
| 330 request_viewer_handle = new PrintPreviewRequestViewerHandle( |
258 web_contents, scheme_, path_after_query_separator, data_callback.Pass(), | 331 web_contents, scheme_, path_after_query_separator, data_callback.Pass(), |
259 dom_distiller_service_->GetDistilledPagePrefs()); | 332 dom_distiller_service_->GetDistilledPagePrefs()); |
| 333 |
| 334 } else { |
| 335 // An empty |path| is invalid, but guard against it. If not empty, assume |
| 336 // |path| starts with '?', which is stripped away. |
| 337 const std::string path_after_query_separator = |
| 338 path.size() > 0 ? path.substr(1) : ""; |
| 339 request_viewer_handle = new RequestViewerHandle( |
| 340 web_contents, scheme_, path_after_query_separator, data_callback.Pass(), |
| 341 dom_distiller_service_->GetDistilledPagePrefs()); |
| 342 } |
| 343 |
260 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 344 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
261 dom_distiller_service_, path, request_viewer_handle, | 345 dom_distiller_service_, path, request_viewer_handle, |
262 web_contents->GetContainerBounds().size()); | 346 web_contents->GetContainerBounds().size()); |
263 | 347 |
264 if (viewer_handle) { | 348 if (viewer_handle) { |
265 // The service returned a |ViewerHandle| and guarantees it will call | 349 // The service returned a |ViewerHandle| and guarantees it will call |
266 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 350 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
267 // request is not cancelled. The |RequestViewerHandle| will delete itself | 351 // request is not cancelled. The |RequestViewerHandle| will delete itself |
268 // after receiving the callback. | 352 // after receiving the callback. |
269 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 353 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
(...skipping 27 matching lines...) Expand all Loading... |
297 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 381 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
298 const { | 382 const { |
299 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; | 383 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; |
300 } | 384 } |
301 | 385 |
302 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { | 386 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { |
303 return "frame-src *;"; | 387 return "frame-src *;"; |
304 } | 388 } |
305 | 389 |
306 } // namespace dom_distiller | 390 } // namespace dom_distiller |
OLD | NEW |