Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "printing/printing_context.h" |
| 6 | |
| 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" |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 12 | #include "printing/units.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | |
| 14 | namespace printing { |
| 15 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 16 | namespace { |
| 17 | const float kCloudPrintMarginInch = 0.25; |
| 18 | } |
| 19 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | PrintingContext::PrintingContext(const std::string& app_locale) |
| 21 | : dialog_box_dismissed_(false), |
| 22 | in_print_job_(false), |
| 23 | abort_printing_(false), |
| 24 | app_locale_(app_locale) { |
| 25 | } |
| 26 | |
| 27 | PrintingContext::~PrintingContext() { |
| 28 | } |
| 29 | |
| 30 | void PrintingContext::set_margin_type(MarginType type) { |
| 31 | DCHECK(type != CUSTOM_MARGINS); |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 32 | settings_.set_margin_type(type); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void PrintingContext::ResetSettings() { |
| 36 | ReleaseContext(); |
| 37 | |
| 38 | settings_.Clear(); |
| 39 | |
| 40 | in_print_job_ = false; |
| 41 | dialog_box_dismissed_ = false; |
| 42 | abort_printing_ = false; |
| 43 | } |
| 44 | |
| 45 | PrintingContext::Result PrintingContext::OnError() { |
| 46 | ResetSettings(); |
| 47 | return abort_printing_ ? CANCEL : FAILED; |
| 48 | } |
| 49 | |
| 50 | PrintingContext::Result PrintingContext::UpdatePrintSettings( |
| 51 | const base::DictionaryValue& job_settings, |
| 52 | const PageRanges& ranges) { |
| 53 | ResetSettings(); |
| 54 | |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 55 | if (!PrintSettingsInitializer::InitSettings(job_settings, ranges, |
| 56 | &settings_)) { |
| 57 | NOTREACHED(); |
| 58 | return OnError(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 61 | bool print_to_pdf = false; |
| 62 | bool is_cloud_dialog = false; |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 63 | bool print_with_privet = false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 64 | |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 65 | if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 66 | !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) || |
| 67 | !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) { |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 68 | NOTREACHED(); |
| 69 | return OnError(); |
| 70 | } |
| 71 | |
| 72 | bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); |
| 73 | bool open_in_external_preview = |
| 74 | job_settings.HasKey(kSettingOpenPDFInPreview); |
| 75 | |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 76 | if (!open_in_external_preview && (print_to_pdf || print_to_cloud || |
| 77 | is_cloud_dialog || print_with_privet)) { |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 78 | settings_.set_dpi(kDefaultPdfDpi); |
| 79 | // Cloud print should get size and rect from capabilities received from |
| 80 | // server. |
| 81 | gfx::Size paper_size(GetPdfPaperSizeDeviceUnits()); |
| 82 | gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height()); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 83 | if (print_to_cloud || print_with_privet) { |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 84 | paper_rect.Inset( |
| 85 | kCloudPrintMarginInch * settings_.device_units_per_inch(), |
| 86 | kCloudPrintMarginInch * settings_.device_units_per_inch()); |
| 87 | } |
| 88 | DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi); |
| 89 | settings_.SetPrinterPrintableArea(paper_size, paper_rect, true); |
| 90 | return OK; |
| 91 | } |
| 92 | |
| 93 | return UpdatePrinterSettings(open_in_external_preview); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace printing |