| 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/browser/dom_distiller_viewer_source.h
" | 5 #include "components/dom_distiller/content/browser/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/metrics/user_metrics.h" | 14 #include "base/metrics/user_metrics.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/dom_distiller/content/browser/distiller_javascript_service_
impl.h" |
| 16 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 17 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 17 #include "components/dom_distiller/content/browser/external_feedback_reporter.h" | 18 #include "components/dom_distiller/content/browser/external_feedback_reporter.h" |
| 19 #include "components/dom_distiller/content/common/distiller_page_notifier_servic
e.mojom.h" |
| 18 #include "components/dom_distiller/core/distilled_page_prefs.h" | 20 #include "components/dom_distiller/core/distilled_page_prefs.h" |
| 19 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 21 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
| 20 #include "components/dom_distiller/core/dom_distiller_service.h" | 22 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 21 #include "components/dom_distiller/core/experiments.h" | 23 #include "components/dom_distiller/core/experiments.h" |
| 22 #include "components/dom_distiller/core/feedback_reporter.h" | 24 #include "components/dom_distiller/core/feedback_reporter.h" |
| 23 #include "components/dom_distiller/core/task_tracker.h" | 25 #include "components/dom_distiller/core/task_tracker.h" |
| 24 #include "components/dom_distiller/core/url_constants.h" | 26 #include "components/dom_distiller/core/url_constants.h" |
| 25 #include "components/dom_distiller/core/url_utils.h" | 27 #include "components/dom_distiller/core/url_utils.h" |
| 26 #include "components/dom_distiller/core/viewer.h" | 28 #include "components/dom_distiller/core/viewer.h" |
| 27 #include "content/public/browser/navigation_details.h" | 29 #include "content/public/browser/navigation_details.h" |
| 28 #include "content/public/browser/navigation_entry.h" | 30 #include "content/public/browser/navigation_entry.h" |
| 29 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
| 30 #include "content/public/browser/render_view_host.h" | 32 #include "content/public/browser/render_view_host.h" |
| 31 #include "content/public/browser/user_metrics.h" | 33 #include "content/public/browser/user_metrics.h" |
| 32 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/browser/web_contents_observer.h" | 35 #include "content/public/browser/web_contents_observer.h" |
| 36 #include "content/public/common/service_registry.h" |
| 34 #include "grit/components_strings.h" | 37 #include "grit/components_strings.h" |
| 35 #include "net/base/url_util.h" | 38 #include "net/base/url_util.h" |
| 36 #include "net/url_request/url_request.h" | 39 #include "net/url_request/url_request.h" |
| 40 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h" |
| 37 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
| 38 | 42 |
| 39 namespace dom_distiller { | 43 namespace dom_distiller { |
| 40 | 44 |
| 41 // Handles receiving data asynchronously for a specific entry, and passing | 45 // Handles receiving data asynchronously for a specific entry, and passing |
| 42 // it along to the data callback for the data source. Lifetime matches that of | 46 // it along to the data callback for the data source. Lifetime matches that of |
| 43 // the current main frame's page in the Viewer instance. | 47 // the current main frame's page in the Viewer instance. |
| 44 class DomDistillerViewerSource::RequestViewerHandle | 48 class DomDistillerViewerSource::RequestViewerHandle |
| 45 : public DomDistillerRequestViewBase, | 49 : public DomDistillerRequestViewBase, |
| 46 public content::WebContentsObserver { | 50 public content::WebContentsObserver { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (web_contents()) { | 114 if (web_contents()) { |
| 111 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); | 115 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); |
| 112 } | 116 } |
| 113 } | 117 } |
| 114 } | 118 } |
| 115 | 119 |
| 116 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( | 120 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( |
| 117 const content::LoadCommittedDetails& details, | 121 const content::LoadCommittedDetails& details, |
| 118 const content::FrameNavigateParams& params) { | 122 const content::FrameNavigateParams& params) { |
| 119 const GURL& navigation = details.entry->GetURL(); | 123 const GURL& navigation = details.entry->GetURL(); |
| 120 if (details.is_in_page || ( | 124 if (details.is_in_page || (navigation.SchemeIs(expected_scheme_.c_str()) && |
| 121 navigation.SchemeIs(expected_scheme_.c_str()) && | 125 expected_request_path_ == navigation.query())) { |
| 122 expected_request_path_ == navigation.query())) { | |
| 123 // In-page navigations, as well as the main view request can be ignored. | 126 // In-page navigations, as well as the main view request can be ignored. |
| 124 return; | 127 return; |
| 125 } | 128 } |
| 126 | 129 |
| 127 Cancel(); | 130 Cancel(); |
| 128 } | 131 } |
| 129 | 132 |
| 130 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( | 133 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( |
| 131 base::TerminationStatus status) { | 134 base::TerminationStatus status) { |
| 132 Cancel(); | 135 Cancel(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 waiting_for_page_ready_ = false; | 155 waiting_for_page_ready_ = false; |
| 153 SendJavaScript(viewer::GetJavaScript()); | 156 SendJavaScript(viewer::GetJavaScript()); |
| 154 SendJavaScript(viewer::GetErrorPageJs()); | 157 SendJavaScript(viewer::GetErrorPageJs()); |
| 155 std::string title(l10n_util::GetStringUTF8( | 158 std::string title(l10n_util::GetStringUTF8( |
| 156 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)); | 159 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)); |
| 157 SendJavaScript(viewer::GetSetTitleJs(title)); | 160 SendJavaScript(viewer::GetSetTitleJs(title)); |
| 158 SendJavaScript(viewer::GetSetTextDirectionJs(std::string("auto"))); | 161 SendJavaScript(viewer::GetSetTextDirectionJs(std::string("auto"))); |
| 159 if (ShouldShowFeedbackForm()) { | 162 if (ShouldShowFeedbackForm()) { |
| 160 SendJavaScript(viewer::GetShowFeedbackFormJs()); | 163 SendJavaScript(viewer::GetShowFeedbackFormJs()); |
| 161 } | 164 } |
| 165 |
| 162 Cancel(); // This will cause the object to clean itself up. | 166 Cancel(); // This will cause the object to clean itself up. |
| 163 return; | 167 return; |
| 164 } | 168 } |
| 165 | 169 |
| 166 if (render_frame_host->GetParent()) { | 170 if (render_frame_host->GetParent()) { |
| 167 return; | 171 return; |
| 168 } | 172 } |
| 169 waiting_for_page_ready_ = false; | 173 waiting_for_page_ready_ = false; |
| 170 if (buffer_.empty()) { | 174 if (buffer_.empty()) { |
| 171 return; | 175 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 190 return scheme_ + "://"; | 194 return scheme_ + "://"; |
| 191 } | 195 } |
| 192 | 196 |
| 193 void DomDistillerViewerSource::StartDataRequest( | 197 void DomDistillerViewerSource::StartDataRequest( |
| 194 const std::string& path, | 198 const std::string& path, |
| 195 int render_process_id, | 199 int render_process_id, |
| 196 int render_frame_id, | 200 int render_frame_id, |
| 197 const content::URLDataSource::GotDataCallback& callback) { | 201 const content::URLDataSource::GotDataCallback& callback) { |
| 198 content::RenderFrameHost* render_frame_host = | 202 content::RenderFrameHost* render_frame_host = |
| 199 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | 203 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 200 if (!render_frame_host) return; | 204 if (!render_frame_host) |
| 205 return; |
| 201 content::RenderViewHost* render_view_host = | 206 content::RenderViewHost* render_view_host = |
| 202 render_frame_host->GetRenderViewHost(); | 207 render_frame_host->GetRenderViewHost(); |
| 203 DCHECK(render_view_host); | 208 DCHECK(render_view_host); |
| 204 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 209 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
| 205 | 210 |
| 206 if (kViewerCssPath == path) { | 211 if (kViewerCssPath == path) { |
| 207 std::string css = viewer::GetCss(); | 212 std::string css = viewer::GetCss(); |
| 208 callback.Run(base::RefCountedString::TakeString(&css)); | 213 callback.Run(base::RefCountedString::TakeString(&css)); |
| 209 return; | 214 return; |
| 210 } else if (kViewerViewOriginalPath == path) { | 215 } else if (kViewerViewOriginalPath == path) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 239 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 244 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
| 240 dom_distiller_service_, path, request_viewer_handle, | 245 dom_distiller_service_, path, request_viewer_handle, |
| 241 web_contents->GetContainerBounds().size()); | 246 web_contents->GetContainerBounds().size()); |
| 242 | 247 |
| 243 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); | 248 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); |
| 244 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( | 249 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( |
| 245 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), | 250 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), |
| 246 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), | 251 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
| 247 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); | 252 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); |
| 248 | 253 |
| 254 // Add mojo service for JavaScript functionality. This is the receiving end |
| 255 // of this particular service. |
| 256 render_frame_host->GetServiceRegistry()->AddService( |
| 257 base::Bind(&CreateDistillerJavaScriptService)); |
| 258 |
| 259 // Tell the renderer that this is currently a distilled page. |
| 260 DistillerPageNotifierServicePtr page_notifier_service; |
| 261 render_frame_host->GetServiceRegistry()->ConnectToRemoteService( |
| 262 mojo::GetProxy(&page_notifier_service)); |
| 263 DCHECK(page_notifier_service); |
| 264 page_notifier_service->Notify(); |
| 265 |
| 249 if (viewer_handle) { | 266 if (viewer_handle) { |
| 250 // The service returned a |ViewerHandle| and guarantees it will call | 267 // The service returned a |ViewerHandle| and guarantees it will call |
| 251 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 268 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
| 252 // request is not cancelled. The |RequestViewerHandle| will delete itself | 269 // request is not cancelled. The |RequestViewerHandle| will delete itself |
| 253 // after receiving the callback. | 270 // after receiving the callback. |
| 254 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 271 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
| 255 } else { | 272 } else { |
| 256 request_viewer_handle->FlagAsErrorPage(); | 273 request_viewer_handle->FlagAsErrorPage(); |
| 257 } | 274 } |
| 258 | 275 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 285 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 302 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
| 286 const { | 303 const { |
| 287 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; | 304 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; |
| 288 } | 305 } |
| 289 | 306 |
| 290 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { | 307 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { |
| 291 return "frame-src *;"; | 308 return "frame-src *;"; |
| 292 } | 309 } |
| 293 | 310 |
| 294 } // namespace dom_distiller | 311 } // namespace dom_distiller |
| OLD | NEW |