Chromium Code Reviews| 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 <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/ref_counted_memory.h" | |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 14 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/printing/pdf_to_emf_converter.h" | |
|
Lei Zhang
2014/05/20 03:39:29
put this and other newly added includes in a #if d
scottmg
2014/05/20 17:16:24
Done.
| |
| 16 #include "chrome/browser/printing/print_job.h" | 19 #include "chrome/browser/printing/print_job.h" |
| 17 #include "chrome/browser/printing/print_job_manager.h" | 20 #include "chrome/browser/printing/print_job_manager.h" |
| 18 #include "chrome/browser/printing/printer_query.h" | 21 #include "chrome/browser/printing/printer_query.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/simple_message_box.h" | 23 #include "chrome/browser/ui/simple_message_box.h" |
| 21 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/print_messages.h" | 25 #include "chrome/common/print_messages.h" |
| 23 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 29 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
| 30 #include "printing/metafile_impl.h" | 33 #include "printing/metafile_impl.h" |
| 34 #include "printing/pdf_render_settings.h" | |
| 31 #include "printing/printed_document.h" | 35 #include "printing/printed_document.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 33 | 37 |
| 34 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 35 #include "base/command_line.h" | 39 #include "base/command_line.h" |
| 36 #include "chrome/common/chrome_switches.h" | 40 #include "chrome/common/chrome_switches.h" |
| 37 #endif | 41 #endif |
| 38 | 42 |
| 39 #if defined(ENABLE_FULL_PRINTING) | 43 #if defined(ENABLE_FULL_PRINTING) |
| 40 #include "chrome/browser/printing/print_error_dialog.h" | 44 #include "chrome/browser/printing/print_error_dialog.h" |
| 41 #endif | 45 #endif |
| 42 | 46 |
| 43 using base::TimeDelta; | 47 using base::TimeDelta; |
| 44 using content::BrowserThread; | 48 using content::BrowserThread; |
| 45 | 49 |
| 46 #if defined(OS_WIN) | 50 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) |
| 47 // Limits memory usage by raster to 64 MiB. | 51 // Limits memory usage by raster to 64 MiB. |
| 48 const int kMaxRasterSizeInPixels = 16*1024*1024; | 52 const int kMaxRasterSizeInPixels = 16*1024*1024; |
| 49 #endif | 53 #endif |
| 50 | 54 |
| 51 namespace printing { | 55 namespace printing { |
| 52 | 56 |
| 53 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) | 57 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) |
| 54 : content::WebContentsObserver(web_contents), | 58 : content::WebContentsObserver(web_contents), |
| 55 number_pages_(0), | 59 number_pages_(0), |
| 56 printing_succeeded_(false), | 60 printing_succeeded_(false), |
| 57 inside_inner_message_loop_(false), | 61 inside_inner_message_loop_(false), |
| 58 cookie_(0), | 62 cookie_(0), |
| 59 queue_(g_browser_process->print_job_manager()->queue()) { | 63 queue_(g_browser_process->print_job_manager()->queue()) { |
| 60 DCHECK(queue_); | 64 DCHECK(queue_); |
| 61 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 65 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
| 66 defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 62 expecting_first_page_ = true; | 67 expecting_first_page_ = true; |
| 63 #endif | 68 #endif |
| 64 Profile* profile = | 69 Profile* profile = |
| 65 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 70 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 66 printing_enabled_.Init( | 71 printing_enabled_.Init( |
| 67 prefs::kPrintingEnabled, | 72 prefs::kPrintingEnabled, |
| 68 profile->GetPrefs(), | 73 profile->GetPrefs(), |
| 69 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, | 74 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, |
| 70 base::Unretained(this))); | 75 base::Unretained(this))); |
| 71 } | 76 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 DCHECK_GT(cookie, 0); | 122 DCHECK_GT(cookie, 0); |
| 118 DCHECK_GT(number_pages, 0); | 123 DCHECK_GT(number_pages, 0); |
| 119 number_pages_ = number_pages; | 124 number_pages_ = number_pages; |
| 120 OpportunisticallyCreatePrintJob(cookie); | 125 OpportunisticallyCreatePrintJob(cookie); |
| 121 } | 126 } |
| 122 | 127 |
| 123 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { | 128 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { |
| 124 cookie_ = cookie; | 129 cookie_ = cookie; |
| 125 } | 130 } |
| 126 | 131 |
| 132 #if defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 133 void PrintViewManagerBase::OnPdfToEmfConverted( | |
| 134 const PrintHostMsg_DidPrintPage_Params& params, | |
| 135 double scale_factor, | |
| 136 const std::vector<base::FilePath>& emf_files) { | |
| 137 PrintedDocument* document = print_job_->document(); | |
| 138 if (!document) | |
| 139 return; | |
| 140 | |
| 141 for (size_t i = 0; i < emf_files.size(); ++i) { | |
| 142 scoped_ptr<printing::Emf> metafile(new printing::Emf); | |
| 143 if (!metafile->InitFromFile(emf_files[i])) { | |
| 144 NOTREACHED() << "Invalid metafile"; | |
| 145 web_contents()->Stop(); | |
| 146 return; | |
| 147 } | |
| 148 // Update the rendered document. It will send notifications to the listener. | |
| 149 document->SetPage(i, | |
| 150 metafile.release(), | |
| 151 scale_factor, | |
| 152 params.page_size, | |
| 153 params.content_area); | |
| 154 } | |
| 155 | |
| 156 ShouldQuitFromInnerMessageLoop(); | |
| 157 } | |
| 158 #endif // WIN_PDF_METAFILE_FOR_PRINTING | |
| 159 | |
| 127 void PrintViewManagerBase::OnDidPrintPage( | 160 void PrintViewManagerBase::OnDidPrintPage( |
| 128 const PrintHostMsg_DidPrintPage_Params& params) { | 161 const PrintHostMsg_DidPrintPage_Params& params) { |
| 129 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) | 162 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) |
| 130 return; | 163 return; |
| 131 | 164 |
| 132 PrintedDocument* document = print_job_->document(); | 165 PrintedDocument* document = print_job_->document(); |
| 133 if (!document || params.document_cookie != document->cookie()) { | 166 if (!document || params.document_cookie != document->cookie()) { |
| 134 // Out of sync. It may happen since we are completely asynchronous. Old | 167 // Out of sync. It may happen since we are completely asynchronous. Old |
| 135 // spurious messages can be received if one of the processes is overloaded. | 168 // spurious messages can be received if one of the processes is overloaded. |
| 136 return; | 169 return; |
| 137 } | 170 } |
| 138 | 171 |
| 139 #if defined(OS_WIN) || defined(OS_MACOSX) | 172 #if (defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)) || \ |
| 173 defined(OS_MACOSX) | |
| 140 const bool metafile_must_be_valid = true; | 174 const bool metafile_must_be_valid = true; |
| 141 #elif defined(OS_POSIX) | 175 #elif defined(OS_POSIX) || defined(WIN_PDF_METAFILE_FOR_PRINTING) |
| 142 const bool metafile_must_be_valid = expecting_first_page_; | 176 const bool metafile_must_be_valid = expecting_first_page_; |
| 143 expecting_first_page_ = false; | 177 expecting_first_page_ = false; |
| 144 #endif | 178 #endif |
| 145 | 179 |
| 146 base::SharedMemory shared_buf(params.metafile_data_handle, true); | 180 base::SharedMemory shared_buf(params.metafile_data_handle, true); |
| 147 if (metafile_must_be_valid) { | 181 if (metafile_must_be_valid) { |
| 148 if (!shared_buf.Map(params.data_size)) { | 182 if (!shared_buf.Map(params.data_size)) { |
| 149 NOTREACHED() << "couldn't map"; | 183 NOTREACHED() << "couldn't map"; |
| 150 web_contents()->Stop(); | 184 web_contents()->Stop(); |
| 151 return; | 185 return; |
| 152 } | 186 } |
| 153 } | 187 } |
| 154 | 188 |
| 155 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); | 189 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); |
| 156 if (metafile_must_be_valid) { | 190 if (metafile_must_be_valid) { |
| 157 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { | 191 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { |
| 158 NOTREACHED() << "Invalid metafile header"; | 192 NOTREACHED() << "Invalid metafile header"; |
| 159 web_contents()->Stop(); | 193 web_contents()->Stop(); |
| 160 return; | 194 return; |
| 161 } | 195 } |
| 162 } | 196 } |
| 163 | 197 |
| 164 #if defined(OS_WIN) | 198 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) |
| 165 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); | 199 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); |
| 166 int raster_size = std::min(params.page_size.GetArea(), | 200 int raster_size = |
| 167 kMaxRasterSizeInPixels); | 201 std::min(params.page_size.GetArea(), kMaxRasterSizeInPixels); |
| 168 if (big_emf) { | 202 if (big_emf) { |
| 169 scoped_ptr<NativeMetafile> raster_metafile( | 203 scoped_ptr<NativeMetafile> raster_metafile( |
| 170 metafile->RasterizeMetafile(raster_size)); | 204 metafile->RasterizeMetafile(raster_size)); |
| 171 if (raster_metafile.get()) { | 205 if (raster_metafile.get()) { |
| 172 metafile.swap(raster_metafile); | 206 metafile.swap(raster_metafile); |
| 173 } else if (big_emf) { | 207 } else if (big_emf) { |
| 174 // Don't fall back to emf here. | 208 // Don't fall back to emf here. |
| 175 NOTREACHED() << "size:" << params.data_size; | 209 NOTREACHED() << "size:" << params.data_size; |
| 176 TerminatePrintJob(true); | 210 TerminatePrintJob(true); |
| 177 web_contents()->Stop(); | 211 web_contents()->Stop(); |
| 178 return; | 212 return; |
| 179 } | 213 } |
| 180 } | 214 } |
| 181 #endif | 215 #endif // OS_WIN && !WIN_PDF_METAFILE_FOR_PRINTING |
| 182 | 216 |
| 217 #if !defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 183 // Update the rendered document. It will send notifications to the listener. | 218 // Update the rendered document. It will send notifications to the listener. |
| 184 document->SetPage(params.page_number, | 219 document->SetPage(params.page_number, |
| 185 metafile.release(), | 220 metafile.release(), |
| 186 params.actual_shrink, | 221 params.actual_shrink, |
| 187 params.page_size, | 222 params.page_size, |
| 188 params.content_area); | 223 params.content_area); |
| 189 | 224 |
| 190 ShouldQuitFromInnerMessageLoop(); | 225 ShouldQuitFromInnerMessageLoop(); |
| 226 #else | |
| 227 if (metafile_must_be_valid) { | |
| 228 scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes( | |
| 229 reinterpret_cast<const unsigned char*>(shared_buf.memory()), | |
| 230 params.data_size); | |
| 231 | |
| 232 if (!pdf_to_emf_converter_) | |
| 233 pdf_to_emf_converter_ = PdfToEmfConverter::CreateDefault(); | |
| 234 | |
| 235 const int printer_dpi = 600; | |
|
Lei Zhang
2014/05/20 03:39:29
nit: kPrinterDpi
scottmg
2014/05/20 17:16:24
Done.
| |
| 236 pdf_to_emf_converter_->Start( | |
| 237 bytes, | |
| 238 printing::PdfRenderSettings(params.content_area, printer_dpi, false), | |
| 239 base::Bind(&PrintViewManagerBase::OnPdfToEmfConverted, | |
| 240 base::Unretained(this), | |
| 241 params)); | |
| 242 } | |
| 243 #endif // !WIN_PDF_METAFILE_FOR_PRINTING | |
| 191 } | 244 } |
| 192 | 245 |
| 193 void PrintViewManagerBase::OnPrintingFailed(int cookie) { | 246 void PrintViewManagerBase::OnPrintingFailed(int cookie) { |
| 194 if (cookie != cookie_) { | 247 if (cookie != cookie_) { |
| 195 NOTREACHED(); | 248 NOTREACHED(); |
| 196 return; | 249 return; |
| 197 } | 250 } |
| 198 | 251 |
| 199 #if defined(ENABLE_FULL_PRINTING) | 252 #if defined(ENABLE_FULL_PRINTING) |
| 200 chrome::ShowPrintErrorDialog(); | 253 chrome::ShowPrintErrorDialog(); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 if (print_job_.get() && | 440 if (print_job_.get() && |
| 388 print_job_->document() && | 441 print_job_->document() && |
| 389 !print_job_->document()->IsComplete()) { | 442 !print_job_->document()->IsComplete()) { |
| 390 DCHECK(!result); | 443 DCHECK(!result); |
| 391 // That failed. | 444 // That failed. |
| 392 TerminatePrintJob(true); | 445 TerminatePrintJob(true); |
| 393 } else { | 446 } else { |
| 394 // DO NOT wait for the job to finish. | 447 // DO NOT wait for the job to finish. |
| 395 ReleasePrintJob(); | 448 ReleasePrintJob(); |
| 396 } | 449 } |
| 397 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 450 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
| 451 defined(WIN_PDF_METAFILE_FOR_PRINTING) | |
| 398 expecting_first_page_ = true; | 452 expecting_first_page_ = true; |
| 399 #endif | 453 #endif |
| 400 } | 454 } |
| 401 | 455 |
| 402 void PrintViewManagerBase::PrintingDone(bool success) { | 456 void PrintViewManagerBase::PrintingDone(bool success) { |
| 403 if (!print_job_.get()) | 457 if (!print_job_.get()) |
| 404 return; | 458 return; |
| 405 Send(new PrintMsg_PrintingDone(routing_id(), success)); | 459 Send(new PrintMsg_PrintingDone(routing_id(), success)); |
| 406 } | 460 } |
| 407 | 461 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 scoped_refptr<printing::PrinterQuery> printer_query; | 585 scoped_refptr<printing::PrinterQuery> printer_query; |
| 532 printer_query = queue_->PopPrinterQuery(cookie); | 586 printer_query = queue_->PopPrinterQuery(cookie); |
| 533 if (!printer_query) | 587 if (!printer_query) |
| 534 return; | 588 return; |
| 535 BrowserThread::PostTask( | 589 BrowserThread::PostTask( |
| 536 BrowserThread::IO, FROM_HERE, | 590 BrowserThread::IO, FROM_HERE, |
| 537 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 591 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
| 538 } | 592 } |
| 539 | 593 |
| 540 } // namespace printing | 594 } // namespace printing |
| OLD | NEW |