Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(880)

Side by Side Diff: components/dom_distiller/content/browser/dom_distiller_viewer_source.cc

Issue 1231083007: Expose distiller functions to JavaScript (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-content
Patch Set: Move dependencies to correct files Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 return scheme_ + "://"; 180 return scheme_ + "://";
178 } 181 }
179 182
180 void DomDistillerViewerSource::StartDataRequest( 183 void DomDistillerViewerSource::StartDataRequest(
181 const std::string& path, 184 const std::string& path,
182 int render_process_id, 185 int render_process_id,
183 int render_frame_id, 186 int render_frame_id,
184 const content::URLDataSource::GotDataCallback& callback) { 187 const content::URLDataSource::GotDataCallback& callback) {
185 content::RenderFrameHost* render_frame_host = 188 content::RenderFrameHost* render_frame_host =
186 content::RenderFrameHost::FromID(render_process_id, render_frame_id); 189 content::RenderFrameHost::FromID(render_process_id, render_frame_id);
187 if (!render_frame_host) return; 190 if (!render_frame_host)
191 return;
188 content::RenderViewHost* render_view_host = 192 content::RenderViewHost* render_view_host =
189 render_frame_host->GetRenderViewHost(); 193 render_frame_host->GetRenderViewHost();
190 DCHECK(render_view_host); 194 DCHECK(render_view_host);
191 CHECK_EQ(0, render_view_host->GetEnabledBindings()); 195 CHECK_EQ(0, render_view_host->GetEnabledBindings());
192 196
193 if (kViewerCssPath == path) { 197 if (kViewerCssPath == path) {
194 std::string css = viewer::GetCss(); 198 std::string css = viewer::GetCss();
195 callback.Run(base::RefCountedString::TakeString(&css)); 199 callback.Run(base::RefCountedString::TakeString(&css));
196 return; 200 return;
197 } else if (kViewerViewOriginalPath == path) { 201 } else if (kViewerViewOriginalPath == path) {
(...skipping 28 matching lines...) Expand all
226 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( 230 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest(
227 dom_distiller_service_, path, request_viewer_handle, 231 dom_distiller_service_, path, request_viewer_handle,
228 web_contents->GetContainerBounds().size()); 232 web_contents->GetContainerBounds().size());
229 233
230 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey)); 234 GURL current_url(url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey));
231 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml( 235 std::string unsafe_page_html = viewer::GetUnsafeArticleTemplateHtml(
232 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(), 236 url_utils::GetOriginalUrlFromDistillerUrl(current_url).spec(),
233 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(), 237 dom_distiller_service_->GetDistilledPagePrefs()->GetTheme(),
234 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily()); 238 dom_distiller_service_->GetDistilledPagePrefs()->GetFontFamily());
235 239
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
236 if (viewer_handle) { 252 if (viewer_handle) {
237 // The service returned a |ViewerHandle| and guarantees it will call 253 // The service returned a |ViewerHandle| and guarantees it will call
238 // the |RequestViewerHandle|, so passing ownership to it, to ensure the 254 // the |RequestViewerHandle|, so passing ownership to it, to ensure the
239 // request is not cancelled. The |RequestViewerHandle| will delete itself 255 // request is not cancelled. The |RequestViewerHandle| will delete itself
240 // after receiving the callback. 256 // after receiving the callback.
241 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); 257 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass());
242 } else { 258 } else {
243 request_viewer_handle->FlagAsErrorPage(); 259 request_viewer_handle->FlagAsErrorPage();
244 } 260 }
245 261
(...skipping 26 matching lines...) Expand all
272 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() 288 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc()
273 const { 289 const {
274 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;"; 290 return "object-src 'none'; style-src 'self' https://fonts.googleapis.com;";
275 } 291 }
276 292
277 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { 293 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const {
278 return "frame-src *;"; 294 return "frame-src *;";
279 } 295 }
280 296
281 } // namespace dom_distiller 297 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698