Bug 1363482: Part 4 - Preload INI files off-thread during startup. r?erahm
MozReview-Commit-ID: CgQLEvjUDOJ
--- a/xpcom/base/nsINIParser.cpp
+++ b/xpcom/base/nsINIParser.cpp
@@ -5,30 +5,25 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Moz headers (alphabetical)
#include "nsCRTGlue.h"
#include "nsError.h"
#include "nsIFile.h"
#include "nsINIParser.h"
#include "mozilla/FileUtils.h" // AutoFILE
+#include "mozilla/URLPreloader.h"
// System headers (alphabetical)
#include <stdio.h>
#include <stdlib.h>
#ifdef XP_WIN
#include <windows.h>
#endif
-#if defined(XP_WIN)
-#define READ_BINARYMODE L"rb"
-#else
-#define READ_BINARYMODE "r"
-#endif
-
using namespace mozilla;
#ifdef XP_WIN
inline FILE*
TS_tfopen(const char* aPath, const wchar_t* aMode)
{
wchar_t wPath[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, aPath, -1, wPath, MAX_PATH);
@@ -60,134 +55,64 @@ public:
void operator=(FILE* aFp) { fp_ = aFp; }
private:
FILE* fp_;
};
nsresult
nsINIParser::Init(nsIFile* aFile)
{
- /* open the file. Don't use OpenANSIFileDesc, because you mustn't
- pass FILE* across shared library boundaries, which may be using
- different CRTs */
-
- AutoFILE fd;
-
-#ifdef XP_WIN
- nsAutoString path;
- nsresult rv = aFile->GetPath(path);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return rv;
+ auto result = URLPreloader::ReadFile(aFile);
+ if (result.isErr()) {
+ return result.unwrapErr();
}
- fd = _wfopen(path.get(), READ_BINARYMODE);
-#else
- nsAutoCString path;
- aFile->GetNativePath(path);
-
- fd = fopen(path.get(), READ_BINARYMODE);
-#endif
-
- if (!fd) {
- return NS_ERROR_FAILURE;
- }
-
- return InitFromFILE(fd);
+ return InitFromString(result.unwrap());
}
nsresult
nsINIParser::Init(const char* aPath)
{
- /* open the file */
- AutoFILE fd(TS_tfopen(aPath, READ_BINARYMODE));
- if (!fd) {
- return NS_ERROR_FAILURE;
+ auto result = URLPreloader::ReadFile(nsDependentCString(aPath));
+ if (result.isErr()) {
+ return result.unwrapErr();
}
- return InitFromFILE(fd);
+ return InitFromString(result.unwrap());
}
static const char kNL[] = "\r\n";
static const char kEquals[] = "=";
static const char kWhitespace[] = " \t";
static const char kRBracket[] = "]";
nsresult
-nsINIParser::InitFromFILE(FILE* aFd)
+nsINIParser::InitFromString(const nsCString& aStr)
{
- /* get file size */
- if (fseek(aFd, 0, SEEK_END) != 0) {
- return NS_ERROR_FAILURE;
- }
-
- long flen = ftell(aFd);
- /* zero-sized file, or an error */
- if (flen <= 0) {
- return NS_ERROR_FAILURE;
- }
-
- /* malloc an internal buf the size of the file */
- mFileContents = MakeUnique<char[]>(flen + 2);
- if (!mFileContents) {
- return NS_ERROR_OUT_OF_MEMORY;
- }
+ char* buffer;
- /* read the file in one swoop */
- if (fseek(aFd, 0, SEEK_SET) != 0) {
- return NS_BASE_STREAM_OSERROR;
- }
-
- int rd = fread(mFileContents.get(), sizeof(char), flen, aFd);
- if (rd != flen) {
- return NS_BASE_STREAM_OSERROR;
- }
-
- // We write a UTF16 null so that the file is easier to convert to UTF8
- mFileContents[flen] = mFileContents[flen + 1] = '\0';
-
- char* buffer = &mFileContents[0];
-
- if (flen >= 3 &&
- mFileContents[0] == '\xEF' &&
- mFileContents[1] == '\xBB' &&
- mFileContents[2] == '\xBF') {
+ if (StringHead(aStr, 3) == "\xEF\xBB\xBF") {
// Someone set us up the Utf-8 BOM
// This case is easy, since we assume that BOM-less
// files are Utf-8 anyway. Just skip the BOM and process as usual.
- buffer = &mFileContents[3];
- }
+ mFileContents.Append(aStr);
+ buffer = mFileContents.BeginWriting() + 3;
+ } else {
+ if (StringHead(aStr, 2) == "\xFF\xFE") {
+ // Someone set us up the Utf-16LE BOM
+ nsDependentSubstring str(reinterpret_cast<const char16_t*>(aStr.get()),
+ aStr.Length() / 2);
-#ifdef XP_WIN
- if (flen >= 2 &&
- mFileContents[0] == '\xFF' &&
- mFileContents[1] == '\xFE') {
- // Someone set us up the Utf-16LE BOM
- buffer = &mFileContents[2];
- // Get the size required for our Utf8 buffer
- flen = WideCharToMultiByte(CP_UTF8,
- 0,
- reinterpret_cast<LPWSTR>(buffer),
- -1,
- nullptr,
- 0,
- nullptr,
- nullptr);
- if (flen == 0) {
- return NS_ERROR_FAILURE;
+ AppendUTF16toUTF8(Substring(str, 1), mFileContents);
+ } else {
+ mFileContents.Append(aStr);
}
- UniquePtr<char[]> utf8Buffer(new char[flen]);
- if (WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<LPWSTR>(buffer), -1,
- utf8Buffer.get(), flen, nullptr, nullptr) == 0) {
- return NS_ERROR_FAILURE;
- }
- mFileContents = Move(utf8Buffer);
- buffer = mFileContents.get();
+ buffer = mFileContents.BeginWriting();
}
-#endif
char* currSection = nullptr;
// outer loop tokenizes into lines
while (char* token = NS_strtok(kNL, &buffer)) {
if (token[0] == '#' || token[0] == ';') { // it's a comment
continue;
}
--- a/xpcom/base/nsINIParser.h
+++ b/xpcom/base/nsINIParser.h
@@ -105,14 +105,14 @@ private:
}
const char* key;
const char* value;
mozilla::UniquePtr<INIValue> next;
};
nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
- mozilla::UniquePtr<char[]> mFileContents;
+ nsCString mFileContents;
- nsresult InitFromFILE(FILE* aFd);
+ nsresult InitFromString(const nsCString& aStr);
};
#endif /* nsINIParser_h__ */