OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/printing_context.h" | 5 #include "printing/printing_context.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "printing/page_setup.h" | 9 #include "printing/page_setup.h" |
10 #include "printing/page_size_margins.h" | 10 #include "printing/page_size_margins.h" |
11 #include "printing/print_settings_initializer.h" | 11 #include "printing/print_settings_initializer.h" |
12 | 12 |
13 namespace printing { | 13 namespace printing { |
14 | 14 |
15 PrintingContext::PrintingContext(const std::string& app_locale) | 15 PrintingContext::PrintingContext(const std::string& app_locale) |
16 : dialog_box_dismissed_(false), | 16 : dialog_box_dismissed_(false), |
17 in_print_job_(false), | 17 in_print_job_(false), |
18 abort_printing_(false), | 18 abort_printing_(false), |
19 app_locale_(app_locale) { | 19 app_locale_(app_locale) { |
20 } | 20 } |
21 | 21 |
22 PrintingContext::~PrintingContext() { | 22 PrintingContext::~PrintingContext() { |
23 } | 23 } |
24 | 24 |
25 void PrintingContext::set_margin_type(MarginType type) { | 25 void PrintingContext::set_margin_type(MarginType type) { |
26 DCHECK(type != CUSTOM_MARGINS); | 26 DCHECK(type != CUSTOM_MARGINS); |
27 settings_.margin_type = type; | 27 settings_.set_margin_type(type); |
28 } | 28 } |
29 | 29 |
30 void PrintingContext::ResetSettings() { | 30 void PrintingContext::ResetSettings() { |
31 ReleaseContext(); | 31 ReleaseContext(); |
32 | 32 |
33 settings_.Clear(); | 33 settings_.Clear(); |
34 | 34 |
35 in_print_job_ = false; | 35 in_print_job_ = false; |
36 dialog_box_dismissed_ = false; | 36 dialog_box_dismissed_ = false; |
37 abort_printing_ = false; | 37 abort_printing_ = false; |
38 } | 38 } |
39 | 39 |
40 PrintingContext::Result PrintingContext::OnError() { | 40 PrintingContext::Result PrintingContext::OnError() { |
41 ResetSettings(); | 41 ResetSettings(); |
42 return abort_printing_ ? CANCEL : FAILED; | 42 return abort_printing_ ? CANCEL : FAILED; |
43 } | 43 } |
44 | 44 |
45 PrintingContext::Result PrintingContext::UpdatePrintSettings( | 45 PrintingContext::Result PrintingContext::UpdatePrintSettings( |
46 const base::DictionaryValue& job_settings, | 46 const base::DictionaryValue& job_settings, |
47 const PageRanges& ranges) { | 47 const PageRanges& ranges) { |
48 ResetSettings(); | 48 ResetSettings(); |
49 | 49 |
50 job_settings.GetBoolean(kSettingHeaderFooterEnabled, | 50 if (settings_.dpi() == 0) |
51 &settings_.display_header_footer); | 51 UseDefaultSettings(); |
52 | 52 |
53 int margin_type = DEFAULT_MARGINS; | 53 if (!PrintSettingsInitializer::InitSettings(job_settings, ranges, |
54 if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || | 54 &settings_)) { |
55 (margin_type != DEFAULT_MARGINS && | 55 return OnError(); |
56 margin_type != NO_MARGINS && | |
57 margin_type != CUSTOM_MARGINS && | |
58 margin_type != PRINTABLE_AREA_MARGINS)) { | |
59 margin_type = DEFAULT_MARGINS; | |
60 } | |
61 settings_.margin_type = static_cast<MarginType>(margin_type); | |
62 | |
63 if (margin_type == CUSTOM_MARGINS) { | |
64 printing::PageSizeMargins page_size_margins; | |
65 GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); | |
66 | |
67 PageMargins margins_in_points; | |
68 margins_in_points.Clear(); | |
69 margins_in_points.top = page_size_margins.margin_top; | |
70 margins_in_points.bottom = page_size_margins.margin_bottom; | |
71 margins_in_points.left = page_size_margins.margin_left; | |
72 margins_in_points.right = page_size_margins.margin_right; | |
73 | |
74 settings_.SetCustomMargins(margins_in_points); | |
75 } | 56 } |
76 | 57 |
77 PrintingContext::Result result = UpdatePrinterSettings(job_settings, ranges); | 58 bool print_to_pdf = false; |
78 PrintSettingsInitializer::InitHeaderFooterStrings(job_settings, &settings_); | 59 bool is_cloud_dialog = false; |
79 | 60 |
80 job_settings.GetBoolean(kSettingShouldPrintBackgrounds, | 61 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || |
81 &settings_.should_print_backgrounds); | 62 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { |
82 job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, | 63 return OnError(); |
83 &settings_.selection_only); | 64 } |
84 return result; | 65 |
| 66 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); |
| 67 bool open_in_external_preview = |
| 68 job_settings.HasKey(kSettingOpenPDFInPreview); |
| 69 |
| 70 return |
| 71 UpdatePrinterSettings(print_to_pdf || is_cloud_dialog || print_to_cloud, |
| 72 open_in_external_preview); |
85 } | 73 } |
86 | 74 |
87 } // namespace printing | 75 } // namespace printing |
OLD | NEW |