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"
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
) {
27 result
.width
= DeviceUnitsInPoints(print_size
.width(), device_units_per_inch
);
29 DeviceUnitsInPoints(print_size
.height(), device_units_per_inch
);
33 PP_Rect
PrintAreaToPPPrintArea(const gfx::Rect
& print_area
,
34 int32_t device_units_per_inch
) {
37 DeviceUnitsInPoints(print_area
.origin().x(), device_units_per_inch
);
39 DeviceUnitsInPoints(print_area
.origin().y(), device_units_per_inch
);
41 PrintSizeToPPPrintSize(print_area
.size(), device_units_per_inch
);
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()));
52 context
->UseDefaultSettings() != printing::PrintingContext::OK
) {
53 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(),
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(),
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
);
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
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
);
87 PepperPrintSettingsManager::Result
ComputeDefaultPrintSettings() {
88 return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(),
89 PP_ERROR_NOTSUPPORTED
);
95 void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings(
96 PepperPrintSettingsManager::Callback callback
) {
97 BrowserThread::PostTaskAndReplyWithResult(
100 base::Bind(ComputeDefaultPrintSettings
),
104 } // namespace content