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 "components/dom_distiller/core/task_tracker.h" | 15 #include "components/dom_distiller/core/task_tracker.h" |
15 #include "components/dom_distiller/core/url_constants.h" | 16 #include "components/dom_distiller/core/url_constants.h" |
16 #include "components/dom_distiller/core/viewer.h" | 17 #include "components/dom_distiller/core/viewer.h" |
18 #include "content/public/browser/navigation_details.h" | |
17 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
18 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/web_contents.h" | |
22 #include "content/public/browser/web_contents_observer.h" | |
19 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
20 | 24 |
21 namespace dom_distiller { | 25 namespace dom_distiller { |
22 | 26 |
23 // Handles receiving data asynchronously for a specific entry, and passing | 27 // Handles receiving data asynchronously for a specific entry, and passing |
24 // it along to the data callback for the data source. | 28 // it along to the data callback for the data source. Lifetime matches that of |
29 // the RenderFrameHost for the Viewer instance. | |
Charlie Reis
2014/05/12 22:12:52
nit: Lifetime matches that of the current main fra
Yaron
2014/05/14 17:42:33
Done.
| |
25 class DomDistillerViewerSource::RequestViewerHandle | 30 class DomDistillerViewerSource::RequestViewerHandle |
26 : public ViewRequestDelegate { | 31 : public ViewRequestDelegate, |
32 public content::WebContentsObserver { | |
27 public: | 33 public: |
28 explicit RequestViewerHandle( | 34 explicit RequestViewerHandle( |
35 content::WebContents* web_contents, | |
29 const content::URLDataSource::GotDataCallback& callback); | 36 const content::URLDataSource::GotDataCallback& callback); |
30 virtual ~RequestViewerHandle(); | 37 virtual ~RequestViewerHandle(); |
31 | 38 |
32 // ViewRequestDelegate implementation. | 39 // ViewRequestDelegate implementation. |
33 virtual void OnArticleReady( | 40 virtual void OnArticleReady( |
34 const DistilledArticleProto* article_proto) OVERRIDE; | 41 const DistilledArticleProto* article_proto) OVERRIDE; |
35 | 42 |
36 virtual void OnArticleUpdated( | 43 virtual void OnArticleUpdated( |
37 ArticleDistillationUpdate article_update) OVERRIDE; | 44 ArticleDistillationUpdate article_update) OVERRIDE; |
38 | 45 |
39 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | 46 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
40 | 47 |
48 // WebContentsObserver: | |
49 virtual void DidNavigateMainFrame( | |
50 const content::LoadCommittedDetails& details, | |
51 const content::FrameNavigateParams& params) OVERRIDE; | |
52 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | |
53 virtual void WebContentsDestroyed(content::WebContents* web_contents) | |
54 OVERRIDE; | |
55 virtual void DidFinishLoad( | |
56 int64 frame_id, | |
57 const GURL& validated_url, | |
58 bool is_main_frame, | |
59 content::RenderViewHost* render_view_host) OVERRIDE; | |
60 | |
41 private: | 61 private: |
62 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | |
63 // ready. | |
64 void SendJavaScript(const std::string& buffer); | |
65 | |
66 // Cancels the current view request. Once called, no updates will be | |
67 // propagated to the view, and the request to DomDistillerService will be | |
68 // cancelled. | |
69 void Cancel(); | |
70 | |
42 // The handle to the view request towards the DomDistillerService. It | 71 // The handle to the view request towards the DomDistillerService. It |
43 // needs to be kept around to ensure the distillation request finishes. | 72 // needs to be kept around to ensure the distillation request finishes. |
44 scoped_ptr<ViewerHandle> viewer_handle_; | 73 scoped_ptr<ViewerHandle> viewer_handle_; |
45 | 74 |
46 // This holds the callback to where the data retrieved is sent back. | 75 // WebContents associated with the Viewer's render process. |
76 content::WebContents* web_contents_; | |
77 | |
78 // Holds the callback to where the data retrieved is sent back. | |
47 content::URLDataSource::GotDataCallback callback_; | 79 content::URLDataSource::GotDataCallback callback_; |
80 | |
81 // Number of pages of the distilled article content that have been rendered by | |
82 // the viewer. | |
83 int page_count_; | |
84 | |
85 // Whether the page is sufficiently initialized to handle updates from the | |
86 // distiller. | |
87 bool waiting_for_page_ready_; | |
88 | |
89 // Temporary store of pending JavaScript if the page isn't ready to receive | |
90 // data from distillation. | |
91 std::string buffer_; | |
48 }; | 92 }; |
49 | 93 |
50 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( | 94 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
95 content::WebContents* web_contents, | |
51 const content::URLDataSource::GotDataCallback& callback) | 96 const content::URLDataSource::GotDataCallback& callback) |
52 : callback_(callback) { | 97 : web_contents_(web_contents), |
98 callback_(callback), | |
99 page_count_(0), | |
100 waiting_for_page_ready_(true) { | |
101 content::WebContentsObserver::Observe(web_contents_); | |
53 } | 102 } |
54 | 103 |
55 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { | 104 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
105 // Balanced with constructor although can be a no-op if frame navigated away. | |
106 content::WebContentsObserver::Observe(NULL); | |
107 } | |
108 | |
109 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( | |
110 const std::string& buffer) { | |
111 if (waiting_for_page_ready_) { | |
112 buffer_ += buffer; | |
113 } else { | |
114 if (web_contents_) { | |
115 web_contents_->GetMainFrame()->ExecuteJavaScript( | |
116 base::UTF8ToUTF16(buffer)); | |
117 } | |
118 } | |
119 } | |
120 | |
121 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( | |
122 const content::LoadCommittedDetails& details, | |
123 const content::FrameNavigateParams& params) { | |
124 if (!details.is_in_page) { | |
125 Cancel(); | |
126 } | |
127 } | |
128 | |
129 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( | |
130 base::TerminationStatus status) { | |
131 Cancel(); | |
132 } | |
133 | |
134 void DomDistillerViewerSource::RequestViewerHandle::WebContentsDestroyed( | |
135 content::WebContents* web_contents) { | |
136 Cancel(); | |
137 } | |
138 | |
139 void DomDistillerViewerSource::RequestViewerHandle::Cancel() { | |
140 // Ensure we don't send any incremental updates to the Viewer. | |
141 web_contents_ = NULL; | |
142 | |
143 // No need to listen for notifications. | |
144 content::WebContentsObserver::Observe(NULL); | |
145 | |
146 // Schedule the Viewer for deletion. Ensures distillation is cancelled, and | |
147 // any pending data stored in |buffer_| is released. | |
148 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
149 } | |
150 | |
151 void DomDistillerViewerSource::RequestViewerHandle::DidFinishLoad( | |
152 int64 frame_id, | |
153 const GURL& validated_url, | |
154 bool is_main_frame, | |
155 content::RenderViewHost* render_view_host) { | |
156 if (!is_main_frame || web_contents_ == NULL) { | |
157 return; | |
158 } | |
159 waiting_for_page_ready_ = false; | |
160 if (buffer_.empty()) { | |
161 return; | |
162 } | |
163 if (web_contents_) { | |
164 web_contents_->GetMainFrame()->ExecuteJavaScript( | |
165 base::UTF8ToUTF16(buffer_)); | |
166 } | |
167 buffer_.clear(); | |
56 } | 168 } |
57 | 169 |
58 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( | 170 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
59 const DistilledArticleProto* article_proto) { | 171 const DistilledArticleProto* article_proto) { |
60 std::string unsafe_page_html = viewer::GetUnsafeHtml(article_proto); | 172 if (page_count_ == 0) { |
61 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 173 // This is a single-page article. |
62 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 174 std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto); |
175 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | |
176 } else if (page_count_ == article_proto->pages_size()) { | |
177 // We may still be showing the "Loading" indicator. | |
178 SendJavaScript(viewer::GetToggleLoadingIndicatorJs(true)); | |
179 } else { | |
180 // It's possible that we didn't get some incremental updates from the | |
181 // distiller. Ensure all remaining pages are flushed to the viewer. | |
182 for (;page_count_ < article_proto->pages_size(); page_count_++) { | |
183 const DistilledPageProto& page = article_proto->pages(page_count_); | |
184 SendJavaScript( | |
185 viewer::GetUnsafeIncrementalDistilledPageJs( | |
186 &page, | |
187 page_count_ == article_proto->pages_size())); | |
188 } | |
189 } | |
190 // No need to hold on to the ViewerHandle now that distillation is complete. | |
191 viewer_handle_.reset(); | |
63 } | 192 } |
64 | 193 |
65 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 194 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
66 ArticleDistillationUpdate article_update) { | 195 ArticleDistillationUpdate article_update) { |
67 // TODO(nyquist): Add support for displaying pages incrementally. | 196 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); |
197 page_count_++) { | |
198 const DistilledPageProto& page = | |
199 article_update.GetDistilledPage(page_count_); | |
200 if (page_count_ == 0) { | |
nyquist
2014/05/13 05:01:15
Nit: Add a comment like:
// This is the first page
Yaron
2014/05/14 17:42:33
Done.
| |
201 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml(&page); | |
202 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | |
203 } else { | |
204 SendJavaScript( | |
205 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | |
206 } | |
207 } | |
68 } | 208 } |
69 | 209 |
70 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( | 210 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
71 scoped_ptr<ViewerHandle> viewer_handle) { | 211 scoped_ptr<ViewerHandle> viewer_handle) { |
72 viewer_handle_ = viewer_handle.Pass(); | 212 viewer_handle_ = viewer_handle.Pass(); |
73 } | 213 } |
74 | 214 |
75 DomDistillerViewerSource::DomDistillerViewerSource( | 215 DomDistillerViewerSource::DomDistillerViewerSource( |
76 DomDistillerServiceInterface* dom_distiller_service, | 216 DomDistillerServiceInterface* dom_distiller_service, |
77 const std::string& scheme) | 217 const std::string& scheme) |
(...skipping 13 matching lines...) Expand all Loading... | |
91 int render_frame_id, | 231 int render_frame_id, |
92 const content::URLDataSource::GotDataCallback& callback) { | 232 const content::URLDataSource::GotDataCallback& callback) { |
93 content::RenderFrameHost* render_frame_host = | 233 content::RenderFrameHost* render_frame_host = |
94 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | 234 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
95 DCHECK(render_frame_host); | 235 DCHECK(render_frame_host); |
96 content::RenderViewHost* render_view_host = | 236 content::RenderViewHost* render_view_host = |
97 render_frame_host->GetRenderViewHost(); | 237 render_frame_host->GetRenderViewHost(); |
98 DCHECK(render_view_host); | 238 DCHECK(render_view_host); |
99 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 239 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
100 | 240 |
101 if (kCssPath == path) { | 241 if (kViewerCssPath == path) { |
102 std::string css = viewer::GetCss(); | 242 std::string css = viewer::GetCss(); |
103 callback.Run(base::RefCountedString::TakeString(&css)); | 243 callback.Run(base::RefCountedString::TakeString(&css)); |
104 return; | 244 return; |
105 } | 245 } |
106 | 246 if (kViewerJsPath == path) { |
247 std::string js = viewer::GetJavaScript(); | |
248 callback.Run(base::RefCountedString::TakeString(&js)); | |
249 return; | |
250 } | |
251 content::WebContents* web_contents = | |
252 content::WebContents::FromRenderFrameHost( | |
253 content::RenderFrameHost::FromID(render_process_id, | |
254 render_frame_id)); | |
255 DCHECK(web_contents); | |
107 RequestViewerHandle* request_viewer_handle = | 256 RequestViewerHandle* request_viewer_handle = |
108 new RequestViewerHandle(callback); | 257 new RequestViewerHandle(web_contents, callback); |
109 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 258 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
110 dom_distiller_service_, path, request_viewer_handle); | 259 dom_distiller_service_, path, request_viewer_handle); |
111 | 260 |
112 if (viewer_handle) { | 261 if (viewer_handle) { |
113 // The service returned a |ViewerHandle| and guarantees it will call | 262 // The service returned a |ViewerHandle| and guarantees it will call |
114 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 263 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
115 // request is not cancelled. The |RequestViewerHandle| will delete itself | 264 // request is not cancelled. The |RequestViewerHandle| will delete itself |
116 // after receiving the callback. | 265 // after receiving the callback. |
117 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 266 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
118 } else { | 267 } else { |
119 // The service did not return a |ViewerHandle|, which means the | 268 // The service did not return a |ViewerHandle|, which means the |
120 // |RequestViewerHandle| will never be called, so clean up now. | 269 // |RequestViewerHandle| will never be called, so clean up now. |
121 delete request_viewer_handle; | 270 delete request_viewer_handle; |
122 | 271 |
123 std::string error_page_html = viewer::GetErrorPageHtml(); | 272 std::string error_page_html = viewer::GetErrorPageHtml(); |
124 callback.Run(base::RefCountedString::TakeString(&error_page_html)); | 273 callback.Run(base::RefCountedString::TakeString(&error_page_html)); |
125 } | 274 } |
126 }; | 275 }; |
127 | 276 |
128 std::string DomDistillerViewerSource::GetMimeType( | 277 std::string DomDistillerViewerSource::GetMimeType( |
129 const std::string& path) const { | 278 const std::string& path) const { |
130 if (path == kCssPath) | 279 if (kViewerCssPath == path) |
nyquist
2014/05/13 05:01:15
Nit: Following the style of the rest of this CL, a
Yaron
2014/05/14 17:42:33
Done.
| |
131 return "text/css"; | 280 return "text/css"; |
281 else if (kViewerJsPath == path) | |
282 return "text/javascript"; | |
132 return "text/html"; | 283 return "text/html"; |
133 } | 284 } |
134 | 285 |
135 bool DomDistillerViewerSource::ShouldServiceRequest( | 286 bool DomDistillerViewerSource::ShouldServiceRequest( |
136 const net::URLRequest* request) const { | 287 const net::URLRequest* request) const { |
137 return request->url().SchemeIs(scheme_.c_str()); | 288 return request->url().SchemeIs(scheme_.c_str()); |
138 } | 289 } |
139 | 290 |
140 // TODO(nyquist): Start tracking requests using this method. | 291 // TODO(nyquist): Start tracking requests using this method. |
141 void DomDistillerViewerSource::WillServiceRequest( | 292 void DomDistillerViewerSource::WillServiceRequest( |
142 const net::URLRequest* request, | 293 const net::URLRequest* request, |
143 std::string* path) const { | 294 std::string* path) const { |
144 } | 295 } |
145 | 296 |
146 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 297 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
147 const { | 298 const { |
148 return "object-src 'none'; style-src 'self';"; | 299 return "object-src 'none'; style-src 'self';"; |
149 } | 300 } |
150 | 301 |
151 } // namespace dom_distiller | 302 } // namespace dom_distiller |
OLD | NEW |