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