Revert of Re-enable ScreenOrientationListenerTest.java and run locking on UI thread...
[chromium-blink-merge.git] / printing / printing_context.cc
blobbcafb40ad82a37eaa2b73683f26f607751277075
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 ResetSettings();
48 return abort_printing_ ? CANCEL : FAILED;
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 return UpdatePrintSettings(*pdf_settings);
69 PrintingContext::Result PrintingContext::UpdatePrintSettings(
70 const base::DictionaryValue& job_settings) {
71 ResetSettings();
73 if (!PrintSettingsFromJobSettings(job_settings, &settings_)) {
74 NOTREACHED();
75 return OnError();
78 bool print_to_pdf = false;
79 bool is_cloud_dialog = false;
80 bool print_with_privet = false;
82 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
83 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) ||
84 !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) {
85 NOTREACHED();
86 return OnError();
89 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
90 bool open_in_external_preview =
91 job_settings.HasKey(kSettingOpenPDFInPreview);
93 if (!open_in_external_preview && (print_to_pdf || print_to_cloud ||
94 is_cloud_dialog || print_with_privet)) {
95 settings_.set_dpi(kDefaultPdfDpi);
96 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
97 if (!settings_.requested_media().size_microns.IsEmpty()) {
98 float deviceMicronsPerDeviceUnit =
99 (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch();
100 paper_size = gfx::Size(settings_.requested_media().size_microns.width() /
101 deviceMicronsPerDeviceUnit,
102 settings_.requested_media().size_microns.height() /
103 deviceMicronsPerDeviceUnit);
105 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
106 if (print_to_cloud || print_with_privet) {
107 paper_rect.Inset(
108 kCloudPrintMarginInch * settings_.device_units_per_inch(),
109 kCloudPrintMarginInch * settings_.device_units_per_inch());
111 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
112 return OK;
115 return UpdatePrinterSettings(open_in_external_preview);
118 } // namespace printing