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(article_proto, |
robliao
2014/08/10 02:43:45
Consistency: Wrap these arguments to the next line
sunangel
2014/08/11 21:51:33
Can you explain what you mean? Git cl format turne
robliao
2014/08/11 22:00:45
Look at GetUnsafePartialArticleHtml below. Use tha
sunangel
2014/08/12 02:09:55
Done.
| |
194 distilled_page_prefs_->GetTheme(), | |
195 distilled_page_prefs_->GetFontFamily()); | |
192 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 196 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
193 } else if (page_count_ == article_proto->pages_size()) { | 197 } else if (page_count_ == article_proto->pages_size()) { |
194 // We may still be showing the "Loading" indicator. | 198 // We may still be showing the "Loading" indicator. |
195 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); | 199 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); |
196 } else { | 200 } else { |
197 // It's possible that we didn't get some incremental updates from the | 201 // It's possible that we didn't get some incremental updates from the |
198 // distiller. Ensure all remaining pages are flushed to the viewer. | 202 // distiller. Ensure all remaining pages are flushed to the viewer. |
199 for (;page_count_ < article_proto->pages_size(); page_count_++) { | 203 for (;page_count_ < article_proto->pages_size(); page_count_++) { |
200 const DistilledPageProto& page = article_proto->pages(page_count_); | 204 const DistilledPageProto& page = article_proto->pages(page_count_); |
201 SendJavaScript( | 205 SendJavaScript( |
202 viewer::GetUnsafeIncrementalDistilledPageJs( | 206 viewer::GetUnsafeIncrementalDistilledPageJs( |
203 &page, | 207 &page, |
204 page_count_ == article_proto->pages_size())); | 208 page_count_ == article_proto->pages_size())); |
205 } | 209 } |
206 } | 210 } |
207 // No need to hold on to the ViewerHandle now that distillation is complete. | 211 // No need to hold on to the ViewerHandle now that distillation is complete. |
208 viewer_handle_.reset(); | 212 viewer_handle_.reset(); |
209 } | 213 } |
210 | 214 |
211 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 215 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
212 ArticleDistillationUpdate article_update) { | 216 ArticleDistillationUpdate article_update) { |
213 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); | 217 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); |
214 page_count_++) { | 218 page_count_++) { |
215 const DistilledPageProto& page = | 219 const DistilledPageProto& page = |
216 article_update.GetDistilledPage(page_count_); | 220 article_update.GetDistilledPage(page_count_); |
217 if (page_count_ == 0) { | 221 if (page_count_ == 0) { |
218 // This is the first page, so send Viewer page scaffolding too. | 222 // This is the first page, so send Viewer page scaffolding too. |
219 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml( | 223 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml( |
220 &page, distilled_page_prefs_->GetTheme()); | 224 &page, |
225 distilled_page_prefs_->GetTheme(), | |
226 distilled_page_prefs_->GetFontFamily()); | |
221 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 227 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
222 } else { | 228 } else { |
223 SendJavaScript( | 229 SendJavaScript( |
224 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | 230 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); |
225 } | 231 } |
226 } | 232 } |
227 } | 233 } |
228 | 234 |
229 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( | 235 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
230 scoped_ptr<ViewerHandle> viewer_handle) { | 236 scoped_ptr<ViewerHandle> viewer_handle) { |
231 viewer_handle_ = viewer_handle.Pass(); | 237 viewer_handle_ = viewer_handle.Pass(); |
232 } | 238 } |
233 | 239 |
234 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( | 240 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( |
235 DistilledPagePrefs::Theme new_theme) { | 241 DistilledPagePrefs::Theme new_theme) { |
236 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); | 242 SendJavaScript(viewer::GetDistilledPageThemeJs(new_theme)); |
237 } | 243 } |
238 | 244 |
245 void DomDistillerViewerSource::RequestViewerHandle::OnChangeFontFamily( | |
246 DistilledPagePrefs::FontFamily new_font) { | |
247 SendJavaScript(viewer::GetDistilledPageFontFamilyJs(new_font)); | |
248 } | |
249 | |
239 DomDistillerViewerSource::DomDistillerViewerSource( | 250 DomDistillerViewerSource::DomDistillerViewerSource( |
240 DomDistillerServiceInterface* dom_distiller_service, | 251 DomDistillerServiceInterface* dom_distiller_service, |
241 const std::string& scheme) | 252 const std::string& scheme) |
242 : scheme_(scheme), dom_distiller_service_(dom_distiller_service) { | 253 : scheme_(scheme), dom_distiller_service_(dom_distiller_service) { |
243 } | 254 } |
244 | 255 |
245 DomDistillerViewerSource::~DomDistillerViewerSource() { | 256 DomDistillerViewerSource::~DomDistillerViewerSource() { |
246 } | 257 } |
247 | 258 |
248 std::string DomDistillerViewerSource::GetSource() const { | 259 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 | 304 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
294 // request is not cancelled. The |RequestViewerHandle| will delete itself | 305 // request is not cancelled. The |RequestViewerHandle| will delete itself |
295 // after receiving the callback. | 306 // after receiving the callback. |
296 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 307 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
297 } else { | 308 } else { |
298 // The service did not return a |ViewerHandle|, which means the | 309 // The service did not return a |ViewerHandle|, which means the |
299 // |RequestViewerHandle| will never be called, so clean up now. | 310 // |RequestViewerHandle| will never be called, so clean up now. |
300 delete request_viewer_handle; | 311 delete request_viewer_handle; |
301 | 312 |
302 std::string error_page_html = viewer::GetErrorPageHtml( | 313 std::string error_page_html = viewer::GetErrorPageHtml( |
303 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme()); | 314 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
315 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); | |
304 callback.Run(base::RefCountedString::TakeString(&error_page_html)); | 316 callback.Run(base::RefCountedString::TakeString(&error_page_html)); |
305 } | 317 } |
306 }; | 318 }; |
307 | 319 |
308 std::string DomDistillerViewerSource::GetMimeType( | 320 std::string DomDistillerViewerSource::GetMimeType( |
309 const std::string& path) const { | 321 const std::string& path) const { |
310 if (kViewerCssPath == path) { | 322 if (kViewerCssPath == path) { |
311 return "text/css"; | 323 return "text/css"; |
312 } | 324 } |
313 if (kViewerJsPath == path) { | 325 if (kViewerJsPath == path) { |
(...skipping 12 matching lines...) Expand all Loading... | |
326 const net::URLRequest* request, | 338 const net::URLRequest* request, |
327 std::string* path) const { | 339 std::string* path) const { |
328 } | 340 } |
329 | 341 |
330 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 342 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
331 const { | 343 const { |
332 return "object-src 'none'; style-src 'self';"; | 344 return "object-src 'none'; style-src 'self';"; |
333 } | 345 } |
334 | 346 |
335 } // namespace dom_distiller | 347 } // namespace dom_distiller |
OLD | NEW |