Bug 491311 - Simplify "Add to recent documents" code in nsDownload::SetState. r=gavin
authorAiko <Seno.Aiko@live.com>
Sun, 28 Jun 2009 10:59:37 +0200
changeset 29782 6107ad66e56d
parent 29781 fe6e0d8924bd
child 29783 4ceba37b3b22
push id106
push userhsivonen@iki.fi
push dateSun, 28 Jun 2009 17:44:42 +0000
reviewersgavin
bugs491311
milestone1.9.2a1pre
Bug 491311 - Simplify "Add to recent documents" code in nsDownload::SetState. r=gavin
toolkit/components/downloads/src/nsDownloadManager.cpp
toolkit/components/downloads/test/unit/test_bug_401430.js
--- a/toolkit/components/downloads/src/nsDownloadManager.cpp
+++ b/toolkit/components/downloads/src/nsDownloadManager.cpp
@@ -2201,29 +2201,18 @@ nsDownload::SetState(DownloadState aStat
 
         // On windows, add the download to the system's "recent documents"
         // list, with a pref to disable.
         {
           PRBool addToRecentDocs = PR_TRUE;
           if (pref)
             pref->GetBoolPref(PREF_BDM_ADDTORECENTDOCS, &addToRecentDocs);
 
-          LPSHELLFOLDER lpShellFolder = NULL;
-          if (addToRecentDocs && SUCCEEDED(::SHGetDesktopFolder(&lpShellFolder))) {
-            PRUnichar *filePath = ToNewUnicode(path);
-            LPITEMIDLIST lpItemIDList = NULL;
-            if (SUCCEEDED(lpShellFolder->ParseDisplayName(NULL, NULL, filePath,
-                                                          NULL, &lpItemIDList,
-                                                          NULL))) {
-              ::SHAddToRecentDocs(SHARD_PIDL, lpItemIDList);
-              ::CoTaskMemFree(lpItemIDList);
-            }
-            nsMemory::Free(filePath);
-            lpShellFolder->Release();
-          }
+          if (addToRecentDocs)
+            ::SHAddToRecentDocs(SHARD_PATHW, path.get());
         }
 
         // On Vista and up, we rely on native security prompting when users
         // open executable content. If the option is set, add meta data to the
         // 'Zone.Identifier' resource fork of the file which indicates this
         // content came from the internet.
         {
           nsCOMPtr<nsIPrefBranch> pref =
--- a/toolkit/components/downloads/test/unit/test_bug_401430.js
+++ b/toolkit/components/downloads/test/unit/test_bug_401430.js
@@ -35,47 +35,54 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 // This tests the "add to recent documents" functionality of the DM
 
 const nsIDownloadManager = Ci.nsIDownloadManager;
 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager);
 
-const resultFileName = "test" + Date.now() + ".doc";
+// Make sure Unicode is supported:
+// U+00E3 : LATIN SMALL LETTER A WITH TILDE
+// U+041B : CYRILLIC CAPITAL LETTER EL
+// U+3056 : HIRAGANA LETTER ZA
+const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc";
 
 function checkResult() {
-  do_check_true(checkRecentDocsFor(resultFileName));
-
-  // delete the saved file
+  // delete the saved file (this doesn't affect the "recent documents" list)
   var resultFile = dirSvc.get("ProfD", Ci.nsIFile);
   resultFile.append(resultFileName);
   resultFile.remove(false);
 
+  do_check_true(checkRecentDocsFor(resultFileName));
   do_test_finished();
 }
 
 function checkRecentDocsFor(aFileName) {
   var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"].
                         createInstance(Ci.nsIWindowsRegKey);
   var recentDocsPath =
         "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs";
   recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
                      recentDocsPath,
                      Ci.nsIWindowsRegKey.ACCESS_READ);
   var count = recentDocsKey.valueCount;
   for (var i = 0; i < count; ++i) {
     var valueName = recentDocsKey.getValueName(i);
     var binValue = recentDocsKey.readBinaryValue(valueName);
 
-    // "fields" in the data are separated by double nulls, use only the first
-    var fileName = binValue.split("\0\0")[0];
+    // "fields" in the data are separated by \0 wide characters, which are
+    // returned as two \0 "bytes" by readBinaryValue. Use only the first field.
+    var fileNameRaw = binValue.split("\0\0")[0];
 
-    // Remove any embeded single nulls
-    fileName = fileName.replace(/\x00/g, "");
+    // Convert the filename from UTF-16LE.
+    var fileName = "";
+    for (var c = 0; c < fileNameRaw.length; c += 2)
+      fileName += String.fromCharCode(fileNameRaw.charCodeAt(c) |
+                                      fileNameRaw.charCodeAt(c+1) * 256);
 
     if (aFileName == fileName)
       return true;
   }
   return false;
 }
 
 var httpserv = null;