OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" |
| 6 |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "printing/printing_context.h" |
| 10 #include "printing/units.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 namespace { |
| 15 |
| 16 #if defined(ENABLE_PRINTING) |
| 17 // Print units conversion functions. |
| 18 int32_t DeviceUnitsInPoints(int32_t device_units, |
| 19 int32_t device_units_per_inch) { |
| 20 return printing::ConvertUnit(device_units, printing::kPointsPerInch, |
| 21 device_units_per_inch); |
| 22 } |
| 23 |
| 24 PP_Size PrintSizeToPPPrintSize(const gfx::Size& print_size, |
| 25 int32_t device_units_per_inch) { |
| 26 PP_Size result; |
| 27 result.width = DeviceUnitsInPoints(print_size.width(), device_units_per_inch); |
| 28 result.height = DeviceUnitsInPoints(print_size.height(), |
| 29 device_units_per_inch); |
| 30 return result; |
| 31 } |
| 32 |
| 33 PP_Rect PrintAreaToPPPrintArea(const gfx::Rect& print_area, |
| 34 int32_t device_units_per_inch) { |
| 35 PP_Rect result; |
| 36 result.point.x = DeviceUnitsInPoints(print_area.origin().x(), |
| 37 device_units_per_inch); |
| 38 result.point.y = DeviceUnitsInPoints(print_area.origin().y(), |
| 39 device_units_per_inch); |
| 40 result.size = PrintSizeToPPPrintSize(print_area.size(), |
| 41 device_units_per_inch); |
| 42 return result; |
| 43 } |
| 44 |
| 45 PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { |
| 46 // This function should run on the UI thread because |PrintingContext| methods |
| 47 // call into platform APIs. |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 scoped_ptr<printing::PrintingContext> context( |
| 50 printing::PrintingContext::Create(std::string())); |
| 51 if (!context.get()) { |
| 52 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), |
| 53 PP_ERROR_FAILED); |
| 54 } |
| 55 context->UseDefaultSettings(); |
| 56 const printing::PrintSettings& print_settings = context->settings(); |
| 57 const printing::PageSetup& page_setup = |
| 58 print_settings.page_setup_device_units(); |
| 59 int device_units_per_inch = print_settings.device_units_per_inch(); |
| 60 PP_PrintSettings_Dev settings; |
| 61 settings.printable_area = PrintAreaToPPPrintArea( |
| 62 page_setup.printable_area(), device_units_per_inch); |
| 63 settings.content_area = PrintAreaToPPPrintArea( |
| 64 page_setup.content_area(), device_units_per_inch); |
| 65 settings.paper_size = PrintSizeToPPPrintSize( |
| 66 page_setup.physical_size(), device_units_per_inch); |
| 67 settings.dpi = print_settings.dpi(); |
| 68 |
| 69 // The remainder of the attributes are hard-coded to the defaults as set |
| 70 // elsewhere. |
| 71 settings.orientation = PP_PRINTORIENTATION_NORMAL; |
| 72 settings.grayscale = PP_FALSE; |
| 73 settings.print_scaling_option = PP_PRINTSCALINGOPTION_SOURCE_SIZE; |
| 74 |
| 75 // TODO(raymes): Should be computed in the same way as |
| 76 // |PluginInstance::GetPreferredPrintOutputFormat|. |
| 77 // |PP_PRINTOUTPUTFORMAT_PDF| is currently the only supported format though, |
| 78 // so just make it the default. |
| 79 settings.format = PP_PRINTOUTPUTFORMAT_PDF; |
| 80 return PepperPrintSettingsManager::Result(settings, PP_OK); |
| 81 } |
| 82 #else |
| 83 PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { |
| 84 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), |
| 85 PP_ERROR_NOTSUPPORTED); |
| 86 } |
| 87 #endif |
| 88 |
| 89 } // namespace |
| 90 |
| 91 void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings( |
| 92 PepperPrintSettingsManager::Callback callback) { |
| 93 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, |
| 94 base::Bind(ComputeDefaultPrintSettings), callback); |
| 95 } |
| 96 |
| 97 } // namespace content |
OLD | NEW |