Bug 1186785 - Replace nsBaseHashtable::EnumerateRead() calls in toolkit/ with iterators. r=froydnj.
authorNicholas Nethercote <nnethercote@mozilla.com>
Mon, 19 Oct 2015 15:46:46 -0700
changeset 291942 551ecd4bb718f7f2d00df8716a143ac4f43d0dca
parent 291941 92eb00ee19771b8ea7027d10acec971430a69143
child 291943 fa1abcf44453ef605a4e1b33cc5e99879a35af32
push idunknown
push userunknown
push dateunknown
reviewersfroydnj
bugs1186785
milestone44.0a1
Bug 1186785 - Replace nsBaseHashtable::EnumerateRead() calls in toolkit/ with iterators. r=froydnj.
toolkit/components/filewatcher/NativeFileWatcherWin.cpp
toolkit/components/places/nsNavHistory.cpp
toolkit/components/remote/nsGTKRemoteService.cpp
toolkit/components/remote/nsGTKRemoteService.h
toolkit/crashreporter/nsExceptionHandler.cpp
--- a/toolkit/components/filewatcher/NativeFileWatcherWin.cpp
+++ b/toolkit/components/filewatcher/NativeFileWatcherWin.cpp
@@ -1,8 +1,10 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /**
  * Native implementation of Watcher operations.
  */
 #include "NativeFileWatcherWin.h"
