| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/core/dom_distiller_request_view_base.h" | 5 #include "components/dom_distiller/core/dom_distiller_request_view_base.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/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/dom_distiller/core/distilled_page_prefs.h" | 14 #include "components/dom_distiller/core/distilled_page_prefs.h" |
| 15 #include "components/dom_distiller/core/dom_distiller_service.h" | 15 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 16 #include "components/dom_distiller/core/task_tracker.h" | 16 #include "components/dom_distiller/core/task_tracker.h" |
| 17 #include "components/dom_distiller/core/url_constants.h" | 17 #include "components/dom_distiller/core/url_constants.h" |
| 18 #include "components/dom_distiller/core/viewer.h" | 18 #include "components/dom_distiller/core/viewer.h" |
| 19 #include "content/public/browser/navigation_details.h" | 19 #include "content/public/browser/navigation_details.h" |
| 20 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
| 21 #include "content/public/browser/url_data_source.h" | 21 #include "content/public/browser/url_data_source.h" |
| 22 #include "net/base/url_util.h" | 22 #include "net/base/url_util.h" |
| 23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 24 | 24 |
| 25 namespace dom_distiller { | 25 namespace dom_distiller { |
| 26 | 26 |
| 27 DomDistillerRequestViewBase::DomDistillerRequestViewBase( | 27 DomDistillerRequestViewBase::DomDistillerRequestViewBase( |
| 28 scoped_ptr<DistillerDataCallback> callback, | |
| 29 DistilledPagePrefs* distilled_page_prefs) | 28 DistilledPagePrefs* distilled_page_prefs) |
| 30 : callback_(callback.Pass()), | 29 : page_count_(0), |
| 31 page_count_(0), | |
| 32 distilled_page_prefs_(distilled_page_prefs), | 30 distilled_page_prefs_(distilled_page_prefs), |
| 33 is_error_page_(false) { | 31 is_error_page_(false) { |
| 34 } | 32 } |
| 35 | 33 |
| 36 DomDistillerRequestViewBase::~DomDistillerRequestViewBase() { | 34 DomDistillerRequestViewBase::~DomDistillerRequestViewBase() { |
| 37 } | 35 } |
| 38 | 36 |
| 39 void DomDistillerRequestViewBase::FlagAsErrorPage() { | 37 void DomDistillerRequestViewBase::FlagAsErrorPage() { |
| 40 is_error_page_ = true; | 38 is_error_page_ = true; |
| 41 std::string error_page_html = | |
| 42 viewer::GetErrorPageHtml(distilled_page_prefs_->GetTheme(), | |
| 43 distilled_page_prefs_->GetFontFamily()); | |
| 44 callback_->RunCallback(error_page_html); | |
| 45 } | 39 } |
| 46 | 40 |
| 47 bool DomDistillerRequestViewBase::IsErrorPage() { | 41 bool DomDistillerRequestViewBase::IsErrorPage() { |
| 48 return is_error_page_; | 42 return is_error_page_; |
| 49 } | 43 } |
| 50 | 44 |
| 51 void DomDistillerRequestViewBase::OnArticleReady( | 45 void DomDistillerRequestViewBase::OnArticleReady( |
| 52 const DistilledArticleProto* article_proto) { | 46 const DistilledArticleProto* article_proto) { |
| 53 if (page_count_ == 0) { | 47 if (page_count_ == 0) { |
| 54 const DistilledPageProto* cur_page; | 48 std::string text_direction; |
| 55 if (article_proto->pages().size() < 1) { | 49 if (article_proto->pages().size() > 0) { |
| 56 cur_page = new DistilledPageProto(); | 50 text_direction = article_proto->pages(0).text_direction(); |
| 57 } else { | 51 } else { |
| 58 cur_page = &article_proto->pages(0); | 52 text_direction = "auto"; |
| 59 } | 53 } |
| 60 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( | 54 // Send first page, title, and text direction to client. |
| 61 cur_page, distilled_page_prefs_->GetTheme(), | 55 SendJavaScript(viewer::GetSetTitleJs(article_proto->title())); |
| 62 distilled_page_prefs_->GetFontFamily()); | 56 SendJavaScript(viewer::GetSetTextDirectionJs(text_direction)); |
| 63 callback_->RunCallback(unsafe_page_html); | |
| 64 // Send first page to client. | |
| 65 SendJavaScript(viewer::GetUnsafeArticleContentJs(article_proto)); | 57 SendJavaScript(viewer::GetUnsafeArticleContentJs(article_proto)); |
| 66 // If any content was loaded, show the feedback form. | 58 // If any content was loaded, show the feedback form. |
| 67 SendJavaScript(viewer::GetShowFeedbackFormJs()); | 59 SendJavaScript(viewer::GetShowFeedbackFormJs()); |
| 68 } else if (page_count_ == article_proto->pages_size()) { | 60 } else if (page_count_ == article_proto->pages_size()) { |
| 69 // We may still be showing the "Loading" indicator. | 61 // We may still be showing the "Loading" indicator. |
| 70 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); | 62 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); |
| 71 } else { | 63 } else { |
| 72 // It's possible that we didn't get some incremental updates from the | 64 // It's possible that we didn't get some incremental updates from the |
| 73 // distiller. Ensure all remaining pages are flushed to the viewer. | 65 // distiller. Ensure all remaining pages are flushed to the viewer. |
| 74 for (; page_count_ < article_proto->pages_size(); page_count_++) { | 66 for (; page_count_ < article_proto->pages_size(); page_count_++) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 85 ArticleDistillationUpdate article_update) { | 77 ArticleDistillationUpdate article_update) { |
| 86 for (; page_count_ < static_cast<int>(article_update.GetPagesSize()); | 78 for (; page_count_ < static_cast<int>(article_update.GetPagesSize()); |
| 87 page_count_++) { | 79 page_count_++) { |
| 88 const DistilledPageProto& page = | 80 const DistilledPageProto& page = |
| 89 article_update.GetDistilledPage(page_count_); | 81 article_update.GetDistilledPage(page_count_); |
| 90 // Send the page content to the client. This will execute after the page is | 82 // Send the page content to the client. This will execute after the page is |
| 91 // ready. | 83 // ready. |
| 92 SendJavaScript(viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | 84 SendJavaScript(viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); |
| 93 | 85 |
| 94 if (page_count_ == 0) { | 86 if (page_count_ == 0) { |
| 95 // This is the first page, so send Viewer page scaffolding too. | 87 // This is the first page, so send the title and text direction to the |
| 96 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( | 88 // client. |
| 97 &page, distilled_page_prefs_->GetTheme(), | 89 SendJavaScript(viewer::GetSetTitleJs(page.title())); |
| 98 distilled_page_prefs_->GetFontFamily()); | 90 SendJavaScript(viewer::GetSetTextDirectionJs(page.text_direction())); |
| 99 callback_->RunCallback(unsafe_page_html); | |
| 100 // If any content was loaded, show the feedback form. | 91 // If any content was loaded, show the feedback form. |
| 101 SendJavaScript(viewer::GetShowFeedbackFormJs()); | 92 SendJavaScript(viewer::GetShowFeedbackFormJs()); |
| 102 } | 93 } |
| 103 } | 94 } |
| 104 } | 95 } |
| 105 | 96 |
| 106 void DomDistillerRequestViewBase::OnChangeTheme( | 97 void DomDistillerRequestViewBase::OnChangeTheme( |
| 107 DistilledPagePrefs::Theme new_theme) { | 98 DistilledPagePrefs::Theme new_theme) { |
| 108 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); | 99 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); |
| 109 } | 100 } |
| 110 | 101 |
| 111 void DomDistillerRequestViewBase::OnChangeFontFamily( | 102 void DomDistillerRequestViewBase::OnChangeFontFamily( |
| 112 DistilledPagePrefs::FontFamily new_font) { | 103 DistilledPagePrefs::FontFamily new_font) { |
| 113 SendJavaScript(viewer::GetDistilledPageFontFamilyJs(new_font)); | 104 SendJavaScript(viewer::GetDistilledPageFontFamilyJs(new_font)); |
| 114 } | 105 } |
| 115 | 106 |
| 116 void DomDistillerRequestViewBase::TakeViewerHandle( | 107 void DomDistillerRequestViewBase::TakeViewerHandle( |
| 117 scoped_ptr<ViewerHandle> viewer_handle) { | 108 scoped_ptr<ViewerHandle> viewer_handle) { |
| 118 viewer_handle_ = viewer_handle.Pass(); | 109 viewer_handle_ = viewer_handle.Pass(); |
| 110 // Getting the viewer handle means this is not an error page, show the |
| 111 // loading indicator. |
| 112 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(false)); |
| 119 } | 113 } |
| 120 | 114 |
| 121 } // namespace dom_distiller | 115 } // namespace dom_distiller |
| OLD | NEW |