Make the ChromeOS session name more robust w.r.t. the device type.
[chromium-blink-merge.git] / printing / printing_context.cc
blob16f1103f5cc5f754c6e59ffd5e58eaf49a808fb1
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 in_print_job_(false),
24 abort_printing_(false) {
25 CHECK(delegate_);
28 PrintingContext::~PrintingContext() {
31 void PrintingContext::set_margin_type(MarginType type) {
32 DCHECK(type != CUSTOM_MARGINS);
33 settings_.set_margin_type(type);
36 void PrintingContext::ResetSettings() {
37 ReleaseContext();
39 settings_.Clear();
41 in_print_job_ = false;
42 abort_printing_ = false;
45 PrintingContext::Result PrintingContext::OnError() {
46 Result result = abort_printing_ ? CANCEL : FAILED;
47 ResetSettings();
48 return result;
51 PrintingContext::Result PrintingContext::UsePdfSettings() {
52 scoped_ptr<base::DictionaryValue> pdf_settings(new base::DictionaryValue);
53 pdf_settings->SetBoolean(kSettingHeaderFooterEnabled, false);
54 pdf_settings->SetBoolean(kSettingShouldPrintBackgrounds, false);
55 pdf_settings->SetBoolean(kSettingShouldPrintSelectionOnly, false);
56 pdf_settings->SetInteger(kSettingMarginsType, printing::NO_MARGINS);
57 pdf_settings->SetBoolean(kSettingCollate, true);
58 pdf_settings->SetInteger(kSettingCopies, 1);
59 pdf_settings->SetInteger(kSettingColor, printing::COLOR);
60 pdf_settings->SetInteger(kSettingDuplexMode, printing::SIMPLEX);
61 pdf_settings->SetBoolean(kSettingLandscape, false);
62 pdf_settings->SetString(kSettingDeviceName, "");
63 pdf_settings->SetBoolean(kSettingPrintToPDF, true);
64 pdf_settings->SetBoolean(kSettingCloudPrintDialog, false);
65 pdf_settings->SetBoolean(kSettingPrintWithPrivet, false);
66 pdf_settings->SetBoolean(kSettingPrintWithExtension, false);
67 return UpdatePrintSettings(*pdf_settings);
70 PrintingContext::Result PrintingContext::UpdatePrintSettings(
71 const base::DictionaryValue& job_settings) {
72 ResetSettings();
74 if (!PrintSettingsFromJobSettings(job_settings, &settings_)) {
75 NOTREACHED();
76 return OnError();
79 bool print_to_pdf = false;
80 bool is_cloud_dialog = false;
81 bool print_with_privet = false;
82 bool print_with_extension = 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 !job_settings.GetBoolean(kSettingPrintWithExtension,
88 &print_with_extension)) {
89 NOTREACHED();
90 return OnError();
93 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
94 bool open_in_external_preview =
95 job_settings.HasKey(kSettingOpenPDFInPreview);
97 if (!open_in_external_preview &&
98 (print_to_pdf || print_to_cloud || is_cloud_dialog || print_with_privet ||
99 print_with_extension)) {
100 settings_.set_dpi(kDefaultPdfDpi);
101 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
102 if (!settings_.requested_media().size_microns.IsEmpty()) {
103 float deviceMicronsPerDeviceUnit =
104 (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch();
105 paper_size = gfx::Size(settings_.requested_media().size_microns.width() /
106 deviceMicronsPerDeviceUnit,
107 settings_.requested_media().size_microns.height() /
108 deviceMicronsPerDeviceUnit);
110 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
111 if (print_to_cloud || print_with_privet) {
112 paper_rect.Inset(
113 kCloudPrintMarginInch * settings_.device_units_per_inch(),
114 kCloudPrintMarginInch * settings_.device_units_per_inch());
116 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
117 return OK;
120 bool show_system_dialog = false;
121 job_settings.GetBoolean(kSettingShowSystemDialog, &show_system_dialog);
123 int page_count = 0;
124 job_settings.GetInteger(kSettingPreviewPageCount, &page_count);
126 return UpdatePrinterSettings(open_in_external_preview, show_system_dialog,
127 page_count);
130 } // namespace printing