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