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 <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/user_metrics.h" | 14 #include "base/metrics/user_metrics.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/dom_distiller/core/distilled_page_prefs.h" | 16 #include "components/dom_distiller/core/distilled_page_prefs.h" |
| 17 #include "components/dom_distiller/core/dom_distiller_service.h" | 17 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 18 #include "components/dom_distiller/core/task_tracker.h" | 18 #include "components/dom_distiller/core/task_tracker.h" |
| 19 #include "components/dom_distiller/core/url_constants.h" | 19 #include "components/dom_distiller/core/url_constants.h" |
| 20 #include "components/dom_distiller/core/viewer.h" | 20 #include "components/dom_distiller/core/viewer.h" |
| 21 #include "content/public/browser/navigation_details.h" | 21 #include "content/public/browser/navigation_details.h" |
| 22 #include "content/public/browser/navigation_entry.h" | 22 #include "content/public/browser/navigation_entry.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 25 #include "content/public/browser/user_metrics.h" | 25 #include "content/public/browser/user_metrics.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
| 28 #include "content/public/browser/web_contents_user_data.h" | |
| 28 #include "net/base/url_util.h" | 29 #include "net/base/url_util.h" |
| 29 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
| 30 | 31 |
| 32 namespace { | |
| 33 | |
| 34 // Handles sending conent to pages that are in an error state (i.e. the URL | |
| 35 // was bad or could not be loaded). | |
| 36 class WebContentsErrorObserver | |
|
cjhopman
2015/03/21 02:33:22
If distillation returned really really fast, could
mdjones
2015/03/24 01:32:42
Yes, I have added a second observer to watch for t
| |
| 37 : public content::WebContentsObserver, | |
| 38 public content::WebContentsUserData<WebContentsErrorObserver> { | |
| 39 public: | |
| 40 // Once the page has finished loading, send an error message. | |
| 41 void DocumentOnLoadCompletedInMainFrame() override { | |
| 42 web_contents()->GetMainFrame()->ExecuteJavaScript( | |
| 43 base::UTF8ToUTF16(dom_distiller::viewer::GetErrorPageJs())); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 friend class content::WebContentsUserData<WebContentsErrorObserver>; | |
| 48 explicit WebContentsErrorObserver(content::WebContents* web_contents) | |
| 49 : WebContentsObserver(web_contents) {} | |
| 50 | |
| 51 ~WebContentsErrorObserver() override { | |
| 52 content::WebContentsObserver::Observe(NULL); | |
| 53 } | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(WebContentsErrorObserver); | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsErrorObserver); | |
| 61 | |
| 31 namespace dom_distiller { | 62 namespace dom_distiller { |
| 32 | 63 |
| 33 // Handles receiving data asynchronously for a specific entry, and passing | 64 // Handles receiving data asynchronously for a specific entry, and passing |
| 34 // it along to the data callback for the data source. Lifetime matches that of | 65 // it along to the data callback for the data source. Lifetime matches that of |
| 35 // the current main frame's page in the Viewer instance. | 66 // the current main frame's page in the Viewer instance. |
| 36 class DomDistillerViewerSource::RequestViewerHandle | 67 class DomDistillerViewerSource::RequestViewerHandle |
| 37 : public ViewRequestDelegate, | 68 : public ViewRequestDelegate, |
| 38 public content::WebContentsObserver, | 69 public content::WebContentsObserver, |
| 39 public DistilledPagePrefs::Observer { | 70 public DistilledPagePrefs::Observer { |
| 40 public: | 71 public: |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 if (buffer_.empty()) { | 212 if (buffer_.empty()) { |
| 182 return; | 213 return; |
| 183 } | 214 } |
| 184 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); | 215 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); |
| 185 buffer_.clear(); | 216 buffer_.clear(); |
| 186 } | 217 } |
| 187 | 218 |
| 188 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( | 219 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
| 189 const DistilledArticleProto* article_proto) { | 220 const DistilledArticleProto* article_proto) { |
| 190 if (page_count_ == 0) { | 221 if (page_count_ == 0) { |
| 191 // This is a single-page article. | 222 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( |
| 192 std::string unsafe_page_html = | 223 &article_proto->pages(0), |
| 193 viewer::GetUnsafeArticleHtml( | 224 distilled_page_prefs_->GetTheme(), |
| 194 article_proto, | 225 distilled_page_prefs_->GetFontFamily()); |
| 195 distilled_page_prefs_->GetTheme(), | |
| 196 distilled_page_prefs_->GetFontFamily()); | |
| 197 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 226 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
| 227 | |
| 228 // Send the actual article content to the page. | |
| 229 SendJavaScript(viewer::GetUnsafeArticleContentJs(article_proto)); | |
| 198 } else if (page_count_ == article_proto->pages_size()) { | 230 } else if (page_count_ == article_proto->pages_size()) { |
| 199 // We may still be showing the "Loading" indicator. | 231 // We may still be showing the "Loading" indicator. |
| 200 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); | 232 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); |
| 201 } else { | 233 } else { |
| 202 // It's possible that we didn't get some incremental updates from the | 234 // It's possible that we didn't get some incremental updates from the |
| 203 // distiller. Ensure all remaining pages are flushed to the viewer. | 235 // distiller. Ensure all remaining pages are flushed to the viewer. |
| 204 for (;page_count_ < article_proto->pages_size(); page_count_++) { | 236 for (;page_count_ < article_proto->pages_size(); page_count_++) { |
| 205 const DistilledPageProto& page = article_proto->pages(page_count_); | 237 const DistilledPageProto& page = article_proto->pages(page_count_); |
| 206 SendJavaScript( | 238 SendJavaScript( |
| 207 viewer::GetUnsafeIncrementalDistilledPageJs( | 239 viewer::GetUnsafeIncrementalDistilledPageJs( |
| 208 &page, | 240 &page, |
| 209 page_count_ == article_proto->pages_size())); | 241 page_count_ == article_proto->pages_size())); |
| 210 } | 242 } |
| 211 } | 243 } |
| 212 // No need to hold on to the ViewerHandle now that distillation is complete. | 244 // No need to hold on to the ViewerHandle now that distillation is complete. |
| 213 viewer_handle_.reset(); | 245 viewer_handle_.reset(); |
| 214 } | 246 } |
| 215 | 247 |
| 216 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 248 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
| 217 ArticleDistillationUpdate article_update) { | 249 ArticleDistillationUpdate article_update) { |
| 218 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); | 250 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); |
| 219 page_count_++) { | 251 page_count_++) { |
| 220 const DistilledPageProto& page = | 252 const DistilledPageProto& page = |
| 221 article_update.GetDistilledPage(page_count_); | 253 article_update.GetDistilledPage(page_count_); |
| 222 if (page_count_ == 0) { | 254 if (page_count_ == 0) { |
| 223 // This is the first page, so send Viewer page scaffolding too. | 255 // This is the first page, so send Viewer page scaffolding too. |
| 224 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml( | 256 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( |
| 225 &page, | 257 &page, |
| 226 distilled_page_prefs_->GetTheme(), | 258 distilled_page_prefs_->GetTheme(), |
| 227 distilled_page_prefs_->GetFontFamily()); | 259 distilled_page_prefs_->GetFontFamily()); |
| 228 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 260 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
| 229 } else { | |
| 230 SendJavaScript( | |
| 231 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | |
| 232 } | 261 } |
| 262 // Send the actual article content to the page. | |
| 263 SendJavaScript(viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | |
| 233 } | 264 } |
| 234 } | 265 } |
| 235 | 266 |
| 236 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( | 267 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
| 237 scoped_ptr<ViewerHandle> viewer_handle) { | 268 scoped_ptr<ViewerHandle> viewer_handle) { |
| 238 viewer_handle_ = viewer_handle.Pass(); | 269 viewer_handle_ = viewer_handle.Pass(); |
| 239 } | 270 } |
| 240 | 271 |
| 241 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( | 272 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( |
| 242 DistilledPagePrefs::Theme new_theme) { | 273 DistilledPagePrefs::Theme new_theme) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 // The service returned a |ViewerHandle| and guarantees it will call | 338 // The service returned a |ViewerHandle| and guarantees it will call |
| 308 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 339 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
| 309 // request is not cancelled. The |RequestViewerHandle| will delete itself | 340 // request is not cancelled. The |RequestViewerHandle| will delete itself |
| 310 // after receiving the callback. | 341 // after receiving the callback. |
| 311 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 342 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
| 312 } else { | 343 } else { |
| 313 // The service did not return a |ViewerHandle|, which means the | 344 // The service did not return a |ViewerHandle|, which means the |
| 314 // |RequestViewerHandle| will never be called, so clean up now. | 345 // |RequestViewerHandle| will never be called, so clean up now. |
| 315 delete request_viewer_handle; | 346 delete request_viewer_handle; |
| 316 | 347 |
| 348 WebContentsErrorObserver::CreateForWebContents(web_contents); | |
| 349 | |
| 317 std::string error_page_html = viewer::GetErrorPageHtml( | 350 std::string error_page_html = viewer::GetErrorPageHtml( |
| 318 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), | 351 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
| 319 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); | 352 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); |
| 320 callback.Run(base::RefCountedString::TakeString(&error_page_html)); | 353 callback.Run(base::RefCountedString::TakeString(&error_page_html)); |
| 321 } | 354 } |
| 322 }; | 355 }; |
| 323 | 356 |
| 324 std::string DomDistillerViewerSource::GetMimeType( | 357 std::string DomDistillerViewerSource::GetMimeType( |
| 325 const std::string& path) const { | 358 const std::string& path) const { |
| 326 if (kViewerCssPath == path) { | 359 if (kViewerCssPath == path) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 342 const net::URLRequest* request, | 375 const net::URLRequest* request, |
| 343 std::string* path) const { | 376 std::string* path) const { |
| 344 } | 377 } |
| 345 | 378 |
| 346 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 379 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
| 347 const { | 380 const { |
| 348 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; | 381 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; |
| 349 } | 382 } |
| 350 | 383 |
| 351 } // namespace dom_distiller | 384 } // namespace dom_distiller |
| OLD | NEW |