Fix DisownOpener and related tests.
[chromium-blink-merge.git] / printing / printing_context.cc
blobc7f22a5003d30292edc910e508f677cd00c8e122
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(const std::string& app_locale)
22 : dialog_box_dismissed_(false),
23 in_print_job_(false),
24 abort_printing_(false),
25 app_locale_(app_locale) {
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 dialog_box_dismissed_ = false;
43 abort_printing_ = false;
46 PrintingContext::Result PrintingContext::OnError() {
47 Result result = abort_printing_ ? CANCEL : FAILED;
48 ResetSettings();
49 return result;
52 PrintingContext::Result PrintingContext::UsePdfSettings() {
53 scoped_ptr<base::DictionaryValue> pdf_settings(new base::DictionaryValue);
54 pdf_settings->SetBoolean(kSettingHeaderFooterEnabled, false);
55 pdf_settings->SetBoolean(kSettingShouldPrintBackgrounds, false);
56 pdf_settings->SetBoolean(kSettingShouldPrintSelectionOnly, false);
57 pdf_settings->SetInteger(kSettingMarginsType, printing::NO_MARGINS);
58 pdf_settings->SetBoolean(kSettingCollate, true);
59 pdf_settings->SetInteger(kSettingCopies, 1);
60 pdf_settings->SetInteger(kSettingColor, printing::COLOR);
61 pdf_settings->SetInteger(kSettingDuplexMode, printing::SIMPLEX);
62 pdf_settings->SetBoolean(kSettingLandscape, false);
63 pdf_settings->SetString(kSettingDeviceName, "");
64 pdf_settings->SetBoolean(kSettingPrintToPDF, true);
65 pdf_settings->SetBoolean(kSettingCloudPrintDialog, false);
66 pdf_settings->SetBoolean(kSettingPrintWithPrivet, 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;
83 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
84 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) ||
85 !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) {
86 NOTREACHED();
87 return OnError();
90 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
91 bool open_in_external_preview =
92 job_settings.HasKey(kSettingOpenPDFInPreview);
94 if (!open_in_external_preview && (print_to_pdf || print_to_cloud ||
95 is_cloud_dialog || print_with_privet)) {
96 settings_.set_dpi(kDefaultPdfDpi);
97 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
98 if (!settings_.requested_media().size_microns.IsEmpty()) {
99 float deviceMicronsPerDeviceUnit =
100 (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch();
101 paper_size = gfx::Size(settings_.requested_media().size_microns.width() /
102 deviceMicronsPerDeviceUnit,
103 settings_.requested_media().size_microns.height() /
104 deviceMicronsPerDeviceUnit);
106 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
107 if (print_to_cloud || print_with_privet) {
108 paper_rect.Inset(
109 kCloudPrintMarginInch * settings_.device_units_per_inch(),
110 kCloudPrintMarginInch * settings_.device_units_per_inch());
112 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
113 return OK;
116 return UpdatePrinterSettings(open_in_external_preview);
119 } // namespace printing