Chromium Code Reviews| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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;"); |
| 152 initial_content_loaded_ = true; | |
|
nyquist
2016/02/11 15:57:53
It feels like this should also call MaybePrintPrev
mvendramini_hp
2016/02/11 19:00:46
I think it's unnecessary, but it wouldn't hurt to
nyquist
2016/02/17 23:09:38
So if you want to do that instead, then I think yo
mvendramini_hp
2016/02/18 12:27:02
I agree it looks unsymmetric, and I think removing
| |
| 153 } | |
| 154 | |
| 155 void MaybePrintPreview() { | |
| 156 // Wait until we are done distilling the article and the target | |
| 157 // WebContents is ready for printing. | |
| 158 if (!distilled_article_ready_ || !initial_content_loaded_) | |
| 159 return; | |
| 160 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
|
nyquist
2016/02/11 15:57:53
Nit: I think this would read easier if you added a
mvendramini_hp
2016/02/11 19:00:46
Okay, done!
| |
| 161 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
| 162 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
| 144 } | 163 } |
| 145 | 164 |
| 146 void DidNavigateMainFrame( | 165 void DidNavigateMainFrame( |
| 147 const content::LoadCommittedDetails& details, | 166 const content::LoadCommittedDetails& details, |
| 148 const content::FrameNavigateParams& params) override { | 167 const content::FrameNavigateParams& params) override { |
| 149 // The second content loads signals that the distilled contents have | 168 // The second content loads signals that the distilled contents have |
| 150 // been delivered to the page via inline JavaScript execution. | 169 // been delivered to the page via inline JavaScript execution. |
| 151 if (web_contents()->GetController().GetEntryCount() > 1) { | 170 if (web_contents()->GetController().GetEntryCount() > 1) { |
| 152 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 171 MaybePrintPreview(); |
| 153 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
| 154 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
| 155 } | 172 } |
| 156 } | 173 } |
| 157 | 174 |
| 158 void DidGetRedirectForResourceRequest( | 175 void DidGetRedirectForResourceRequest( |
| 159 content::RenderFrameHost* render_frame_host, | 176 content::RenderFrameHost* render_frame_host, |
| 160 const content::ResourceRedirectDetails& details) override { | 177 const content::ResourceRedirectDetails& details) override { |
| 161 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) | 178 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) |
| 162 return; | 179 return; |
| 163 // Redirects are unsupported for distilled content renderers. | 180 // Redirects are unsupported for distilled content renderers. |
| 164 on_failed_callback_.Run(); | 181 on_failed_callback_.Run(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 194 } | 211 } |
| 195 break; | 212 break; |
| 196 } | 213 } |
| 197 | 214 |
| 198 default: | 215 default: |
| 199 NOTREACHED() << "Unexpected notification sent."; | 216 NOTREACHED() << "Unexpected notification sent."; |
| 200 break; | 217 break; |
| 201 } | 218 } |
| 202 } | 219 } |
| 203 | 220 |
| 221 void OnArticleReady( | |
| 222 const dom_distiller::DistilledArticleProto* article_proto) override { | |
| 223 distilled_article_ready_ = true; | |
| 224 MaybePrintPreview(); | |
| 225 } | |
| 226 | |
| 227 void OnArticleUpdated( | |
| 228 dom_distiller::ArticleDistillationUpdate article_update) override {} | |
| 229 | |
| 204 private: | 230 private: |
| 205 scoped_ptr<base::DictionaryValue> settings_; | 231 scoped_ptr<base::DictionaryValue> settings_; |
| 206 content::NotificationRegistrar notification_registrar_; | 232 content::NotificationRegistrar notification_registrar_; |
| 207 | 233 |
| 208 // The callback called when the preview failed. | 234 // The callback called when the preview failed. |
| 209 base::Closure on_failed_callback_; | 235 base::Closure on_failed_callback_; |
| 236 | |
| 237 bool distilled_article_ready_; | |
| 238 bool initial_content_loaded_; | |
| 210 }; | 239 }; |
| 211 | 240 |
| 212 const base::Feature PrintPreviewDistiller::kFeature = { | 241 const base::Feature PrintPreviewDistiller::kFeature = { |
| 213 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, | 242 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, |
| 214 }; | 243 }; |
| 215 | 244 |
| 216 bool PrintPreviewDistiller::IsEnabled() { | 245 bool PrintPreviewDistiller::IsEnabled() { |
| 217 return base::FeatureList::IsEnabled(kFeature); | 246 return base::FeatureList::IsEnabled(kFeature); |
| 218 } | 247 } |
| 219 | 248 |
| 220 PrintPreviewDistiller::PrintPreviewDistiller( | 249 PrintPreviewDistiller::PrintPreviewDistiller( |
| 221 WebContents* source_web_contents, | 250 WebContents* source_web_contents, |
| 222 const base::Closure on_failed_callback, | 251 const base::Closure on_failed_callback, |
| 223 scoped_ptr<base::DictionaryValue> settings) { | 252 scoped_ptr<base::DictionaryValue> settings) { |
| 224 content::SessionStorageNamespace* session_storage_namespace = | 253 content::SessionStorageNamespace* session_storage_namespace = |
| 225 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); | 254 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); |
| 226 CreateDestinationWebContents(session_storage_namespace, source_web_contents, | 255 CreateDestinationWebContents(session_storage_namespace, source_web_contents, |
| 227 std::move(settings), on_failed_callback); | 256 std::move(settings), on_failed_callback); |
| 228 | 257 |
| 229 DCHECK(web_contents_); | 258 DCHECK(web_contents_); |
| 230 ::DistillAndView(source_web_contents, web_contents_.get()); | 259 ::DistillAndView(source_web_contents, web_contents_.get()); |
| 260 | |
| 261 DomDistillerService* dom_distiller_service = | |
| 262 DomDistillerServiceFactory::GetForBrowserContext( | |
| 263 source_web_contents->GetBrowserContext()); | |
| 264 | |
| 265 const GURL& url = source_web_contents->GetLastCommittedURL(); | |
| 266 | |
| 267 viewer_handle_ = dom_distiller_service->AddViewRequestDelegate( | |
| 268 web_contents_delegate_.get(), url); | |
| 231 } | 269 } |
| 232 | 270 |
| 233 void PrintPreviewDistiller::CreateDestinationWebContents( | 271 void PrintPreviewDistiller::CreateDestinationWebContents( |
| 234 SessionStorageNamespace* session_storage_namespace, | 272 SessionStorageNamespace* session_storage_namespace, |
| 235 WebContents* source_web_contents, | 273 WebContents* source_web_contents, |
| 236 scoped_ptr<base::DictionaryValue> settings, | 274 scoped_ptr<base::DictionaryValue> settings, |
| 237 const base::Closure on_failed_callback) { | 275 const base::Closure on_failed_callback) { |
| 238 DCHECK(!web_contents_); | 276 DCHECK(!web_contents_); |
| 239 | 277 |
| 240 web_contents_.reset( | 278 web_contents_.reset( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 WebContents* source_web_contents) { | 312 WebContents* source_web_contents) { |
| 275 // TODO(ajwong): Remove the temporary map once prerendering is aware of | 313 // TODO(ajwong): Remove the temporary map once prerendering is aware of |
| 276 // multiple session storage namespaces per tab. | 314 // multiple session storage namespaces per tab. |
| 277 content::SessionStorageNamespaceMap session_storage_namespace_map; | 315 content::SessionStorageNamespaceMap session_storage_namespace_map; |
| 278 Profile* profile = | 316 Profile* profile = |
| 279 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); | 317 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); |
| 280 session_storage_namespace_map[std::string()] = session_storage_namespace; | 318 session_storage_namespace_map[std::string()] = session_storage_namespace; |
| 281 return WebContents::CreateWithSessionStorage( | 319 return WebContents::CreateWithSessionStorage( |
| 282 WebContents::CreateParams(profile), session_storage_namespace_map); | 320 WebContents::CreateParams(profile), session_storage_namespace_map); |
| 283 } | 321 } |
| OLD | NEW |