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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | 66 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't |
67 // ready. | 67 // ready. |
68 void SendJavaScript(const std::string& buffer); | 68 void SendJavaScript(const std::string& buffer); |
69 | 69 |
70 // Cancels the current view request. Once called, no updates will be | 70 // Cancels the current view request. Once called, no updates will be |
71 // propagated to the view, and the request to DomDistillerService will be | 71 // propagated to the view, and the request to DomDistillerService will be |
72 // cancelled. | 72 // cancelled. |
73 void Cancel(); | 73 void Cancel(); |
74 | 74 |
75 // DistilledPagePrefs::Observer implementation: | 75 // DistilledPagePrefs::Observer implementation: |
| 76 virtual void OnChangeFontFamily( |
| 77 DistilledPagePrefs::FontFamily new_font_family) OVERRIDE; |
76 virtual void OnChangeTheme(DistilledPagePrefs::Theme new_theme) OVERRIDE; | 78 virtual void OnChangeTheme(DistilledPagePrefs::Theme new_theme) OVERRIDE; |
77 | 79 |
78 // The handle to the view request towards the DomDistillerService. It | 80 // The handle to the view request towards the DomDistillerService. It |
79 // needs to be kept around to ensure the distillation request finishes. | 81 // needs to be kept around to ensure the distillation request finishes. |
80 scoped_ptr<ViewerHandle> viewer_handle_; | 82 scoped_ptr<ViewerHandle> viewer_handle_; |
81 | 83 |
82 // The scheme hosting the current view request; | 84 // The scheme hosting the current view request; |
83 std::string expected_scheme_; | 85 std::string expected_scheme_; |
84 | 86 |
85 // The query path for the current view request. | 87 // The query path for the current view request. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 return; | 182 return; |
181 } | 183 } |
182 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); | 184 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); |
183 buffer_.clear(); | 185 buffer_.clear(); |
184 } | 186 } |
185 | 187 |
186 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( | 188 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
187 const DistilledArticleProto* article_proto) { | 189 const DistilledArticleProto* article_proto) { |
188 if (page_count_ == 0) { | 190 if (page_count_ == 0) { |
189 // This is a single-page article. | 191 // This is a single-page article. |
190 std::string unsafe_page_html = viewer::GetUnsafeArticleHtml( | 192 std::string unsafe_page_html = |
191 article_proto, distilled_page_prefs_->GetTheme()); | 193 viewer::GetUnsafeArticleHtml( |
| 194 article_proto, |
| 195 distilled_page_prefs_->GetTheme(), |
| 196 distilled_page_prefs_->GetFontFamily()); |
192 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 197 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
193 } else if (page_count_ == article_proto->pages_size()) { | 198 } else if (page_count_ == article_proto->pages_size()) { |
194 // We may still be showing the "Loading" indicator. | 199 // We may still be showing the "Loading" indicator. |
195 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); | 200 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); |
196 } else { | 201 } else { |
197 // It's possible that we didn't get some incremental updates from the | 202 // It's possible that we didn't get some incremental updates from the |
198 // distiller. Ensure all remaining pages are flushed to the viewer. | 203 // distiller. Ensure all remaining pages are flushed to the viewer. |
199 for (;page_count_ < article_proto->pages_size(); page_count_++) { | 204 for (;page_count_ < article_proto->pages_size(); page_count_++) { |
200 const DistilledPageProto& page = article_proto->pages(page_count_); | 205 const DistilledPageProto& page = article_proto->pages(page_count_); |
201 SendJavaScript( | 206 SendJavaScript( |
202 viewer::GetUnsafeIncrementalDistilledPageJs( | 207 viewer::GetUnsafeIncrementalDistilledPageJs( |
203 &page, | 208 &page, |
204 page_count_ == article_proto->pages_size())); | 209 page_count_ == article_proto->pages_size())); |
205 } | 210 } |
206 } | 211 } |
207 // No need to hold on to the ViewerHandle now that distillation is complete. | 212 // No need to hold on to the ViewerHandle now that distillation is complete. |
208 viewer_handle_.reset(); | 213 viewer_handle_.reset(); |
209 } | 214 } |
210 | 215 |
211 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 216 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
212 ArticleDistillationUpdate article_update) { | 217 ArticleDistillationUpdate article_update) { |
213 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); | 218 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); |
214 page_count_++) { | 219 page_count_++) { |
215 const DistilledPageProto& page = | 220 const DistilledPageProto& page = |
216 article_update.GetDistilledPage(page_count_); | 221 article_update.GetDistilledPage(page_count_); |
217 if (page_count_ == 0) { | 222 if (page_count_ == 0) { |
218 // This is the first page, so send Viewer page scaffolding too. | 223 // This is the first page, so send Viewer page scaffolding too. |
219 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml( | 224 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml( |
220 &page, distilled_page_prefs_->GetTheme()); | 225 &page, |
| 226 distilled_page_prefs_->GetTheme(), |
| 227 distilled_page_prefs_->GetFontFamily()); |
221 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 228 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
222 } else { | 229 } else { |
223 SendJavaScript( | 230 SendJavaScript( |
224 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | 231 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); |
225 } | 232 } |
226 } | 233 } |
227 } | 234 } |
228 | 235 |
229 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( | 236 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
230 scoped_ptr<ViewerHandle> viewer_handle) { | 237 scoped_ptr<ViewerHandle> viewer_handle) { |
231 viewer_handle_ = viewer_handle.Pass(); | 238 viewer_handle_ = viewer_handle.Pass(); |
232 } | 239 } |
233 | 240 |
234 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( | 241 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( |
235 DistilledPagePrefs::Theme new_theme) { | 242 DistilledPagePrefs::Theme new_theme) { |
236 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); | 243 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); |
237 } | 244 } |
238 | 245 |
| 246 void DomDistillerViewerSource::RequestViewerHandle::OnChangeFontFamily( |
| 247 DistilledPagePrefs::FontFamily new_font) { |
| 248 SendJavaScript(viewer::GetDistilledPageFontFamilyJs(new_font)); |
| 249 } |
| 250 |
239 DomDistillerViewerSource::DomDistillerViewerSource( | 251 DomDistillerViewerSource::DomDistillerViewerSource( |
240 DomDistillerServiceInterface* dom_distiller_service, | 252 DomDistillerServiceInterface* dom_distiller_service, |
241 const std::string& scheme) | 253 const std::string& scheme) |
242 : scheme_(scheme), dom_distiller_service_(dom_distiller_service) { | 254 : scheme_(scheme), dom_distiller_service_(dom_distiller_service) { |
243 } | 255 } |
244 | 256 |
245 DomDistillerViewerSource::~DomDistillerViewerSource() { | 257 DomDistillerViewerSource::~DomDistillerViewerSource() { |
246 } | 258 } |
247 | 259 |
248 std::string DomDistillerViewerSource::GetSource() const { | 260 std::string DomDistillerViewerSource::GetSource() const { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 305 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
294 // request is not cancelled. The |RequestViewerHandle| will delete itself | 306 // request is not cancelled. The |RequestViewerHandle| will delete itself |
295 // after receiving the callback. | 307 // after receiving the callback. |
296 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 308 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
297 } else { | 309 } else { |
298 // The service did not return a |ViewerHandle|, which means the | 310 // The service did not return a |ViewerHandle|, which means the |
299 // |RequestViewerHandle| will never be called, so clean up now. | 311 // |RequestViewerHandle| will never be called, so clean up now. |
300 delete request_viewer_handle; | 312 delete request_viewer_handle; |
301 | 313 |
302 std::string error_page_html = viewer::GetErrorPageHtml( | 314 std::string error_page_html = viewer::GetErrorPageHtml( |
303 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme()); | 315 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
| 316 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); |
304 callback.Run(base::RefCountedString::TakeString(&error_page_html)); | 317 callback.Run(base::RefCountedString::TakeString(&error_page_html)); |
305 } | 318 } |
306 }; | 319 }; |
307 | 320 |
308 std::string DomDistillerViewerSource::GetMimeType( | 321 std::string DomDistillerViewerSource::GetMimeType( |
309 const std::string& path) const { | 322 const std::string& path) const { |
310 if (kViewerCssPath == path) { | 323 if (kViewerCssPath == path) { |
311 return "text/css"; | 324 return "text/css"; |
312 } | 325 } |
313 if (kViewerJsPath == path) { | 326 if (kViewerJsPath == path) { |
(...skipping 12 matching lines...) Expand all Loading... |
326 const net::URLRequest* request, | 339 const net::URLRequest* request, |
327 std::string* path) const { | 340 std::string* path) const { |
328 } | 341 } |
329 | 342 |
330 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 343 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
331 const { | 344 const { |
332 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; | 345 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; |
333 } | 346 } |
334 | 347 |
335 } // namespace dom_distiller | 348 } // namespace dom_distiller |
OLD | NEW |