Index: components/dom_distiller/content/dom_distiller_viewer_source.cc |
diff --git a/components/dom_distiller/content/dom_distiller_viewer_source.cc b/components/dom_distiller/content/dom_distiller_viewer_source.cc |
index 26c650266d3d6f5e0bde7c13e774c7cef8c9cf95..016539403fd356b1ee3f8ea6655bb95cac7b7c5f 100644 |
--- a/components/dom_distiller/content/dom_distiller_viewer_source.cc |
+++ b/components/dom_distiller/content/dom_distiller_viewer_source.cc |
@@ -12,6 +12,8 @@ |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
#include "base/strings/utf_string_conversions.h" |
+#include "components/dom_distiller/core/dom_distiller_service.h" |
+#include "components/dom_distiller/core/reader_mode_preferences.h" |
#include "components/dom_distiller/core/task_tracker.h" |
#include "components/dom_distiller/core/url_constants.h" |
#include "components/dom_distiller/core/viewer.h" |
@@ -31,16 +33,18 @@ namespace dom_distiller { |
// the current main frame's page in the Viewer instance. |
class DomDistillerViewerSource::RequestViewerHandle |
: public ViewRequestDelegate, |
- public content::WebContentsObserver { |
+ public content::WebContentsObserver, |
+ public ReaderModePrefs::Observer { |
public: |
explicit RequestViewerHandle( |
content::WebContents* web_contents, |
const std::string& expected_scheme, |
const std::string& expected_request_path, |
- const content::URLDataSource::GotDataCallback& callback); |
+ const content::URLDataSource::GotDataCallback& callback, |
+ ReaderModePrefs* reader_mode_prefs); |
virtual ~RequestViewerHandle(); |
- // ViewRequestDelegate implementation. |
+ // ViewRequestDelegate implementation: |
virtual void OnArticleReady( |
const DistilledArticleProto* article_proto) OVERRIDE; |
@@ -49,7 +53,7 @@ class DomDistillerViewerSource::RequestViewerHandle |
void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
- // WebContentsObserver: |
+ // content::WebContentsObserver implementation: |
virtual void DidNavigateMainFrame( |
const content::LoadCommittedDetails& details, |
const content::FrameNavigateParams& params) OVERRIDE; |
@@ -71,6 +75,9 @@ class DomDistillerViewerSource::RequestViewerHandle |
// cancelled. |
void Cancel(); |
+ // ReaderModePrefs::Observer implementation: |
+ virtual void OnChangeTheme(ReaderModePrefs::Theme new_theme) OVERRIDE; |
+ |
// The handle to the view request towards the DomDistillerService. It |
// needs to be kept around to ensure the distillation request finishes. |
scoped_ptr<ViewerHandle> viewer_handle_; |
@@ -98,25 +105,32 @@ class DomDistillerViewerSource::RequestViewerHandle |
// Temporary store of pending JavaScript if the page isn't ready to receive |
// data from distillation. |
std::string buffer_; |
+ |
+ // Interface for accessing preferences for reader mode. |
+ ReaderModePrefs* reader_mode_prefs_; |
}; |
DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
content::WebContents* web_contents, |
const std::string& expected_scheme, |
const std::string& expected_request_path, |
- const content::URLDataSource::GotDataCallback& callback) |
+ const content::URLDataSource::GotDataCallback& callback, |
+ ReaderModePrefs* reader_mode_prefs) |
: web_contents_(web_contents), |
expected_scheme_(expected_scheme), |
expected_request_path_(expected_request_path), |
callback_(callback), |
page_count_(0), |
- waiting_for_page_ready_(true) { |
+ waiting_for_page_ready_(true), |
+ reader_mode_prefs_(reader_mode_prefs) { |
content::WebContentsObserver::Observe(web_contents_); |
+ reader_mode_prefs_->AddObserver(this); |
} |
DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
// Balanced with constructor although can be a no-op if frame navigated away. |
content::WebContentsObserver::Observe(NULL); |
+ reader_mode_prefs_->RemoveObserver(this); |
} |
void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( |
@@ -190,7 +204,8 @@ void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
const DistilledArticleProto* article_proto) { |
if (page_count_ == 0) { |
// This is a single-page article. |
- std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto); |
+ std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto, |
+ reader_mode_prefs_->GetTheme()); |
callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
} else if (page_count_ == article_proto->pages_size()) { |
// We may still be showing the "Loading" indicator. |
@@ -218,7 +233,8 @@ void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
article_update.GetDistilledPage(page_count_); |
if (page_count_ == 0) { |
// This is the first page, so send Viewer page scaffolding too. |
- std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml(&page); |
+ std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml(&page, |
+ reader_mode_prefs_->GetTheme()); |
callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
} else { |
SendJavaScript( |
@@ -232,6 +248,14 @@ void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
viewer_handle_ = viewer_handle.Pass(); |
} |
+void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( |
+ ReaderModePrefs::Theme new_theme) { |
+ std::ostringstream js; |
nyquist
2014/06/30 21:35:40
Create a method in the viewer for Get...Js().
The
smaslo
2014/07/07 17:14:14
I'm not sure what you are talking about in your se
smaslo
2014/07/07 17:15:43
Done.
|
+ js << "document.body.className='" << |
+ viewer::GetBodyCssClass(new_theme) << "';"; |
+ SendJavaScript(js.str()); |
+} |
+ |
DomDistillerViewerSource::DomDistillerViewerSource( |
DomDistillerServiceInterface* dom_distiller_service, |
const std::string& scheme) |
@@ -278,7 +302,8 @@ void DomDistillerViewerSource::StartDataRequest( |
const std::string path_after_query_separator = |
path.size() > 0 ? path.substr(1) : ""; |
RequestViewerHandle* request_viewer_handle = new RequestViewerHandle( |
- web_contents, scheme_, path_after_query_separator, callback); |
+ web_contents, scheme_, path_after_query_separator, callback, |
+ dom_distiller_service_->GetReaderModePrefs()); |
scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
dom_distiller_service_, path, request_viewer_handle); |
@@ -293,7 +318,8 @@ void DomDistillerViewerSource::StartDataRequest( |
// |RequestViewerHandle| will never be called, so clean up now. |
delete request_viewer_handle; |
- std::string error_page_html = viewer::GetErrorPageHtml(); |
+ std::string error_page_html = viewer::GetErrorPageHtml( |
+ dom_distiller_service_->GetReaderModePrefs()->GetTheme()); |
callback.Run(base::RefCountedString::TakeString(&error_page_html)); |
} |
}; |