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..4c2245740575bb2e4558b600201b737e25c5d58e 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,25 @@ class PrintPreviewDistiller::WebContentsDelegateImpl |
dom_distiller::RunIsolatedJavaScript( |
web_contents()->GetMainFrame(), |
"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.
|
+ initial_content_loaded_ = true; |
+ MaybePrintPreview(); |
+ } |
+ |
+ void MaybePrintPreview() { |
+ // Wait until we are done distilling the article and the target |
+ // WebContents is ready for printing. |
+ |
+ // initial_content_loaded_ is expected to be always |
+ // true (set by DidFinishLoad) before attempting to |
+ // carry out the print preview |
+ DCHECK(initial_content_loaded_); |
+ |
+ if (!distilled_article_ready_) |
+ return; |
+ |
+ RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
+ rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
+ rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings_)); |
} |
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)
|
@@ -149,9 +176,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 +226,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 +265,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( |