@@ -246,31 +248,16 @@ public:
     return NS_OK;
   }
 
 private:
   nsCOMPtr<nsIThread> mWorkerThread;
 };
 
 /**
- * An helper callback function used to print information about any
- * pending watch when shutting down the nsINativeFileWatcher service.
- */
-static PLDHashOperator
-WatchedPathsInfoHashtableTraverser(nsVoidPtrHashKey::KeyType key,
-                                   WatchedResourceDescriptor* watchedResource,
-                                   void* userArg)
-{
-  FILEWATCHERLOG("NativeFileWatcherIOTask::DeactivateRunnableMethod - "
-                 "%S is still being watched.", watchedResource->mPath.get());
-
-  return PL_DHASH_NEXT;
-}
-
-/**
  * This runnable is dispatched from the main thread to get the notifications of the
  * changes in the watched resources by continuously calling the blocking function
  * GetQueuedCompletionStatus. This function queries the status of the Completion I/O
  * port initialized in the main thread. The watched resources are registered to the
  * completion I/O port when calling |addPath|.
  *
  * Instead of using a loop within the Run() method, the Runnable reschedules itself
  * by issuing a NS_DispatchToCurrentThread(this) before exiting. This is done to allow
@@ -850,18 +837,21 @@ NativeFileWatcherIOTask::DeactivateRunna
   MOZ_ASSERT(!NS_IsMainThread());
 
   // Remind users to manually remove the watches before quitting.
   MOZ_ASSERT(!mWatchedResourcesByHandle.Count(),
              "Clients of the nsINativeFileWatcher must remove "
              "watches manually before quitting.");
 
   // Log any pending watch.
-  (void)mWatchedResourcesByHandle.EnumerateRead(
-    &WatchedPathsInfoHashtableTraverser, nullptr);
+  for (auto it = mWatchedResourcesByHandle.Iter(); !it.Done(); it.Next()) {
+    FILEWATCHERLOG("NativeFileWatcherIOTask::DeactivateRunnableMethod - "
+                   "%S is still being watched.", it.UserData()->mPath.get());
+
+  }
 
   // We return immediately if |mShuttingDown| is true (see below for
   // details about the shutdown protocol being followed).
   if (mShuttingDown) {
     // If this happens, we are in a strange situation.
     FILEWATCHERLOG(
       "NativeFileWatcherIOTask::DeactivateRunnableMethod - We are already shutting down.");
     MOZ_CRASH();
--- a/toolkit/components/places/nsNavHistory.cpp
+++ b/toolkit/components/places/nsNavHistory.cpp
@@ -1,9 +1,10 @@
-//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <stdio.h>
 
 #include "mozilla/DebugOnly.h"
 
@@ -2147,29 +2148,16 @@ nsNavHistory::ConstructQueryString(
                                            useLimitClause, aAddParams,
                                            hasSearchTerms);
   rv = queryStringBuilder.GetQueryString(queryString);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
-PLDHashOperator BindAdditionalParameter(nsNavHistory::StringHash::KeyType aParamName,
-                                        nsCString aParamValue,
-                                        void* aStatement)
-{
-  mozIStorageStatement* stmt = static_cast<mozIStorageStatement*>(aStatement);
-
-  nsresult rv = stmt->BindUTF8StringByName(aParamName, aParamValue);
-  if (NS_FAILED(rv))
-    return PL_DHASH_STOP;
-
-  return PL_DHASH_NEXT;
-}
-
 // nsNavHistory::GetQueryResults
 //
 //    Call this to get the results from a complex query. This is used by
 //    nsNavHistoryQueryResultNode to populate its children. For simple bookmark
 //    queries, use nsNavBookmarks::QueryFolderChildren.
 //
 //    THIS DOES NOT DO SORTING. You will need to sort the container yourself
 //    when you get the results. This is because sorting depends on tree
@@ -2216,17 +2204,22 @@ nsNavHistory::GetQueryResults(nsNavHisto
     // bind parameters
     int32_t i;
     for (i = 0; i < aQueries.Count(); i++) {
       rv = BindQueryClauseParameters(statement, i, aQueries[i], aOptions);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
 
-  addParams.EnumerateRead(BindAdditionalParameter, statement.get());
+  for (auto iter = addParams.Iter(); !iter.Done(); iter.Next()) {
+    nsresult rv = statement->BindUTF8StringByName(iter.Key(), iter.Data());
+    if (NS_FAILED(rv)) {
+      break;
+    }
+  }
 
   // Optimize the case where there is no need for any post-query filtering.
   if (NeedToFilterResultSet(aQueries, aOptions)) {
     // Generate the top-level results.
     nsCOMArray<nsNavHistoryResultNode> toplevel;
     rv = ResultsAsList(statement, aOptions, &toplevel);
     NS_ENSURE_SUCCESS(rv, rv);
 
@@ -3042,17 +3035,23 @@ nsNavHistory::AsyncExecuteLegacyQueries(
   if (paramsPresent) {
     // bind parameters
     int32_t i;
     for (i = 0; i < queries.Count(); i++) {
       rv = BindQueryClauseParameters(statement, i, queries[i], options);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
-  addParams.EnumerateRead(BindAdditionalParameter, statement.get());
+
+  for (auto iter = addParams.Iter(); !iter.Done(); iter.Next()) {
+    nsresult rv = statement->BindUTF8StringByName(iter.Key(), iter.Data());
+    if (NS_FAILED(rv)) {
+      break;
+    }
+  }
 
   rv = statement->ExecuteAsync(aCallback, _stmt);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
--- a/toolkit/components/remote/nsGTKRemoteService.cpp
+++ b/toolkit/components/remote/nsGTKRemoteService.cpp
@@ -38,33 +38,23 @@ nsGTKRemoteService::Startup(const char* 
   if (mServerWindow) return NS_ERROR_ALREADY_INITIALIZED;
 
   XRemoteBaseStartup(aAppName, aProfileName);
 
   mServerWindow = gtk_invisible_new();
   gtk_widget_realize(mServerWindow);
   HandleCommandsFor(mServerWindow, nullptr);
 
-  mWindows.EnumerateRead(StartupHandler, this);
+  for (auto iter = mWindows.Iter(); !iter.Done(); iter.Next()) {
+    HandleCommandsFor(iter.Key(), iter.UserData());
+  }
 
   return NS_OK;
 }
 
-PLDHashOperator
-nsGTKRemoteService::StartupHandler(GtkWidget* aKey,
-                                   nsIWeakReference* aData,
-                                   void* aClosure)
-{
-  GtkWidget* widget = (GtkWidget*) aKey;
-  nsGTKRemoteService* aThis = (nsGTKRemoteService*) aClosure;
-
-  aThis->HandleCommandsFor(widget, aData);
-  return PL_DHASH_NEXT;
-}
-
 static nsIWidget* GetMainWidget(nsIDOMWindow* aWindow)
 {
   // get the native window for this instance
   nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
   NS_ENSURE_TRUE(window, nullptr);
 
   nsCOMPtr<nsIBaseWindow> baseWindow
     (do_QueryInterface(window->GetDocShell()));
--- a/toolkit/components/remote/nsGTKRemoteService.h
+++ b/toolkit/components/remote/nsGTKRemoteService.h
@@ -29,21 +29,16 @@ public:
 
 private:
   ~nsGTKRemoteService() { }
 
   void HandleCommandsFor(GtkWidget* aWidget,
                          nsIWeakReference* aWindow);
 
 
-  static PLDHashOperator StartupHandler(GtkWidget* aKey,
-                                        nsIWeakReference* aData,
-                                        void* aClosure);
-
-
   static gboolean HandlePropertyChange(GtkWidget *widget,
                                        GdkEventProperty *event,
                                        nsIWeakReference* aThis);
 
 
   virtual void SetDesktopStartupIDOrTimestamp(const nsACString& aDesktopStartupID,
                                               uint32_t aTimestamp) override;
 
--- a/toolkit/crashreporter/nsExceptionHandler.cpp
+++ b/toolkit/crashreporter/nsExceptionHandler.cpp
@@ -1,9 +1,10 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsExceptionHandler.h"
 #include "nsDataHashtable.h"
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/dom/CrashReporterChild.h"
@@ -1703,33 +1704,16 @@ IsInWhitelist(const nsACString& key)
   for (size_t i = 0; i < ArrayLength(kCrashEventAnnotations); ++i) {
     if (key.EqualsASCII(kCrashEventAnnotations[i])) {
       return true;
     }
   }
   return false;
 }
 
-static PLDHashOperator EnumerateEntries(const nsACString& key,
-                                        nsCString entry,
-                                        void* userData)
-{
-  if (!entry.IsEmpty()) {
-    NS_NAMED_LITERAL_CSTRING(kEquals, "=");
-    NS_NAMED_LITERAL_CSTRING(kNewline, "\n");
-    nsAutoCString line = key + kEquals + entry + kNewline;
-
-    crashReporterAPIData->Append(line);
-    if (IsInWhitelist(key)) {
-      crashEventAPIData->Append(line);
-    }
-  }
-  return PL_DHASH_NEXT;
-}
-
 // This function is miscompiled with MSVC 2005/2008 when PGO is on.
 #ifdef _MSC_VER
 #pragma optimize("", off)
 #endif
 static nsresult
 EscapeAnnotation(const nsACString& key, const nsACString& data, nsCString& escapedData)
 {
   if (DoFindInReadable(key, NS_LITERAL_CSTRING("=")) ||
@@ -1813,17 +1797,30 @@ nsresult AnnotateCrashReport(const nsACS
 
   MutexAutoLock lock(*crashReporterAPILock);
 
   crashReporterAPIData_Hash->Put(key, escapedData);
 
   // now rebuild the file contents
   crashReporterAPIData->Truncate(0);
   crashEventAPIData->Truncate(0);
-  crashReporterAPIData_Hash->EnumerateRead(EnumerateEntries, nullptr);
+  for (auto it = crashReporterAPIData_Hash->Iter(); !it.Done(); it.Next()) {
+    const nsACString& key = it.Key();
+    nsCString entry = it.Data();
+    if (!entry.IsEmpty()) {
+      NS_NAMED_LITERAL_CSTRING(kEquals, "=");
+      NS_NAMED_LITERAL_CSTRING(kNewline, "\n");
+      nsAutoCString line = key + kEquals + entry + kNewline;
+
+      crashReporterAPIData->Append(line);
+      if (IsInWhitelist(key)) {
+        crashEventAPIData->Append(line);
+      }
+    }
+  }
 
   return NS_OK;
 }
 
 nsresult RemoveCrashReportAnnotation(const nsACString& key)
 {
   return AnnotateCrashReport(key, NS_LITERAL_CSTRING(""));
 }
@@ -2526,65 +2523,48 @@ struct Blacklist {
         return true;
     return false;
   }
 
   const char** mItems;
   const int mLen;
 };
 
-struct EnumerateAnnotationsContext {
-  const Blacklist& blacklist;
-  PRFileDesc* fd;
-};
-
 static void
 WriteAnnotation(PRFileDesc* fd, const nsACString& key, const nsACString& value)
 {
   PR_Write(fd, key.BeginReading(), key.Length());
   PR_Write(fd, "=", 1);
   PR_Write(fd, value.BeginReading(), value.Length());
   PR_Write(fd, "\n", 1);
 }
 
-static PLDHashOperator
-EnumerateAnnotations(const nsACString& key,
-                     nsCString entry,
-                     void* userData)
-{
-  EnumerateAnnotationsContext* ctx =
-    static_cast<EnumerateAnnotationsContext*>(userData);
-  const Blacklist& blacklist = ctx->blacklist;
-
-  // skip entries in the blacklist
-  if (blacklist.Contains(key))
-      return PL_DHASH_NEXT;
-
-  WriteAnnotation(ctx->fd, key, entry);
-
-  return PL_DHASH_NEXT;
-}
-
 static bool
 WriteExtraData(nsIFile* extraFile,
                const AnnotationTable& data,
                const Blacklist& blacklist,
                bool writeCrashTime=false,
                bool truncate=false)
 {
   PRFileDesc* fd;
   int truncOrAppend = truncate ? PR_TRUNCATE : PR_APPEND;
-  nsresult rv = 
+  nsresult rv =
     extraFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | truncOrAppend,
                                 0600, &fd);
   if (NS_FAILED(rv))
     return false;
 
-  EnumerateAnnotationsContext ctx = { blacklist, fd };
-  data.EnumerateRead(EnumerateAnnotations, &ctx);
+  for (auto iter = data.ConstIter(); !iter.Done(); iter.Next()) {
+    // Skip entries in the blacklist.
+    const nsACString& key = iter.Key();
+    if (blacklist.Contains(key)) {
+        continue;
+    }
+    WriteAnnotation(fd, key, iter.Data());
+  }
 
   if (writeCrashTime) {
     time_t crashTime = time(nullptr);
     char crashTimeString[32];
     XP_TTOA(crashTime, crashTimeString, 10);
 
     WriteAnnotation(fd,
                     nsDependentCString("CrashTime"),