| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "chrome/browser/printing/print_view_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 13 #include "base/timer.h" | 14 #include "base/timer.h" |
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/printing/print_error_dialog.h" | 17 #include "chrome/browser/printing/print_error_dialog.h" |
| 17 #include "chrome/browser/printing/print_job.h" | 18 #include "chrome/browser/printing/print_job.h" |
| 18 #include "chrome/browser/printing/print_job_manager.h" | 19 #include "chrome/browser/printing/print_job_manager.h" |
| 19 #include "chrome/browser/printing/print_preview_tab_controller.h" | 20 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 20 #include "chrome/browser/printing/print_view_manager_observer.h" | 21 #include "chrome/browser/printing/print_view_manager_observer.h" |
| 21 #include "chrome/browser/printing/printer_query.h" | 22 #include "chrome/browser/printing/printer_query.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 24 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 25 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 25 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/print_messages.h" | 29 #include "chrome/common/print_messages.h" |
| 28 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/notification_details.h" | 31 #include "content/public/browser/notification_details.h" |
| 30 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
| 32 #include "content/public/browser/render_view_host.h" | 34 #include "content/public/browser/render_view_host.h" |
| 33 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 34 #include "content/public/browser/web_contents_view.h" | 36 #include "content/public/browser/web_contents_view.h" |
| 35 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
| 36 #include "printing/metafile.h" | 38 #include "printing/metafile.h" |
| 37 #include "printing/metafile_impl.h" | 39 #include "printing/metafile_impl.h" |
| 38 #include "printing/print_destination_interface.h" | 40 #include "printing/print_destination_interface.h" |
| 39 #include "printing/printed_document.h" | 41 #include "printing/printed_document.h" |
| 40 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
| 41 | 43 |
| 42 using base::TimeDelta; | 44 using base::TimeDelta; |
| 43 using content::BrowserThread; | 45 using content::BrowserThread; |
| 44 | 46 |
| 45 namespace { | 47 namespace { |
| 46 | 48 |
| 47 // Keeps track of pending scripted print preview closures. | 49 // Keeps track of pending scripted print preview closures. |
| 48 // No locking, only access on the UI thread. | 50 // No locking, only access on the UI thread. |
| 49 typedef std::map<content::RenderProcessHost*, base::Closure> | 51 typedef std::map<content::RenderProcessHost*, base::Closure> |
| 50 ScriptedPrintPreviewClosureMap; | 52 ScriptedPrintPreviewClosureMap; |
| 51 static base::LazyInstance<ScriptedPrintPreviewClosureMap> | 53 static base::LazyInstance<ScriptedPrintPreviewClosureMap> |
| 52 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; | 54 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; |
| 53 | 55 |
| 56 // Limits memory usage by raster to 64 MiB. |
| 57 const int kMaxRasterSizeInPixels = 16*1024*1024; |
| 58 |
| 54 } // namespace | 59 } // namespace |
| 55 | 60 |
| 56 namespace printing { | 61 namespace printing { |
| 57 | 62 |
| 58 PrintViewManager::PrintViewManager(TabContents* tab) | 63 PrintViewManager::PrintViewManager(TabContents* tab) |
| 59 : content::WebContentsObserver(tab->web_contents()), | 64 : content::WebContentsObserver(tab->web_contents()), |
| 60 tab_(tab), | 65 tab_(tab), |
| 61 number_pages_(0), | 66 number_pages_(0), |
| 62 printing_succeeded_(false), | 67 printing_succeeded_(false), |
| 63 inside_inner_message_loop_(false), | 68 inside_inner_message_loop_(false), |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) | 218 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) |
| 214 return; | 219 return; |
| 215 | 220 |
| 216 PrintedDocument* document = print_job_->document(); | 221 PrintedDocument* document = print_job_->document(); |
| 217 if (!document || params.document_cookie != document->cookie()) { | 222 if (!document || params.document_cookie != document->cookie()) { |
| 218 // Out of sync. It may happen since we are completely asynchronous. Old | 223 // Out of sync. It may happen since we are completely asynchronous. Old |
| 219 // spurious messages can be received if one of the processes is overloaded. | 224 // spurious messages can be received if one of the processes is overloaded. |
| 220 return; | 225 return; |
| 221 } | 226 } |
| 222 | 227 |
| 223 #if defined(OS_WIN) | |
| 224 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx | |
| 225 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 | |
| 226 // MB, it can fail to print and not send an error message. | |
| 227 if (params.data_size && params.data_size >= 350*1024*1024) { | |
| 228 NOTREACHED() << "size:" << params.data_size; | |
| 229 TerminatePrintJob(true); | |
| 230 web_contents()->Stop(); | |
| 231 return; | |
| 232 } | |
| 233 #endif | |
| 234 | |
| 235 #if defined(OS_WIN) || defined(OS_MACOSX) | 228 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 236 const bool metafile_must_be_valid = true; | 229 const bool metafile_must_be_valid = true; |
| 237 #elif defined(OS_POSIX) | 230 #elif defined(OS_POSIX) |
| 238 const bool metafile_must_be_valid = expecting_first_page_; | 231 const bool metafile_must_be_valid = expecting_first_page_; |
| 239 expecting_first_page_ = false; | 232 expecting_first_page_ = false; |
| 240 #endif | 233 #endif |
| 241 | 234 |
| 242 base::SharedMemory shared_buf(params.metafile_data_handle, true); | 235 base::SharedMemory shared_buf(params.metafile_data_handle, true); |
| 243 if (metafile_must_be_valid) { | 236 if (metafile_must_be_valid) { |
| 244 if (!shared_buf.Map(params.data_size)) { | 237 if (!shared_buf.Map(params.data_size)) { |
| 245 NOTREACHED() << "couldn't map"; | 238 NOTREACHED() << "couldn't map"; |
| 246 web_contents()->Stop(); | 239 web_contents()->Stop(); |
| 247 return; | 240 return; |
| 248 } | 241 } |
| 249 } | 242 } |
| 250 | 243 |
| 251 scoped_ptr<Metafile> metafile(new NativeMetafile); | 244 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); |
| 252 if (metafile_must_be_valid) { | 245 if (metafile_must_be_valid) { |
| 253 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { | 246 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { |
| 254 NOTREACHED() << "Invalid metafile header"; | 247 NOTREACHED() << "Invalid metafile header"; |
| 255 web_contents()->Stop(); | 248 web_contents()->Stop(); |
| 256 return; | 249 return; |
| 257 } | 250 } |
| 258 } | 251 } |
| 259 | 252 |
| 253 #if defined(OS_WIN) |
| 254 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); |
| 255 const CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 256 if (big_emf || |
| 257 (cmdline && cmdline->HasSwitch(switches::kPrintRaster)) || |
| 258 (!print_job_->settings().supports_alpha_blend() && |
| 259 metafile->IsAlphaBlendUsed())) { |
| 260 int raster_size = std::min(params.page_size.GetArea(), |
| 261 kMaxRasterSizeInPixels); |
| 262 scoped_ptr<NativeMetafile> raster_metafile( |
| 263 metafile->RasterizeMetafile(raster_size)); |
| 264 if (raster_metafile.get()) { |
| 265 metafile.swap(raster_metafile); |
| 266 } else if (big_emf) { |
| 267 // Don't fall back to emf here. |
| 268 NOTREACHED() << "size:" << params.data_size; |
| 269 TerminatePrintJob(true); |
| 270 web_contents()->Stop(); |
| 271 return; |
| 272 } |
| 273 } |
| 274 |
| 275 #endif |
| 276 |
| 260 // Update the rendered document. It will send notifications to the listener. | 277 // Update the rendered document. It will send notifications to the listener. |
| 261 document->SetPage(params.page_number, | 278 document->SetPage(params.page_number, |
| 262 metafile.release(), | 279 metafile.release(), |
| 263 params.actual_shrink, | 280 params.actual_shrink, |
| 264 params.page_size, | 281 params.page_size, |
| 265 params.content_area); | 282 params.content_area); |
| 266 | 283 |
| 267 ShouldQuitFromInnerMessageLoop(); | 284 ShouldQuitFromInnerMessageLoop(); |
| 268 } | 285 } |
| 269 | 286 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 scoped_refptr<printing::PrinterQuery> printer_query; | 672 scoped_refptr<printing::PrinterQuery> printer_query; |
| 656 print_job_manager->PopPrinterQuery(cookie, &printer_query); | 673 print_job_manager->PopPrinterQuery(cookie, &printer_query); |
| 657 if (!printer_query.get()) | 674 if (!printer_query.get()) |
| 658 return; | 675 return; |
| 659 BrowserThread::PostTask( | 676 BrowserThread::PostTask( |
| 660 BrowserThread::IO, FROM_HERE, | 677 BrowserThread::IO, FROM_HERE, |
| 661 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 678 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
| 662 } | 679 } |
| 663 | 680 |
| 664 } // namespace printing | 681 } // namespace printing |
| OLD | NEW |