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/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "components/dom_distiller/core/dom_distiller_service.h" | |
16 #include "components/dom_distiller/core/reader_mode_preferences.h" | |
15 #include "components/dom_distiller/core/task_tracker.h" | 17 #include "components/dom_distiller/core/task_tracker.h" |
16 #include "components/dom_distiller/core/url_constants.h" | 18 #include "components/dom_distiller/core/url_constants.h" |
17 #include "components/dom_distiller/core/viewer.h" | 19 #include "components/dom_distiller/core/viewer.h" |
18 #include "content/public/browser/navigation_details.h" | 20 #include "content/public/browser/navigation_details.h" |
19 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
20 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
24 #include "net/base/url_util.h" | 26 #include "net/base/url_util.h" |
25 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
26 | 28 |
27 namespace dom_distiller { | 29 namespace dom_distiller { |
28 | 30 |
29 // Handles receiving data asynchronously for a specific entry, and passing | 31 // Handles receiving data asynchronously for a specific entry, and passing |
30 // it along to the data callback for the data source. Lifetime matches that of | 32 // it along to the data callback for the data source. Lifetime matches that of |
31 // the current main frame's page in the Viewer instance. | 33 // the current main frame's page in the Viewer instance. |
32 class DomDistillerViewerSource::RequestViewerHandle | 34 class DomDistillerViewerSource::RequestViewerHandle |
33 : public ViewRequestDelegate, | 35 : public ViewRequestDelegate, |
34 public content::WebContentsObserver { | 36 public content::WebContentsObserver, |
37 public ReaderModePrefs::Observer { | |
35 public: | 38 public: |
36 explicit RequestViewerHandle( | 39 explicit RequestViewerHandle( |
37 content::WebContents* web_contents, | 40 content::WebContents* web_contents, |
38 const std::string& expected_scheme, | 41 const std::string& expected_scheme, |
39 const std::string& expected_request_path, | 42 const std::string& expected_request_path, |
40 const content::URLDataSource::GotDataCallback& callback); | 43 const content::URLDataSource::GotDataCallback& callback, |
44 ReaderModePrefs* reader_mode_prefs); | |
41 virtual ~RequestViewerHandle(); | 45 virtual ~RequestViewerHandle(); |
42 | 46 |
43 // ViewRequestDelegate implementation. | 47 // ViewRequestDelegate implementation. |
nyquist
2014/06/26 20:39:57
// ViewRequestDelegate implementation:
smaslo
2014/06/27 18:33:16
Done.
| |
44 virtual void OnArticleReady( | 48 virtual void OnArticleReady( |
45 const DistilledArticleProto* article_proto) OVERRIDE; | 49 const DistilledArticleProto* article_proto) OVERRIDE; |
46 | 50 |
47 virtual void OnArticleUpdated( | 51 virtual void OnArticleUpdated( |
48 ArticleDistillationUpdate article_update) OVERRIDE; | 52 ArticleDistillationUpdate article_update) OVERRIDE; |
49 | 53 |
50 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | 54 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
51 | 55 |
52 // WebContentsObserver: | 56 // WebContentsObserver: |
nyquist
2014/06/26 20:39:57
// content::WebContentsObserver implementation:
smaslo
2014/06/27 18:33:17
Done.
| |
53 virtual void DidNavigateMainFrame( | 57 virtual void DidNavigateMainFrame( |
54 const content::LoadCommittedDetails& details, | 58 const content::LoadCommittedDetails& details, |
55 const content::FrameNavigateParams& params) OVERRIDE; | 59 const content::FrameNavigateParams& params) OVERRIDE; |
56 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | 60 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
57 virtual void WebContentsDestroyed() OVERRIDE; | 61 virtual void WebContentsDestroyed() OVERRIDE; |
58 virtual void DidFinishLoad( | 62 virtual void DidFinishLoad( |
59 int64 frame_id, | 63 int64 frame_id, |
60 const GURL& validated_url, | 64 const GURL& validated_url, |
61 bool is_main_frame, | 65 bool is_main_frame, |
62 content::RenderViewHost* render_view_host) OVERRIDE; | 66 content::RenderViewHost* render_view_host) OVERRIDE; |
63 | 67 |
68 // ReaderModePrefs::Observer | |
nyquist
2014/06/26 20:39:58
// ReaderModePrefs::Observer implementation:
smaslo
2014/06/27 18:33:17
Done.
| |
69 virtual void OnChangeTheme(ReaderModePrefs* reader_mode_prefs) OVERRIDE; | |
nyquist
2014/06/26 20:39:58
Also, is it possible to make all of these interfac
smaslo
2014/06/27 18:33:16
Done.
| |
70 | |
71 void SetBodyClassName(); | |
nyquist
2014/06/26 20:39:57
How about SendBodyClassName()? And could it take t
smaslo
2014/06/27 18:33:17
Done.
| |
72 | |
64 private: | 73 private: |
65 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | 74 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't |
66 // ready. | 75 // ready. |
67 void SendJavaScript(const std::string& buffer); | 76 void SendJavaScript(const std::string& buffer); |
68 | 77 |
69 // Cancels the current view request. Once called, no updates will be | 78 // Cancels the current view request. Once called, no updates will be |
70 // propagated to the view, and the request to DomDistillerService will be | 79 // propagated to the view, and the request to DomDistillerService will be |
71 // cancelled. | 80 // cancelled. |
72 void Cancel(); | 81 void Cancel(); |
73 | 82 |
(...skipping 17 matching lines...) Expand all Loading... | |
91 // the viewer. | 100 // the viewer. |
92 int page_count_; | 101 int page_count_; |
93 | 102 |
94 // Whether the page is sufficiently initialized to handle updates from the | 103 // Whether the page is sufficiently initialized to handle updates from the |
95 // distiller. | 104 // distiller. |
96 bool waiting_for_page_ready_; | 105 bool waiting_for_page_ready_; |
97 | 106 |
98 // Temporary store of pending JavaScript if the page isn't ready to receive | 107 // Temporary store of pending JavaScript if the page isn't ready to receive |
99 // data from distillation. | 108 // data from distillation. |
100 std::string buffer_; | 109 std::string buffer_; |
110 | |
111 ReaderModePrefs* reader_mode_prefs_; | |
nyquist
2014/06/26 20:39:57
Add a comment. For example, what can we get from t
smaslo
2014/06/27 18:33:17
Done.
| |
101 }; | 112 }; |
102 | 113 |
103 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( | 114 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
104 content::WebContents* web_contents, | 115 content::WebContents* web_contents, |
105 const std::string& expected_scheme, | 116 const std::string& expected_scheme, |
106 const std::string& expected_request_path, | 117 const std::string& expected_request_path, |
107 const content::URLDataSource::GotDataCallback& callback) | 118 const content::URLDataSource::GotDataCallback& callback, |
119 ReaderModePrefs* reader_mode_prefs) | |
108 : web_contents_(web_contents), | 120 : web_contents_(web_contents), |
109 expected_scheme_(expected_scheme), | 121 expected_scheme_(expected_scheme), |
110 expected_request_path_(expected_request_path), | 122 expected_request_path_(expected_request_path), |
111 callback_(callback), | 123 callback_(callback), |
112 page_count_(0), | 124 page_count_(0), |
113 waiting_for_page_ready_(true) { | 125 waiting_for_page_ready_(true), |
126 reader_mode_prefs_(reader_mode_prefs) { | |
114 content::WebContentsObserver::Observe(web_contents_); | 127 content::WebContentsObserver::Observe(web_contents_); |
128 reader_mode_prefs->AddObserver(this); | |
nyquist
2014/06/26 20:39:57
Nit: Use reader_mode_prefs_
smaslo
2014/06/27 18:33:17
Done.
| |
115 } | 129 } |
116 | 130 |
117 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { | 131 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
118 // Balanced with constructor although can be a no-op if frame navigated away. | 132 // Balanced with constructor although can be a no-op if frame navigated away. |
119 content::WebContentsObserver::Observe(NULL); | 133 content::WebContentsObserver::Observe(NULL); |
134 reader_mode_prefs_->RemoveObserver(this); | |
120 } | 135 } |
121 | 136 |
122 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( | 137 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( |
123 const std::string& buffer) { | 138 const std::string& buffer) { |
124 if (waiting_for_page_ready_) { | 139 if (waiting_for_page_ready_) { |
125 buffer_ += buffer; | 140 buffer_ += buffer; |
126 } else { | 141 } else { |
127 if (web_contents_) { | 142 if (web_contents_) { |
128 web_contents_->GetMainFrame()->ExecuteJavaScript( | 143 web_contents_->GetMainFrame()->ExecuteJavaScript( |
129 base::UTF8ToUTF16(buffer)); | 144 base::UTF8ToUTF16(buffer)); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 // It's possible that we didn't get some incremental updates from the | 214 // It's possible that we didn't get some incremental updates from the |
200 // distiller. Ensure all remaining pages are flushed to the viewer. | 215 // distiller. Ensure all remaining pages are flushed to the viewer. |
201 for (;page_count_ < article_proto->pages_size(); page_count_++) { | 216 for (;page_count_ < article_proto->pages_size(); page_count_++) { |
202 const DistilledPageProto& page = article_proto->pages(page_count_); | 217 const DistilledPageProto& page = article_proto->pages(page_count_); |
203 SendJavaScript( | 218 SendJavaScript( |
204 viewer::GetUnsafeIncrementalDistilledPageJs( | 219 viewer::GetUnsafeIncrementalDistilledPageJs( |
205 &page, | 220 &page, |
206 page_count_ == article_proto->pages_size())); | 221 page_count_ == article_proto->pages_size())); |
207 } | 222 } |
208 } | 223 } |
224 SendJavaScript("alert(0);"); | |
nyquist
2014/06/26 20:39:58
You probably don't want this.
smaslo
2014/06/27 18:33:17
Done.
| |
225 SetBodyClassName(); | |
209 // No need to hold on to the ViewerHandle now that distillation is complete. | 226 // No need to hold on to the ViewerHandle now that distillation is complete. |
210 viewer_handle_.reset(); | 227 viewer_handle_.reset(); |
211 } | 228 } |
212 | 229 |
213 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 230 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
214 ArticleDistillationUpdate article_update) { | 231 ArticleDistillationUpdate article_update) { |
215 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); | 232 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); |
216 page_count_++) { | 233 page_count_++) { |
217 const DistilledPageProto& page = | 234 const DistilledPageProto& page = |
218 article_update.GetDistilledPage(page_count_); | 235 article_update.GetDistilledPage(page_count_); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 content::WebContents* web_contents = | 288 content::WebContents* web_contents = |
272 content::WebContents::FromRenderFrameHost( | 289 content::WebContents::FromRenderFrameHost( |
273 content::RenderFrameHost::FromID(render_process_id, | 290 content::RenderFrameHost::FromID(render_process_id, |
274 render_frame_id)); | 291 render_frame_id)); |
275 DCHECK(web_contents); | 292 DCHECK(web_contents); |
276 // An empty |path| is invalid, but guard against it. If not empty, assume | 293 // An empty |path| is invalid, but guard against it. If not empty, assume |
277 // |path| starts with '?', which is stripped away. | 294 // |path| starts with '?', which is stripped away. |
278 const std::string path_after_query_separator = | 295 const std::string path_after_query_separator = |
279 path.size() > 0 ? path.substr(1) : ""; | 296 path.size() > 0 ? path.substr(1) : ""; |
280 RequestViewerHandle* request_viewer_handle = new RequestViewerHandle( | 297 RequestViewerHandle* request_viewer_handle = new RequestViewerHandle( |
281 web_contents, scheme_, path_after_query_separator, callback); | 298 web_contents, scheme_, path_after_query_separator, callback, |
299 dom_distiller_service_->GetReaderModePrefs()); | |
282 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 300 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
283 dom_distiller_service_, path, request_viewer_handle); | 301 dom_distiller_service_, path, request_viewer_handle); |
284 | 302 |
285 if (viewer_handle) { | 303 if (viewer_handle) { |
286 // The service returned a |ViewerHandle| and guarantees it will call | 304 // The service returned a |ViewerHandle| and guarantees it will call |
287 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 305 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
288 // request is not cancelled. The |RequestViewerHandle| will delete itself | 306 // request is not cancelled. The |RequestViewerHandle| will delete itself |
289 // after receiving the callback. | 307 // after receiving the callback. |
290 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 308 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
291 } else { | 309 } else { |
(...skipping 26 matching lines...) Expand all Loading... | |
318 void DomDistillerViewerSource::WillServiceRequest( | 336 void DomDistillerViewerSource::WillServiceRequest( |
319 const net::URLRequest* request, | 337 const net::URLRequest* request, |
320 std::string* path) const { | 338 std::string* path) const { |
321 } | 339 } |
322 | 340 |
323 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 341 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
324 const { | 342 const { |
325 return "object-src 'none'; style-src 'self';"; | 343 return "object-src 'none'; style-src 'self';"; |
326 } | 344 } |
327 | 345 |
346 void DomDistillerViewerSource::RequestViewerHandle::OnChangeTheme( | |
nyquist
2014/06/26 20:39:57
Could you move these methods up to the rest of the
smaslo
2014/06/27 18:33:17
Done.
| |
347 ReaderModePrefs* ReaderModePrefs) { | |
348 SetBodyClassName(); | |
349 } | |
350 | |
351 void DomDistillerViewerSource::RequestViewerHandle::SetBodyClassName() { | |
352 std::string s = "document.body.className='"; | |
nyquist
2014/06/26 20:39:57
Could you maybe use std::ostringstream?
std::ostr
smaslo
2014/06/27 18:33:17
Done.
| |
353 s += reader_mode_prefs_->GetBodyCssClass(); | |
354 s += "';"; | |
355 SendJavaScript(s); | |
356 } | |
357 | |
328 } // namespace dom_distiller | 358 } // namespace dom_distiller |
OLD | NEW |