Bug 1411480 (attempt 2) - Change PrefSaveData's element type. r=glandium.

Because nsCString is nicer than UniqueFreePtr<char>.

The patch also streamlines pref_savePrefs(). And also changes StrEscape() to
always put quotations around the result, because that's needed in both call
sites.

MozReview-Commit-ID: HT7HZz4QiSN

--HG--
extra : rebase_source : 442bb6002e004803a9ecb637239a000f11ec5774
This commit is contained in:
Nicholas Nethercote 2017-10-26 16:48:11 +11:00
Родитель bfdd7e8e70
Коммит 63f0f0af1c
1 изменённых файлов: 23 добавлений и 29 удалений

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

@ -137,7 +137,7 @@ using namespace mozilla;
struct PrefHashEntry; struct PrefHashEntry;
typedef nsTArray<mozilla::UniqueFreePtr<char>> PrefSaveData; typedef nsTArray<nsCString> PrefSaveData;
static PrefHashEntry* static PrefHashEntry*
pref_HashTableLookup(const char* aKey); pref_HashTableLookup(const char* aKey);
@ -390,10 +390,15 @@ PREF_Cleanup()
PREF_CleanupPrefs(); PREF_CleanupPrefs();
} }
// Note that this appends to aResult, and does not assign! // Assign to aResult a quoted, escaped copy of aOriginal.
static void static void
StrEscape(const char* aOriginal, nsCString& aResult) StrEscape(const char* aOriginal, nsCString& aResult)
{ {
if (aOriginal == nullptr) {
aResult.AssignLiteral("\"\"");
return;
}
// JavaScript does not allow quotes, slashes, or line terminators inside // JavaScript does not allow quotes, slashes, or line terminators inside
// strings so we must escape them. ECMAScript defines four line terminators, // strings so we must escape them. ECMAScript defines four line terminators,
// but we're only worrying about \r and \n here. We currently feed our pref // but we're only worrying about \r and \n here. We currently feed our pref
@ -406,9 +411,7 @@ StrEscape(const char* aOriginal, nsCString& aResult)
// \u2029. // \u2029.
const char* p; const char* p;
if (aOriginal == nullptr) { aResult.Assign('"');
return;
}
// Paranoid worst case all slashes will free quickly. // Paranoid worst case all slashes will free quickly.
for (p = aOriginal; *p; ++p) { for (p = aOriginal; *p; ++p) {
@ -434,6 +437,8 @@ StrEscape(const char* aOriginal, nsCString& aResult)
break; break;
} }
} }
aResult.Append('"');
} }
// //
@ -552,10 +557,6 @@ pref_savePrefs()
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) { for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
auto pref = static_cast<PrefHashEntry*>(iter.Get()); auto pref = static_cast<PrefHashEntry*>(iter.Get());
nsAutoCString prefValue;
nsAutoCString prefPrefix;
prefPrefix.AssignLiteral("user_pref(\"");
// where we're getting our pref from // where we're getting our pref from
PrefValue* sourcePref; PrefValue* sourcePref;
@ -571,25 +572,22 @@ pref_savePrefs()
continue; continue;
} }
// strings are in quotes! nsAutoCString prefName;
StrEscape(pref->mKey, prefName);
nsAutoCString prefValue;
if (pref->mPrefFlags.IsTypeString()) { if (pref->mPrefFlags.IsTypeString()) {
prefValue = '\"';
StrEscape(sourcePref->mStringVal, prefValue); StrEscape(sourcePref->mStringVal, prefValue);
prefValue += '\"';
} else if (pref->mPrefFlags.IsTypeInt()) { } else if (pref->mPrefFlags.IsTypeInt()) {
prefValue.AppendInt(sourcePref->mIntVal); prefValue.AppendInt(sourcePref->mIntVal);
} else if (pref->mPrefFlags.IsTypeBool()) { } else if (pref->mPrefFlags.IsTypeBool()) {
prefValue = (sourcePref->mBoolVal) ? "true" : "false"; prefValue = sourcePref->mBoolVal ? "true" : "false";
} }
nsAutoCString prefName; nsPrintfCString str("user_pref(%s, %s);", prefName.get(), prefValue.get());
StrEscape(pref->mKey, prefName); savedPrefs.AppendElement(str);
savedPrefs.AppendElement()->reset(
ToNewCString(prefPrefix + prefName + NS_LITERAL_CSTRING("\", ") +
prefValue + NS_LITERAL_CSTRING(");")));
} }
return savedPrefs; return savedPrefs;
@ -3409,16 +3407,14 @@ public:
struct CharComparator struct CharComparator
{ {
bool LessThan(const mozilla::UniqueFreePtr<char>& a, bool LessThan(const nsCString& aA, const nsCString& aB) const
const mozilla::UniqueFreePtr<char>& b) const
{ {
return strcmp(a.get(), b.get()) < 0; return aA < aB;
} }
bool Equals(const mozilla::UniqueFreePtr<char>& a, bool Equals(const nsCString& aA, const nsCString& aB) const
const mozilla::UniqueFreePtr<char>& b) const
{ {
return strcmp(a.get(), b.get()) == 0; return aA == aB;
} }
}; };
@ -3429,10 +3425,8 @@ public:
outStream->Write( outStream->Write(
kPrefFileHeader, sizeof(kPrefFileHeader) - 1, &writeAmount); kPrefFileHeader, sizeof(kPrefFileHeader) - 1, &writeAmount);
for (auto& prefptr : aPrefs) { for (nsCString& pref : aPrefs) {
char* pref = prefptr.get(); outStream->Write(pref.get(), pref.Length(), &writeAmount);
MOZ_ASSERT(pref);
outStream->Write(pref, strlen(pref), &writeAmount);
outStream->Write(NS_LINEBREAK, NS_LINEBREAK_LEN, &writeAmount); outStream->Write(NS_LINEBREAK, NS_LINEBREAK_LEN, &writeAmount);
} }