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_no_system_dialog.h" |
| 6 | |
| 7 | #include <unicode/ulocdata.h> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/values.h" |
| 11 | #include "printing/metafile.h" |
| 12 | #include "printing/print_job_constants.h" |
| 13 | #include "printing/units.h" |
| 14 | |
| 15 | namespace printing { |
| 16 | |
| 17 | // static |
| 18 | PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 19 | return static_cast<PrintingContext*>( |
| 20 | new PrintingContextNoSystemDialog(app_locale)); |
| 21 | } |
| 22 | |
| 23 | PrintingContextNoSystemDialog::PrintingContextNoSystemDialog( |
| 24 | const std::string& app_locale) : PrintingContext(app_locale) { |
| 25 | } |
| 26 | |
| 27 | PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() { |
| 28 | ReleaseContext(); |
| 29 | } |
| 30 | |
| 31 | void PrintingContextNoSystemDialog::AskUserForSettings( |
| 32 | gfx::NativeView parent_view, |
| 33 | int max_pages, |
| 34 | bool has_selection, |
| 35 | const PrintSettingsCallback& callback) { |
| 36 | // We don't want to bring up a dialog here. Ever. Just signal the callback. |
| 37 | callback.Run(OK); |
| 38 | } |
| 39 | |
| 40 | PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() { |
| 41 | DCHECK(!in_print_job_); |
| 42 | |
| 43 | ResetSettings(); |
| 44 | // TODO(abodenha): Fetch these settings from the OS where possible. See |
| 45 | // bug 102583. |
| 46 | // TODO(sanjeevr): We need a better feedback loop between the cloud print |
| 47 | // dialog and this code. |
| 48 | int dpi = 300; |
| 49 | gfx::Size physical_size_device_units; |
| 50 | gfx::Rect printable_area_device_units; |
| 51 | int32_t width = 0; |
| 52 | int32_t height = 0; |
| 53 | UErrorCode error = U_ZERO_ERROR; |
| 54 | ulocdata_getPaperSize(app_locale_.c_str(), &height, &width, &error); |
| 55 | if (error > U_ZERO_ERROR) { |
| 56 | // If the call failed, assume a paper size of 8.5 x 11 inches. |
| 57 | LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: " |
| 58 | << error; |
| 59 | width = static_cast<int>(8.5 * dpi); |
| 60 | height = static_cast<int>(11 * dpi); |
| 61 | } else { |
| 62 | // ulocdata_getPaperSize returns the width and height in mm. |
| 63 | // Convert this to pixels based on the dpi. |
| 64 | width = static_cast<int>(ConvertUnitDouble(width, 25.4, 1.0) * dpi); |
| 65 | height = static_cast<int>(ConvertUnitDouble(height, 25.4, 1.0) * dpi); |
| 66 | } |
| 67 | |
| 68 | physical_size_device_units.SetSize(width, height); |
| 69 | |
| 70 | // Assume full page is printable for now. |
| 71 | printable_area_device_units.SetRect(0, 0, width, height); |
| 72 | |
| 73 | settings_.set_dpi(dpi); |
| 74 | settings_.SetPrinterPrintableArea(physical_size_device_units, |
| 75 | printable_area_device_units, |
| 76 | dpi); |
| 77 | |
| 78 | return OK; |
| 79 | } |
| 80 | |
| 81 | PrintingContext::Result PrintingContextNoSystemDialog::UpdatePrinterSettings( |
| 82 | const DictionaryValue& job_settings, const PageRanges& ranges) { |
| 83 | bool landscape = false; |
| 84 | |
| 85 | if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) |
| 86 | return OnError(); |
| 87 | |
| 88 | if (settings_.dpi() == 0) |
| 89 | UseDefaultSettings(); |
| 90 | |
| 91 | settings_.SetOrientation(landscape); |
| 92 | settings_.ranges = ranges; |
| 93 | |
| 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | PrintingContext::Result PrintingContextNoSystemDialog::InitWithSettings( |
| 98 | const PrintSettings& settings) { |
| 99 | DCHECK(!in_print_job_); |
| 100 | |
| 101 | settings_ = settings; |
| 102 | |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | PrintingContext::Result PrintingContextNoSystemDialog::NewDocument( |
| 107 | const string16& document_name) { |
| 108 | DCHECK(!in_print_job_); |
| 109 | in_print_job_ = true; |
| 110 | |
| 111 | return OK; |
| 112 | } |
| 113 | |
| 114 | PrintingContext::Result PrintingContextNoSystemDialog::NewPage() { |
| 115 | if (abort_printing_) |
| 116 | return CANCEL; |
| 117 | DCHECK(in_print_job_); |
| 118 | |
| 119 | // Intentional No-op. |
| 120 | |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | PrintingContext::Result PrintingContextNoSystemDialog::PageDone() { |
| 125 | if (abort_printing_) |
| 126 | return CANCEL; |
| 127 | DCHECK(in_print_job_); |
| 128 | |
| 129 | // Intentional No-op. |
| 130 | |
| 131 | return OK; |
| 132 | } |
| 133 | |
| 134 | PrintingContext::Result PrintingContextNoSystemDialog::DocumentDone() { |
| 135 | if (abort_printing_) |
| 136 | return CANCEL; |
| 137 | DCHECK(in_print_job_); |
| 138 | |
| 139 | ResetSettings(); |
| 140 | return OK; |
| 141 | } |
| 142 | |
| 143 | void PrintingContextNoSystemDialog::Cancel() { |
| 144 | abort_printing_ = true; |
| 145 | in_print_job_ = false; |
| 146 | } |
| 147 | |
| 148 | void PrintingContextNoSystemDialog::ReleaseContext() { |
| 149 | // Intentional No-op. |
| 150 | } |
| 151 | |
| 152 | gfx::NativeDrawingContext PrintingContextNoSystemDialog::context() const { |
| 153 | // Intentional No-op. |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | } // namespace printing |
| 158 | |