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/browser/dom_distiller_viewer_source.h " | 5 #include "components/dom_distiller/content/browser/dom_distiller_viewer_source.h " |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 25 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
| 26 #include "components/dom_distiller/core/dom_distiller_service.h" | 26 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 27 #include "components/dom_distiller/core/experiments.h" | 27 #include "components/dom_distiller/core/experiments.h" |
| 28 #include "components/dom_distiller/core/feedback_reporter.h" | 28 #include "components/dom_distiller/core/feedback_reporter.h" |
| 29 #include "components/dom_distiller/core/task_tracker.h" | 29 #include "components/dom_distiller/core/task_tracker.h" |
| 30 #include "components/dom_distiller/core/url_constants.h" | 30 #include "components/dom_distiller/core/url_constants.h" |
| 31 #include "components/dom_distiller/core/url_utils.h" | 31 #include "components/dom_distiller/core/url_utils.h" |
| 32 #include "components/dom_distiller/core/viewer.h" | 32 #include "components/dom_distiller/core/viewer.h" |
| 33 #include "content/public/browser/navigation_details.h" | 33 #include "content/public/browser/navigation_details.h" |
| 34 #include "content/public/browser/navigation_entry.h" | 34 #include "content/public/browser/navigation_entry.h" |
| 35 #include "content/public/browser/navigation_handle.h" | |
| 35 #include "content/public/browser/render_frame_host.h" | 36 #include "content/public/browser/render_frame_host.h" |
| 36 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
| 37 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/browser/web_contents_observer.h" | 39 #include "content/public/browser/web_contents_observer.h" |
| 39 #include "grit/components_strings.h" | 40 #include "grit/components_strings.h" |
| 40 #include "mojo/public/cpp/bindings/strong_binding.h" | 41 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 41 #include "net/base/url_util.h" | 42 #include "net/base/url_util.h" |
| 42 #include "net/url_request/url_request.h" | 43 #include "net/url_request/url_request.h" |
| 43 #include "services/shell/public/cpp/interface_provider.h" | 44 #include "services/shell/public/cpp/interface_provider.h" |
| 44 #include "services/shell/public/cpp/interface_registry.h" | 45 #include "services/shell/public/cpp/interface_registry.h" |
| 45 #include "ui/base/l10n/l10n_util.h" | 46 #include "ui/base/l10n/l10n_util.h" |
| 46 | 47 |
| 47 namespace dom_distiller { | 48 namespace dom_distiller { |
| 48 | 49 |
| 49 // Handles receiving data asynchronously for a specific entry, and passing | 50 // Handles receiving data asynchronously for a specific entry, and passing |
| 50 // it along to the data callback for the data source. Lifetime matches that of | 51 // it along to the data callback for the data source. Lifetime matches that of |
| 51 // the current main frame's page in the Viewer instance. | 52 // the current main frame's page in the Viewer instance. |
| 52 class DomDistillerViewerSource::RequestViewerHandle | 53 class DomDistillerViewerSource::RequestViewerHandle |
| 53 : public DomDistillerRequestViewBase, | 54 : public DomDistillerRequestViewBase, |
| 54 public content::WebContentsObserver { | 55 public content::WebContentsObserver { |
| 55 public: | 56 public: |
| 56 RequestViewerHandle(content::WebContents* web_contents, | 57 RequestViewerHandle(content::WebContents* web_contents, |
| 57 const std::string& expected_scheme, | 58 const std::string& expected_scheme, |
| 58 const std::string& expected_request_path, | 59 const std::string& expected_request_path, |
| 59 DistilledPagePrefs* distilled_page_prefs); | 60 DistilledPagePrefs* distilled_page_prefs, |
| 61 std::unique_ptr<DistillerUIHandle> ui_handle); | |
| 60 ~RequestViewerHandle() override; | 62 ~RequestViewerHandle() override; |
| 61 | 63 |
| 62 // content::WebContentsObserver implementation: | 64 // content::WebContentsObserver implementation: |
| 63 void DidNavigateMainFrame( | 65 void DidFinishNavigation( |
| 64 const content::LoadCommittedDetails& details, | 66 content::NavigationHandle* navigation_handle) override; |
| 65 const content::FrameNavigateParams& params) override; | |
| 66 void RenderProcessGone(base::TerminationStatus status) override; | 67 void RenderProcessGone(base::TerminationStatus status) override; |
| 67 void WebContentsDestroyed() override; | 68 void WebContentsDestroyed() override; |
| 68 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 69 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 69 const GURL& validated_url) override; | 70 const GURL& validated_url) override; |
| 70 | 71 |
| 71 private: | 72 private: |
| 72 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | 73 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't |
| 73 // ready. | 74 // ready. |
| 74 void SendJavaScript(const std::string& buffer) override; | 75 void SendJavaScript(const std::string& buffer) override; |
| 75 | 76 |
| 76 // Cancels the current view request. Once called, no updates will be | 77 // Cancels the current view request. Once called, no updates will be |
| 77 // propagated to the view, and the request to DomDistillerService will be | 78 // propagated to the view, and the request to DomDistillerService will be |
| 78 // cancelled. | 79 // cancelled. |
| 79 void Cancel(); | 80 void Cancel(); |
| 80 | 81 |
| 81 // The scheme hosting the current view request; | 82 // The scheme hosting the current view request; |
| 82 std::string expected_scheme_; | 83 std::string expected_scheme_; |
| 83 | 84 |
| 84 // The query path for the current view request. | 85 // The query path for the current view request. |
| 85 std::string expected_request_path_; | 86 std::string expected_request_path_; |
| 86 | 87 |
| 87 // Whether the page is sufficiently initialized to handle updates from the | 88 // Whether the page is sufficiently initialized to handle updates from the |
| 88 // distiller. | 89 // distiller. |
| 89 bool waiting_for_page_ready_; | 90 bool waiting_for_page_ready_; |
| 90 | 91 |
| 91 // Temporary store of pending JavaScript if the page isn't ready to receive | 92 // Temporary store of pending JavaScript if the page isn't ready to receive |
| 92 // data from distillation. | 93 // data from distillation. |
| 93 std::string buffer_; | 94 std::string buffer_; |
| 95 | |
| 96 // An object for accessing chrome-specific UI controls including external | |
| 97 // feedback and opening the distiller settings. | |
| 98 std::unique_ptr<DistillerUIHandle> distiller_ui_handle_; | |
| 94 }; | 99 }; |
| 95 | 100 |
| 96 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( | 101 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
| 97 content::WebContents* web_contents, | 102 content::WebContents* web_contents, |
| 98 const std::string& expected_scheme, | 103 const std::string& expected_scheme, |
| 99 const std::string& expected_request_path, | 104 const std::string& expected_request_path, |
| 100 DistilledPagePrefs* distilled_page_prefs) | 105 DistilledPagePrefs* distilled_page_prefs, |
| 106 std::unique_ptr<DistillerUIHandle> ui_handle) | |
| 101 : DomDistillerRequestViewBase(distilled_page_prefs), | 107 : DomDistillerRequestViewBase(distilled_page_prefs), |
| 102 expected_scheme_(expected_scheme), | 108 expected_scheme_(expected_scheme), |
| 103 expected_request_path_(expected_request_path), | 109 expected_request_path_(expected_request_path), |
| 104 waiting_for_page_ready_(true) { | 110 waiting_for_page_ready_(true), |
| 111 distiller_ui_handle_(std::move(ui_handle)) { | |
| 105 content::WebContentsObserver::Observe(web_contents); | 112 content::WebContentsObserver::Observe(web_contents); |
| 106 distilled_page_prefs_->AddObserver(this); | 113 distilled_page_prefs_->AddObserver(this); |
| 107 } | 114 } |
| 108 | 115 |
| 109 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { | 116 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
| 110 distilled_page_prefs_->RemoveObserver(this); | 117 distilled_page_prefs_->RemoveObserver(this); |
| 111 } | 118 } |
| 112 | 119 |
| 113 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( | 120 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( |
| 114 const std::string& buffer) { | 121 const std::string& buffer) { |
| 115 if (waiting_for_page_ready_) { | 122 if (waiting_for_page_ready_) { |
| 116 buffer_ += buffer; | 123 buffer_ += buffer; |
| 117 } else { | 124 } else { |
| 118 DCHECK(buffer_.empty()); | 125 DCHECK(buffer_.empty()); |
| 119 if (web_contents()) { | 126 if (web_contents()) { |
| 120 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); | 127 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); |
| 121 } | 128 } |
| 122 } | 129 } |
| 123 } | 130 } |
| 124 | 131 |
| 125 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( | 132 void DomDistillerViewerSource::RequestViewerHandle::DidFinishNavigation( |
| 126 const content::LoadCommittedDetails& details, | 133 content::NavigationHandle* navigation_handle) { |
| 127 const content::FrameNavigateParams& params) { | 134 const GURL& navigation = navigation_handle->GetURL(); |
| 128 const GURL& navigation = details.entry->GetURL(); | 135 bool expected_main_view_request = |
| 129 if (details.is_in_page || (navigation.SchemeIs(expected_scheme_.c_str()) && | 136 navigation.SchemeIs(expected_scheme_.c_str()) && |
| 130 expected_request_path_ == navigation.query())) { | 137 expected_request_path_ == navigation.query(); |
| 138 if (navigation_handle->IsSamePage() || expected_main_view_request) { | |
| 131 // In-page navigations, as well as the main view request can be ignored. | 139 // In-page navigations, as well as the main view request can be ignored. |
| 140 if (expected_main_view_request) { | |
| 141 content::RenderFrameHost* render_frame_host = | |
| 142 navigation_handle->GetRenderFrameHost(); | |
| 143 content::RenderViewHost* render_view_host = | |
| 144 render_frame_host->GetRenderViewHost(); | |
| 145 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | |
|
nyquist
2016/09/19 21:39:51
This is our security check to ensure we never, eve
jam
2016/09/19 22:13:05
Right, the RVH is not available at that time so we
nyquist
2016/09/19 22:26:59
OK. Thanks for checking!
| |
| 146 | |
| 147 // Add mojo service for JavaScript functionality. This is the receiving | |
| 148 // end of this particular service. | |
| 149 render_frame_host->GetInterfaceRegistry()->AddInterface( | |
| 150 base::Bind(&CreateDistillerJavaScriptService, | |
| 151 render_frame_host, | |
| 152 distiller_ui_handle_.get())); | |
| 153 | |
| 154 // Tell the renderer that this is currently a distilled page. | |
| 155 mojom::DistillerPageNotifierServicePtr page_notifier_service; | |
| 156 render_frame_host->GetRemoteInterfaces()->GetInterface( | |
| 157 &page_notifier_service); | |
| 158 DCHECK(page_notifier_service); | |
| 159 page_notifier_service->NotifyIsDistillerPage(); | |
| 160 } | |
| 132 return; | 161 return; |
| 133 } | 162 } |
| 134 | 163 |
| 135 Cancel(); | 164 Cancel(); |
| 136 } | 165 } |
| 137 | 166 |
| 138 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( | 167 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( |
| 139 base::TerminationStatus status) { | 168 base::TerminationStatus status) { |
| 140 Cancel(); | 169 Cancel(); |
| 141 } | 170 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 | 224 |
| 196 void DomDistillerViewerSource::StartDataRequest( | 225 void DomDistillerViewerSource::StartDataRequest( |
| 197 const std::string& path, | 226 const std::string& path, |
| 198 int render_process_id, | 227 int render_process_id, |
| 199 int render_frame_id, | 228 int render_frame_id, |
| 200 const content::URLDataSource::GotDataCallback& callback) { | 229 const content::URLDataSource::GotDataCallback& callback) { |
| 201 content::RenderFrameHost* render_frame_host = | 230 content::RenderFrameHost* render_frame_host = |
| 202 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | 231 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 203 if (!render_frame_host) | 232 if (!render_frame_host) |
| 204 return; | 233 return; |
| 205 content::RenderViewHost* render_view_host = | |
| 206 render_frame_host->GetRenderViewHost(); | |
| 207 DCHECK(render_view_host); | |
| 208 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | |
| 209 | |
| 210 if (kViewerCssPath == path) { | 234 if (kViewerCssPath == path) { |
| 211 std::string css = viewer::GetCss(); | 235 std::string css = viewer::GetCss(); |
| 212 callback.Run(base::RefCountedString::TakeString(&css)); | 236 callback.Run(base::RefCountedString::TakeString(&css)); |
| 213 return; | 237 return; |
| 214 } else if (kViewerLoadingImagePath == path) { | 238 } else if (kViewerLoadingImagePath == path) { |
| 215 std::string image = viewer::GetLoadingImage(); | 239 std::string image = viewer::GetLoadingImage(); |
| 216 callback.Run(base::RefCountedString::TakeString(&image)); | 240 callback.Run(base::RefCountedString::TakeString(&image)); |
| 217 return; | 241 return; |
| 218 } else if (base::StartsWith(path, kViewerSaveFontScalingPath, | 242 } else if (base::StartsWith(path, kViewerSaveFontScalingPath, |
| 219 base::CompareCase::SENSITIVE)) { | 243 base::CompareCase::SENSITIVE)) { |
| 220 double scale = 1.0; | 244 double scale = 1.0; |
| 221 if (base::StringToDouble( | 245 if (base::StringToDouble( |
| 222 path.substr(strlen(kViewerSaveFontScalingPath)), &scale)) { | 246 path.substr(strlen(kViewerSaveFontScalingPath)), &scale)) { |
| 223 dom_distiller_service_->GetDistilledPagePrefs()->SetFontScaling(scale); | 247 dom_distiller_service_->GetDistilledPagePrefs()->SetFontScaling(scale); |
| 224 } | 248 } |
| 225 } | 249 } |
| 226 content::WebContents* web_contents = | 250 content::WebContents* web_contents = |
| 227 content::WebContents::FromRenderFrameHost(render_frame_host); | 251 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 228 DCHECK(web_contents); | 252 DCHECK(web_contents); |
| 229 // An empty |path| is invalid, but guard against it. If not empty, assume | 253 // An empty |path| is invalid, but guard against it. If not empty, assume |
| 230 // |path| starts with '?', which is stripped away. | 254 // |path| starts with '?', which is stripped away. |
| 231 const std::string path_after_query_separator = | 255 const std::string path_after_query_separator = |
| 232 path.size() > 0 ? path.substr(1) : ""; | 256 path.size() > 0 ? path.substr(1) : ""; |
| 233 RequestViewerHandle* request_viewer_handle = | 257 RequestViewerHandle* request_viewer_handle = |
| 234 new RequestViewerHandle(web_contents, scheme_, path_after_query_separator, | 258 new RequestViewerHandle(web_contents, scheme_, path_after_query_separator, |
| 235 dom_distiller_service_->GetDistilledPagePrefs()); | 259 dom_distiller_service_->GetDistilledPagePrefs(), |
| 260 std::move(distiller_ui_handle_)); | |
|
nyquist
2016/09/19 21:39:51
Maybe I'm misunderstanding this, but I thought tha
jam
2016/09/19 22:13:05
Thanks for catching that (somehow I misremembered
| |
| 236 std::unique_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 261 std::unique_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
| 237 dom_distiller_service_, path, request_viewer_handle, | 262 dom_distiller_service_, path, request_viewer_handle, |
| 238 web_contents->GetContainerBounds().size()); | 263 web_contents->GetContainerBounds().size()); |
| 239 | 264 |
| 240 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); | 265 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); |
| 241 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( | 266 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( |
| 242 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), | 267 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), |
| 243 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), | 268 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
| 244 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); | 269 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); |
| 245 | 270 |
| 246 // Add mojo service for JavaScript functionality. This is the receiving end | |
| 247 // of this particular service. | |
| 248 render_frame_host->GetInterfaceRegistry()->AddInterface( | |
| 249 base::Bind(&CreateDistillerJavaScriptService, | |
| 250 render_frame_host, | |
| 251 distiller_ui_handle_.get())); | |
| 252 | |
| 253 // Tell the renderer that this is currently a distilled page. | |
| 254 mojom::DistillerPageNotifierServicePtr page_notifier_service; | |
| 255 render_frame_host->GetRemoteInterfaces()->GetInterface( | |
| 256 &page_notifier_service); | |
| 257 DCHECK(page_notifier_service); | |
| 258 page_notifier_service->NotifyIsDistillerPage(); | |
| 259 | |
| 260 if (viewer_handle) { | 271 if (viewer_handle) { |
| 261 // The service returned a |ViewerHandle| and guarantees it will call | 272 // The service returned a |ViewerHandle| and guarantees it will call |
| 262 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 273 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
| 263 // request is not cancelled. The |RequestViewerHandle| will delete itself | 274 // request is not cancelled. The |RequestViewerHandle| will delete itself |
| 264 // after receiving the callback. | 275 // after receiving the callback. |
| 265 request_viewer_handle->TakeViewerHandle(std::move(viewer_handle)); | 276 request_viewer_handle->TakeViewerHandle(std::move(viewer_handle)); |
| 266 } else { | 277 } else { |
| 267 request_viewer_handle->FlagAsErrorPage(); | 278 request_viewer_handle->FlagAsErrorPage(); |
| 268 } | 279 } |
| 269 | 280 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 295 std::string DomDistillerViewerSource::GetContentSecurityPolicyStyleSrc() | 306 std::string DomDistillerViewerSource::GetContentSecurityPolicyStyleSrc() |
| 296 const { | 307 const { |
| 297 return "style-src 'self' https://fonts.googleapis.com;"; | 308 return "style-src 'self' https://fonts.googleapis.com;"; |
| 298 } | 309 } |
| 299 | 310 |
| 300 std::string DomDistillerViewerSource::GetContentSecurityPolicyChildSrc() const { | 311 std::string DomDistillerViewerSource::GetContentSecurityPolicyChildSrc() const { |
| 301 return "child-src *;"; | 312 return "child-src *;"; |
| 302 } | 313 } |
| 303 | 314 |
| 304 } // namespace dom_distiller | 315 } // namespace dom_distiller |
| OLD | NEW |