OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/ui/webui/print_preview/print_preview_distiller.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | |
15 #include "chrome/browser/dom_distiller/tab_utils.h" | 16 #include "chrome/browser/dom_distiller/tab_utils.h" |
16 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 17 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
17 #include "chrome/browser/printing/print_preview_message_handler.h" | 18 #include "chrome/browser/printing/print_preview_message_handler.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/web_contents_sizer.h" | 20 #include "chrome/browser/ui/web_contents_sizer.h" |
20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/prerender_messages.h" | 22 #include "chrome/common/prerender_messages.h" |
22 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 23 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
24 #include "components/dom_distiller/core/dom_distiller_service.h" | |
25 #include "components/dom_distiller/core/task_tracker.h" | |
23 #include "components/printing/common/print_messages.h" | 26 #include "components/printing/common/print_messages.h" |
24 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
26 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
27 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
28 #include "content/public/browser/render_widget_host.h" | 31 #include "content/public/browser/render_widget_host.h" |
29 #include "content/public/browser/resource_request_details.h" | 32 #include "content/public/browser/resource_request_details.h" |
30 #include "content/public/browser/session_storage_namespace.h" | 33 #include "content/public/browser/session_storage_namespace.h" |
31 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_contents_delegate.h" | 35 #include "content/public/browser/web_contents_delegate.h" |
33 #include "content/public/browser/web_contents_observer.h" | 36 #include "content/public/browser/web_contents_observer.h" |
34 | 37 |
35 using content::OpenURLParams; | 38 using content::OpenURLParams; |
36 using content::RenderViewHost; | 39 using content::RenderViewHost; |
37 using content::SessionStorageNamespace; | 40 using content::SessionStorageNamespace; |
38 using content::WebContents; | 41 using content::WebContents; |
42 using dom_distiller::DomDistillerService; | |
43 using dom_distiller::DomDistillerServiceFactory; | |
39 | 44 |
40 class PrintPreviewDistiller::WebContentsDelegateImpl | 45 class PrintPreviewDistiller::WebContentsDelegateImpl |
41 : public content::WebContentsDelegate, | 46 : public content::WebContentsDelegate, |
42 public content::NotificationObserver, | 47 public content::NotificationObserver, |
43 public content::WebContentsObserver { | 48 public content::WebContentsObserver, |
49 public dom_distiller::ViewRequestDelegate { | |
44 public: | 50 public: |
45 explicit WebContentsDelegateImpl(WebContents* web_contents, | 51 explicit WebContentsDelegateImpl(WebContents* web_contents, |
46 scoped_ptr<base::DictionaryValue> settings, | 52 scoped_ptr<base::DictionaryValue> settings, |
47 const base::Closure on_failed_callback) | 53 const base::Closure on_failed_callback) |
48 : content::WebContentsObserver(web_contents), | 54 : content::WebContentsObserver(web_contents), |
49 settings_(std::move(settings)), | 55 settings_(std::move(settings)), |
50 on_failed_callback_(on_failed_callback) { | 56 on_failed_callback_(on_failed_callback), |
57 distilled_article_ready_(false), | |
58 initial_content_loaded_(false) { | |
51 web_contents->SetDelegate(this); | 59 web_contents->SetDelegate(this); |
52 | 60 |
53 // Close ourselves when the application is shutting down. | 61 // Close ourselves when the application is shutting down. |
54 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 62 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
55 content::NotificationService::AllSources()); | 63 content::NotificationService::AllSources()); |
56 | 64 |
57 // Register to inform new RenderViews that we're rendering. | 65 // Register to inform new RenderViews that we're rendering. |
58 notification_registrar_.Add( | 66 notification_registrar_.Add( |
59 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 67 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
60 content::Source<WebContents>(web_contents)); | 68 content::Source<WebContents>(web_contents)); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( | 141 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( |
134 render_frame_host->GetRoutingID(), true)); | 142 render_frame_host->GetRoutingID(), true)); |
135 } | 143 } |
136 | 144 |
137 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 145 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
138 const GURL& validated_url) override { | 146 const GURL& validated_url) override { |
139 // Ask the page to trigger an anchor navigation once the distilled | 147 // Ask the page to trigger an anchor navigation once the distilled |
140 // contents are added to the page. | 148 // contents are added to the page. |
141 dom_distiller::RunIsolatedJavaScript( | 149 dom_distiller::RunIsolatedJavaScript( |
142 web_contents()->GetMainFrame(), | 150 web_contents()->GetMainFrame(), |
143 "navigate_on_initial_content_load = true;"); | 151 "navigate_on_initial_content_load = true;"); |
mdjones
2016/02/23 19:23:30
Is this still needed now that you get the article
mvendramini_hp
2016/02/23 19:45:20
Maybe we could remove everything related to initia
nyquist
2016/02/24 01:53:08
Yeah, I think that would be great. It would hopefu
mvendramini_hp
2016/02/24 12:30:54
Done.
| |
152 initial_content_loaded_ = true; | |
153 MaybePrintPreview(); | |
154 } | |
155 | |
156 void MaybePrintPreview() { | |
157 // Wait until we are done distilling the article and the target | |
158 // WebContents is ready for printing. | |
159 | |
160 // initial_content_loaded_ is expected to be always | |
161 // true (set by DidFinishLoad) before attempting to | |
162 // carry out the print preview | |
163 DCHECK(initial_content_loaded_); | |
164 | |
165 if (!distilled_article_ready_) | |
166 return; | |
167 | |
168 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
169 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
170 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
144 } | 171 } |
145 | 172 |
146 void DidNavigateMainFrame( | 173 void DidNavigateMainFrame( |
mdjones
2016/02/23 19:23:30
Similarly, this method seems unnecessary now that
mvendramini_hp
2016/02/23 19:45:20
Yes, it could be removed, (see my former comment)
| |
147 const content::LoadCommittedDetails& details, | 174 const content::LoadCommittedDetails& details, |
148 const content::FrameNavigateParams& params) override { | 175 const content::FrameNavigateParams& params) override { |
149 // The second content loads signals that the distilled contents have | 176 // The second content loads signals that the distilled contents have |
150 // been delivered to the page via inline JavaScript execution. | 177 // been delivered to the page via inline JavaScript execution. |
151 if (web_contents()->GetController().GetEntryCount() > 1) { | 178 if (web_contents()->GetController().GetEntryCount() > 1) { |
152 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 179 MaybePrintPreview(); |
153 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
154 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
155 } | 180 } |
156 } | 181 } |
157 | 182 |
158 void DidGetRedirectForResourceRequest( | 183 void DidGetRedirectForResourceRequest( |
159 content::RenderFrameHost* render_frame_host, | 184 content::RenderFrameHost* render_frame_host, |
160 const content::ResourceRedirectDetails& details) override { | 185 const content::ResourceRedirectDetails& details) override { |
161 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) | 186 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) |
162 return; | 187 return; |
163 // Redirects are unsupported for distilled content renderers. | 188 // Redirects are unsupported for distilled content renderers. |
164 on_failed_callback_.Run(); | 189 on_failed_callback_.Run(); |
(...skipping 29 matching lines...) Expand all Loading... | |
194 } | 219 } |
195 break; | 220 break; |
196 } | 221 } |
197 | 222 |
198 default: | 223 default: |
199 NOTREACHED() << "Unexpected notification sent."; | 224 NOTREACHED() << "Unexpected notification sent."; |
200 break; | 225 break; |
201 } | 226 } |
202 } | 227 } |
203 | 228 |
229 void OnArticleReady( | |
230 const dom_distiller::DistilledArticleProto* article_proto) override { | |
231 distilled_article_ready_ = true; | |
232 MaybePrintPreview(); | |
233 } | |
234 | |
235 void OnArticleUpdated( | |
236 dom_distiller::ArticleDistillationUpdate article_update) override {} | |
237 | |
204 private: | 238 private: |
205 scoped_ptr<base::DictionaryValue> settings_; | 239 scoped_ptr<base::DictionaryValue> settings_; |
206 content::NotificationRegistrar notification_registrar_; | 240 content::NotificationRegistrar notification_registrar_; |
207 | 241 |
208 // The callback called when the preview failed. | 242 // The callback called when the preview failed. |
209 base::Closure on_failed_callback_; | 243 base::Closure on_failed_callback_; |
244 | |
245 bool distilled_article_ready_; | |
246 bool initial_content_loaded_; | |
210 }; | 247 }; |
211 | 248 |
212 const base::Feature PrintPreviewDistiller::kFeature = { | 249 const base::Feature PrintPreviewDistiller::kFeature = { |
213 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, | 250 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, |
214 }; | 251 }; |
215 | 252 |
216 bool PrintPreviewDistiller::IsEnabled() { | 253 bool PrintPreviewDistiller::IsEnabled() { |
217 return base::FeatureList::IsEnabled(kFeature); | 254 return base::FeatureList::IsEnabled(kFeature); |
218 } | 255 } |
219 | 256 |
220 PrintPreviewDistiller::PrintPreviewDistiller( | 257 PrintPreviewDistiller::PrintPreviewDistiller( |
221 WebContents* source_web_contents, | 258 WebContents* source_web_contents, |
222 const base::Closure on_failed_callback, | 259 const base::Closure on_failed_callback, |
223 scoped_ptr<base::DictionaryValue> settings) { | 260 scoped_ptr<base::DictionaryValue> settings) { |
224 content::SessionStorageNamespace* session_storage_namespace = | 261 content::SessionStorageNamespace* session_storage_namespace = |
225 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); | 262 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); |
226 CreateDestinationWebContents(session_storage_namespace, source_web_contents, | 263 CreateDestinationWebContents(session_storage_namespace, source_web_contents, |
227 std::move(settings), on_failed_callback); | 264 std::move(settings), on_failed_callback); |
228 | 265 |
229 DCHECK(web_contents_); | 266 DCHECK(web_contents_); |
230 ::DistillAndView(source_web_contents, web_contents_.get()); | 267 ::DistillAndView(source_web_contents, web_contents_.get()); |
268 | |
269 DomDistillerService* dom_distiller_service = | |
270 DomDistillerServiceFactory::GetForBrowserContext( | |
271 source_web_contents->GetBrowserContext()); | |
272 | |
273 const GURL& url = source_web_contents->GetLastCommittedURL(); | |
274 | |
275 viewer_handle_ = dom_distiller_service->AddViewRequestDelegate( | |
276 web_contents_delegate_.get(), url); | |
231 } | 277 } |
232 | 278 |
233 void PrintPreviewDistiller::CreateDestinationWebContents( | 279 void PrintPreviewDistiller::CreateDestinationWebContents( |
234 SessionStorageNamespace* session_storage_namespace, | 280 SessionStorageNamespace* session_storage_namespace, |
235 WebContents* source_web_contents, | 281 WebContents* source_web_contents, |
236 scoped_ptr<base::DictionaryValue> settings, | 282 scoped_ptr<base::DictionaryValue> settings, |
237 const base::Closure on_failed_callback) { | 283 const base::Closure on_failed_callback) { |
238 DCHECK(!web_contents_); | 284 DCHECK(!web_contents_); |
239 | 285 |
240 web_contents_.reset( | 286 web_contents_.reset( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 WebContents* source_web_contents) { | 320 WebContents* source_web_contents) { |
275 // TODO(ajwong): Remove the temporary map once prerendering is aware of | 321 // TODO(ajwong): Remove the temporary map once prerendering is aware of |
276 // multiple session storage namespaces per tab. | 322 // multiple session storage namespaces per tab. |
277 content::SessionStorageNamespaceMap session_storage_namespace_map; | 323 content::SessionStorageNamespaceMap session_storage_namespace_map; |
278 Profile* profile = | 324 Profile* profile = |
279 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); | 325 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); |
280 session_storage_namespace_map[std::string()] = session_storage_namespace; | 326 session_storage_namespace_map[std::string()] = session_storage_namespace; |
281 return WebContents::CreateWithSessionStorage( | 327 return WebContents::CreateWithSessionStorage( |
282 WebContents::CreateParams(profile), session_storage_namespace_map); | 328 WebContents::CreateParams(profile), session_storage_namespace_map); |
283 } | 329 } |
OLD | NEW |