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_settings_initializer.h"
12 #include "printing/units.h"
17 const float kCloudPrintMarginInch
= 0.25;
20 PrintingContext::PrintingContext(const std::string
& app_locale
)
21 : dialog_box_dismissed_(false),
23 abort_printing_(false),
24 app_locale_(app_locale
) {
27 PrintingContext::~PrintingContext() {
30 void PrintingContext::set_margin_type(MarginType type
) {
31 DCHECK(type
!= CUSTOM_MARGINS
);
32 settings_
.set_margin_type(type
);
35 void PrintingContext::ResetSettings() {
40 in_print_job_
= false;
41 dialog_box_dismissed_
= false;
42 abort_printing_
= false;
45 PrintingContext::Result
PrintingContext::OnError() {
47 return abort_printing_
? CANCEL
: FAILED
;
50 PrintingContext::Result
PrintingContext::UpdatePrintSettings(
51 const base::DictionaryValue
& job_settings
,
52 const PageRanges
& ranges
) {
55 if (!PrintSettingsInitializer::InitSettings(job_settings
, ranges
,
61 bool print_to_pdf
= false;
62 bool is_cloud_dialog
= false;
63 bool print_with_privet
= false;
65 if (!job_settings
.GetBoolean(kSettingPrintToPDF
, &print_to_pdf
) ||
66 !job_settings
.GetBoolean(kSettingCloudPrintDialog
, &is_cloud_dialog
) ||
67 !job_settings
.GetBoolean(kSettingPrintWithPrivet
, &print_with_privet
)) {
72 bool print_to_cloud
= job_settings
.HasKey(kSettingCloudPrintId
);
73 bool open_in_external_preview
=
74 job_settings
.HasKey(kSettingOpenPDFInPreview
);
76 if (!open_in_external_preview
&& (print_to_pdf
|| print_to_cloud
||
77 is_cloud_dialog
|| print_with_privet
)) {
78 settings_
.set_dpi(kDefaultPdfDpi
);
79 // Cloud print should get size and rect from capabilities received from
81 gfx::Size
paper_size(GetPdfPaperSizeDeviceUnits());
82 gfx::Rect
paper_rect(0, 0, paper_size
.width(), paper_size
.height());
83 if (print_to_cloud
|| print_with_privet
) {
85 kCloudPrintMarginInch
* settings_
.device_units_per_inch(),
86 kCloudPrintMarginInch
* settings_
.device_units_per_inch());
88 DCHECK_EQ(settings_
.device_units_per_inch(), kDefaultPdfDpi
);
89 settings_
.SetPrinterPrintableArea(paper_size
, paper_rect
, true);
93 return UpdatePrinterSettings(open_in_external_preview
);
96 } // namespace printing