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" | |
17 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 16 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
18 #include "components/dom_distiller/content/browser/external_feedback_reporter.h" | 17 #include "components/dom_distiller/content/browser/external_feedback_reporter.h" |
19 #include "components/dom_distiller/content/common/distiller_page_notifier_servic
e.mojom.h" | |
20 #include "components/dom_distiller/core/distilled_page_prefs.h" | 18 #include "components/dom_distiller/core/distilled_page_prefs.h" |
21 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" | 19 #include "components/dom_distiller/core/dom_distiller_request_view_base.h" |
22 #include "components/dom_distiller/core/dom_distiller_service.h" | 20 #include "components/dom_distiller/core/dom_distiller_service.h" |
23 #include "components/dom_distiller/core/experiments.h" | 21 #include "components/dom_distiller/core/experiments.h" |
24 #include "components/dom_distiller/core/feedback_reporter.h" | 22 #include "components/dom_distiller/core/feedback_reporter.h" |
25 #include "components/dom_distiller/core/task_tracker.h" | 23 #include "components/dom_distiller/core/task_tracker.h" |
26 #include "components/dom_distiller/core/url_constants.h" | 24 #include "components/dom_distiller/core/url_constants.h" |
27 #include "components/dom_distiller/core/url_utils.h" | 25 #include "components/dom_distiller/core/url_utils.h" |
28 #include "components/dom_distiller/core/viewer.h" | 26 #include "components/dom_distiller/core/viewer.h" |
29 #include "content/public/browser/navigation_details.h" | 27 #include "content/public/browser/navigation_details.h" |
30 #include "content/public/browser/navigation_entry.h" | 28 #include "content/public/browser/navigation_entry.h" |
31 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
32 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
33 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
34 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
35 #include "content/public/browser/web_contents_observer.h" | 33 #include "content/public/browser/web_contents_observer.h" |
36 #include "content/public/common/service_registry.h" | |
37 #include "grit/components_strings.h" | 34 #include "grit/components_strings.h" |
38 #include "net/base/url_util.h" | 35 #include "net/base/url_util.h" |
39 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
40 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h" | |
41 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
42 | 38 |
43 namespace dom_distiller { | 39 namespace dom_distiller { |
44 | 40 |
45 // Handles receiving data asynchronously for a specific entry, and passing | 41 // Handles receiving data asynchronously for a specific entry, and passing |
46 // it along to the data callback for the data source. Lifetime matches that of | 42 // it along to the data callback for the data source. Lifetime matches that of |
47 // the current main frame's page in the Viewer instance. | 43 // the current main frame's page in the Viewer instance. |
48 class DomDistillerViewerSource::RequestViewerHandle | 44 class DomDistillerViewerSource::RequestViewerHandle |
49 : public DomDistillerRequestViewBase, | 45 : public DomDistillerRequestViewBase, |
50 public content::WebContentsObserver { | 46 public content::WebContentsObserver { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 if (web_contents()) { | 110 if (web_contents()) { |
115 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); | 111 RunIsolatedJavaScript(web_contents()->GetMainFrame(), buffer); |
116 } | 112 } |
117 } | 113 } |
118 } | 114 } |
119 | 115 |
120 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( | 116 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( |
121 const content::LoadCommittedDetails& details, | 117 const content::LoadCommittedDetails& details, |
122 const content::FrameNavigateParams& params) { | 118 const content::FrameNavigateParams& params) { |
123 const GURL& navigation = details.entry->GetURL(); | 119 const GURL& navigation = details.entry->GetURL(); |
124 if (details.is_in_page || (navigation.SchemeIs(expected_scheme_.c_str()) && | 120 if (details.is_in_page || ( |
125 expected_request_path_ == navigation.query())) { | 121 navigation.SchemeIs(expected_scheme_.c_str()) && |
| 122 expected_request_path_ == navigation.query())) { |
126 // In-page navigations, as well as the main view request can be ignored. | 123 // In-page navigations, as well as the main view request can be ignored. |
127 return; | 124 return; |
128 } | 125 } |
129 | 126 |
130 Cancel(); | 127 Cancel(); |
131 } | 128 } |
132 | 129 |
133 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( | 130 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( |
134 base::TerminationStatus status) { | 131 base::TerminationStatus status) { |
135 Cancel(); | 132 Cancel(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 return scheme_ + "://"; | 177 return scheme_ + "://"; |
181 } | 178 } |
182 | 179 |
183 void DomDistillerViewerSource::StartDataRequest( | 180 void DomDistillerViewerSource::StartDataRequest( |
184 const std::string& path, | 181 const std::string& path, |
185 int render_process_id, | 182 int render_process_id, |
186 int render_frame_id, | 183 int render_frame_id, |
187 const content::URLDataSource::GotDataCallback& callback) { | 184 const content::URLDataSource::GotDataCallback& callback) { |
188 content::RenderFrameHost* render_frame_host = | 185 content::RenderFrameHost* render_frame_host = |
189 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | 186 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
190 if (!render_frame_host) | 187 if (!render_frame_host) return; |
191 return; | |
192 content::RenderViewHost* render_view_host = | 188 content::RenderViewHost* render_view_host = |
193 render_frame_host->GetRenderViewHost(); | 189 render_frame_host->GetRenderViewHost(); |
194 DCHECK(render_view_host); | 190 DCHECK(render_view_host); |
195 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 191 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
196 | 192 |
197 if (kViewerCssPath == path) { | 193 if (kViewerCssPath == path) { |
198 std::string css = viewer::GetCss(); | 194 std::string css = viewer::GetCss(); |
199 callback.Run(base::RefCountedString::TakeString(&css)); | 195 callback.Run(base::RefCountedString::TakeString(&css)); |
200 return; | 196 return; |
201 } else if (kViewerViewOriginalPath == path) { | 197 } else if (kViewerViewOriginalPath == path) { |
(...skipping 28 matching lines...) Expand all Loading... |
230 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 226 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
231 dom_distiller_service_, path, request_viewer_handle, | 227 dom_distiller_service_, path, request_viewer_handle, |
232 web_contents->GetContainerBounds().size()); | 228 web_contents->GetContainerBounds().size()); |
233 | 229 |
234 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); | 230 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); |
235 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( | 231 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( |
236 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), | 232 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), |
237 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), | 233 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), |
238 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); | 234 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); |
239 | 235 |
240 // Add mojo service for JavaScript functionality. This is the receiving end | |
241 // of this particular service. | |
242 render_frame_host->GetServiceRegistry()->AddService( | |
243 base::Bind(&CreateDistillerJavaScriptService)); | |
244 | |
245 // Tell the renderer that this is currently a distilled page. | |
246 DistillerPageNotifierServicePtr page_notifier_service; | |
247 render_frame_host->GetServiceRegistry()->ConnectToRemoteService( | |
248 mojo::GetProxy(&page_notifier_service)); | |
249 DCHECK(page_notifier_service); | |
250 page_notifier_service->NotifyIsDistillerPage(); | |
251 | |
252 if (viewer_handle) { | 236 if (viewer_handle) { |
253 // The service returned a |ViewerHandle| and guarantees it will call | 237 // The service returned a |ViewerHandle| and guarantees it will call |
254 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 238 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
255 // request is not cancelled. The |RequestViewerHandle| will delete itself | 239 // request is not cancelled. The |RequestViewerHandle| will delete itself |
256 // after receiving the callback. | 240 // after receiving the callback. |
257 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 241 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
258 } else { | 242 } else { |
259 request_viewer_handle->FlagAsErrorPage(); | 243 request_viewer_handle->FlagAsErrorPage(); |
260 } | 244 } |
261 | 245 |
(...skipping 26 matching lines...) Expand all Loading... |
288 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 272 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
289 const { | 273 const { |
290 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; | 274 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; |
291 } | 275 } |
292 | 276 |
293 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { | 277 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { |
294 return "frame-src *;"; | 278 return "frame-src *;"; |
295 } | 279 } |
296 | 280 |
297 } // namespace dom_distiller | 281 } // namespace dom_distiller |
OLD | NEW |