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/. */
5 
6 #include "nsPrintSettingsService.h"
7 
8 #include "mozilla/embedding/PPrinting.h"
9 #include "mozilla/layout/RemotePrintJobChild.h"
10 #include "mozilla/RefPtr.h"
11 #include "nsCoord.h"
12 #include "nsIPrinterList.h"
13 #include "nsPrintingProxy.h"
14 #include "nsReadableUtils.h"
15 #include "nsPrintSettingsImpl.h"
16 #include "nsIPrintSession.h"
17 #include "nsServiceManagerUtils.h"
18 #include "nsSize.h"
19 
20 #include "nsArray.h"
21 #include "nsXPCOM.h"
22 #include "nsXULAppAPI.h"
23 
24 #include "nsIStringEnumerator.h"
25 #include "stdlib.h"
26 #include "mozilla/StaticPrefs_print.h"
27 #include "mozilla/Preferences.h"
28 #include "nsPrintfCString.h"
29 
30 using namespace mozilla;
31 using namespace mozilla::embedding;
32 
33 typedef mozilla::layout::RemotePrintJobChild RemotePrintJobChild;
34 
35 NS_IMPL_ISUPPORTS(nsPrintSettingsService, nsIPrintSettingsService)
36 
37 // Pref Constants
38 static const char kMarginTop[] = "print_margin_top";
39 static const char kMarginLeft[] = "print_margin_left";
40 static const char kMarginBottom[] = "print_margin_bottom";
41 static const char kMarginRight[] = "print_margin_right";
42 static const char kEdgeTop[] = "print_edge_top";
43 static const char kEdgeLeft[] = "print_edge_left";
44 static const char kEdgeBottom[] = "print_edge_bottom";
45 static const char kEdgeRight[] = "print_edge_right";
46 
47 static const char kUnwriteableMarginTopTwips[] =
48     "print_unwriteable_margin_top_twips";
49 static const char kUnwriteableMarginLeftTwips[] =
50     "print_unwriteable_margin_left_twips";
51 static const char kUnwriteableMarginBottomTwips[] =
52     "print_unwriteable_margin_bottom_twips";
53 static const char kUnwriteableMarginRightTwips[] =
54     "print_unwriteable_margin_right_twips";
55 
56 // These are legacy versions of the above UnwriteableMargin prefs. The new ones,
57 // which are in twips, were introduced to more accurately record the values.
58 static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top";
59 static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left";
60 static const char kUnwriteableMarginBottom[] =
61     "print_unwriteable_margin_bottom";
62 static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
63 
64 // Prefs for Print Options
65 static const char kPrintHeaderStrLeft[] = "print_headerleft";
66 static const char kPrintHeaderStrCenter[] = "print_headercenter";
67 static const char kPrintHeaderStrRight[] = "print_headerright";
68 static const char kPrintFooterStrLeft[] = "print_footerleft";
69 static const char kPrintFooterStrCenter[] = "print_footercenter";
70 static const char kPrintFooterStrRight[] = "print_footerright";
71 
72 // Additional Prefs
73 static const char kPrintReversed[] = "print_reversed";
74 static const char kPrintInColor[] = "print_in_color";
75 static const char kPrintPaperId[] = "print_paper_id";
76 static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
77 static const char kPrintPaperWidth[] = "print_paper_width";
78 static const char kPrintPaperHeight[] = "print_paper_height";
79 static const char kPrintOrientation[] = "print_orientation";
80 static const char kPrinterName[] = "print_printer";
81 static const char kPrintToFile[] = "print_to_file";
82 static const char kPrintToFileName[] = "print_to_filename";
83 static const char kPrintPageDelay[] = "print_page_delay";
84 static const char kPrintBGColors[] = "print_bgcolor";
85 static const char kPrintBGImages[] = "print_bgimages";
86 static const char kPrintShrinkToFit[] = "print_shrink_to_fit";
87 static const char kPrintScaling[] = "print_scaling";
88 static const char kPrintResolution[] = "print_resolution";
89 static const char kPrintDuplex[] = "print_duplex";
90 
91 static const char kJustLeft[] = "left";
92 static const char kJustCenter[] = "center";
93 static const char kJustRight[] = "right";
94 
95 #define NS_PRINTER_LIST_CONTRACTID "@mozilla.org/gfx/printerlist;1"
96 
Init()97 nsresult nsPrintSettingsService::Init() { return NS_OK; }
98 
99 NS_IMETHODIMP
SerializeToPrintData(nsIPrintSettings * aSettings,PrintData * data)100 nsPrintSettingsService::SerializeToPrintData(nsIPrintSettings* aSettings,
101                                              PrintData* data) {
102   aSettings->GetPageRanges(data->pageRanges());
103 
104   aSettings->GetEdgeTop(&data->edgeTop());
105   aSettings->GetEdgeLeft(&data->edgeLeft());
106   aSettings->GetEdgeBottom(&data->edgeBottom());
107   aSettings->GetEdgeRight(&data->edgeRight());
108 
109   aSettings->GetMarginTop(&data->marginTop());
110   aSettings->GetMarginLeft(&data->marginLeft());
111   aSettings->GetMarginBottom(&data->marginBottom());
112   aSettings->GetMarginRight(&data->marginRight());
113   aSettings->GetUnwriteableMarginTop(&data->unwriteableMarginTop());
114   aSettings->GetUnwriteableMarginLeft(&data->unwriteableMarginLeft());
115   aSettings->GetUnwriteableMarginBottom(&data->unwriteableMarginBottom());
116   aSettings->GetUnwriteableMarginRight(&data->unwriteableMarginRight());
117 
118   aSettings->GetScaling(&data->scaling());
119 
120   data->printBGColors() = aSettings->GetPrintBGColors();
121   data->printBGImages() = aSettings->GetPrintBGImages();
122 
123   data->honorPageRuleMargins() = aSettings->GetHonorPageRuleMargins();
124   data->showMarginGuides() = aSettings->GetShowMarginGuides();
125   data->isPrintSelectionRBEnabled() = aSettings->GetIsPrintSelectionRBEnabled();
126   data->printSelectionOnly() = aSettings->GetPrintSelectionOnly();
127 
128   aSettings->GetTitle(data->title());
129   aSettings->GetDocURL(data->docURL());
130 
131   aSettings->GetHeaderStrLeft(data->headerStrLeft());
132   aSettings->GetHeaderStrCenter(data->headerStrCenter());
133   aSettings->GetHeaderStrRight(data->headerStrRight());
134 
135   aSettings->GetFooterStrLeft(data->footerStrLeft());
136   aSettings->GetFooterStrCenter(data->footerStrCenter());
137   aSettings->GetFooterStrRight(data->footerStrRight());
138 
139   aSettings->GetIsCancelled(&data->isCancelled());
140   aSettings->GetPrintSilent(&data->printSilent());
141   aSettings->GetShrinkToFit(&data->shrinkToFit());
142 
143   aSettings->GetPaperId(data->paperId());
144   aSettings->GetPaperWidth(&data->paperWidth());
145   aSettings->GetPaperHeight(&data->paperHeight());
146   aSettings->GetPaperSizeUnit(&data->paperSizeUnit());
147 
148   aSettings->GetPrintReversed(&data->printReversed());
149   aSettings->GetPrintInColor(&data->printInColor());
150   aSettings->GetOrientation(&data->orientation());
151 
152   aSettings->GetNumCopies(&data->numCopies());
153   aSettings->GetNumPagesPerSheet(&data->numPagesPerSheet());
154 
155   aSettings->GetPrinterName(data->printerName());
156 
157   aSettings->GetPrintToFile(&data->printToFile());
158 
159   aSettings->GetToFileName(data->toFileName());
160 
161   aSettings->GetOutputFormat(&data->outputFormat());
162   aSettings->GetPrintPageDelay(&data->printPageDelay());
163   aSettings->GetResolution(&data->resolution());
164   aSettings->GetDuplex(&data->duplex());
165   aSettings->GetIsInitializedFromPrinter(&data->isInitializedFromPrinter());
166   aSettings->GetIsInitializedFromPrefs(&data->isInitializedFromPrefs());
167 
168   // Initialize the platform-specific values that don't
169   // default-initialize, so that we don't send uninitialized data over
170   // IPC (which leads to valgrind warnings, and, for bools, fatal
171   // assertions).
172   // data->driverName() default-initializes
173   // data->deviceName() default-initializes
174   // data->GTKPrintSettings() default-initializes
175 
176   return NS_OK;
177 }
178 
179 NS_IMETHODIMP
DeserializeToPrintSettings(const PrintData & data,nsIPrintSettings * settings)180 nsPrintSettingsService::DeserializeToPrintSettings(const PrintData& data,
181                                                    nsIPrintSettings* settings) {
182   nsCOMPtr<nsIPrintSession> session;
183   nsresult rv = settings->GetPrintSession(getter_AddRefs(session));
184   if (NS_SUCCEEDED(rv) && session) {
185     session->SetRemotePrintJob(
186         static_cast<RemotePrintJobChild*>(data.remotePrintJobChild()));
187   }
188 
189   settings->SetPageRanges(data.pageRanges());
190 
191   settings->SetEdgeTop(data.edgeTop());
192   settings->SetEdgeLeft(data.edgeLeft());
193   settings->SetEdgeBottom(data.edgeBottom());
194   settings->SetEdgeRight(data.edgeRight());
195 
196   settings->SetMarginTop(data.marginTop());
197   settings->SetMarginLeft(data.marginLeft());
198   settings->SetMarginBottom(data.marginBottom());
199   settings->SetMarginRight(data.marginRight());
200   settings->SetUnwriteableMarginTop(data.unwriteableMarginTop());
201   settings->SetUnwriteableMarginLeft(data.unwriteableMarginLeft());
202   settings->SetUnwriteableMarginBottom(data.unwriteableMarginBottom());
203   settings->SetUnwriteableMarginRight(data.unwriteableMarginRight());
204 
205   settings->SetScaling(data.scaling());
206 
207   settings->SetPrintBGColors(data.printBGColors());
208   settings->SetPrintBGImages(data.printBGImages());
209   settings->SetHonorPageRuleMargins(data.honorPageRuleMargins());
210   settings->SetShowMarginGuides(data.showMarginGuides());
211   settings->SetIsPrintSelectionRBEnabled(data.isPrintSelectionRBEnabled());
212   settings->SetPrintSelectionOnly(data.printSelectionOnly());
213 
214   settings->SetTitle(data.title());
215   settings->SetDocURL(data.docURL());
216 
217   // Header strings...
218   settings->SetHeaderStrLeft(data.headerStrLeft());
219   settings->SetHeaderStrCenter(data.headerStrCenter());
220   settings->SetHeaderStrRight(data.headerStrRight());
221 
222   // Footer strings...
223   settings->SetFooterStrLeft(data.footerStrLeft());
224   settings->SetFooterStrCenter(data.footerStrCenter());
225   settings->SetFooterStrRight(data.footerStrRight());
226 
227   settings->SetIsCancelled(data.isCancelled());
228   settings->SetPrintSilent(data.printSilent());
229   settings->SetShrinkToFit(data.shrinkToFit());
230 
231   settings->SetPaperId(data.paperId());
232 
233   settings->SetPaperWidth(data.paperWidth());
234   settings->SetPaperHeight(data.paperHeight());
235   settings->SetPaperSizeUnit(data.paperSizeUnit());
236 
237   settings->SetPrintReversed(data.printReversed());
238   settings->SetPrintInColor(data.printInColor());
239   settings->SetOrientation(data.orientation());
240 
241   settings->SetNumCopies(data.numCopies());
242   settings->SetNumPagesPerSheet(data.numPagesPerSheet());
243 
244   settings->SetPrinterName(data.printerName());
245 
246   settings->SetPrintToFile(data.printToFile());
247 
248   settings->SetToFileName(data.toFileName());
249 
250   settings->SetOutputFormat(data.outputFormat());
251   settings->SetPrintPageDelay(data.printPageDelay());
252   settings->SetResolution(data.resolution());
253   settings->SetDuplex(data.duplex());
254   settings->SetIsInitializedFromPrinter(data.isInitializedFromPrinter());
255   settings->SetIsInitializedFromPrefs(data.isInitializedFromPrefs());
256 
257   return NS_OK;
258 }
259 
260 /** ---------------------------------------------------
261  *  Helper function - Creates the "prefix" for the pref
262  *  It is either "print."
263  *  or "print.printer_<print name>."
264  */
GetPrefName(const char * aPrefName,const nsAString & aPrinterName)265 const char* nsPrintSettingsService::GetPrefName(const char* aPrefName,
266                                                 const nsAString& aPrinterName) {
267   if (!aPrefName || !*aPrefName) {
268     NS_ERROR("Must have a valid pref name!");
269     return aPrefName;
270   }
271 
272   mPrefName.AssignLiteral("print.");
273 
274   if (aPrinterName.Length()) {
275     mPrefName.AppendLiteral("printer_");
276     AppendUTF16toUTF8(aPrinterName, mPrefName);
277     mPrefName.Append('.');
278   }
279   mPrefName += aPrefName;
280 
281   return mPrefName.get();
282 }
283 
284 /**
285  *  This will either read in the generic prefs (not specific to a printer)
286  *  or read the prefs in using the printer name to qualify.
287  *  It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
288  */
ReadPrefs(nsIPrintSettings * aPS,const nsAString & aPrinterName,uint32_t aFlags)289 nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
290                                            const nsAString& aPrinterName,
291                                            uint32_t aFlags) {
292   NS_ENSURE_ARG_POINTER(aPS);
293 
294   bool noValidPrefsFound = true;
295   bool b;
296   nsAutoString str;
297   int32_t iVal;
298   double dbl;
299 
300 #define GETBOOLPREF(_prefname, _retval) \
301   NS_SUCCEEDED(                         \
302       Preferences::GetBool(GetPrefName(_prefname, aPrinterName), _retval))
303 
304 #define GETSTRPREF(_prefname, _retval) \
305   NS_SUCCEEDED(                        \
306       Preferences::GetString(GetPrefName(_prefname, aPrinterName), _retval))
307 
308 #define GETINTPREF(_prefname, _retval) \
309   NS_SUCCEEDED(                        \
310       Preferences::GetInt(GetPrefName(_prefname, aPrinterName), _retval))
311 
312 #define GETDBLPREF(_prefname, _retval) \
313   NS_SUCCEEDED(ReadPrefDouble(GetPrefName(_prefname, aPrinterName), _retval))
314 
315   bool gotPaperSizeFromPrefs = false;
316   int16_t paperSizeUnit;
317   double paperWidth, paperHeight;
318 
319   // Paper size prefs are read as a group
320   if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
321     gotPaperSizeFromPrefs = GETINTPREF(kPrintPaperSizeUnit, &iVal) &&
322                             GETDBLPREF(kPrintPaperWidth, paperWidth) &&
323                             GETDBLPREF(kPrintPaperHeight, paperHeight) &&
324                             GETSTRPREF(kPrintPaperId, str);
325     paperSizeUnit = (int16_t)iVal;
326 
327     if (gotPaperSizeFromPrefs &&
328         paperSizeUnit != nsIPrintSettings::kPaperSizeInches &&
329         paperSizeUnit != nsIPrintSettings::kPaperSizeMillimeters) {
330       gotPaperSizeFromPrefs = false;
331     }
332 
333     if (gotPaperSizeFromPrefs) {
334       // Bug 315687: Sanity check paper size to avoid paper size values in
335       // mm when the size unit flag is inches. The value 100 is arbitrary
336       // and can be changed.
337       gotPaperSizeFromPrefs =
338           (paperSizeUnit != nsIPrintSettings::kPaperSizeInches) ||
339           (paperWidth < 100.0) || (paperHeight < 100.0);
340     }
341 
342     if (gotPaperSizeFromPrefs) {
343       aPS->SetPaperSizeUnit(paperSizeUnit);
344       aPS->SetPaperWidth(paperWidth);
345       aPS->SetPaperHeight(paperHeight);
346       aPS->SetPaperId(str);
347       noValidPrefsFound = false;
348     }
349   }
350 
351   nsIntSize pageSizeInTwips;  // to sanity check margins
352   if (!gotPaperSizeFromPrefs) {
353     aPS->GetPaperSizeUnit(&paperSizeUnit);
354     aPS->GetPaperWidth(&paperWidth);
355     aPS->GetPaperHeight(&paperHeight);
356   }
357   if (paperSizeUnit == nsIPrintSettings::kPaperSizeMillimeters) {
358     pageSizeInTwips = nsIntSize((int)NS_MILLIMETERS_TO_TWIPS(paperWidth),
359                                 (int)NS_MILLIMETERS_TO_TWIPS(paperHeight));
360   } else {
361     pageSizeInTwips = nsIntSize((int)NS_INCHES_TO_TWIPS(paperWidth),
362                                 (int)NS_INCHES_TO_TWIPS(paperHeight));
363   }
364 
365   auto MarginIsOK = [&pageSizeInTwips](const nsIntMargin& aMargin) {
366     return aMargin.top >= 0 && aMargin.right >= 0 && aMargin.bottom >= 0 &&
367            aMargin.left >= 0 && aMargin.LeftRight() < pageSizeInTwips.width &&
368            aMargin.TopBottom() < pageSizeInTwips.height;
369   };
370 
371   if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
372     nsIntMargin margin;
373     bool allPrefsRead =
374         GETINTPREF(kUnwriteableMarginTopTwips, &margin.top) &&
375         GETINTPREF(kUnwriteableMarginRightTwips, &margin.right) &&
376         GETINTPREF(kUnwriteableMarginBottomTwips, &margin.bottom) &&
377         GETINTPREF(kUnwriteableMarginLeftTwips, &margin.left);
378     if (!allPrefsRead) {
379       // We failed to read the new unwritable margin twips prefs. Try to read
380       // the old ones in case they exist.
381       allPrefsRead =
382           ReadInchesIntToTwipsPref(
383               GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top) &&
384           ReadInchesIntToTwipsPref(
385               GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left) &&
386           ReadInchesIntToTwipsPref(
387               GetPrefName(kUnwriteableMarginBottom, aPrinterName),
388               margin.bottom) &&
389           ReadInchesIntToTwipsPref(
390               GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right);
391     }
392     // SetUnwriteableMarginInTwips does its own validation and drops negative
393     // values individually.  We still want to block overly large values though,
394     // so we do that part of MarginIsOK manually.
395     if (allPrefsRead && margin.LeftRight() < pageSizeInTwips.width &&
396         margin.TopBottom() < pageSizeInTwips.height) {
397       aPS->SetUnwriteableMarginInTwips(margin);
398       noValidPrefsFound = false;
399     }
400   }
401 
402   if (aFlags & nsIPrintSettings::kInitSaveMargins) {
403     int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
404     nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
405     bool prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName),
406                                           margin.top);
407     prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
408                                      margin.left) ||
409                prefRead;
410     prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
411                                      margin.bottom) ||
412                prefRead;
413 
414     prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName),
415                                      margin.right) ||
416                prefRead;
417     ;
418     if (prefRead && MarginIsOK(margin)) {
419       aPS->SetMarginInTwips(margin);
420       noValidPrefsFound = false;
421     }
422   }
423 
424   if (aFlags & nsIPrintSettings::kInitSaveEdges) {
425     nsIntMargin margin(0, 0, 0, 0);
426     bool prefRead = ReadInchesIntToTwipsPref(
427         GetPrefName(kEdgeTop, aPrinterName), margin.top);
428     prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
429                                         margin.left) ||
430                prefRead;
431 
432     prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
433                                         margin.bottom) ||
434                prefRead;
435 
436     prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
437                                         margin.right) ||
438                prefRead;
439     ;
440     if (prefRead && MarginIsOK(margin)) {
441       aPS->SetEdgeInTwips(margin);
442       noValidPrefsFound = false;
443     }
444   }
445 
446   if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
447     if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
448       aPS->SetHeaderStrLeft(str);
449       noValidPrefsFound = false;
450     }
451   }
452 
453   if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
454     if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
455       aPS->SetHeaderStrCenter(str);
456       noValidPrefsFound = false;
457     }
458   }
459 
460   if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
461     if (GETSTRPREF(kPrintHeaderStrRight, str)) {
462       aPS->SetHeaderStrRight(str);
463       noValidPrefsFound = false;
464     }
465   }
466 
467   if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
468     if (GETSTRPREF(kPrintFooterStrLeft, str)) {
469       aPS->SetFooterStrLeft(str);
470       noValidPrefsFound = false;
471     }
472   }
473 
474   if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
475     if (GETSTRPREF(kPrintFooterStrCenter, str)) {
476       aPS->SetFooterStrCenter(str);
477       noValidPrefsFound = false;
478     }
479   }
480 
481   if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
482     if (GETSTRPREF(kPrintFooterStrRight, str)) {
483       aPS->SetFooterStrRight(str);
484       noValidPrefsFound = false;
485     }
486   }
487 
488   if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
489     if (GETBOOLPREF(kPrintBGColors, &b)) {
490       aPS->SetPrintBGColors(b);
491       noValidPrefsFound = false;
492     }
493   }
494 
495   if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
496     if (GETBOOLPREF(kPrintBGImages, &b)) {
497       aPS->SetPrintBGImages(b);
498       noValidPrefsFound = false;
499     }
500   }
501 
502   if (aFlags & nsIPrintSettings::kInitSaveReversed) {
503     if (GETBOOLPREF(kPrintReversed, &b)) {
504       aPS->SetPrintReversed(b);
505       noValidPrefsFound = false;
506     }
507   }
508 
509   if (aFlags & nsIPrintSettings::kInitSaveInColor) {
510     if (GETBOOLPREF(kPrintInColor, &b)) {
511       aPS->SetPrintInColor(b);
512       noValidPrefsFound = false;
513     }
514   }
515 
516   if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
517     if (GETINTPREF(kPrintOrientation, &iVal) &&
518         (iVal == nsIPrintSettings::kPortraitOrientation ||
519          iVal == nsIPrintSettings::kLandscapeOrientation)) {
520       aPS->SetOrientation(iVal);
521       noValidPrefsFound = false;
522     }
523   }
524 
525   if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
526     if (GETBOOLPREF(kPrintToFile, &b)) {
527       aPS->SetPrintToFile(b);
528       noValidPrefsFound = false;
529     }
530   }
531 
532   if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
533     if (GETSTRPREF(kPrintToFileName, str)) {
534       if (StringEndsWith(str, u".ps"_ns)) {
535         // We only support PDF since bug 1425188 landed.  Users may still have
536         // prefs with .ps filenames if they last saved a file as Postscript
537         // though, so we fix that up here.  (The pref values will be
538         // overwritten the next time they save to file as a PDF.)
539         str.Truncate(str.Length() - 2);
540         str.AppendLiteral("pdf");
541       }
542       aPS->SetToFileName(str);
543       noValidPrefsFound = false;
544     }
545   }
546 
547   if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
548     // milliseconds
549     if (GETINTPREF(kPrintPageDelay, &iVal) && iVal >= 0 && iVal <= 1000) {
550       aPS->SetPrintPageDelay(iVal);
551       noValidPrefsFound = false;
552     }
553   }
554 
555   if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
556     if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
557       aPS->SetShrinkToFit(b);
558       noValidPrefsFound = false;
559     }
560   }
561 
562   if (aFlags & nsIPrintSettings::kInitSaveScaling) {
563     // The limits imposed here are fairly arbitrary and mainly intended to
564     // purge bad values which tend to be negative and/or very large.  If we
565     // get complaints from users that settings outside these values "aren't
566     // saved" then we can consider increasing them.
567     if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
568       aPS->SetScaling(dbl);
569       noValidPrefsFound = false;
570     }
571   }
572 
573   if (aFlags & nsIPrintSettings::kInitSaveResolution) {
574     // DPI. Again, an arbitrary range mainly to purge bad values that have made
575     // their way into user prefs.
576     if (GETINTPREF(kPrintResolution, &iVal) && iVal >= 50 && iVal <= 12000) {
577       aPS->SetResolution(iVal);
578       noValidPrefsFound = false;
579     }
580   }
581 
582   if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
583     if (GETINTPREF(kPrintDuplex, &iVal)) {
584       aPS->SetDuplex(iVal);
585       noValidPrefsFound = false;
586     }
587   }
588 
589   // Not Reading In:
590   //   Number of Copies
591 
592   return noValidPrefsFound ? NS_ERROR_NOT_AVAILABLE : NS_OK;
593 }
594 
WritePrefs(nsIPrintSettings * aPS,const nsAString & aPrinterName,uint32_t aFlags)595 nsresult nsPrintSettingsService::WritePrefs(nsIPrintSettings* aPS,
596                                             const nsAString& aPrinterName,
597                                             uint32_t aFlags) {
598   NS_ENSURE_ARG_POINTER(aPS);
599 
600   if (aFlags & nsIPrintSettings::kInitSaveMargins) {
601     nsIntMargin margin = aPS->GetMarginInTwips();
602     WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top);
603     WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
604                              margin.left);
605     WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
606                              margin.bottom);
607     WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
608                              margin.right);
609   }
610 
611   if (aFlags & nsIPrintSettings::kInitSaveEdges) {
612     nsIntMargin edge = aPS->GetEdgeInTwips();
613     WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), edge.top);
614     WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
615                                 edge.left);
616     WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
617                                 edge.bottom);
618     WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
619                                 edge.right);
620   }
621 
622   if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
623     nsIntMargin unwriteableMargin = aPS->GetUnwriteableMarginInTwips();
624     Preferences::SetInt(GetPrefName(kUnwriteableMarginTopTwips, aPrinterName),
625                         unwriteableMargin.top);
626     Preferences::SetInt(GetPrefName(kUnwriteableMarginLeftTwips, aPrinterName),
627                         unwriteableMargin.left);
628     Preferences::SetInt(
629         GetPrefName(kUnwriteableMarginBottomTwips, aPrinterName),
630         unwriteableMargin.bottom);
631     Preferences::SetInt(GetPrefName(kUnwriteableMarginRightTwips, aPrinterName),
632                         unwriteableMargin.right);
633 
634     // Remove the old unwriteableMargin prefs.
635     Preferences::ClearUser(GetPrefName(kUnwriteableMarginTop, aPrinterName));
636     Preferences::ClearUser(GetPrefName(kUnwriteableMarginRight, aPrinterName));
637     Preferences::ClearUser(GetPrefName(kUnwriteableMarginBottom, aPrinterName));
638     Preferences::ClearUser(GetPrefName(kUnwriteableMarginLeft, aPrinterName));
639   }
640 
641   // Paper size prefs are saved as a group
642   if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
643     int16_t sizeUnit;
644     double width, height;
645     nsString name;
646 
647     if (NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) &&
648         NS_SUCCEEDED(aPS->GetPaperWidth(&width)) &&
649         NS_SUCCEEDED(aPS->GetPaperHeight(&height)) &&
650         NS_SUCCEEDED(aPS->GetPaperId(name))) {
651       Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
652                           int32_t(sizeUnit));
653       WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width);
654       WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height);
655       Preferences::SetString(GetPrefName(kPrintPaperId, aPrinterName), name);
656     }
657   }
658 
659   bool b;
660   nsString uStr;
661   int32_t iVal;
662   double dbl;
663 
664   if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
665     if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(uStr))) {
666       Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
667                              uStr);
668     }
669   }
670 
671   if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
672     if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(uStr))) {
673       Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
674                              uStr);
675     }
676   }
677 
678   if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
679     if (NS_SUCCEEDED(aPS->GetHeaderStrRight(uStr))) {
680       Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
681                              uStr);
682     }
683   }
684 
685   if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
686     if (NS_SUCCEEDED(aPS->GetFooterStrLeft(uStr))) {
687       Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
688                              uStr);
689     }
690   }
691 
692   if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
693     if (NS_SUCCEEDED(aPS->GetFooterStrCenter(uStr))) {
694       Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
695                              uStr);
696     }
697   }
698 
699   if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
700     if (NS_SUCCEEDED(aPS->GetFooterStrRight(uStr))) {
701       Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
702                              uStr);
703     }
704   }
705 
706   if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
707     b = aPS->GetPrintBGColors();
708     Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
709   }
710 
711   if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
712     b = aPS->GetPrintBGImages();
713     Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
714   }
715 
716   if (aFlags & nsIPrintSettings::kInitSaveReversed) {
717     if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
718       Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
719     }
720   }
721 
722   if (aFlags & nsIPrintSettings::kInitSaveInColor) {
723     if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
724       Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
725     }
726   }
727 
728   if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
729     if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
730       Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
731     }
732   }
733 
734   // Only the general version of this pref is saved
735   if ((aFlags & nsIPrintSettings::kInitSavePrinterName) &&
736       aPrinterName.IsEmpty()) {
737     if (NS_SUCCEEDED(aPS->GetPrinterName(uStr))) {
738       Preferences::SetString(kPrinterName, uStr);
739     }
740   }
741 
742   if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
743     if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
744       Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b);
745     }
746   }
747 
748   if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
749     if (NS_SUCCEEDED(aPS->GetToFileName(uStr))) {
750       Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr);
751     }
752   }
753 
754   if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
755     if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
756       Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
757     }
758   }
759 
760   if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
761     if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
762       Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
763     }
764   }
765 
766   if (aFlags & nsIPrintSettings::kInitSaveScaling) {
767     if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
768       WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
769     }
770   }
771 
772   if (aFlags & nsIPrintSettings::kInitSaveResolution) {
773     if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) {
774       Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal);
775     }
776   }
777 
778   if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
779     if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) {
780       Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal);
781     }
782   }
783 
784   // Not Writing Out:
785   //   Number of Copies
786 
787   return NS_OK;
788 }
789 
790 NS_IMETHODIMP
GetDefaultPrintSettingsForPrinting(nsIPrintSettings ** aPrintSettings)791 nsPrintSettingsService::GetDefaultPrintSettingsForPrinting(
792     nsIPrintSettings** aPrintSettings) {
793   nsresult rv = GetNewPrintSettings(aPrintSettings);
794   NS_ENSURE_SUCCESS(rv, rv);
795 
796   nsIPrintSettings* settings = *aPrintSettings;
797 
798   nsAutoString printerName;
799   settings->GetPrinterName(printerName);
800   if (printerName.IsEmpty()) {
801     GetLastUsedPrinterName(printerName);
802     settings->SetPrinterName(printerName);
803   }
804   InitPrintSettingsFromPrinter(printerName, settings);
805   InitPrintSettingsFromPrefs(settings, true, nsIPrintSettings::kInitSaveAll);
806   return NS_OK;
807 }
808 
809 NS_IMETHODIMP
GetNewPrintSettings(nsIPrintSettings ** aNewPrintSettings)810 nsPrintSettingsService::GetNewPrintSettings(
811     nsIPrintSettings** aNewPrintSettings) {
812   return _CreatePrintSettings(aNewPrintSettings);
813 }
814 
815 NS_IMETHODIMP
GetLastUsedPrinterName(nsAString & aLastUsedPrinterName)816 nsPrintSettingsService::GetLastUsedPrinterName(
817     nsAString& aLastUsedPrinterName) {
818   aLastUsedPrinterName.Truncate();
819   Preferences::GetString(kPrinterName, aLastUsedPrinterName);
820   return NS_OK;
821 }
822 
823 NS_IMETHODIMP
InitPrintSettingsFromPrinter(const nsAString & aPrinterName,nsIPrintSettings * aPrintSettings)824 nsPrintSettingsService::InitPrintSettingsFromPrinter(
825     const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
826   // Don't get print settings from the printer in the child when printing via
827   // parent, these will be retrieved in the parent later in the print process.
828   if (XRE_IsContentProcess() && StaticPrefs::print_print_via_parent()) {
829     return NS_OK;
830   }
831 
832   NS_ENSURE_ARG_POINTER(aPrintSettings);
833 
834 #ifdef DEBUG
835   nsString printerName;
836   aPrintSettings->GetPrinterName(printerName);
837   if (!printerName.Equals(aPrinterName)) {
838     NS_WARNING("Printer names should match!");
839   }
840 #endif
841 
842   bool isInitialized;
843   aPrintSettings->GetIsInitializedFromPrinter(&isInitialized);
844   if (isInitialized) return NS_OK;
845 
846   nsresult rv;
847   nsCOMPtr<nsIPrinterList> printerList =
848       do_GetService(NS_PRINTER_LIST_CONTRACTID, &rv);
849   NS_ENSURE_SUCCESS(rv, rv);
850 
851   rv = printerList->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings);
852   NS_ENSURE_SUCCESS(rv, rv);
853 
854   aPrintSettings->SetIsInitializedFromPrinter(true);
855   return rv;
856 }
857 
858 /** ---------------------------------------------------
859  *  Helper function - Returns either the name or sets the length to zero
860  */
GetAdjustedPrinterName(nsIPrintSettings * aPS,bool aUsePNP,nsAString & aPrinterName)861 static nsresult GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP,
862                                        nsAString& aPrinterName) {
863   NS_ENSURE_ARG_POINTER(aPS);
864 
865   aPrinterName.Truncate();
866   if (!aUsePNP) return NS_OK;
867 
868   // Get the Printer Name from the PrintSettings
869   // to use as a prefix for Pref Names
870   nsresult rv = aPS->GetPrinterName(aPrinterName);
871   NS_ENSURE_SUCCESS(rv, rv);
872 
873   // Convert any whitespaces, carriage returns or newlines to _
874   // The below algorithm is supposedly faster than using iterators
875   constexpr auto replSubstr = u"_"_ns;
876   const char* replaceStr = " \n\r";
877 
878   int32_t x;
879   for (x = 0; x < (int32_t)strlen(replaceStr); x++) {
880     char16_t uChar = replaceStr[x];
881 
882     int32_t i = 0;
883     while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) {
884       aPrinterName.Replace(i, 1, replSubstr);
885       i++;
886     }
887   }
888   return NS_OK;
889 }
890 
891 NS_IMETHODIMP
InitPrintSettingsFromPrefs(nsIPrintSettings * aPS,bool aUsePNP,uint32_t aFlags)892 nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
893                                                    bool aUsePNP,
894                                                    uint32_t aFlags) {
895   NS_ENSURE_ARG_POINTER(aPS);
896 
897   bool isInitialized;
898   aPS->GetIsInitializedFromPrefs(&isInitialized);
899 
900   if (isInitialized) {
901     return NS_OK;
902   }
903 
904   auto globalPrintSettings = aFlags;
905 #ifndef MOZ_WIDGET_ANDROID
906   globalPrintSettings &= nsIPrintSettings::kGlobalSettings;
907 #endif
908 
909   nsAutoString prtName;
910   // read any non printer specific prefs
911   // with empty printer name
912   nsresult rv = ReadPrefs(aPS, prtName, globalPrintSettings);
913   if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) {
914     NS_WARNING("ReadPrefs failed");
915   }
916 
917   // Get the Printer Name from the PrintSettings to use as a prefix for Pref
918   // Names
919   rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
920   NS_ENSURE_SUCCESS(rv, rv);
921 
922   if (prtName.IsEmpty()) {
923     NS_WARNING("Caller should supply a printer name.");
924     return NS_OK;
925   }
926 
927   // Now read any printer specific prefs
928   rv = ReadPrefs(aPS, prtName, aFlags);
929   if (NS_SUCCEEDED(rv)) {
930     aPS->SetIsInitializedFromPrefs(true);
931   }
932 
933   return NS_OK;
934 }
935 
936 /**
937  *  Save all of the printer settings; if we can find a printer name, save
938  *  printer-specific preferences. Otherwise, save generic ones.
939  */
SavePrintSettingsToPrefs(nsIPrintSettings * aPS,bool aUsePrinterNamePrefix,uint32_t aFlags)940 nsresult nsPrintSettingsService::SavePrintSettingsToPrefs(
941     nsIPrintSettings* aPS, bool aUsePrinterNamePrefix, uint32_t aFlags) {
942   NS_ENSURE_ARG_POINTER(aPS);
943   MOZ_DIAGNOSTIC_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
944 
945   // Get the printer name from the PrinterSettings for an optional prefix.
946   nsAutoString prtName;
947   nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName);
948   NS_ENSURE_SUCCESS(rv, rv);
949 
950   // Write the prefs, with or without a printer name prefix.
951   return WritePrefs(aPS, prtName, aFlags);
952 }
953 
954 //-----------------------------------------------------
955 //-- Protected Methods --------------------------------
956 //-----------------------------------------------------
ReadPrefDouble(const char * aPrefId,double & aVal)957 nsresult nsPrintSettingsService::ReadPrefDouble(const char* aPrefId,
958                                                 double& aVal) {
959   NS_ENSURE_ARG_POINTER(aPrefId);
960 
961   nsAutoCString str;
962   nsresult rv = Preferences::GetCString(aPrefId, str);
963   if (NS_FAILED(rv) || str.IsEmpty()) {
964     return NS_ERROR_NOT_AVAILABLE;
965   }
966 
967   double value = str.ToDouble(&rv);
968   if (NS_FAILED(rv)) {
969     return rv;
970   }
971 
972   aVal = value;
973   return NS_OK;
974 }
975 
WritePrefDouble(const char * aPrefId,double aVal)976 nsresult nsPrintSettingsService::WritePrefDouble(const char* aPrefId,
977                                                  double aVal) {
978   NS_ENSURE_ARG_POINTER(aPrefId);
979 
980   nsAutoCString str;
981   str.AppendFloat(aVal);
982   return Preferences::SetCString(aPrefId, str);
983 }
984 
ReadInchesToTwipsPref(const char * aPrefId,int32_t & aTwips)985 bool nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId,
986                                                    int32_t& aTwips) {
987   nsAutoString str;
988   nsresult rv = Preferences::GetString(aPrefId, str);
989   if (NS_FAILED(rv) || str.IsEmpty()) {
990     return false;
991   }
992 
993   float inches = str.ToFloat(&rv);
994   if (NS_FAILED(rv)) {
995     return false;
996   }
997 
998   aTwips = NS_INCHES_TO_INT_TWIPS(inches);
999   return true;
1000 }
1001 
WriteInchesFromTwipsPref(const char * aPrefId,int32_t aTwips)1002 void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId,
1003                                                       int32_t aTwips) {
1004   double inches = NS_TWIPS_TO_INCHES(aTwips);
1005   nsAutoCString inchesStr;
1006   inchesStr.AppendFloat(inches);
1007 
1008   Preferences::SetCString(aPrefId, inchesStr);
1009 }
1010 
ReadInchesIntToTwipsPref(const char * aPrefId,int32_t & aTwips)1011 bool nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId,
1012                                                       int32_t& aTwips) {
1013   int32_t value;
1014   nsresult rv = Preferences::GetInt(aPrefId, &value);
1015   if (NS_FAILED(rv)) {
1016     return false;
1017   }
1018 
1019   aTwips = NS_INCHES_TO_INT_TWIPS(float(value) / 100.0f);
1020   return true;
1021 }
1022 
WriteInchesIntFromTwipsPref(const char * aPrefId,int32_t aTwips)1023 void nsPrintSettingsService::WriteInchesIntFromTwipsPref(const char* aPrefId,
1024                                                          int32_t aTwips) {
1025   Preferences::SetInt(aPrefId,
1026                       int32_t(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f));
1027 }
1028 
ReadJustification(const char * aPrefId,int16_t & aJust,int16_t aInitValue)1029 void nsPrintSettingsService::ReadJustification(const char* aPrefId,
1030                                                int16_t& aJust,
1031                                                int16_t aInitValue) {
1032   aJust = aInitValue;
1033   nsAutoString justStr;
1034   if (NS_SUCCEEDED(Preferences::GetString(aPrefId, justStr))) {
1035     if (justStr.EqualsASCII(kJustRight)) {
1036       aJust = nsIPrintSettings::kJustRight;
1037     } else if (justStr.EqualsASCII(kJustCenter)) {
1038       aJust = nsIPrintSettings::kJustCenter;
1039     } else {
1040       aJust = nsIPrintSettings::kJustLeft;
1041     }
1042   }
1043 }
1044 
1045 //---------------------------------------------------
WriteJustification(const char * aPrefId,int16_t aJust)1046 void nsPrintSettingsService::WriteJustification(const char* aPrefId,
1047                                                 int16_t aJust) {
1048   switch (aJust) {
1049     case nsIPrintSettings::kJustLeft:
1050       Preferences::SetCString(aPrefId, kJustLeft);
1051       break;
1052 
1053     case nsIPrintSettings::kJustCenter:
1054       Preferences::SetCString(aPrefId, kJustCenter);
1055       break;
1056 
1057     case nsIPrintSettings::kJustRight:
1058       Preferences::SetCString(aPrefId, kJustRight);
1059       break;
1060   }  // switch
1061 }
1062