1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsPrintSettingsService.h"
8 #include "mozilla/embedding/PPrintingTypes.h"
9 #include "mozilla/layout/RemotePrintJobChild.h"
10 #include "mozilla/RefPtr.h"
12 #include "nsIPrinterList.h"
13 #include "nsReadableUtils.h"
14 #include "nsPrintSettingsImpl.h"
15 #include "nsServiceManagerUtils.h"
20 #include "nsXULAppAPI.h"
22 #include "nsIStringEnumerator.h"
24 #include "mozilla/StaticPrefs_print.h"
25 #include "mozilla/Preferences.h"
26 #include "nsPrintfCString.h"
28 using namespace mozilla
;
29 using namespace mozilla::embedding
;
31 typedef mozilla::layout::RemotePrintJobChild RemotePrintJobChild
;
33 NS_IMPL_ISUPPORTS(nsPrintSettingsService
, nsIPrintSettingsService
)
36 static const char kMarginTop
[] = "print_margin_top";
37 static const char kMarginLeft
[] = "print_margin_left";
38 static const char kMarginBottom
[] = "print_margin_bottom";
39 static const char kMarginRight
[] = "print_margin_right";
40 static const char kEdgeTop
[] = "print_edge_top";
41 static const char kEdgeLeft
[] = "print_edge_left";
42 static const char kEdgeBottom
[] = "print_edge_bottom";
43 static const char kEdgeRight
[] = "print_edge_right";
45 static const char kUnwriteableMarginTopTwips
[] =
46 "print_unwriteable_margin_top_twips";
47 static const char kUnwriteableMarginLeftTwips
[] =
48 "print_unwriteable_margin_left_twips";
49 static const char kUnwriteableMarginBottomTwips
[] =
50 "print_unwriteable_margin_bottom_twips";
51 static const char kUnwriteableMarginRightTwips
[] =
52 "print_unwriteable_margin_right_twips";
54 // These are legacy versions of the above UnwriteableMargin prefs. The new ones,
55 // which are in twips, were introduced to more accurately record the values.
56 static const char kUnwriteableMarginTop
[] = "print_unwriteable_margin_top";
57 static const char kUnwriteableMarginLeft
[] = "print_unwriteable_margin_left";
58 static const char kUnwriteableMarginBottom
[] =
59 "print_unwriteable_margin_bottom";
60 static const char kUnwriteableMarginRight
[] = "print_unwriteable_margin_right";
62 // Prefs for Print Options
63 static const char kPrintHeaderStrLeft
[] = "print_headerleft";
64 static const char kPrintHeaderStrCenter
[] = "print_headercenter";
65 static const char kPrintHeaderStrRight
[] = "print_headerright";
66 static const char kPrintFooterStrLeft
[] = "print_footerleft";
67 static const char kPrintFooterStrCenter
[] = "print_footercenter";
68 static const char kPrintFooterStrRight
[] = "print_footerright";
71 static const char kPrintReversed
[] = "print_reversed";
72 static const char kPrintInColor
[] = "print_in_color";
73 static const char kPrintPaperId
[] = "print_paper_id";
74 static const char kPrintPaperSizeUnit
[] = "print_paper_size_unit";
75 static const char kPrintPaperWidth
[] = "print_paper_width";
76 static const char kPrintPaperHeight
[] = "print_paper_height";
77 static const char kPrintOrientation
[] = "print_orientation";
78 static const char kPrinterName
[] = "print_printer";
79 static const char kPrintToFile
[] = "print_to_file";
80 static const char kPrintToFileName
[] = "print_to_filename";
81 static const char kPrintPageDelay
[] = "print_page_delay";
82 static const char kPrintBGColors
[] = "print_bgcolor";
83 static const char kPrintBGImages
[] = "print_bgimages";
84 static const char kPrintShrinkToFit
[] = "print_shrink_to_fit";
85 static const char kPrintScaling
[] = "print_scaling";
86 static const char kPrintDuplex
[] = "print_duplex";
88 static const char kJustLeft
[] = "left";
89 static const char kJustCenter
[] = "center";
90 static const char kJustRight
[] = "right";
92 #define NS_PRINTER_LIST_CONTRACTID "@mozilla.org/gfx/printerlist;1"
94 nsresult
nsPrintSettingsService::Init() { return NS_OK
; }
97 nsPrintSettingsService::SerializeToPrintData(nsIPrintSettings
* aSettings
,
99 aSettings
->GetPageRanges(data
->pageRanges());
101 aSettings
->GetEdgeTop(&data
->edgeTop());
102 aSettings
->GetEdgeLeft(&data
->edgeLeft());
103 aSettings
->GetEdgeBottom(&data
->edgeBottom());
104 aSettings
->GetEdgeRight(&data
->edgeRight());
106 aSettings
->GetMarginTop(&data
->marginTop());
107 aSettings
->GetMarginLeft(&data
->marginLeft());
108 aSettings
->GetMarginBottom(&data
->marginBottom());
109 aSettings
->GetMarginRight(&data
->marginRight());
110 aSettings
->GetUnwriteableMarginTop(&data
->unwriteableMarginTop());
111 aSettings
->GetUnwriteableMarginLeft(&data
->unwriteableMarginLeft());
112 aSettings
->GetUnwriteableMarginBottom(&data
->unwriteableMarginBottom());
113 aSettings
->GetUnwriteableMarginRight(&data
->unwriteableMarginRight());
115 aSettings
->GetScaling(&data
->scaling());
117 data
->printBGColors() = aSettings
->GetPrintBGColors();
118 data
->printBGImages() = aSettings
->GetPrintBGImages();
120 data
->honorPageRuleMargins() = aSettings
->GetHonorPageRuleMargins();
121 data
->showMarginGuides() = aSettings
->GetShowMarginGuides();
122 data
->printSelectionOnly() = aSettings
->GetPrintSelectionOnly();
124 aSettings
->GetTitle(data
->title());
125 aSettings
->GetDocURL(data
->docURL());
127 aSettings
->GetHeaderStrLeft(data
->headerStrLeft());
128 aSettings
->GetHeaderStrCenter(data
->headerStrCenter());
129 aSettings
->GetHeaderStrRight(data
->headerStrRight());
131 aSettings
->GetFooterStrLeft(data
->footerStrLeft());
132 aSettings
->GetFooterStrCenter(data
->footerStrCenter());
133 aSettings
->GetFooterStrRight(data
->footerStrRight());
135 aSettings
->GetPrintSilent(&data
->printSilent());
136 aSettings
->GetShrinkToFit(&data
->shrinkToFit());
138 aSettings
->GetPaperId(data
->paperId());
139 aSettings
->GetPaperWidth(&data
->paperWidth());
140 aSettings
->GetPaperHeight(&data
->paperHeight());
141 aSettings
->GetPaperSizeUnit(&data
->paperSizeUnit());
143 aSettings
->GetPrintReversed(&data
->printReversed());
144 aSettings
->GetPrintInColor(&data
->printInColor());
145 aSettings
->GetOrientation(&data
->orientation());
147 aSettings
->GetNumCopies(&data
->numCopies());
148 aSettings
->GetNumPagesPerSheet(&data
->numPagesPerSheet());
150 data
->outputDestination() = aSettings
->GetOutputDestination();
152 data
->outputFormat() = aSettings
->GetOutputFormat();
153 data
->printPageDelay() = aSettings
->GetPrintPageDelay();
154 data
->resolution() = aSettings
->GetResolution();
155 data
->duplex() = aSettings
->GetDuplex();
157 aSettings
->GetIsInitializedFromPrinter(&data
->isInitializedFromPrinter());
158 aSettings
->GetIsInitializedFromPrefs(&data
->isInitializedFromPrefs());
160 // Initialize the platform-specific values that don't
161 // default-initialize, so that we don't send uninitialized data over
162 // IPC (which leads to valgrind warnings, and, for bools, fatal
164 // data->driverName() default-initializes
165 // data->deviceName() default-initializes
166 // data->GTKPrintSettings() default-initializes
172 nsPrintSettingsService::DeserializeToPrintSettings(const PrintData
& data
,
173 nsIPrintSettings
* settings
) {
174 settings
->SetPageRanges(data
.pageRanges());
176 settings
->SetEdgeTop(data
.edgeTop());
177 settings
->SetEdgeLeft(data
.edgeLeft());
178 settings
->SetEdgeBottom(data
.edgeBottom());
179 settings
->SetEdgeRight(data
.edgeRight());
181 settings
->SetMarginTop(data
.marginTop());
182 settings
->SetMarginLeft(data
.marginLeft());
183 settings
->SetMarginBottom(data
.marginBottom());
184 settings
->SetMarginRight(data
.marginRight());
185 settings
->SetUnwriteableMarginTop(data
.unwriteableMarginTop());
186 settings
->SetUnwriteableMarginLeft(data
.unwriteableMarginLeft());
187 settings
->SetUnwriteableMarginBottom(data
.unwriteableMarginBottom());
188 settings
->SetUnwriteableMarginRight(data
.unwriteableMarginRight());
190 settings
->SetScaling(data
.scaling());
192 settings
->SetPrintBGColors(data
.printBGColors());
193 settings
->SetPrintBGImages(data
.printBGImages());
194 settings
->SetHonorPageRuleMargins(data
.honorPageRuleMargins());
195 settings
->SetShowMarginGuides(data
.showMarginGuides());
196 settings
->SetPrintSelectionOnly(data
.printSelectionOnly());
198 settings
->SetTitle(data
.title());
199 settings
->SetDocURL(data
.docURL());
202 settings
->SetHeaderStrLeft(data
.headerStrLeft());
203 settings
->SetHeaderStrCenter(data
.headerStrCenter());
204 settings
->SetHeaderStrRight(data
.headerStrRight());
207 settings
->SetFooterStrLeft(data
.footerStrLeft());
208 settings
->SetFooterStrCenter(data
.footerStrCenter());
209 settings
->SetFooterStrRight(data
.footerStrRight());
211 settings
->SetPrintSilent(data
.printSilent());
212 settings
->SetShrinkToFit(data
.shrinkToFit());
214 settings
->SetPaperId(data
.paperId());
216 settings
->SetPaperWidth(data
.paperWidth());
217 settings
->SetPaperHeight(data
.paperHeight());
218 settings
->SetPaperSizeUnit(data
.paperSizeUnit());
220 settings
->SetPrintReversed(data
.printReversed());
221 settings
->SetPrintInColor(data
.printInColor());
222 settings
->SetOrientation(data
.orientation());
224 settings
->SetNumCopies(data
.numCopies());
225 settings
->SetNumPagesPerSheet(data
.numPagesPerSheet());
227 settings
->SetOutputDestination(
228 nsIPrintSettings::OutputDestinationType(data
.outputDestination()));
229 // Output stream intentionally unset, child processes shouldn't care about it.
231 settings
->SetOutputFormat(data
.outputFormat());
232 settings
->SetPrintPageDelay(data
.printPageDelay());
233 settings
->SetResolution(data
.resolution());
234 settings
->SetDuplex(data
.duplex());
235 settings
->SetIsInitializedFromPrinter(data
.isInitializedFromPrinter());
236 settings
->SetIsInitializedFromPrefs(data
.isInitializedFromPrefs());
241 /** ---------------------------------------------------
242 * Helper function - Creates the "prefix" for the pref
243 * It is either "print."
244 * or "print.printer_<print name>."
246 const char* nsPrintSettingsService::GetPrefName(const char* aPrefName
,
247 const nsAString
& aPrinterName
) {
248 if (!aPrefName
|| !*aPrefName
) {
249 NS_ERROR("Must have a valid pref name!");
253 mPrefName
.AssignLiteral("print.");
255 if (aPrinterName
.Length()) {
256 mPrefName
.AppendLiteral("printer_");
257 AppendUTF16toUTF8(aPrinterName
, mPrefName
);
258 mPrefName
.Append('.');
260 mPrefName
+= aPrefName
;
262 return mPrefName
.get();
266 * This will either read in the generic prefs (not specific to a printer)
267 * or read the prefs in using the printer name to qualify.
268 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
270 nsresult
nsPrintSettingsService::ReadPrefs(nsIPrintSettings
* aPS
,
271 const nsAString
& aPrinterName
,
273 NS_ENSURE_ARG_POINTER(aPS
);
275 bool noValidPrefsFound
= true;
281 #define GETBOOLPREF(_prefname, _retval) \
283 Preferences::GetBool(GetPrefName(_prefname, aPrinterName), _retval))
285 #define GETSTRPREF(_prefname, _retval) \
287 Preferences::GetString(GetPrefName(_prefname, aPrinterName), _retval))
289 #define GETINTPREF(_prefname, _retval) \
291 Preferences::GetInt(GetPrefName(_prefname, aPrinterName), _retval))
293 #define GETDBLPREF(_prefname, _retval) \
294 NS_SUCCEEDED(ReadPrefDouble(GetPrefName(_prefname, aPrinterName), _retval))
296 bool gotPaperSizeFromPrefs
= false;
297 int16_t paperSizeUnit
;
298 double paperWidth
, paperHeight
;
300 // Paper size prefs are read as a group
301 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
302 gotPaperSizeFromPrefs
= GETINTPREF(kPrintPaperSizeUnit
, &iVal
) &&
303 GETDBLPREF(kPrintPaperWidth
, paperWidth
) &&
304 GETDBLPREF(kPrintPaperHeight
, paperHeight
) &&
305 GETSTRPREF(kPrintPaperId
, str
);
306 paperSizeUnit
= (int16_t)iVal
;
308 if (gotPaperSizeFromPrefs
&&
309 paperSizeUnit
!= nsIPrintSettings::kPaperSizeInches
&&
310 paperSizeUnit
!= nsIPrintSettings::kPaperSizeMillimeters
) {
311 gotPaperSizeFromPrefs
= false;
314 if (gotPaperSizeFromPrefs
) {
315 // Bug 315687: Sanity check paper size to avoid paper size values in
316 // mm when the size unit flag is inches. The value 100 is arbitrary
317 // and can be changed.
318 gotPaperSizeFromPrefs
=
319 (paperSizeUnit
!= nsIPrintSettings::kPaperSizeInches
) ||
320 (paperWidth
< 100.0) || (paperHeight
< 100.0);
323 if (gotPaperSizeFromPrefs
) {
324 aPS
->SetPaperSizeUnit(paperSizeUnit
);
325 aPS
->SetPaperWidth(paperWidth
);
326 aPS
->SetPaperHeight(paperHeight
);
327 aPS
->SetPaperId(str
);
328 noValidPrefsFound
= false;
332 nsIntSize pageSizeInTwips
; // to sanity check margins
333 if (!gotPaperSizeFromPrefs
) {
334 aPS
->GetPaperSizeUnit(&paperSizeUnit
);
335 aPS
->GetPaperWidth(&paperWidth
);
336 aPS
->GetPaperHeight(&paperHeight
);
338 if (paperSizeUnit
== nsIPrintSettings::kPaperSizeMillimeters
) {
339 pageSizeInTwips
= nsIntSize((int)NS_MILLIMETERS_TO_TWIPS(paperWidth
),
340 (int)NS_MILLIMETERS_TO_TWIPS(paperHeight
));
342 pageSizeInTwips
= nsIntSize((int)NS_INCHES_TO_TWIPS(paperWidth
),
343 (int)NS_INCHES_TO_TWIPS(paperHeight
));
346 auto MarginIsOK
= [&pageSizeInTwips
](const nsIntMargin
& aMargin
) {
347 return aMargin
.top
>= 0 && aMargin
.right
>= 0 && aMargin
.bottom
>= 0 &&
348 aMargin
.left
>= 0 && aMargin
.LeftRight() < pageSizeInTwips
.width
&&
349 aMargin
.TopBottom() < pageSizeInTwips
.height
;
352 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
355 GETINTPREF(kUnwriteableMarginTopTwips
, &margin
.top
) &&
356 GETINTPREF(kUnwriteableMarginRightTwips
, &margin
.right
) &&
357 GETINTPREF(kUnwriteableMarginBottomTwips
, &margin
.bottom
) &&
358 GETINTPREF(kUnwriteableMarginLeftTwips
, &margin
.left
);
360 // We failed to read the new unwritable margin twips prefs. Try to read
361 // the old ones in case they exist.
363 ReadInchesIntToTwipsPref(
364 GetPrefName(kUnwriteableMarginTop
, aPrinterName
), margin
.top
) &&
365 ReadInchesIntToTwipsPref(
366 GetPrefName(kUnwriteableMarginLeft
, aPrinterName
), margin
.left
) &&
367 ReadInchesIntToTwipsPref(
368 GetPrefName(kUnwriteableMarginBottom
, aPrinterName
),
370 ReadInchesIntToTwipsPref(
371 GetPrefName(kUnwriteableMarginRight
, aPrinterName
), margin
.right
);
373 // SetUnwriteableMarginInTwips does its own validation and drops negative
374 // values individually. We still want to block overly large values though,
375 // so we do that part of MarginIsOK manually.
376 if (allPrefsRead
&& margin
.LeftRight() < pageSizeInTwips
.width
&&
377 margin
.TopBottom() < pageSizeInTwips
.height
) {
378 aPS
->SetUnwriteableMarginInTwips(margin
);
379 noValidPrefsFound
= false;
383 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
384 int32_t halfInch
= NS_INCHES_TO_INT_TWIPS(0.5);
385 nsIntMargin
margin(halfInch
, halfInch
, halfInch
, halfInch
);
386 bool prefRead
= ReadInchesToTwipsPref(GetPrefName(kMarginTop
, aPrinterName
),
388 prefRead
= ReadInchesToTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
),
391 prefRead
= ReadInchesToTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
395 prefRead
= ReadInchesToTwipsPref(GetPrefName(kMarginRight
, aPrinterName
),
399 if (prefRead
&& MarginIsOK(margin
)) {
400 aPS
->SetMarginInTwips(margin
);
401 noValidPrefsFound
= false;
405 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
406 nsIntMargin
margin(0, 0, 0, 0);
407 bool prefRead
= ReadInchesIntToTwipsPref(
408 GetPrefName(kEdgeTop
, aPrinterName
), margin
.top
);
409 prefRead
= ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
),
413 prefRead
= ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
417 prefRead
= ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
),
421 if (prefRead
&& MarginIsOK(margin
)) {
422 aPS
->SetEdgeInTwips(margin
);
423 noValidPrefsFound
= false;
427 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
428 if (GETSTRPREF(kPrintHeaderStrLeft
, str
)) {
429 aPS
->SetHeaderStrLeft(str
);
430 noValidPrefsFound
= false;
434 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
435 if (GETSTRPREF(kPrintHeaderStrCenter
, str
)) {
436 aPS
->SetHeaderStrCenter(str
);
437 noValidPrefsFound
= false;
441 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
442 if (GETSTRPREF(kPrintHeaderStrRight
, str
)) {
443 aPS
->SetHeaderStrRight(str
);
444 noValidPrefsFound
= false;
448 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
449 if (GETSTRPREF(kPrintFooterStrLeft
, str
)) {
450 aPS
->SetFooterStrLeft(str
);
451 noValidPrefsFound
= false;
455 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
456 if (GETSTRPREF(kPrintFooterStrCenter
, str
)) {
457 aPS
->SetFooterStrCenter(str
);
458 noValidPrefsFound
= false;
462 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
463 if (GETSTRPREF(kPrintFooterStrRight
, str
)) {
464 aPS
->SetFooterStrRight(str
);
465 noValidPrefsFound
= false;
469 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
470 if (GETBOOLPREF(kPrintBGColors
, &b
)) {
471 aPS
->SetPrintBGColors(b
);
472 noValidPrefsFound
= false;
476 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
477 if (GETBOOLPREF(kPrintBGImages
, &b
)) {
478 aPS
->SetPrintBGImages(b
);
479 noValidPrefsFound
= false;
483 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
484 if (GETBOOLPREF(kPrintReversed
, &b
)) {
485 aPS
->SetPrintReversed(b
);
486 noValidPrefsFound
= false;
490 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
491 if (GETBOOLPREF(kPrintInColor
, &b
)) {
492 aPS
->SetPrintInColor(b
);
493 noValidPrefsFound
= false;
497 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
498 if (GETINTPREF(kPrintOrientation
, &iVal
) &&
499 (iVal
== nsIPrintSettings::kPortraitOrientation
||
500 iVal
== nsIPrintSettings::kLandscapeOrientation
)) {
501 aPS
->SetOrientation(iVal
);
502 noValidPrefsFound
= false;
506 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
507 if (GETBOOLPREF(kPrintToFile
, &b
)) {
508 aPS
->SetOutputDestination(
509 b
? nsIPrintSettings::kOutputDestinationFile
510 : nsIPrintSettings::kOutputDestinationPrinter
);
511 noValidPrefsFound
= false;
515 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
516 if (GETSTRPREF(kPrintToFileName
, str
)) {
517 if (StringEndsWith(str
, u
".ps"_ns
)) {
518 // We only support PDF since bug 1425188 landed. Users may still have
519 // prefs with .ps filenames if they last saved a file as Postscript
520 // though, so we fix that up here. (The pref values will be
521 // overwritten the next time they save to file as a PDF.)
522 str
.Truncate(str
.Length() - 2);
523 str
.AppendLiteral("pdf");
525 aPS
->SetToFileName(str
);
526 noValidPrefsFound
= false;
530 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
532 if (GETINTPREF(kPrintPageDelay
, &iVal
) && iVal
>= 0 && iVal
<= 1000) {
533 aPS
->SetPrintPageDelay(iVal
);
534 noValidPrefsFound
= false;
538 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
539 if (GETBOOLPREF(kPrintShrinkToFit
, &b
)) {
540 aPS
->SetShrinkToFit(b
);
541 noValidPrefsFound
= false;
545 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
546 // The limits imposed here are fairly arbitrary and mainly intended to
547 // purge bad values which tend to be negative and/or very large. If we
548 // get complaints from users that settings outside these values "aren't
549 // saved" then we can consider increasing them.
550 if (GETDBLPREF(kPrintScaling
, dbl
) && dbl
>= 0.05 && dbl
<= 20) {
551 aPS
->SetScaling(dbl
);
552 noValidPrefsFound
= false;
556 if (aFlags
& nsIPrintSettings::kInitSaveDuplex
) {
557 if (GETINTPREF(kPrintDuplex
, &iVal
)) {
558 aPS
->SetDuplex(iVal
);
559 noValidPrefsFound
= false;
567 return noValidPrefsFound
? NS_ERROR_NOT_AVAILABLE
: NS_OK
;
570 nsresult
nsPrintSettingsService::WritePrefs(nsIPrintSettings
* aPS
,
571 const nsAString
& aPrinterName
,
573 NS_ENSURE_ARG_POINTER(aPS
);
575 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
576 nsIntMargin margin
= aPS
->GetMarginInTwips();
577 WriteInchesFromTwipsPref(GetPrefName(kMarginTop
, aPrinterName
), margin
.top
);
578 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
),
580 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
582 WriteInchesFromTwipsPref(GetPrefName(kMarginRight
, aPrinterName
),
586 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
587 nsIntMargin edge
= aPS
->GetEdgeInTwips();
588 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop
, aPrinterName
), edge
.top
);
589 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
),
591 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
593 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
),
597 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
598 nsIntMargin unwriteableMargin
= aPS
->GetUnwriteableMarginInTwips();
599 Preferences::SetInt(GetPrefName(kUnwriteableMarginTopTwips
, aPrinterName
),
600 unwriteableMargin
.top
);
601 Preferences::SetInt(GetPrefName(kUnwriteableMarginLeftTwips
, aPrinterName
),
602 unwriteableMargin
.left
);
604 GetPrefName(kUnwriteableMarginBottomTwips
, aPrinterName
),
605 unwriteableMargin
.bottom
);
606 Preferences::SetInt(GetPrefName(kUnwriteableMarginRightTwips
, aPrinterName
),
607 unwriteableMargin
.right
);
609 // Remove the old unwriteableMargin prefs.
610 Preferences::ClearUser(GetPrefName(kUnwriteableMarginTop
, aPrinterName
));
611 Preferences::ClearUser(GetPrefName(kUnwriteableMarginRight
, aPrinterName
));
612 Preferences::ClearUser(GetPrefName(kUnwriteableMarginBottom
, aPrinterName
));
613 Preferences::ClearUser(GetPrefName(kUnwriteableMarginLeft
, aPrinterName
));
616 // Paper size prefs are saved as a group
617 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
619 double width
, height
;
622 if (NS_SUCCEEDED(aPS
->GetPaperSizeUnit(&sizeUnit
)) &&
623 NS_SUCCEEDED(aPS
->GetPaperWidth(&width
)) &&
624 NS_SUCCEEDED(aPS
->GetPaperHeight(&height
)) &&
625 NS_SUCCEEDED(aPS
->GetPaperId(name
))) {
626 Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit
, aPrinterName
),
628 WritePrefDouble(GetPrefName(kPrintPaperWidth
, aPrinterName
), width
);
629 WritePrefDouble(GetPrefName(kPrintPaperHeight
, aPrinterName
), height
);
630 Preferences::SetString(GetPrefName(kPrintPaperId
, aPrinterName
), name
);
639 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
640 if (NS_SUCCEEDED(aPS
->GetHeaderStrLeft(uStr
))) {
641 Preferences::SetString(GetPrefName(kPrintHeaderStrLeft
, aPrinterName
),
646 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
647 if (NS_SUCCEEDED(aPS
->GetHeaderStrCenter(uStr
))) {
648 Preferences::SetString(GetPrefName(kPrintHeaderStrCenter
, aPrinterName
),
653 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
654 if (NS_SUCCEEDED(aPS
->GetHeaderStrRight(uStr
))) {
655 Preferences::SetString(GetPrefName(kPrintHeaderStrRight
, aPrinterName
),
660 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
661 if (NS_SUCCEEDED(aPS
->GetFooterStrLeft(uStr
))) {
662 Preferences::SetString(GetPrefName(kPrintFooterStrLeft
, aPrinterName
),
667 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
668 if (NS_SUCCEEDED(aPS
->GetFooterStrCenter(uStr
))) {
669 Preferences::SetString(GetPrefName(kPrintFooterStrCenter
, aPrinterName
),
674 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
675 if (NS_SUCCEEDED(aPS
->GetFooterStrRight(uStr
))) {
676 Preferences::SetString(GetPrefName(kPrintFooterStrRight
, aPrinterName
),
681 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
682 b
= aPS
->GetPrintBGColors();
683 Preferences::SetBool(GetPrefName(kPrintBGColors
, aPrinterName
), b
);
686 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
687 b
= aPS
->GetPrintBGImages();
688 Preferences::SetBool(GetPrefName(kPrintBGImages
, aPrinterName
), b
);
691 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
692 if (NS_SUCCEEDED(aPS
->GetPrintReversed(&b
))) {
693 Preferences::SetBool(GetPrefName(kPrintReversed
, aPrinterName
), b
);
697 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
698 if (NS_SUCCEEDED(aPS
->GetPrintInColor(&b
))) {
699 Preferences::SetBool(GetPrefName(kPrintInColor
, aPrinterName
), b
);
703 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
704 if (NS_SUCCEEDED(aPS
->GetOrientation(&iVal
))) {
705 Preferences::SetInt(GetPrefName(kPrintOrientation
, aPrinterName
), iVal
);
709 // Only the general version of this pref is saved
710 if ((aFlags
& nsIPrintSettings::kInitSavePrinterName
) &&
711 aPrinterName
.IsEmpty()) {
712 if (NS_SUCCEEDED(aPS
->GetPrinterName(uStr
))) {
713 Preferences::SetString(kPrinterName
, uStr
);
717 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
718 Preferences::SetBool(GetPrefName(kPrintToFile
, aPrinterName
),
719 aPS
->GetOutputDestination() ==
720 nsIPrintSettings::kOutputDestinationFile
);
723 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
724 if (NS_SUCCEEDED(aPS
->GetToFileName(uStr
))) {
725 Preferences::SetString(GetPrefName(kPrintToFileName
, aPrinterName
), uStr
);
729 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
730 if (NS_SUCCEEDED(aPS
->GetPrintPageDelay(&iVal
))) {
731 Preferences::SetInt(GetPrefName(kPrintPageDelay
, aPrinterName
), iVal
);
735 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
736 if (NS_SUCCEEDED(aPS
->GetShrinkToFit(&b
))) {
737 Preferences::SetBool(GetPrefName(kPrintShrinkToFit
, aPrinterName
), b
);
741 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
742 if (NS_SUCCEEDED(aPS
->GetScaling(&dbl
))) {
743 WritePrefDouble(GetPrefName(kPrintScaling
, aPrinterName
), dbl
);
747 if (aFlags
& nsIPrintSettings::kInitSaveDuplex
) {
748 if (NS_SUCCEEDED(aPS
->GetDuplex(&iVal
))) {
749 Preferences::SetInt(GetPrefName(kPrintDuplex
, aPrinterName
), iVal
);
761 nsPrintSettingsService::GetDefaultPrintSettingsForPrinting(
762 nsIPrintSettings
** aPrintSettings
) {
763 nsresult rv
= CreateNewPrintSettings(aPrintSettings
);
764 NS_ENSURE_SUCCESS(rv
, rv
);
766 nsIPrintSettings
* settings
= *aPrintSettings
;
768 // For security reasons, we don't pass the printer name to content processes.
769 // Once bug 1776169 is fixed, we can just assert that this is the parent
771 bool usePrinterName
= XRE_IsParentProcess();
773 if (usePrinterName
) {
774 nsAutoString printerName
;
775 settings
->GetPrinterName(printerName
);
776 if (printerName
.IsEmpty()) {
777 GetLastUsedPrinterName(printerName
);
778 settings
->SetPrinterName(printerName
);
780 InitPrintSettingsFromPrinter(printerName
, settings
);
783 InitPrintSettingsFromPrefs(settings
, usePrinterName
,
784 nsIPrintSettings::kInitSaveAll
);
790 nsPrintSettingsService::CreateNewPrintSettings(
791 nsIPrintSettings
** aNewPrintSettings
) {
792 return _CreatePrintSettings(aNewPrintSettings
);
796 nsPrintSettingsService::GetLastUsedPrinterName(
797 nsAString
& aLastUsedPrinterName
) {
798 MOZ_ASSERT(XRE_IsParentProcess());
800 aLastUsedPrinterName
.Truncate();
801 Preferences::GetString(kPrinterName
, aLastUsedPrinterName
);
806 nsPrintSettingsService::InitPrintSettingsFromPrinter(
807 const nsAString
& aPrinterName
, nsIPrintSettings
* aPrintSettings
) {
808 // Don't get print settings from the printer in the child when printing via
809 // parent, these will be retrieved in the parent later in the print process.
810 if (XRE_IsContentProcess() && StaticPrefs::print_print_via_parent()) {
814 NS_ENSURE_ARG_POINTER(aPrintSettings
);
817 nsString printerName
;
818 aPrintSettings
->GetPrinterName(printerName
);
819 if (!printerName
.Equals(aPrinterName
)) {
820 NS_WARNING("Printer names should match!");
825 aPrintSettings
->GetIsInitializedFromPrinter(&isInitialized
);
826 if (isInitialized
) return NS_OK
;
829 nsCOMPtr
<nsIPrinterList
> printerList
=
830 do_GetService(NS_PRINTER_LIST_CONTRACTID
, &rv
);
831 NS_ENSURE_SUCCESS(rv
, rv
);
833 rv
= printerList
->InitPrintSettingsFromPrinter(aPrinterName
, aPrintSettings
);
834 NS_ENSURE_SUCCESS(rv
, rv
);
836 aPrintSettings
->SetIsInitializedFromPrinter(true);
840 /** ---------------------------------------------------
841 * Helper function - Returns either the name or sets the length to zero
843 static nsresult
GetAdjustedPrinterName(nsIPrintSettings
* aPS
, bool aUsePNP
,
844 nsAString
& aPrinterName
) {
845 NS_ENSURE_ARG_POINTER(aPS
);
847 aPrinterName
.Truncate();
848 if (!aUsePNP
) return NS_OK
;
850 // Get the Printer Name from the PrintSettings
851 // to use as a prefix for Pref Names
852 nsresult rv
= aPS
->GetPrinterName(aPrinterName
);
853 NS_ENSURE_SUCCESS(rv
, rv
);
855 // Convert any whitespaces, carriage returns or newlines to _
856 // The below algorithm is supposedly faster than using iterators
857 constexpr auto replSubstr
= u
"_"_ns
;
858 const char* replaceStr
= " \n\r";
861 for (x
= 0; x
< (int32_t)strlen(replaceStr
); x
++) {
862 char16_t uChar
= replaceStr
[x
];
865 while ((i
= aPrinterName
.FindChar(uChar
, i
)) != kNotFound
) {
866 aPrinterName
.Replace(i
, 1, replSubstr
);
874 nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings
* aPS
,
877 NS_ENSURE_ARG_POINTER(aPS
);
880 aPS
->GetIsInitializedFromPrefs(&isInitialized
);
886 auto globalPrintSettings
= aFlags
;
887 #ifndef MOZ_WIDGET_ANDROID
888 globalPrintSettings
&= nsIPrintSettings::kGlobalSettings
;
891 nsAutoString prtName
;
892 // read any non printer specific prefs
893 // with empty printer name
894 nsresult rv
= ReadPrefs(aPS
, prtName
, globalPrintSettings
);
895 if (NS_FAILED(rv
) && rv
!= NS_ERROR_NOT_AVAILABLE
) {
896 NS_WARNING("ReadPrefs failed");
899 // Get the Printer Name from the PrintSettings to use as a prefix for Pref
901 rv
= GetAdjustedPrinterName(aPS
, aUsePNP
, prtName
);
902 NS_ENSURE_SUCCESS(rv
, rv
);
904 if (prtName
.IsEmpty()) {
905 NS_WARNING("Caller should supply a printer name.");
909 // Now read any printer specific prefs
910 rv
= ReadPrefs(aPS
, prtName
, aFlags
);
911 if (NS_SUCCEEDED(rv
)) {
912 aPS
->SetIsInitializedFromPrefs(true);
919 * Save all of the printer settings; if we can find a printer name, save
920 * printer-specific preferences. Otherwise, save generic ones.
922 nsresult
nsPrintSettingsService::SavePrintSettingsToPrefs(
923 nsIPrintSettings
* aPS
, bool aUsePrinterNamePrefix
, uint32_t aFlags
) {
924 NS_ENSURE_ARG_POINTER(aPS
);
925 MOZ_DIAGNOSTIC_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default
);
927 // Get the printer name from the PrinterSettings for an optional prefix.
928 nsAutoString prtName
;
929 nsresult rv
= GetAdjustedPrinterName(aPS
, aUsePrinterNamePrefix
, prtName
);
930 NS_ENSURE_SUCCESS(rv
, rv
);
932 #ifndef MOZ_WIDGET_ANDROID
933 // On most platforms we should always use a prefix when saving print settings
934 // to prefs. Saving without a prefix risks breaking printing for users
935 // without a good way for us to fix things for them (unprefixed prefs act as
936 // defaults and can result in values being inappropriately propagated to
938 if (prtName
.IsEmpty() && aFlags
!= nsIPrintSettings::kInitSavePrinterName
) {
939 MOZ_DIAGNOSTIC_ASSERT(false, "Print settings must be saved with a prefix");
940 return NS_ERROR_FAILURE
;
944 return WritePrefs(aPS
, prtName
, aFlags
);
947 //-----------------------------------------------------
948 //-- Protected Methods --------------------------------
949 //-----------------------------------------------------
950 nsresult
nsPrintSettingsService::ReadPrefDouble(const char* aPrefId
,
952 NS_ENSURE_ARG_POINTER(aPrefId
);
955 nsresult rv
= Preferences::GetCString(aPrefId
, str
);
956 if (NS_FAILED(rv
) || str
.IsEmpty()) {
957 return NS_ERROR_NOT_AVAILABLE
;
960 double value
= str
.ToDouble(&rv
);
969 nsresult
nsPrintSettingsService::WritePrefDouble(const char* aPrefId
,
971 NS_ENSURE_ARG_POINTER(aPrefId
);
974 str
.AppendFloat(aVal
);
975 return Preferences::SetCString(aPrefId
, str
);
978 bool nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId
,
981 nsresult rv
= Preferences::GetString(aPrefId
, str
);
982 if (NS_FAILED(rv
) || str
.IsEmpty()) {
986 float inches
= str
.ToFloat(&rv
);
991 aTwips
= NS_INCHES_TO_INT_TWIPS(inches
);
995 void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId
,
997 double inches
= NS_TWIPS_TO_INCHES(aTwips
);
998 nsAutoCString inchesStr
;
999 inchesStr
.AppendFloat(inches
);
1001 Preferences::SetCString(aPrefId
, inchesStr
);
1004 bool nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId
,
1007 nsresult rv
= Preferences::GetInt(aPrefId
, &value
);
1008 if (NS_FAILED(rv
)) {
1012 aTwips
= NS_INCHES_TO_INT_TWIPS(float(value
) / 100.0f
);
1016 void nsPrintSettingsService::WriteInchesIntFromTwipsPref(const char* aPrefId
,
1018 Preferences::SetInt(aPrefId
,
1019 int32_t(NS_TWIPS_TO_INCHES(aTwips
) * 100.0f
+ 0.5f
));
1022 void nsPrintSettingsService::ReadJustification(const char* aPrefId
,
1024 int16_t aInitValue
) {
1026 nsAutoString justStr
;
1027 if (NS_SUCCEEDED(Preferences::GetString(aPrefId
, justStr
))) {
1028 if (justStr
.EqualsASCII(kJustRight
)) {
1029 aJust
= nsIPrintSettings::kJustRight
;
1030 } else if (justStr
.EqualsASCII(kJustCenter
)) {
1031 aJust
= nsIPrintSettings::kJustCenter
;
1033 aJust
= nsIPrintSettings::kJustLeft
;
1038 //---------------------------------------------------
1039 void nsPrintSettingsService::WriteJustification(const char* aPrefId
,
1042 case nsIPrintSettings::kJustLeft
:
1043 Preferences::SetCString(aPrefId
, kJustLeft
);
1046 case nsIPrintSettings::kJustCenter
:
1047 Preferences::SetCString(aPrefId
, kJustCenter
);
1050 case nsIPrintSettings::kJustRight
:
1051 Preferences::SetCString(aPrefId
, kJustRight
);