| 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/tab_utils.h" | 15 #include "chrome/browser/dom_distiller/tab_utils.h" |
| 16 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 16 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 17 #include "chrome/browser/printing/print_preview_message_handler.h" | 17 #include "chrome/browser/printing/print_preview_message_handler.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/web_contents_sizer.h" | 19 #include "chrome/browser/ui/web_contents_sizer.h" |
| 20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/prerender_messages.h" | 21 #include "chrome/common/prerender_messages.h" |
| 22 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 22 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 23 #include "components/printing/common/print_messages.h" | 23 #include "components/printing/common/print_messages.h" |
| 24 #include "content/public/browser/navigation_details.h" |
| 24 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 26 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/render_widget_host.h" | 29 #include "content/public/browser/render_widget_host.h" |
| 29 #include "content/public/browser/resource_request_details.h" | 30 #include "content/public/browser/resource_request_details.h" |
| 30 #include "content/public/browser/session_storage_namespace.h" | 31 #include "content/public/browser/session_storage_namespace.h" |
| 31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_contents_delegate.h" | 33 #include "content/public/browser/web_contents_delegate.h" |
| 33 #include "content/public/browser/web_contents_observer.h" | 34 #include "content/public/browser/web_contents_observer.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 content::RenderFrameHost* render_frame_host) override { | 129 content::RenderFrameHost* render_frame_host) override { |
| 129 // When a new RenderFrame is created for a distilled rendering | 130 // When a new RenderFrame is created for a distilled rendering |
| 130 // WebContents, tell the new RenderFrame it's being used for | 131 // WebContents, tell the new RenderFrame it's being used for |
| 131 // prerendering before any navigations occur. Note that this is | 132 // prerendering before any navigations occur. Note that this is |
| 132 // always triggered before the first navigation, so there's no | 133 // always triggered before the first navigation, so there's no |
| 133 // need to send the message just after the WebContents is created. | 134 // need to send the message just after the WebContents is created. |
| 134 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( | 135 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( |
| 135 render_frame_host->GetRoutingID(), true)); | 136 render_frame_host->GetRoutingID(), true)); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 139 void DoPrintPreview() { |
| 139 const GURL& validated_url) override { | 140 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| 140 // Ask the page to trigger an anchor navigation once the distilled | 141 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
| 141 // contents are added to the page. | 142 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); |
| 142 dom_distiller::RunIsolatedJavaScript( | |
| 143 web_contents()->GetMainFrame(), | |
| 144 "navigate_on_initial_content_load = true;"); | |
| 145 } | |
| 146 | |
| 147 void DidNavigateMainFrame( | |
| 148 const content::LoadCommittedDetails& details, | |
| 149 const content::FrameNavigateParams& params) override { | |
| 150 // The second content loads signals that the distilled contents have | |
| 151 // been delivered to the page via inline JavaScript execution. | |
| 152 if (web_contents()->GetController().GetEntryCount() > 1) { | |
| 153 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
| 154 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | |
| 155 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); | |
| 156 } | |
| 157 } | 143 } |
| 158 | 144 |
| 159 void DidGetRedirectForResourceRequest( | 145 void DidGetRedirectForResourceRequest( |
| 160 content::RenderFrameHost* render_frame_host, | 146 content::RenderFrameHost* render_frame_host, |
| 161 const content::ResourceRedirectDetails& details) override { | 147 const content::ResourceRedirectDetails& details) override { |
| 162 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) | 148 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME) |
| 163 return; | 149 return; |
| 164 // Redirects are unsupported for distilled content renderers. | 150 // Redirects are unsupported for distilled content renderers. |
| 165 on_failed_callback_.Run(); | 151 on_failed_callback_.Run(); |
| 166 } | 152 } |
| 167 | 153 |
| 168 void RenderProcessGone(base::TerminationStatus status) override { | 154 void RenderProcessGone(base::TerminationStatus status) override { |
| 169 on_failed_callback_.Run(); | 155 on_failed_callback_.Run(); |
| 170 } | 156 } |
| 171 | 157 |
| 158 void DidNavigateMainFrame( |
| 159 const content::LoadCommittedDetails& details, |
| 160 const content::FrameNavigateParams& params) override { |
| 161 // Wait until we are done distilling the article and the target |
| 162 // WebContents is ready for printing. |
| 163 // The navigation to notify print preview that content is on the page will |
| 164 // be an in-page navigation. |
| 165 if (!details.is_in_page) |
| 166 return; |
| 167 // TODO(mvendra, wychen): resources (images, web fonts) are not necessarily |
| 168 // ready at this point - so deferring the call to |
| 169 // DoPrintPreview could be warranted |
| 170 DoPrintPreview(); |
| 171 } |
| 172 |
| 172 void Observe(int type, | 173 void Observe(int type, |
| 173 const content::NotificationSource& source, | 174 const content::NotificationSource& source, |
| 174 const content::NotificationDetails& details) override { | 175 const content::NotificationDetails& details) override { |
| 175 switch (type) { | 176 switch (type) { |
| 176 // TODO(davidben): Try to remove this in favor of relying on | 177 // TODO(davidben): Try to remove this in favor of relying on |
| 177 // FINAL_STATUS_PROFILE_DESTROYED. | 178 // FINAL_STATUS_PROFILE_DESTROYED. |
| 178 case chrome::NOTIFICATION_APP_TERMINATING: | 179 case chrome::NOTIFICATION_APP_TERMINATING: |
| 179 on_failed_callback_.Run(); | 180 on_failed_callback_.Run(); |
| 180 return; | 181 return; |
| 181 | 182 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 WebContents* source_web_contents) { | 276 WebContents* source_web_contents) { |
| 276 // TODO(ajwong): Remove the temporary map once prerendering is aware of | 277 // TODO(ajwong): Remove the temporary map once prerendering is aware of |
| 277 // multiple session storage namespaces per tab. | 278 // multiple session storage namespaces per tab. |
| 278 content::SessionStorageNamespaceMap session_storage_namespace_map; | 279 content::SessionStorageNamespaceMap session_storage_namespace_map; |
| 279 Profile* profile = | 280 Profile* profile = |
| 280 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); | 281 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); |
| 281 session_storage_namespace_map[std::string()] = session_storage_namespace; | 282 session_storage_namespace_map[std::string()] = session_storage_namespace; |
| 282 return WebContents::CreateWithSessionStorage( | 283 return WebContents::CreateWithSessionStorage( |
| 283 WebContents::CreateParams(profile), session_storage_namespace_map); | 284 WebContents::CreateParams(profile), session_storage_namespace_map); |
| 284 } | 285 } |
| OLD | NEW |