1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "printing/printing_context.h" 6 7 #include "base/logging.h" 8 #include "base/values.h" 9 #include "printing/page_setup.h" 10 #include "printing/page_size_margins.h" 11 #include "printing/print_job_constants.h" 12 #include "printing/print_settings_conversion.h" 13 #include "printing/units.h" 14 15 namespace printing { 16 17 namespace { 18 const float kCloudPrintMarginInch = 0.25; 19 } 20 21 PrintingContext::PrintingContext(Delegate* delegate) 22 : delegate_(delegate), 23 dialog_box_dismissed_(false), 24 in_print_job_(false), 25 abort_printing_(false) { 26 CHECK(delegate_); 27 } 28 29 PrintingContext::~PrintingContext() { 30 } 31 32 void PrintingContext::set_margin_type(MarginType type) { 33 DCHECK(type != CUSTOM_MARGINS); 34 settings_.set_margin_type(type); 35 } 36 37 void PrintingContext::ResetSettings() { 38 ReleaseContext(); 39 40 settings_.Clear(); 41 42 in_print_job_ = false; 43 dialog_box_dismissed_ = false; 44 abort_printing_ = false; 45 } 46 47 PrintingContext::Result PrintingContext::OnError() { 48 Result result = abort_printing_ ? CANCEL : FAILED; 49 ResetSettings(); 50 return result; 51 } 52 53 PrintingContext::Result PrintingContext::UsePdfSettings() { 54 scoped_ptr<base::DictionaryValue> pdf_settings(new base::DictionaryValue); 55 pdf_settings->SetBoolean(kSettingHeaderFooterEnabled, false); 56 pdf_settings->SetBoolean(kSettingShouldPrintBackgrounds, false); 57 pdf_settings->SetBoolean(kSettingShouldPrintSelectionOnly, false); 58 pdf_settings->SetInteger(kSettingMarginsType, printing::NO_MARGINS); 59 pdf_settings->SetBoolean(kSettingCollate, true); 60 pdf_settings->SetInteger(kSettingCopies, 1); 61 pdf_settings->SetInteger(kSettingColor, printing::COLOR); 62 pdf_settings->SetInteger(kSettingDuplexMode, printing::SIMPLEX); 63 pdf_settings->SetBoolean(kSettingLandscape, false); 64 pdf_settings->SetString(kSettingDeviceName, ""); 65 pdf_settings->SetBoolean(kSettingPrintToPDF, true); 66 pdf_settings->SetBoolean(kSettingCloudPrintDialog, false); 67 pdf_settings->SetBoolean(kSettingPrintWithPrivet, false); 68 return UpdatePrintSettings(*pdf_settings); 69 } 70 71 PrintingContext::Result PrintingContext::UpdatePrintSettings( 72 const base::DictionaryValue& job_settings) { 73 ResetSettings(); 74 75 if (!PrintSettingsFromJobSettings(job_settings, &settings_)) { 76 NOTREACHED(); 77 return OnError(); 78 } 79 80 bool print_to_pdf = false; 81 bool is_cloud_dialog = false; 82 bool print_with_privet = false; 83 84 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || 85 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) || 86 !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) { 87 NOTREACHED(); 88 return OnError(); 89 } 90 91 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); 92 bool open_in_external_preview = 93 job_settings.HasKey(kSettingOpenPDFInPreview); 94 95 if (!open_in_external_preview && (print_to_pdf || print_to_cloud || 96 is_cloud_dialog || print_with_privet)) { 97 settings_.set_dpi(kDefaultPdfDpi); 98 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits()); 99 if (!settings_.requested_media().size_microns.IsEmpty()) { 100 float deviceMicronsPerDeviceUnit = 101 (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch(); 102 paper_size = gfx::Size(settings_.requested_media().size_microns.width() / 103 deviceMicronsPerDeviceUnit, 104 settings_.requested_media().size_microns.height() / 105 deviceMicronsPerDeviceUnit); 106 } 107 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height()); 108 if (print_to_cloud || print_with_privet) { 109 paper_rect.Inset( 110 kCloudPrintMarginInch * settings_.device_units_per_inch(), 111 kCloudPrintMarginInch * settings_.device_units_per_inch()); 112 } 113 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true); 114 return OK; 115 } 116 117 bool show_system_dialog = false; 118 job_settings.GetBoolean(printing::kSettingShowSystemDialog, 119 &show_system_dialog); 120 121 return UpdatePrinterSettings(open_in_external_preview, show_system_dialog); 122 } 123 124 } // namespace printing 125