Bug 1669149 p5: Make nsPrintSettingsService::ReadPrefs return NS_ERROR_NOT_AVAILABLE, not NS_OK, if no prefs are read. r=jwatt

This allows us to detect if any prefs were read in JavaScript.

Depends on D99807

Differential Revision: https://phabricator.services.mozilla.com/D99808
This commit is contained in:
Bob Owen 2021-11-16 08:17:04 +00:00
Родитель 773d934c75
Коммит 571f1db901
2 изменённых файлов: 88 добавлений и 58 удалений

Просмотреть файл

@ -293,6 +293,7 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
uint32_t aFlags) {
NS_ENSURE_ARG_POINTER(aPS);
bool noValidPrefsFound = true;
bool b;
nsAutoString str;
int32_t iVal;
@ -345,6 +346,7 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
aPS->SetPaperWidth(paperWidth);
aPS->SetPaperHeight(paperHeight);
aPS->SetPaperId(str);
noValidPrefsFound = false;
}
}
@ -378,115 +380,138 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
if (!allPrefsRead) {
// We failed to read the new unwritable margin twips prefs. Try to read
// the old ones in case they exist.
ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName),
margin.top, kUnwriteableMarginTop);
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left,
kUnwriteableMarginLeft);
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginBottom, aPrinterName), margin.bottom,
kUnwriteableMarginBottom);
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right,
kUnwriteableMarginRight);
allPrefsRead =
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginBottom, aPrinterName),
margin.bottom) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right);
}
// SetUnwriteableMarginInTwips does its own validation and drops negative
// values individually. We still want to block overly large values though,
// so we do that part of MarginIsOK manually.
if (margin.LeftRight() < pageSizeInTwips.width &&
if (allPrefsRead && margin.LeftRight() < pageSizeInTwips.width &&
margin.TopBottom() < pageSizeInTwips.height) {
aPS->SetUnwriteableMarginInTwips(margin);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveMargins) {
int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top,
kMarginTop);
ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left,
kMarginLeft);
ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
margin.bottom, kMarginBottom);
ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right,
kMarginRight);
if (MarginIsOK(margin)) {
bool prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName),
margin.top);
prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
margin.left) ||
prefRead;
prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
margin.bottom) ||
prefRead;
prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName),
margin.right) ||
prefRead;
;
if (prefRead && MarginIsOK(margin)) {
aPS->SetMarginInTwips(margin);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveEdges) {
nsIntMargin margin(0, 0, 0, 0);
ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top,
kEdgeTop);
ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left,
kEdgeLeft);
ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
margin.bottom, kEdgeBottom);
ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
margin.right, kEdgeRight);
if (MarginIsOK(margin)) {
bool prefRead = ReadInchesIntToTwipsPref(
GetPrefName(kEdgeTop, aPrinterName), margin.top);
prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
margin.left) ||
prefRead;
prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
margin.bottom) ||
prefRead;
prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
margin.right) ||
prefRead;
;
if (prefRead && MarginIsOK(margin)) {
aPS->SetEdgeInTwips(margin);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
aPS->SetHeaderStrLeft(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
aPS->SetHeaderStrCenter(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
if (GETSTRPREF(kPrintHeaderStrRight, str)) {
aPS->SetHeaderStrRight(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
if (GETSTRPREF(kPrintFooterStrLeft, str)) {
aPS->SetFooterStrLeft(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
if (GETSTRPREF(kPrintFooterStrCenter, str)) {
aPS->SetFooterStrCenter(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
if (GETSTRPREF(kPrintFooterStrRight, str)) {
aPS->SetFooterStrRight(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
if (GETBOOLPREF(kPrintBGColors, &b)) {
aPS->SetPrintBGColors(b);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
if (GETBOOLPREF(kPrintBGImages, &b)) {
aPS->SetPrintBGImages(b);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveReversed) {
if (GETBOOLPREF(kPrintReversed, &b)) {
aPS->SetPrintReversed(b);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveInColor) {
if (GETBOOLPREF(kPrintInColor, &b)) {
aPS->SetPrintInColor(b);
noValidPrefsFound = false;
}
}
@ -495,12 +520,14 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
(iVal == nsIPrintSettings::kPortraitOrientation ||
iVal == nsIPrintSettings::kLandscapeOrientation)) {
aPS->SetOrientation(iVal);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
if (GETBOOLPREF(kPrintToFile, &b)) {
aPS->SetPrintToFile(b);
noValidPrefsFound = false;
}
}
@ -515,6 +542,7 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
str.AppendLiteral("pdf");
}
aPS->SetToFileName(str);
noValidPrefsFound = false;
}
}
@ -522,12 +550,14 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
// milliseconds
if (GETINTPREF(kPrintPageDelay, &iVal) && iVal >= 0 && iVal <= 1000) {
aPS->SetPrintPageDelay(iVal);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
aPS->SetShrinkToFit(b);
noValidPrefsFound = false;
}
}
@ -538,6 +568,7 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
// saved" then we can consider increasing them.
if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
aPS->SetScaling(dbl);
noValidPrefsFound = false;
}
}
@ -546,19 +577,21 @@ nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
// their way into user prefs.
if (GETINTPREF(kPrintResolution, &iVal) && iVal >= 50 && iVal <= 12000) {
aPS->SetResolution(iVal);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
if (GETINTPREF(kPrintDuplex, &iVal)) {
aPS->SetDuplex(iVal);
noValidPrefsFound = false;
}
}
// Not Reading In:
// Number of Copies
return NS_OK;
return noValidPrefsFound ? NS_ERROR_NOT_AVAILABLE : NS_OK;
}
nsresult nsPrintSettingsService::WritePrefs(nsIPrintSettings* aPS,
@ -879,7 +912,9 @@ nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
// read any non printer specific prefs
// with empty printer name
nsresult rv = ReadPrefs(aPS, prtName, globalPrintSettings);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) {
NS_WARNING("ReadPrefs failed");
}
// Get the Printer Name from the PrintSettings to use as a prefix for Pref
// Names
@ -893,7 +928,9 @@ nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
// Now read any printer specific prefs
rv = ReadPrefs(aPS, prtName, aFlags);
if (NS_SUCCEEDED(rv)) aPS->SetIsInitializedFromPrefs(true);
if (NS_SUCCEEDED(rv)) {
aPS->SetIsInitializedFromPrefs(true);
}
return NS_OK;
}
@ -956,23 +993,21 @@ nsresult nsPrintSettingsService::WritePrefDouble(const char* aPrefId,
return Preferences::SetCString(aPrefId, str);
}
void nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId,
int32_t& aTwips,
const char* aMarginPref) {
bool nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId,
int32_t& aTwips) {
nsAutoString str;
nsresult rv = Preferences::GetString(aPrefId, str);
if (NS_FAILED(rv) || str.IsEmpty()) {
rv = Preferences::GetString(aMarginPref, str);
return false;
}
if (NS_SUCCEEDED(rv) && !str.IsEmpty()) {
nsresult errCode;
float inches = str.ToFloat(&errCode);
if (NS_SUCCEEDED(errCode)) {
aTwips = NS_INCHES_TO_INT_TWIPS(inches);
} else {
aTwips = 0;
}
float inches = str.ToFloat(&rv);
if (NS_FAILED(rv)) {
return false;
}
aTwips = NS_INCHES_TO_INT_TWIPS(inches);
return true;
}
void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId,
@ -984,19 +1019,16 @@ void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId,
Preferences::SetCString(aPrefId, inchesStr);
}
void nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId,
int32_t& aTwips,
const char* aMarginPref) {
bool nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId,
int32_t& aTwips) {
int32_t value;
nsresult rv = Preferences::GetInt(aPrefId, &value);
if (NS_FAILED(rv)) {
rv = Preferences::GetInt(aMarginPref, &value);
}
if (NS_SUCCEEDED(rv)) {
aTwips = NS_INCHES_TO_INT_TWIPS(float(value) / 100.0f);
} else {
aTwips = 0;
return false;
}
aTwips = NS_INCHES_TO_INT_TWIPS(float(value) / 100.0f);
return true;
}
void nsPrintSettingsService::WriteInchesIntFromTwipsPref(const char* aPrefId,

Просмотреть файл

@ -46,11 +46,9 @@ class nsPrintSettingsService : public nsIPrintSettingsService {
void ReadJustification(const char* aPrefId, int16_t& aJust,
int16_t aInitValue);
void WriteJustification(const char* aPrefId, int16_t aJust);
void ReadInchesToTwipsPref(const char* aPrefId, int32_t& aTwips,
const char* aMarginPref);
bool ReadInchesToTwipsPref(const char* aPrefId, int32_t& aTwips);
void WriteInchesFromTwipsPref(const char* aPrefId, int32_t aTwips);
void ReadInchesIntToTwipsPref(const char* aPrefId, int32_t& aTwips,
const char* aMarginPref);
bool ReadInchesIntToTwipsPref(const char* aPrefId, int32_t& aTwips);
void WriteInchesIntFromTwipsPref(const char* aPrefId, int32_t aTwips);
nsresult ReadPrefDouble(const char* aPrefId, double& aVal);