From 1f106e1216a88f695f04de5829b0064d592d2edc Mon Sep 17 00:00:00 2001 From: roytam1 Date: Sat, 14 Jul 2018 19:42:58 +0800 Subject: [PATCH 1/3] nsFilePicker: Fix Browse Folder function in NT 3.51 --- widget/src/windows/nsFilePicker.cpp | 24 ++++++++++++++++++++---- widget/src/windows/nsFilePicker.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/widget/src/windows/nsFilePicker.cpp b/widget/src/windows/nsFilePicker.cpp index bc5d61c0..5c593e81 100644 --- a/widget/src/windows/nsFilePicker.cpp +++ b/widget/src/windows/nsFilePicker.cpp @@ -89,6 +89,7 @@ char nsFilePicker::mLastUsedDirectory[MAX_PATH+1] = { 0 }; nsFilePicker::nsFilePicker() { mSelectedType = 1; + mWinVer = GetVersion() & 0xFF; } //------------------------------------------------------------------------- @@ -153,7 +154,7 @@ NS_IMETHODIMP nsFilePicker::ShowW(PRInt16 *aReturnVal) #ifndef WINCE - if (mMode == modeGetFolder) { + if (mMode == modeGetFolder && mWinVer > 3) { PRUnichar dirBuffer[MAX_PATH+1]; wcsncpy(dirBuffer, initialDir.get(), MAX_PATH); @@ -244,13 +245,17 @@ NS_IMETHODIMP nsFilePicker::ShowW(PRInt16 *aReturnVal) #ifndef WINCE try { #endif - if (mMode == modeOpen) { + if (mMode == modeOpen || mMode == modeGetFolder) { // FILE MUST EXIST! - ofn.Flags |= OFN_FILEMUSTEXIST; + if(mMode == modeOpen) ofn.Flags |= OFN_FILEMUSTEXIST; + else { + fileBuffer[0]='t'; fileBuffer[1]='h'; fileBuffer[2]='i'; fileBuffer[3]='s'; + fileBuffer[4]='D'; fileBuffer[5]='i'; fileBuffer[6]='r'; + } result = nsToolkit::mGetOpenFileName(&ofn); } else if (mMode == modeOpenMultiple) { - ofn.Flags |= OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER; + ofn.Flags |= OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | (mWinVer > 3 ? OFN_EXPLORER : 0); result = nsToolkit::mGetOpenFileName(&ofn); } else if (mMode == modeSave) { @@ -345,6 +350,17 @@ NS_IMETHODIMP nsFilePicker::ShowW(PRInt16 *aReturnVal) } } else { + // Get DirPath from Full Pathname + if(mMode == modeGetFolder) { + int cnt; + nsAutoString dirName(fileBuffer); + for(cnt=dirName.Length()-1;cnt > 1; cnt--) { + if (fileBuffer[cnt] == '\\') { + fileBuffer[cnt] = 0; + break; + } + } + } // I think it also needs a conversion here (to unicode since appending to nsString) // but doing that generates garbage file name, weird. mUnicodeFile.Assign(fileBuffer); diff --git a/widget/src/windows/nsFilePicker.h b/widget/src/windows/nsFilePicker.h index 7bf79f31..fa89abfe 100644 --- a/widget/src/windows/nsFilePicker.h +++ b/widget/src/windows/nsFilePicker.h @@ -97,6 +97,7 @@ protected: static char mLastUsedDirectory[]; nsString mUnicodeFile; static nsString mLastUsedUnicodeDirectory; + PRInt32 mWinVer; }; #endif // nsFilePicker_h__ From d1a5d94f4b2c84df103c9da3bf0144348a3c9259 Mon Sep 17 00:00:00 2001 From: roytam1 Date: Sat, 14 Jul 2018 19:43:54 +0800 Subject: [PATCH 2/3] contentAreaUtils: wrap them with try-catch blocks as they can fail --- toolkit/content/contentAreaUtils.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/toolkit/content/contentAreaUtils.js b/toolkit/content/contentAreaUtils.js index 5361849b..4681e311 100644 --- a/toolkit/content/contentAreaUtils.js +++ b/toolkit/content/contentAreaUtils.js @@ -515,9 +515,11 @@ function getTargetFile(aFpP, aSkipPrompt) { var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties); - - var dir = fileLocator.get(getSpecialFolderKey(aFolder), Components.interfaces.nsILocalFile); - + var dir; + try { + dir = fileLocator.get(getSpecialFolderKey(aFolder), Components.interfaces.nsILocalFile); + } + catch (e) {} var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService); bundle = bundle.createBundle("chrome://mozapps/locale/downloads/unknownContentType.properties"); @@ -544,6 +546,7 @@ function getTargetFile(aFpP, aSkipPrompt) if (!aSkipPrompt || !useDownloadDir || !dir) { // If we're asking the user where to save the file, root the Save As... // dialog on they place they last picked. + if(!dir) { try { dir = prefs.getComplexValue("lastDir", nsILocalFile); } @@ -552,8 +555,15 @@ function getTargetFile(aFpP, aSkipPrompt) var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties); - dir = fileLocator.get(getSpecialFolderKey("Desktop"), nsILocalFile); + try { + dir = fileLocator.get(getSpecialFolderKey("Desktop"), nsILocalFile); + } + catch (r) { + // no clue, try current dir + dir = '.'; + } } + } var fp = makeFilePicker(); var titleKey = aFpP.fpTitleKey || "SaveLinkTitle"; From 8444083c28b912945cba0a7203ca02082594d09b Mon Sep 17 00:00:00 2001 From: roytam1 Date: Sat, 14 Jul 2018 20:49:47 +0800 Subject: [PATCH 3/3] pref/main: _getDownloadsFolder() and fileLocator.get() can throw, wrap them with try-catch block --- browser/components/preferences/main.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/browser/components/preferences/main.js b/browser/components/preferences/main.js index ef0ded86..e296a1f1 100644 --- a/browser/components/preferences/main.js +++ b/browser/components/preferences/main.js @@ -307,10 +307,12 @@ var gMainPane = { */ _folderToIndex: function (aFolder) { + try { if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop"))) return 0; else if (aFolder.equals(this._getDownloadsFolder("Downloads"))) return 1; + } catch(e) {} return 2; }, @@ -326,8 +328,13 @@ var gMainPane = { { var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties); - var dir = fileLocator.get(this._getSpecialFolderKey(aFolder), + var dir; + try { + dir = fileLocator.get(this._getSpecialFolderKey(aFolder), Components.interfaces.nsILocalFile); + } catch(e) { + dir = aFolder; + } if (aFolder != "Desktop") dir.append("My Downloads"); // XXX l12y!