Chromium Code Reviews| Index: content/browser/renderer_host/pepper/pepper_print_settings_manager.cc |
| diff --git a/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc b/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..73271139b343af4e9f3b3d1bb8c3ff4b7dacc697 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" |
| + |
| +#include "content/public/browser/browser_thread.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "printing/printing_context.h" |
| +#include "printing/units.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +#if defined(ENABLE_PRINTING) |
| +// Print units conversion functions. |
| +int32_t DeviceUnitsInPoints(int32_t device_units, |
| + int32_t device_units_per_inch) { |
| + |
|
brettw
2012/09/18 21:41:38
Remove blank.
raymes
2012/09/18 23:43:57
Done.
|
| + return printing::ConvertUnit(device_units, printing::kPointsPerInch, |
| + device_units_per_inch); |
| +} |
| + |
| +PP_Size PrintSizeToPPPrintSize(const gfx::Size& print_size, |
| + int32_t device_units_per_inch) { |
| + PP_Size result; |
| + result.width = DeviceUnitsInPoints(print_size.width(), device_units_per_inch); |
| + result.height = DeviceUnitsInPoints(print_size.height(), |
| + device_units_per_inch); |
| + return result; |
| +} |
| + |
| +PP_Rect PrintAreaToPPPrintArea(const gfx::Rect& print_area, |
| + int32_t device_units_per_inch) { |
| + PP_Rect result; |
| + result.point.x = DeviceUnitsInPoints(print_area.origin().x(), |
| + device_units_per_inch); |
| + result.point.y = DeviceUnitsInPoints(print_area.origin().y(), |
| + device_units_per_inch); |
| + result.size = PrintSizeToPPPrintSize(print_area.size(), |
| + device_units_per_inch); |
| + return result; |
| +} |
| + |
| +PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { |
| + // This function should run on the UI thread because |PrintingContext| methods |
| + // call into platform APIs. |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + scoped_ptr<printing::PrintingContext> context( |
| + printing::PrintingContext::Create("")); |
|
brettw
2012/09/18 21:41:38
"" -> std::string()
raymes
2012/09/18 23:43:57
Done.
|
| + if (!context.get()) |
|
brettw
2012/09/18 21:41:38
Need {} for multiline bodies as per style guide.
raymes
2012/09/18 23:43:57
Done.
|
| + return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), |
| + PP_ERROR_FAILED); |
| + context->UseDefaultSettings(); |
| + const printing::PrintSettings& print_settings = context->settings(); |
| + const printing::PageSetup& page_setup = |
| + print_settings.page_setup_device_units(); |
| + int device_units_per_inch = print_settings.device_units_per_inch(); |
| + PP_PrintSettings_Dev settings; |
| + settings.printable_area = PrintAreaToPPPrintArea( |
| + page_setup.printable_area(), device_units_per_inch); |
| + settings.content_area = PrintAreaToPPPrintArea( |
| + page_setup.content_area(), device_units_per_inch); |
| + settings.paper_size = PrintSizeToPPPrintSize( |
| + page_setup.physical_size(), device_units_per_inch); |
| + settings.dpi = print_settings.dpi(); |
| + |
| + // The remainder of the attributes are hard-coded to the defaults as set |
| + // elsewhere. |
| + settings.orientation = PP_PRINTORIENTATION_NORMAL; |
| + settings.grayscale = PP_FALSE; |
| + settings.print_scaling_option = PP_PRINTSCALINGOPTION_SOURCE_SIZE; |
| + |
| + // TODO(raymes): Should be computed in the same way as |
| + // |PluginInstance::GetPreferredPrintOutputFormat|. |
| + // |PP_PRINTOUTPUTFORMAT_PDF| is currently the only supported format though, |
| + // so just make it the default. |
| + settings.format = PP_PRINTOUTPUTFORMAT_PDF; |
| + return PepperPrintSettingsManager::Result(settings, PP_OK); |
| +} |
| +#else |
| +PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { |
| + return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), |
| + PP_ERROR_NOTSUPPORTED); |
| +} |
| +#endif |
| + |
| +} // namespace |
|
brettw
2012/09/18 21:41:38
two spaces before //
raymes
2012/09/18 23:43:57
Done.
|
| + |
| +void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings( |
| + PepperPrintSettingsManager::Callback callback) { |
| + BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, |
| + base::Bind(ComputeDefaultPrintSettings), callback); |
| +} |
| + |
| +MockPepperPrintSettingsManager::MockPepperPrintSettingsManager( |
| + const PP_PrintSettings_Dev& settings) |
| + : settings_(settings) { |
| +} |
| + |
| +void MockPepperPrintSettingsManager::GetDefaultPrintSettings( |
| + PepperPrintSettingsManager::Callback callback) { |
| + callback.Run(PepperPrintSettingsManager::Result(settings_, PP_OK)); |
| +} |
| + |
| +} // namespace content |