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_FULL_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, device_units_per_inch, 21 printing::kPointsPerInch); 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 context->UseDefaultSettings() != printing::PrintingContext::OK) { 53 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), 54 PP_ERROR_FAILED); 55 } 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 if (device_units_per_inch <= 0) { 61 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), 62 PP_ERROR_FAILED); 63 } 64 PP_PrintSettings_Dev settings; 65 settings.printable_area = PrintAreaToPPPrintArea( 66 page_setup.printable_area(), device_units_per_inch); 67 settings.content_area = PrintAreaToPPPrintArea( 68 page_setup.content_area(), device_units_per_inch); 69 settings.paper_size = PrintSizeToPPPrintSize( 70 page_setup.physical_size(), device_units_per_inch); 71 settings.dpi = print_settings.dpi(); 72 73 // The remainder of the attributes are hard-coded to the defaults as set 74 // elsewhere. 75 settings.orientation = PP_PRINTORIENTATION_NORMAL; 76 settings.grayscale = PP_FALSE; 77 settings.print_scaling_option = PP_PRINTSCALINGOPTION_SOURCE_SIZE; 78 79 // TODO(raymes): Should be computed in the same way as 80 // |PluginInstance::GetPreferredPrintOutputFormat|. 81 // |PP_PRINTOUTPUTFORMAT_PDF| is currently the only supported format though, 82 // so just make it the default. 83 settings.format = PP_PRINTOUTPUTFORMAT_PDF; 84 return PepperPrintSettingsManager::Result(settings, PP_OK); 85 } 86 #else 87 PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { 88 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), 89 PP_ERROR_NOTSUPPORTED); 90 } 91 #endif 92 93 } // namespace 94 95 void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings( 96 PepperPrintSettingsManager::Callback callback) { 97 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, 98 base::Bind(ComputeDefaultPrintSettings), callback); 99 } 100 101 } // namespace content 102