Index: chrome/browser/ui/webui/print_preview/print_preview_distiller.cc |
diff --git a/chrome/browser/ui/webui/print_preview/print_preview_distiller.cc b/chrome/browser/ui/webui/print_preview/print_preview_distiller.cc |
index f3076d92fcdc5cc124ac5afff6cd3a56280d1502..9472bc3ab9a18c8eaec2c7fba3ebbdd1e51c1b37 100644 |
--- a/chrome/browser/ui/webui/print_preview/print_preview_distiller.cc |
+++ b/chrome/browser/ui/webui/print_preview/print_preview_distiller.cc |
@@ -12,6 +12,7 @@ |
#include "base/feature_list.h" |
#include "base/strings/utf_string_conversions.h" |
#include "chrome/browser/chrome_notification_types.h" |
+#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
#include "chrome/browser/dom_distiller/tab_utils.h" |
#include "chrome/browser/printing/print_preview_dialog_controller.h" |
#include "chrome/browser/printing/print_preview_message_handler.h" |
@@ -20,6 +21,8 @@ |
#include "chrome/common/chrome_switches.h" |
#include "chrome/common/prerender_messages.h" |
#include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
+#include "components/dom_distiller/core/dom_distiller_service.h" |
+#include "components/dom_distiller/core/task_tracker.h" |
#include "components/printing/common/print_messages.h" |
#include "content/public/browser/notification_service.h" |
#include "content/public/browser/render_frame_host.h" |
@@ -36,18 +39,23 @@ using content::OpenURLParams; |
using content::RenderViewHost; |
using content::SessionStorageNamespace; |
using content::WebContents; |
+using dom_distiller::DomDistillerService; |
+using dom_distiller::DomDistillerServiceFactory; |
class PrintPreviewDistiller::WebContentsDelegateImpl |
: public content::WebContentsDelegate, |
public content::NotificationObserver, |
- public content::WebContentsObserver { |
+ public content::WebContentsObserver, |
+ public dom_distiller::ViewRequestDelegate { |
public: |
explicit WebContentsDelegateImpl(WebContents* web_contents, |
scoped_ptr<base::DictionaryValue> settings, |
const base::Closure on_failed_callback) |
: content::WebContentsObserver(web_contents), |
settings_(std::move(settings)), |
- on_failed_callback_(on_failed_callback) { |
+ on_failed_callback_(on_failed_callback), |
+ distilled_article_ready_(false), |
+ initial_content_loaded_(false) { |
web_contents->SetDelegate(this); |
// Close ourselves when the application is shutting down. |
@@ -141,6 +149,17 @@ class PrintPreviewDistiller::WebContentsDelegateImpl |
dom_distiller::RunIsolatedJavaScript( |
web_contents()->GetMainFrame(), |
"navigate_on_initial_content_load = true;"); |
+ initial_content_loaded_ = true; |
+ } |
+ |
+ void MaybePrintPreview() { |
+ // Wait until we are done distilling the article and the target |
+ // WebContents is ready for printing. |
+ if (distilled_article_ready_ && initial_content_loaded_) { |
Bernhard Bauer
2016/01/19 17:21:02
You could return early if this condition is false.
mvendramini_hp
2016/01/20 11:56:57
Done.
|
+ RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
+ rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
+ rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); |
+ } |
} |
void DidNavigateMainFrame( |
@@ -149,9 +168,7 @@ class PrintPreviewDistiller::WebContentsDelegateImpl |
// The second content loads signals that the distilled contents have |
// been delivered to the page via inline JavaScript execution. |
if (web_contents()->GetController().GetEntryCount() > 1) { |
- RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
- rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
- rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); |
+ MaybePrintPreview(); |
} |
} |
@@ -201,12 +218,24 @@ class PrintPreviewDistiller::WebContentsDelegateImpl |
} |
} |
+ void OnArticleReady( |
+ const dom_distiller::DistilledArticleProto* article_proto) override { |
+ distilled_article_ready_ = true; |
+ MaybePrintPreview(); |
+ } |
+ |
+ void OnArticleUpdated( |
+ dom_distiller::ArticleDistillationUpdate article_update) override {} |
+ |
private: |
scoped_ptr<base::DictionaryValue> settings_; |
content::NotificationRegistrar notification_registrar_; |
// The callback called when the preview failed. |
base::Closure on_failed_callback_; |
+ |
+ bool distilled_article_ready_; |
+ bool initial_content_loaded_; |
}; |
const base::Feature PrintPreviewDistiller::kFeature = { |
@@ -228,6 +257,15 @@ PrintPreviewDistiller::PrintPreviewDistiller( |
DCHECK(web_contents_); |
::DistillAndView(source_web_contents, web_contents_.get()); |
+ |
+ DomDistillerService* dom_distiller_service = |
+ DomDistillerServiceFactory::GetForBrowserContext( |
+ source_web_contents->GetBrowserContext()); |
+ |
+ const GURL& url = source_web_contents->GetLastCommittedURL(); |
+ |
+ viewer_handle_ = dom_distiller_service->AddViewRequestDelegate( |
+ web_contents_delegate_.get(), url); |
} |
void PrintPreviewDistiller::CreateDestinationWebContents( |