Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_print_settings_manager.cc
blobde5f5fec949025bb431c6533e9793172f106e8e7
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.
5 #include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h"
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"
12 namespace content {
14 namespace {
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(
21 device_units, device_units_per_inch, printing::kPointsPerInch);
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 =
29 DeviceUnitsInPoints(print_size.height(), device_units_per_inch);
30 return result;
33 PP_Rect PrintAreaToPPPrintArea(const gfx::Rect& print_area,
34 int32_t device_units_per_inch) {
35 PP_Rect result;
36 result.point.x =
37 DeviceUnitsInPoints(print_area.origin().x(), device_units_per_inch);
38 result.point.y =
39 DeviceUnitsInPoints(print_area.origin().y(), device_units_per_inch);
40 result.size =
41 PrintSizeToPPPrintSize(print_area.size(), device_units_per_inch);
42 return result;
45 PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() {
46 // This function should run on the UI thread because |PrintingContext| methods
47 // call into platform APIs.
48 DCHECK_CURRENTLY_ON(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);
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);
64 PP_PrintSettings_Dev settings;
65 settings.printable_area = PrintAreaToPPPrintArea(page_setup.printable_area(),
66 device_units_per_inch);
67 settings.content_area =
68 PrintAreaToPPPrintArea(page_setup.content_area(), device_units_per_inch);
69 settings.paper_size =
70 PrintSizeToPPPrintSize(page_setup.physical_size(), device_units_per_inch);
71 settings.dpi = print_settings.dpi();
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;
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);
86 #else
87 PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() {
88 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(),
89 PP_ERROR_NOTSUPPORTED);
91 #endif
93 } // namespace
95 void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings(
96 PepperPrintSettingsManager::Callback callback) {
97 BrowserThread::PostTaskAndReplyWithResult(
98 BrowserThread::UI,
99 FROM_HERE,
100 base::Bind(ComputeDefaultPrintSettings),
101 callback);
104 } // namespace content