Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / printing / printing_context.cc
blob352882891cabd60dad4bfd563a3f0aec4878fd49
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.
5 #include "printing/printing_context.h"
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"
15 namespace printing {
17 namespace {
18 const float kCloudPrintMarginInch = 0.25;
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_);
29 PrintingContext::~PrintingContext() {
32 void PrintingContext::set_margin_type(MarginType type) {
33 DCHECK(type != CUSTOM_MARGINS);
34 settings_.set_margin_type(type);
37 void PrintingContext::ResetSettings() {
38 ReleaseContext();
40 settings_.Clear();
42 in_print_job_ = false;
43 dialog_box_dismissed_ = false;
44 abort_printing_ = false;
47 PrintingContext::Result PrintingContext::OnError() {
48 Result result = abort_printing_ ? CANCEL : FAILED;
49 ResetSettings();
50 return result;
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);
71 PrintingContext::Result PrintingContext::UpdatePrintSettings(
72 const base::DictionaryValue& job_settings) {
73 ResetSettings();
75 if (!PrintSettingsFromJobSettings(job_settings, &settings_)) {
76 NOTREACHED();
77 return OnError();
80 bool print_to_pdf = false;
81 bool is_cloud_dialog = false;
82 bool print_with_privet = false;
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();
91 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
92 bool open_in_external_preview =
93 job_settings.HasKey(kSettingOpenPDFInPreview);
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);
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());
113 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
114 return OK;
117 bool show_system_dialog = false;
118 job_settings.GetBoolean(printing::kSettingShowSystemDialog,
119 &show_system_dialog);
121 return UpdatePrinterSettings(open_in_external_preview, show_system_dialog);
124 } // namespace printing