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" |
| 27 #include "content/public/browser/navigation_details.h" | |
| 24 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
| 26 #include "content/public/browser/render_process_host.h" | 30 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/render_widget_host.h" | 32 #include "content/public/browser/render_widget_host.h" |
| 29 #include "content/public/browser/resource_request_details.h" | 33 #include "content/public/browser/resource_request_details.h" |
| 30 #include "content/public/browser/session_storage_namespace.h" | 34 #include "content/public/browser/session_storage_namespace.h" |
| 31 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_contents_delegate.h" | 36 #include "content/public/browser/web_contents_delegate.h" |
| 33 #include "content/public/browser/web_contents_observer.h" | 37 #include "content/public/browser/web_contents_observer.h" |
| 34 | 38 |
| 35 using content::OpenURLParams; | 39 using content::OpenURLParams; |
| 36 using content::RenderViewHost; | 40 using content::RenderViewHost; |
| 37 using content::SessionStorageNamespace; | 41 using content::SessionStorageNamespace; |
| 38 using content::WebContents; | 42 using content::WebContents; |
| 43 using dom_distiller::DomDistillerService; | |
| 44 using dom_distiller::DomDistillerServiceFactory; | |
| 39 | 45 |
| 40 class PrintPreviewDistiller::WebContentsDelegateImpl | 46 class PrintPreviewDistiller::WebContentsDelegateImpl |
| 41 : public content::WebContentsDelegate, | 47 : public content::WebContentsDelegate, |
| 42 public content::NotificationObserver, | 48 public content::NotificationObserver, |
| 43 public content::WebContentsObserver { | 49 public content::WebContentsObserver, |
| 50 public dom_distiller::ViewRequestDelegate { | |
| 44 public: | 51 public: |
| 45 explicit WebContentsDelegateImpl(WebContents* web_contents, | 52 explicit WebContentsDelegateImpl(WebContents* web_contents, |
| 46 scoped_ptr<base::DictionaryValue> settings, | 53 scoped_ptr<base::DictionaryValue> settings, |
| 47 const base::Closure on_failed_callback) | 54 const base::Closure on_failed_callback) |
| 48 : content::WebContentsObserver(web_contents), | 55 : content::WebContentsObserver(web_contents), |
| 49 settings_(std::move(settings)), | 56 settings_(std::move(settings)), |
| 50 on_failed_callback_(on_failed_callback) { | 57 on_failed_callback_(on_failed_callback) { |
| 51 web_contents->SetDelegate(this); | 58 web_contents->SetDelegate(this); |
| 52 | 59 |
| 53 // Close ourselves when the application is shutting down. | 60 // Close ourselves when the application is shutting down. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 content::RenderFrameHost* render_frame_host) override { | 134 content::RenderFrameHost* render_frame_host) override { |
| 128 // When a new RenderFrame is created for a distilled rendering | 135 // When a new RenderFrame is created for a distilled rendering |
| 129 // WebContents, tell the new RenderFrame it's being used for | 136 // WebContents, tell the new RenderFrame it's being used for |
| 130 // prerendering before any navigations occur. Note that this is | 137 // prerendering before any navigations occur. Note that this is |
| 131 // always triggered before the first navigation, so there's no | 138 // always triggered before the first navigation, so there's no |
| 132 // need to send the message just after the WebContents is created. | 139 // need to send the message just after the WebContents is created. |
| 133 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( | 140 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( |
| 134 render_frame_host->GetRoutingID(), true)); | 141 render_frame_host->GetRoutingID(), true)); |
| 135 } | 142 } |
| 136 | 143 |
| 137 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 144 void MaybePrintPreview() { |
| 138 const GURL& validated_url) override { | 145 // Wait until we are done distilling the article and the target |
| 139 // Ask the page to trigger an anchor navigation once the distilled | 146 // WebContents is ready for printing. |
| 140 // contents are added to the page. | |
| 141 dom_distiller::RunIsolatedJavaScript( | |
| 142 web_contents()->GetMainFrame(), | |
| 143 "navigate_on_initial_content_load = true;"); | |
| 144 } | |
| 145 | 147 |
| 146 void DidNavigateMainFrame( | 148 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| 147 const content::LoadCommittedDetails& details, | 149 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
|
wychen
2016/04/06 06:13:15
The resources might still be not ready here, like
mvendramini_hp
2016/04/06 13:39:04
Also, at this point, MaybePrintPreview is no longe
wychen
2016/04/06 19:48:06
Yeah. The name is confusing now. Renaming it would
| |
| 148 const content::FrameNavigateParams& params) override { | 150 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); |
| 149 // The second content loads signals that the distilled contents have | |
| 150 // been delivered to the page via inline JavaScript execution. | |
| 151 if (web_contents()->GetController().GetEntryCount() > 1) { | |
| 152 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
| 153 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
| 154 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
| 155 } | |
| 156 } | 151 } |
| 157 | 152 |
| 158 void DidGetRedirectForResourceRequest( | 153 void DidGetRedirectForResourceRequest( |
| 159 content::RenderFrameHost* render_frame_host, | 154 content::RenderFrameHost* render_frame_host, |
| 160 const content::ResourceRedirectDetails& details) override { | 155 const content::ResourceRedirectDetails& details) override { |
| 161 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) | 156 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) |
| 162 return; | 157 return; |
| 163 // Redirects are unsupported for distilled content renderers. | 158 // Redirects are unsupported for distilled content renderers. |
| 164 on_failed_callback_.Run(); | 159 on_failed_callback_.Run(); |
| 165 } | 160 } |
| 166 | 161 |
| 167 void RenderProcessGone(base::TerminationStatus status) override { | 162 void RenderProcessGone(base::TerminationStatus status) override { |
| 168 on_failed_callback_.Run(); | 163 on_failed_callback_.Run(); |
| 169 } | 164 } |
| 170 | 165 |
| 166 void DidNavigateMainFrame( | |
| 167 const ::content::LoadCommittedDetails& details, | |
| 168 const content::FrameNavigateParams& params) override { | |
| 169 // The navigation to notify print preview that content is on the page will | |
| 170 // be an in-page navigation. | |
| 171 if (!details.is_in_page) | |
| 172 return; | |
| 173 MaybePrintPreview(); | |
| 174 } | |
| 175 | |
| 171 void Observe(int type, | 176 void Observe(int type, |
| 172 const content::NotificationSource& source, | 177 const content::NotificationSource& source, |
| 173 const content::NotificationDetails& details) override { | 178 const content::NotificationDetails& details) override { |
| 174 switch (type) { | 179 switch (type) { |
| 175 // TODO(davidben): Try to remove this in favor of relying on | 180 // TODO(davidben): Try to remove this in favor of relying on |
| 176 // FINAL_STATUS_PROFILE_DESTROYED. | 181 // FINAL_STATUS_PROFILE_DESTROYED. |
| 177 case chrome::NOTIFICATION_APP_TERMINATING: | 182 case chrome::NOTIFICATION_APP_TERMINATING: |
| 178 on_failed_callback_.Run(); | 183 on_failed_callback_.Run(); |
| 179 return; | 184 return; |
| 180 | 185 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 194 } | 199 } |
| 195 break; | 200 break; |
| 196 } | 201 } |
| 197 | 202 |
| 198 default: | 203 default: |
| 199 NOTREACHED() << "Unexpected notification sent."; | 204 NOTREACHED() << "Unexpected notification sent."; |
| 200 break; | 205 break; |
| 201 } | 206 } |
| 202 } | 207 } |
| 203 | 208 |
| 209 void OnArticleReady( | |
| 210 const dom_distiller::DistilledArticleProto* article_proto) override {} | |
| 211 | |
| 212 void OnArticleUpdated( | |
| 213 dom_distiller::ArticleDistillationUpdate article_update) override {} | |
| 214 | |
| 204 private: | 215 private: |
| 205 scoped_ptr<base::DictionaryValue> settings_; | 216 scoped_ptr<base::DictionaryValue> settings_; |
| 206 content::NotificationRegistrar notification_registrar_; | 217 content::NotificationRegistrar notification_registrar_; |
| 207 | 218 |
| 208 // The callback called when the preview failed. | 219 // The callback called when the preview failed. |
| 209 base::Closure on_failed_callback_; | 220 base::Closure on_failed_callback_; |
| 210 }; | 221 }; |
| 211 | 222 |
| 212 const base::Feature PrintPreviewDistiller::kFeature = { | 223 const base::Feature PrintPreviewDistiller::kFeature = { |
| 213 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, | 224 "PrintPreviewDistiller", base::FEATURE_ENABLED_BY_DEFAULT, |
| 214 }; | 225 }; |
| 215 | 226 |
| 216 bool PrintPreviewDistiller::IsEnabled() { | 227 bool PrintPreviewDistiller::IsEnabled() { |
| 217 return base::FeatureList::IsEnabled(kFeature); | 228 return base::FeatureList::IsEnabled(kFeature); |
| 218 } | 229 } |
| 219 | 230 |
| 220 PrintPreviewDistiller::PrintPreviewDistiller( | 231 PrintPreviewDistiller::PrintPreviewDistiller( |
| 221 WebContents* source_web_contents, | 232 WebContents* source_web_contents, |
| 222 const base::Closure on_failed_callback, | 233 const base::Closure on_failed_callback, |
| 223 scoped_ptr<base::DictionaryValue> settings) { | 234 scoped_ptr<base::DictionaryValue> settings) { |
| 224 content::SessionStorageNamespace* session_storage_namespace = | 235 content::SessionStorageNamespace* session_storage_namespace = |
| 225 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); | 236 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); |
| 226 CreateDestinationWebContents(session_storage_namespace, source_web_contents, | 237 CreateDestinationWebContents(session_storage_namespace, source_web_contents, |
| 227 std::move(settings), on_failed_callback); | 238 std::move(settings), on_failed_callback); |
| 228 | 239 |
| 229 DCHECK(web_contents_); | 240 DCHECK(web_contents_); |
| 230 ::DistillAndView(source_web_contents, web_contents_.get()); | 241 ::DistillAndView(source_web_contents, web_contents_.get()); |
| 242 | |
| 243 DomDistillerService* dom_distiller_service = | |
| 244 DomDistillerServiceFactory::GetForBrowserContext( | |
| 245 source_web_contents->GetBrowserContext()); | |
| 246 | |
| 247 const GURL& url = source_web_contents->GetLastCommittedURL(); | |
| 248 | |
| 249 viewer_handle_ = dom_distiller_service->AddViewRequestDelegate( | |
| 250 web_contents_delegate_.get(), url); | |
| 231 } | 251 } |
| 232 | 252 |
| 233 void PrintPreviewDistiller::CreateDestinationWebContents( | 253 void PrintPreviewDistiller::CreateDestinationWebContents( |
| 234 SessionStorageNamespace* session_storage_namespace, | 254 SessionStorageNamespace* session_storage_namespace, |
| 235 WebContents* source_web_contents, | 255 WebContents* source_web_contents, |
| 236 scoped_ptr<base::DictionaryValue> settings, | 256 scoped_ptr<base::DictionaryValue> settings, |
| 237 const base::Closure on_failed_callback) { | 257 const base::Closure on_failed_callback) { |
| 238 DCHECK(!web_contents_); | 258 DCHECK(!web_contents_); |
| 239 | 259 |
| 240 web_contents_.reset( | 260 web_contents_.reset( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 WebContents* source_web_contents) { | 294 WebContents* source_web_contents) { |
| 275 // TODO(ajwong): Remove the temporary map once prerendering is aware of | 295 // TODO(ajwong): Remove the temporary map once prerendering is aware of |
| 276 // multiple session storage namespaces per tab. | 296 // multiple session storage namespaces per tab. |
| 277 content::SessionStorageNamespaceMap session_storage_namespace_map; | 297 content::SessionStorageNamespaceMap session_storage_namespace_map; |
| 278 Profile* profile = | 298 Profile* profile = |
| 279 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); | 299 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); |
| 280 session_storage_namespace_map[std::string()] = session_storage_namespace; | 300 session_storage_namespace_map[std::string()] = session_storage_namespace; |
| 281 return WebContents::CreateWithSessionStorage( | 301 return WebContents::CreateWithSessionStorage( |
| 282 WebContents::CreateParams(profile), session_storage_namespace_map); | 302 WebContents::CreateParams(profile), session_storage_namespace_map); |
| 283 } | 303 } |
| OLD | NEW |