| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/printing/print_view_manager_base.h" | 5 #include "chrome/browser/printing/print_view_manager_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "printing/metafile_impl.h" | 28 #include "printing/metafile_impl.h" |
| 29 #include "printing/printed_document.h" | 29 #include "printing/printed_document.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 31 | 31 |
| 32 #if defined(ENABLE_FULL_PRINTING) | 32 #if defined(ENABLE_FULL_PRINTING) |
| 33 #include "chrome/browser/printing/print_error_dialog.h" | 33 #include "chrome/browser/printing/print_error_dialog.h" |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if defined(WIN_PDF_METAFILE_FOR_PRINTING) | 36 #if defined(OS_WIN) |
| 37 #include "base/memory/ref_counted.h" | 37 #include "base/memory/ref_counted.h" |
| 38 #include "base/memory/ref_counted_memory.h" | 38 #include "base/memory/ref_counted_memory.h" |
| 39 #include "chrome/browser/printing/pdf_to_emf_converter.h" | 39 #include "chrome/browser/printing/pdf_to_emf_converter.h" |
| 40 #include "printing/emf_win.h" |
| 40 #include "printing/pdf_render_settings.h" | 41 #include "printing/pdf_render_settings.h" |
| 41 #endif | 42 #endif |
| 42 | 43 |
| 43 using base::TimeDelta; | 44 using base::TimeDelta; |
| 44 using content::BrowserThread; | 45 using content::BrowserThread; |
| 45 | 46 |
| 46 namespace printing { | 47 namespace printing { |
| 47 | 48 |
| 48 namespace { | 49 namespace { |
| 49 | 50 |
| 50 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 51 // Limits memory usage by raster to 64 MiB. | |
| 52 const int kMaxRasterSizeInPixels = 16*1024*1024; | |
| 53 #endif | |
| 54 | |
| 55 } // namespace | 51 } // namespace |
| 56 | 52 |
| 57 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) | 53 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) |
| 58 : content::WebContentsObserver(web_contents), | 54 : content::WebContentsObserver(web_contents), |
| 59 number_pages_(0), | 55 number_pages_(0), |
| 60 printing_succeeded_(false), | 56 printing_succeeded_(false), |
| 61 inside_inner_message_loop_(false), | 57 inside_inner_message_loop_(false), |
| 62 cookie_(0), | 58 cookie_(0), |
| 63 queue_(g_browser_process->print_job_manager()->queue()) { | 59 queue_(g_browser_process->print_job_manager()->queue()) { |
| 64 DCHECK(queue_.get()); | 60 DCHECK(queue_.get()); |
| 65 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ | 61 #if !defined(OS_MACOSX) |
| 66 defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 67 expecting_first_page_ = true; | 62 expecting_first_page_ = true; |
| 68 #endif | 63 #endif // OS_MACOSX |
| 69 Profile* profile = | 64 Profile* profile = |
| 70 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 65 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 71 printing_enabled_.Init( | 66 printing_enabled_.Init( |
| 72 prefs::kPrintingEnabled, | 67 prefs::kPrintingEnabled, |
| 73 profile->GetPrefs(), | 68 profile->GetPrefs(), |
| 74 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, | 69 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, |
| 75 base::Unretained(this))); | 70 base::Unretained(this))); |
| 76 } | 71 } |
| 77 | 72 |
| 78 PrintViewManagerBase::~PrintViewManagerBase() { | 73 PrintViewManagerBase::~PrintViewManagerBase() { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 DCHECK_GT(cookie, 0); | 117 DCHECK_GT(cookie, 0); |
| 123 DCHECK_GT(number_pages, 0); | 118 DCHECK_GT(number_pages, 0); |
| 124 number_pages_ = number_pages; | 119 number_pages_ = number_pages; |
| 125 OpportunisticallyCreatePrintJob(cookie); | 120 OpportunisticallyCreatePrintJob(cookie); |
| 126 } | 121 } |
| 127 | 122 |
| 128 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { | 123 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { |
| 129 cookie_ = cookie; | 124 cookie_ = cookie; |
| 130 } | 125 } |
| 131 | 126 |
| 132 #if defined(WIN_PDF_METAFILE_FOR_PRINTING) | 127 #if defined(OS_WIN) |
| 133 void PrintViewManagerBase::OnPdfToEmfConverted( | 128 void PrintViewManagerBase::OnPdfToEmfConverted( |
| 134 const PrintHostMsg_DidPrintPage_Params& params, | 129 const PrintHostMsg_DidPrintPage_Params& params, |
| 135 double scale_factor, | 130 double scale_factor, |
| 136 const std::vector<base::FilePath>& emf_files) { | 131 const std::vector<base::FilePath>& emf_files) { |
| 137 if (!print_job_.get()) | 132 if (!print_job_.get()) |
| 138 return; | 133 return; |
| 139 | 134 |
| 140 PrintedDocument* document = print_job_->document(); | 135 PrintedDocument* document = print_job_->document(); |
| 141 if (!document) | 136 if (!document) |
| 142 return; | 137 return; |
| 143 | 138 |
| 144 for (size_t i = 0; i < emf_files.size(); ++i) { | 139 for (size_t i = 0; i < emf_files.size(); ++i) { |
| 145 scoped_ptr<printing::Emf> metafile(new printing::Emf); | 140 scoped_ptr<printing::Emf> metafile(new printing::Emf); |
| 146 if (!metafile->InitFromFile(emf_files[i])) { | 141 if (!metafile->InitFromFile(emf_files[i])) { |
| 147 NOTREACHED() << "Invalid metafile"; | 142 NOTREACHED() << "Invalid metafile"; |
| 148 web_contents()->Stop(); | 143 web_contents()->Stop(); |
| 149 return; | 144 return; |
| 150 } | 145 } |
| 151 // Update the rendered document. It will send notifications to the listener. | 146 // Update the rendered document. It will send notifications to the listener. |
| 152 document->SetPage(i, | 147 document->SetPage(i, |
| 153 metafile.release(), | 148 metafile.release(), |
| 154 scale_factor, | 149 scale_factor, |
| 155 params.page_size, | 150 params.page_size, |
| 156 params.content_area); | 151 params.content_area); |
| 157 } | 152 } |
| 158 | 153 |
| 159 ShouldQuitFromInnerMessageLoop(); | 154 ShouldQuitFromInnerMessageLoop(); |
| 160 } | 155 } |
| 161 #endif // WIN_PDF_METAFILE_FOR_PRINTING | 156 #endif // OS_WIN |
| 162 | 157 |
| 163 void PrintViewManagerBase::OnDidPrintPage( | 158 void PrintViewManagerBase::OnDidPrintPage( |
| 164 const PrintHostMsg_DidPrintPage_Params& params) { | 159 const PrintHostMsg_DidPrintPage_Params& params) { |
| 165 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) | 160 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) |
| 166 return; | 161 return; |
| 167 | 162 |
| 168 PrintedDocument* document = print_job_->document(); | 163 PrintedDocument* document = print_job_->document(); |
| 169 if (!document || params.document_cookie != document->cookie()) { | 164 if (!document || params.document_cookie != document->cookie()) { |
| 170 // Out of sync. It may happen since we are completely asynchronous. Old | 165 // Out of sync. It may happen since we are completely asynchronous. Old |
| 171 // spurious messages can be received if one of the processes is overloaded. | 166 // spurious messages can be received if one of the processes is overloaded. |
| 172 return; | 167 return; |
| 173 } | 168 } |
| 174 | 169 |
| 175 #if (defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)) || \ | 170 #if defined(OS_MACOSX) |
| 176 defined(OS_MACOSX) | |
| 177 const bool metafile_must_be_valid = true; | 171 const bool metafile_must_be_valid = true; |
| 178 #elif defined(OS_POSIX) || defined(WIN_PDF_METAFILE_FOR_PRINTING) | 172 #else |
| 179 const bool metafile_must_be_valid = expecting_first_page_; | 173 const bool metafile_must_be_valid = expecting_first_page_; |
| 180 expecting_first_page_ = false; | 174 expecting_first_page_ = false; |
| 181 #endif | 175 #endif // OS_MACOSX |
| 182 | 176 |
| 183 base::SharedMemory shared_buf(params.metafile_data_handle, true); | 177 base::SharedMemory shared_buf(params.metafile_data_handle, true); |
| 184 if (metafile_must_be_valid) { | 178 if (metafile_must_be_valid) { |
| 185 if (!shared_buf.Map(params.data_size)) { | 179 if (!shared_buf.Map(params.data_size)) { |
| 186 NOTREACHED() << "couldn't map"; | 180 NOTREACHED() << "couldn't map"; |
| 187 web_contents()->Stop(); | 181 web_contents()->Stop(); |
| 188 return; | 182 return; |
| 189 } | 183 } |
| 190 } | 184 } |
| 191 | 185 |
| 192 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); | 186 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); |
| 193 if (metafile_must_be_valid) { | 187 if (metafile_must_be_valid) { |
| 194 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { | 188 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { |
| 195 NOTREACHED() << "Invalid metafile header"; | 189 NOTREACHED() << "Invalid metafile header"; |
| 196 web_contents()->Stop(); | 190 web_contents()->Stop(); |
| 197 return; | 191 return; |
| 198 } | 192 } |
| 199 } | 193 } |
| 200 | 194 |
| 201 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) | 195 #if !defined(OS_WIN) |
| 202 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); | |
| 203 int raster_size = | |
| 204 std::min(params.page_size.GetArea(), kMaxRasterSizeInPixels); | |
| 205 if (big_emf) { | |
| 206 scoped_ptr<NativeMetafile> raster_metafile( | |
| 207 metafile->RasterizeMetafile(raster_size)); | |
| 208 if (raster_metafile.get()) { | |
| 209 metafile.swap(raster_metafile); | |
| 210 } else if (big_emf) { | |
| 211 // Don't fall back to emf here. | |
| 212 NOTREACHED() << "size:" << params.data_size; | |
| 213 TerminatePrintJob(true); | |
| 214 web_contents()->Stop(); | |
| 215 return; | |
| 216 } | |
| 217 } | |
| 218 #endif // OS_WIN && !WIN_PDF_METAFILE_FOR_PRINTING | |
| 219 | |
| 220 #if !defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 221 // Update the rendered document. It will send notifications to the listener. | 196 // Update the rendered document. It will send notifications to the listener. |
| 222 document->SetPage(params.page_number, | 197 document->SetPage(params.page_number, |
| 223 metafile.release(), | 198 metafile.release(), |
| 224 #if defined(OS_WIN) | |
| 225 params.actual_shrink, | |
| 226 #endif // OS_WIN | |
| 227 params.page_size, | 199 params.page_size, |
| 228 params.content_area); | 200 params.content_area); |
| 229 | 201 |
| 230 ShouldQuitFromInnerMessageLoop(); | 202 ShouldQuitFromInnerMessageLoop(); |
| 231 #else | 203 #else |
| 232 if (metafile_must_be_valid) { | 204 if (metafile_must_be_valid) { |
| 233 scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes( | 205 scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes( |
| 234 reinterpret_cast<const unsigned char*>(shared_buf.memory()), | 206 reinterpret_cast<const unsigned char*>(shared_buf.memory()), |
| 235 params.data_size); | 207 params.data_size); |
| 236 | 208 |
| 237 document->DebugDumpData(bytes, FILE_PATH_LITERAL(".pdf")); | 209 document->DebugDumpData(bytes, FILE_PATH_LITERAL(".pdf")); |
| 238 | 210 |
| 239 if (!pdf_to_emf_converter_) | 211 if (!pdf_to_emf_converter_) |
| 240 pdf_to_emf_converter_ = PdfToEmfConverter::CreateDefault(); | 212 pdf_to_emf_converter_ = PdfToEmfConverter::CreateDefault(); |
| 241 | 213 |
| 242 const int kPrinterDpi = print_job_->settings().dpi(); | 214 const int kPrinterDpi = print_job_->settings().dpi(); |
| 243 pdf_to_emf_converter_->Start( | 215 pdf_to_emf_converter_->Start( |
| 244 bytes, | 216 bytes, |
| 245 printing::PdfRenderSettings(params.content_area, kPrinterDpi, true), | 217 printing::PdfRenderSettings(params.content_area, kPrinterDpi, true), |
| 246 base::Bind(&PrintViewManagerBase::OnPdfToEmfConverted, | 218 base::Bind(&PrintViewManagerBase::OnPdfToEmfConverted, |
| 247 base::Unretained(this), | 219 base::Unretained(this), |
| 248 params)); | 220 params)); |
| 249 } | 221 } |
| 250 #endif // !WIN_PDF_METAFILE_FOR_PRINTING | 222 #endif // !OS_WIN |
| 251 } | 223 } |
| 252 | 224 |
| 253 void PrintViewManagerBase::OnPrintingFailed(int cookie) { | 225 void PrintViewManagerBase::OnPrintingFailed(int cookie) { |
| 254 if (cookie != cookie_) { | 226 if (cookie != cookie_) { |
| 255 NOTREACHED(); | 227 NOTREACHED(); |
| 256 return; | 228 return; |
| 257 } | 229 } |
| 258 | 230 |
| 259 #if defined(ENABLE_FULL_PRINTING) | 231 #if defined(ENABLE_FULL_PRINTING) |
| 260 chrome::ShowPrintErrorDialog(); | 232 chrome::ShowPrintErrorDialog(); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 if (print_job_.get() && | 419 if (print_job_.get() && |
| 448 print_job_->document() && | 420 print_job_->document() && |
| 449 !print_job_->document()->IsComplete()) { | 421 !print_job_->document()->IsComplete()) { |
| 450 DCHECK(!result); | 422 DCHECK(!result); |
| 451 // That failed. | 423 // That failed. |
| 452 TerminatePrintJob(true); | 424 TerminatePrintJob(true); |
| 453 } else { | 425 } else { |
| 454 // DO NOT wait for the job to finish. | 426 // DO NOT wait for the job to finish. |
| 455 ReleasePrintJob(); | 427 ReleasePrintJob(); |
| 456 } | 428 } |
| 457 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ | 429 #if !defined(OS_MACOSX) |
| 458 defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 459 expecting_first_page_ = true; | 430 expecting_first_page_ = true; |
| 460 #endif | 431 #endif // OS_MACOSX |
| 461 } | 432 } |
| 462 | 433 |
| 463 void PrintViewManagerBase::PrintingDone(bool success) { | 434 void PrintViewManagerBase::PrintingDone(bool success) { |
| 464 if (!print_job_.get()) | 435 if (!print_job_.get()) |
| 465 return; | 436 return; |
| 466 Send(new PrintMsg_PrintingDone(routing_id(), success)); | 437 Send(new PrintMsg_PrintingDone(routing_id(), success)); |
| 467 } | 438 } |
| 468 | 439 |
| 469 void PrintViewManagerBase::TerminatePrintJob(bool cancel) { | 440 void PrintViewManagerBase::TerminatePrintJob(bool cancel) { |
| 470 if (!print_job_.get()) | 441 if (!print_job_.get()) |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 scoped_refptr<printing::PrinterQuery> printer_query; | 563 scoped_refptr<printing::PrinterQuery> printer_query; |
| 593 printer_query = queue_->PopPrinterQuery(cookie); | 564 printer_query = queue_->PopPrinterQuery(cookie); |
| 594 if (!printer_query.get()) | 565 if (!printer_query.get()) |
| 595 return; | 566 return; |
| 596 BrowserThread::PostTask( | 567 BrowserThread::PostTask( |
| 597 BrowserThread::IO, FROM_HERE, | 568 BrowserThread::IO, FROM_HERE, |
| 598 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 569 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
| 599 } | 570 } |
| 600 | 571 |
| 601 } // namespace printing | 572 } // namespace printing |
| OLD | NEW |