Disable WebContentsViewAuraTest.OverscrollNavigation which is flaky under both Win...
[chromium-blink-merge.git] / printing / printing_context.cc
blob817d59c93b283cc974b581b78e03cd364356ef01
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_initializer.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::UpdatePrintSettings(
52 const base::DictionaryValue& job_settings,
53 const PageRanges& ranges) {
54 ResetSettings();
56 if (!PrintSettingsInitializer::InitSettings(job_settings, ranges,
57 &settings_)) {
58 NOTREACHED();
59 return OnError();
62 bool print_to_pdf = false;
63 bool is_cloud_dialog = false;
64 bool print_with_privet = false;
66 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
67 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) ||
68 !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) {
69 NOTREACHED();
70 return OnError();
73 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
74 bool open_in_external_preview =
75 job_settings.HasKey(kSettingOpenPDFInPreview);
77 if (!open_in_external_preview && (print_to_pdf || print_to_cloud ||
78 is_cloud_dialog || print_with_privet)) {
79 settings_.set_dpi(kDefaultPdfDpi);
80 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
81 const base::DictionaryValue* media_size = NULL;
82 if (job_settings.GetDictionary(kSettingMediaSize, &media_size)) {
83 int width_microns = 0;
84 int height_microns = 0;
85 if (media_size->GetInteger(kSettingMediaSizeWidthMicrons,
86 &width_microns) &&
87 media_size->GetInteger(kSettingMediaSizeHeightMicrons,
88 &height_microns)) {
89 float deviceMicronsPerDeviceUnit =
90 (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch();
91 paper_size = gfx::Size(width_microns / deviceMicronsPerDeviceUnit,
92 height_microns / deviceMicronsPerDeviceUnit);
95 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
96 if (print_to_cloud || print_with_privet) {
97 paper_rect.Inset(
98 kCloudPrintMarginInch * settings_.device_units_per_inch(),
99 kCloudPrintMarginInch * settings_.device_units_per_inch());
101 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
102 return OK;
105 return UpdatePrinterSettings(open_in_external_preview);
108 } // namespace printing