Fix for a crasher in PdfToEmfConverter

Fix a crasher that happens in PdfToEmfConverter when failing to
create a temporary file. We would call set_connection_error_handler
on on pdf_to_emf_converter_ which would be null in that case.
Also adding a browser test to validate that case.

Bug: 814613
Change-Id: Ib336347512d09213494083007d0a3bdc99a25b21
Reviewed-on: https://chromium-review.googlesource.com/932831
Commit-Queue: Jay Civelli <jcivelli@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#538603}(cherry picked from commit 537716a64ef068fc68832e14129e06c0bd4c17be)
Reviewed-on: https://chromium-review.googlesource.com/937821
Reviewed-by: Jay Civelli <jcivelli@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#590}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/chrome/browser/printing/pdf_to_emf_converter.cc b/chrome/browser/printing/pdf_to_emf_converter.cc
index b2e4d2b..3583419 100644
--- a/chrome/browser/printing/pdf_to_emf_converter.cc
+++ b/chrome/browser/printing/pdf_to_emf_converter.cc
@@ -212,6 +212,14 @@
                    StartCallback start_callback);
   ~PdfConverterImpl() override;
 
+  static void set_fail_when_creating_temp_file_for_tests(bool fail) {
+    simulate_failure_creating_temp_file_ = fail;
+  }
+
+  static bool fail_when_creating_temp_file_for_tests() {
+    return simulate_failure_creating_temp_file_;
+  }
+
  private:
   class GetPageCallbackData {
    public:
@@ -287,9 +295,14 @@
 
   base::WeakPtrFactory<PdfConverterImpl> weak_ptr_factory_;
 
+  static bool simulate_failure_creating_temp_file_;
+
   DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl);
 };
 
+// static
+bool PdfConverterImpl::simulate_failure_creating_temp_file_ = false;
+
 std::unique_ptr<MetafilePlayer> PdfConverterImpl::GetFileFromTemp(
     ScopedTempFile temp_file) {
   if (settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 ||
@@ -302,6 +315,9 @@
 }
 
 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) {
+  if (PdfConverterImpl::fail_when_creating_temp_file_for_tests())
+    return ScopedTempFile();
+
   if (!temp_dir->get())
     *temp_dir = base::MakeRefCounted<RefCountedTempDir>();
   ScopedTempFile file;
@@ -540,7 +556,7 @@
   LOG(ERROR) << "Failed to convert PDF: " << error_message;
   base::WeakPtr<PdfConverterImpl> weak_this = weak_ptr_factory_.GetWeakPtr();
   if (!start_callback_.is_null()) {
-    OnPageCount(mojom::PdfToEmfConverterPtr(), 0);
+    std::move(start_callback_).Run(/*page_count=*/0);
     if (!weak_this)
       return;  // Protect against the |start_callback_| deleting |this|.
   }
@@ -570,4 +586,14 @@
                                             std::move(start_callback));
 }
 
+ScopedSimulateFailureCreatingTempFileForTests::
+    ScopedSimulateFailureCreatingTempFileForTests() {
+  PdfConverterImpl::set_fail_when_creating_temp_file_for_tests(true);
+}
+
+ScopedSimulateFailureCreatingTempFileForTests::
+    ~ScopedSimulateFailureCreatingTempFileForTests() {
+  PdfConverterImpl::set_fail_when_creating_temp_file_for_tests(false);
+}
+
 }  // namespace printing
diff --git a/chrome/browser/printing/pdf_to_emf_converter.h b/chrome/browser/printing/pdf_to_emf_converter.h
index 1263c44..5aa61f7 100644
--- a/chrome/browser/printing/pdf_to_emf_converter.h
+++ b/chrome/browser/printing/pdf_to_emf_converter.h
@@ -38,6 +38,16 @@
   virtual void GetPage(int page_number,
                        const GetPageCallback& get_page_callback) = 0;
 };
+
+// Object used by tests to exercise the temporary file creation failure code
+// path. As long as this object is alive, the PdfConverter will fail when
+// creating temporary files.
+class ScopedSimulateFailureCreatingTempFileForTests {
+ public:
+  ScopedSimulateFailureCreatingTempFileForTests();
+  ~ScopedSimulateFailureCreatingTempFileForTests();
+};
+
 }  // namespace printing
 
 #endif  // CHROME_BROWSER_PRINTING_PDF_TO_EMF_CONVERTER_H_
diff --git a/chrome/browser/printing/pdf_to_emf_converter_browsertest.cc b/chrome/browser/printing/pdf_to_emf_converter_browsertest.cc
index bcd515a..c0ab2f7 100644
--- a/chrome/browser/printing/pdf_to_emf_converter_browsertest.cc
+++ b/chrome/browser/printing/pdf_to_emf_converter_browsertest.cc
@@ -79,7 +79,19 @@
 
 }  // namespace
 
-IN_PROC_BROWSER_TEST_F(PDFToEMFConverterBrowserTest, TestFailure) {
+IN_PROC_BROWSER_TEST_F(PDFToEMFConverterBrowserTest, TestFailureNoTempFile) {
+  ScopedSimulateFailureCreatingTempFileForTests fail_creating_temp_file;
+
+  base::RunLoop run_loop;
+  int page_count = -1;
+  std::unique_ptr<PdfConverter> pdf_converter = PdfConverter::StartPdfConverter(
+      base::MakeRefCounted<base::RefCountedStaticMemory>(), PdfRenderSettings(),
+      base::Bind(&StartCallbackImpl, run_loop.QuitClosure(), &page_count));
+  run_loop.Run();
+  EXPECT_EQ(0, page_count);
+}
+
+IN_PROC_BROWSER_TEST_F(PDFToEMFConverterBrowserTest, TestFailureBadPdf) {
   scoped_refptr<base::RefCountedStaticMemory> bad_pdf_data =
       base::MakeRefCounted<base::RefCountedStaticMemory>("0123456789", 10);