Bug 1404198: Part 2j - Switch to NS_NewTimer* everywhere else. r=njn
authorKris Maglione <maglione.k@gmail.com>
Sun, 24 Sep 2017 19:57:48 -0700
changeset 386554 33b50e69601405149cb7100a8bd774a2bad035a6
parent 386553 e405a4ec473c3d510047fff16599a5f73a8d600b
child 386555 2681f1bd9e4130ff603757e1963f6c5341c836ea
push id32695
push userarchaeopteryx@coole-files.de
push dateTue, 17 Oct 2017 09:45:44 +0000
treeherdermozilla-central@0d9c6250f99d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1404198
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1404198: Part 2j - Switch to NS_NewTimer* everywhere else. r=njn MozReview-Commit-ID: LmGIgfmNSmk
accessible/base/nsAccessibilityService.cpp
accessible/generic/DocAccessible.cpp
accessible/xpcom/xpcAccessibilityService.cpp
docshell/base/nsDocShell.cpp
docshell/base/nsDocShellTreeOwner.cpp
editor/composer/nsComposerCommandsUpdater.cpp
editor/composer/nsEditingSession.cpp
editor/libeditor/TextEditRules.cpp
extensions/pref/autoconfig/src/nsAutoConfig.cpp
ipc/glue/BackgroundImpl.cpp
ipc/glue/MessagePump.cpp
mobile/android/components/build/nsAndroidHistory.cpp
parser/html/nsHtml5StreamParser.cpp
security/manager/ssl/DataStorage.cpp
startupcache/StartupCache.cpp
tools/profiler/gecko/ThreadResponsiveness.cpp
uriloader/exthandler/nsExternalHelperAppService.cpp
xpfe/appshell/nsWebShellWindow.cpp
xpfe/components/directory/nsDirectoryViewer.cpp
--- a/accessible/base/nsAccessibilityService.cpp
+++ b/accessible/base/nsAccessibilityService.cpp
@@ -463,19 +463,20 @@ nsAccessibilityService::CreatePluginAcce
 #if defined(XP_WIN) || defined(MOZ_ACCESSIBILITY_ATK)
   RefPtr<nsNPAPIPluginInstance> pluginInstance;
   if (NS_SUCCEEDED(aFrame->GetPluginInstance(getter_AddRefs(pluginInstance))) &&
       pluginInstance) {
 #ifdef XP_WIN
     if (!sPendingPlugins->Contains(aContent) &&
         (Preferences::GetBool("accessibility.delay_plugins") ||
          Compatibility::IsJAWS() || Compatibility::IsWE())) {
-      nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
       RefPtr<PluginTimerCallBack> cb = new PluginTimerCallBack(aContent);
-      timer->InitWithCallback(cb, Preferences::GetUint("accessibility.delay_plugin_time"),
+      nsCOMPtr<nsITimer> timer;
+      NS_NewTimerWithCallback(getter_AddRefs(timer),
+                              cb, Preferences::GetUint("accessibility.delay_plugin_time"),
                               nsITimer::TYPE_ONE_SHOT);
       sPluginTimers->AppendElement(timer);
       sPendingPlugins->AppendElement(aContent);
       return nullptr;
     }
 
     // We need to remove aContent from the pending plugins here to avoid
     // reentrancy.  When the timer fires it calls
--- a/accessible/generic/DocAccessible.cpp
+++ b/accessible/generic/DocAccessible.cpp
@@ -645,25 +645,24 @@ DocAccessible::ScrollPositionDidChange(n
 {
   // Start new timer, if the timer cycles at least 1 full cycle without more scroll position changes,
   // then the ::Notify() method will fire the accessibility event for scroll position changes
   const uint32_t kScrollPosCheckWait = 50;
   if (mScrollWatchTimer) {
     mScrollWatchTimer->SetDelay(kScrollPosCheckWait);  // Create new timer, to avoid leaks
   }
   else {
-    mScrollWatchTimer = do_CreateInstance("@mozilla.org/timer;1");
+    NS_NewTimerWithFuncCallback(getter_AddRefs(mScrollWatchTimer),
+                                ScrollTimerCallback,
+                                this,
+                                kScrollPosCheckWait,
+                                nsITimer::TYPE_REPEATING_SLACK,
+                                "a11y::DocAccessible::ScrollPositionDidChange");
     if (mScrollWatchTimer) {
       NS_ADDREF_THIS(); // Kung fu death grip
-      mScrollWatchTimer->InitWithNamedFuncCallback(
-        ScrollTimerCallback,
-        this,
-        kScrollPosCheckWait,
-        nsITimer::TYPE_REPEATING_SLACK,
-        "a11y::DocAccessible::ScrollPositionDidChange");
     }
   }
   mScrollPositionChangedTicks = 1;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // nsIObserver
 
--- a/accessible/xpcom/xpcAccessibilityService.cpp
+++ b/accessible/xpcom/xpcAccessibilityService.cpp
@@ -72,25 +72,22 @@ xpcAccessibilityService::Release(void)
     return 0;
   }
 
   // When ref count goes down to 1 (held internally as a static reference),
   // it means that there are no more external references to the
   // xpcAccessibilityService and we can attempt to shut down acceessiblity
   // service.
   if (count == 1 && !mShutdownTimer) {
-    mShutdownTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
-    if (mShutdownTimer) {
-      mShutdownTimer->InitWithNamedFuncCallback(
-        ShutdownCallback,
-        this,
-        100,
-        nsITimer::TYPE_ONE_SHOT,
-        "xpcAccessibilityService::Release");
-    }
+    NS_NewTimerWithFuncCallback(getter_AddRefs(mShutdownTimer),
+                                ShutdownCallback,
+                                this,
+                                100,
+                                nsITimer::TYPE_ONE_SHOT,
+                                "xpcAccessibilityService::Release");
   }
 
   return count;
 }
 
 NS_IMPL_QUERY_INTERFACE(xpcAccessibilityService, nsIAccessibilityService)
 
 NS_IMETHODIMP
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -21,16 +21,17 @@
 #include "mozilla/dom/ScreenOrientation.h"
 #include "mozilla/dom/ToJSValue.h"
 #include "mozilla/dom/PermissionMessageUtils.h"
 #include "mozilla/dom/workers/ServiceWorkerManager.h"
 #include "mozilla/EventStateManager.h"
 #include "mozilla/LoadInfo.h"
 #include "mozilla/HTMLEditor.h"
 #include "mozilla/Preferences.h"
+#include "mozilla/ResultExtensions.h"
 #include "mozilla/Services.h"
 #include "mozilla/StartupTimeline.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/Unused.h"
 #include "Navigator.h"
 #include "URIUtils.h"
 #include "mozilla/dom/DocGroup.h"
 #include "mozilla/dom/TabGroup.h"
@@ -451,33 +452,23 @@ nsPingListener::~nsPingListener()
   }
 }
 
 nsresult
 nsPingListener::StartTimeout(DocGroup* aDocGroup)
 {
   NS_ENSURE_ARG(aDocGroup);
 
-  nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
-  timer->SetTarget(aDocGroup->EventTargetFor(TaskCategory::Network));
-
-  if (timer) {
-    nsresult rv =
-      timer->InitWithNamedFuncCallback(OnPingTimeout,
-                                       mLoadGroup,
-                                       PING_TIMEOUT,
-                                       nsITimer::TYPE_ONE_SHOT,
-                                       "nsPingListener::StartTimeout");
-    if (NS_SUCCEEDED(rv)) {
-      mTimer = timer;
-      return NS_OK;
-    }
-  }
-
-  return NS_ERROR_OUT_OF_MEMORY;
+  return NS_NewTimerWithFuncCallback(getter_AddRefs(mTimer),
+                                     OnPingTimeout,
+                                     mLoadGroup,
+                                     PING_TIMEOUT,
+                                     nsITimer::TYPE_ONE_SHOT,
+                                     "nsPingListener::StartTimeout",
+                                     aDocGroup->EventTargetFor(TaskCategory::Network));
 }
 
 NS_IMETHODIMP
 nsPingListener::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
 {
   return NS_OK;
 }
 
@@ -6853,24 +6844,25 @@ nsDocShell::RefreshURI(nsIURI* aURI, int
 
   if (busyFlags & BUSY_FLAGS_BUSY || (!mIsActive && mDisableMetaRefreshWhenInactive)) {
     // We don't  want to create the timer right now. Instead queue up the request
     // and trigger the timer in EndPageLoad() or whenever we become active.
     mRefreshURIList->AppendElement(refreshTimer, /*weak =*/ false);
   } else {
     // There is no page loading going on right now.  Create the
     // timer and fire it right away.
-    nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
-    NS_ENSURE_TRUE(timer, NS_ERROR_FAILURE);
     nsCOMPtr<nsPIDOMWindowOuter> win = GetWindow();
     NS_ENSURE_TRUE(win, NS_ERROR_FAILURE);
 
+    nsCOMPtr<nsITimer> timer;
+    MOZ_TRY_VAR(timer,
+                NS_NewTimerWithCallback(refreshTimer, aDelay, nsITimer::TYPE_ONE_SHOT,
+                                        win->TabGroup()->EventTargetFor(TaskCategory::Network)));
+
     mRefreshURIList->AppendElement(timer, /*weak =*/ false);  // owning timer ref
-    timer->SetTarget(win->TabGroup()->EventTargetFor(TaskCategory::Network));
-    timer->InitWithCallback(refreshTimer, aDelay, nsITimer::TYPE_ONE_SHOT);
   }
   return NS_OK;
 }
 
 nsresult
 nsDocShell::ForceRefreshURIFromTimer(nsIURI* aURI,
                                      int32_t aDelay,
                                      bool aMetaRefresh,
@@ -7340,26 +7332,30 @@ nsDocShell::RefreshURIFromQueue()
 
     if (refreshInfo) {
       // This is the nsRefreshTimer object, waiting to be
       // setup in a timer object and fired.
       // Create the timer and  trigger it.
       uint32_t delay =
         static_cast<nsRefreshTimer*>(
           static_cast<nsITimerCallback*>(refreshInfo))->GetDelay();
-      nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
       nsCOMPtr<nsPIDOMWindowOuter> win = GetWindow();
-      if (timer && win) {
-        // Replace the nsRefreshTimer element in the queue with
-        // its corresponding timer object, so that in case another
-        // load comes through before the timer can go off, the timer will
-        // get cancelled in CancelRefreshURITimer()
-        mRefreshURIList->ReplaceElementAt(timer, n, /*weak =*/ false);
-        timer->SetTarget(win->TabGroup()->EventTargetFor(TaskCategory::Network));
-        timer->InitWithCallback(refreshInfo, delay, nsITimer::TYPE_ONE_SHOT);
+      if (win) {
+        nsCOMPtr<nsITimer> timer;
+        NS_NewTimerWithCallback(getter_AddRefs(timer),
+                                refreshInfo, delay, nsITimer::TYPE_ONE_SHOT,
+                                win->TabGroup()->EventTargetFor(TaskCategory::Network));
+
+        if (timer) {
+          // Replace the nsRefreshTimer element in the queue with
+          // its corresponding timer object, so that in case another
+          // load comes through before the timer can go off, the timer will
+          // get cancelled in CancelRefreshURITimer()
+          mRefreshURIList->ReplaceElementAt(timer, n, /*weak =*/ false);
+        }
       }
     }
   }
 
   return NS_OK;
 }
 
 //*****************************************************************************
--- a/docshell/base/nsDocShellTreeOwner.cpp
+++ b/docshell/base/nsDocShellTreeOwner.cpp
@@ -1239,40 +1239,41 @@ ChromeTooltipListener::MouseMove(nsIDOME
   mouseEvent->GetScreenX(&mMouseScreenX);
   mouseEvent->GetScreenY(&mMouseScreenY);
 
   if (mTooltipTimer) {
     mTooltipTimer->Cancel();
   }
 
   if (!mShowingTooltip && !mTooltipShownOnce) {
-    mTooltipTimer = do_CreateInstance("@mozilla.org/timer;1");
-    if (mTooltipTimer) {
-      nsCOMPtr<EventTarget> eventTarget =
-        aMouseEvent->InternalDOMEvent()->GetTarget();
-      if (eventTarget) {
-        mPossibleTooltipNode = do_QueryInterface(eventTarget);
-        nsCOMPtr<nsIGlobalObject> global(eventTarget->GetOwnerGlobal());
-        if (global) {
-          mTooltipTimer->SetTarget(global->EventTargetFor(TaskCategory::UI));
-        }
+    nsIEventTarget* target = nullptr;
+
+    nsCOMPtr<EventTarget> eventTarget =
+      aMouseEvent->InternalDOMEvent()->GetTarget();
+    if (eventTarget) {
+      mPossibleTooltipNode = do_QueryInterface(eventTarget);
+      nsCOMPtr<nsIGlobalObject> global(eventTarget->GetOwnerGlobal());
+      if (global) {
+        target = global->EventTargetFor(TaskCategory::UI);
       }
-      if (mPossibleTooltipNode) {
-        nsresult rv = mTooltipTimer->InitWithNamedFuncCallback(
-          sTooltipCallback,
-          this,
-          LookAndFeel::GetInt(LookAndFeel::eIntID_TooltipDelay, 500),
-          nsITimer::TYPE_ONE_SHOT,
-          "ChromeTooltipListener::MouseMove");
-        if (NS_FAILED(rv)) {
-          mPossibleTooltipNode = nullptr;
-        }
+    }
+
+    if (mPossibleTooltipNode) {
+      nsresult rv = NS_NewTimerWithFuncCallback(
+        getter_AddRefs(mTooltipTimer),
+        sTooltipCallback,
+        this,
+        LookAndFeel::GetInt(LookAndFeel::eIntID_TooltipDelay, 500),
+        nsITimer::TYPE_ONE_SHOT,
+        "ChromeTooltipListener::MouseMove",
+        target);
+      if (NS_FAILED(rv)) {
+        mPossibleTooltipNode = nullptr;
+        NS_WARNING("Could not create a timer for tooltip tracking");
       }
-    } else {
-      NS_WARNING("Could not create a timer for tooltip tracking");
     }
   } else {
     mTooltipShownOnce = true;
     return HideTooltip();
   }
 
   return NS_OK;
 }
--- a/editor/composer/nsComposerCommandsUpdater.cpp
+++ b/editor/composer/nsComposerCommandsUpdater.cpp
@@ -220,19 +220,18 @@ nsComposerCommandsUpdater::Init(nsPIDOMW
   mDocShell = do_GetWeakReference(aDOMWindow->GetDocShell());
   return NS_OK;
 }
 
 nsresult
 nsComposerCommandsUpdater::PrimeUpdateTimer()
 {
   if (!mUpdateTimer) {
-    nsresult rv = NS_OK;
-    mUpdateTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+    mUpdateTimer = NS_NewTimer();;
+    NS_ENSURE_TRUE(mUpdateTimer, NS_ERROR_OUT_OF_MEMORY);
   }
 
   const uint32_t kUpdateTimerDelay = 150;
   return mUpdateTimer->InitWithCallback(static_cast<nsITimerCallback*>(this),
                                         kUpdateTimerDelay,
                                         nsITimer::TYPE_ONE_SHOT);
 }
 
--- a/editor/composer/nsEditingSession.cpp
+++ b/editor/composer/nsEditingSession.cpp
@@ -962,26 +962,26 @@ nsEditingSession::EndDocumentLoad(nsIWeb
         if (NS_FAILED(rv)) {
           // If we had an error, setup timer to load a blank page later
           if (mLoadBlankDocTimer) {
             // Must cancel previous timer?
             mLoadBlankDocTimer->Cancel();
             mLoadBlankDocTimer = nullptr;
           }
 
-          mLoadBlankDocTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
-          NS_ENSURE_SUCCESS(rv, rv);
-
-          mEditorStatus = eEditorCreationInProgress;
-          mLoadBlankDocTimer->InitWithNamedFuncCallback(
+          rv = NS_NewTimerWithFuncCallback(
+            getter_AddRefs(mLoadBlankDocTimer),
             nsEditingSession::TimerCallback,
             static_cast<void*>(mDocShell.get()),
             10,
             nsITimer::TYPE_ONE_SHOT,
             "nsEditingSession::EndDocumentLoad");
+          NS_ENSURE_SUCCESS(rv, rv);
+
+          mEditorStatus = eEditorCreationInProgress;
         }
       }
     }
   }
   return rv;
 }
 
 
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -708,18 +708,18 @@ TextEditRules::WillInsertText(EditAction
 
     if (LookAndFeel::GetEchoPassword() && !DontEchoPassword()) {
       HideLastPWInput();
       mLastStart = start;
       mLastLength = outString->Length();
       if (mTimer) {
         mTimer->Cancel();
       } else {
-        mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
-        NS_ENSURE_SUCCESS(rv, rv);
+        mTimer = NS_NewTimer();
+        NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
       }
       mTimer->InitWithCallback(this, LookAndFeel::GetPasswordMaskDelay(),
                                nsITimer::TYPE_ONE_SHOT);
     } else {
       FillBufWithPWChars(outString, outString->Length());
     }
   }
 
--- a/extensions/pref/autoconfig/src/nsAutoConfig.cpp
+++ b/extensions/pref/autoconfig/src/nsAutoConfig.cpp
@@ -1,13 +1,14 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* 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 "mozilla/ResultExtensions.h"
 #include "nsAutoConfig.h"
 #include "nsIURI.h"
 #include "nsIHttpChannel.h"
 #include "nsIFileStreams.h"
 #include "nsThreadUtils.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsIObserverService.h"
 #include "nsLiteralString.h"
@@ -330,23 +331,18 @@ nsresult nsAutoConfig::downloadAutoConfi
         }
 
         int32_t minutes;
         rv = mPrefBranch->GetIntPref("autoadmin.refresh_interval",
                                      &minutes);
         if (NS_SUCCEEDED(rv) && minutes > 0) {
             // Create a new timer and pass this nsAutoConfig
             // object as a timer callback.
-            mTimer = do_CreateInstance("@mozilla.org/timer;1",&rv);
-            if (NS_FAILED(rv))
-                return rv;
-            rv = mTimer->InitWithCallback(this, minutes * 60 * 1000,
-                             nsITimer::TYPE_REPEATING_SLACK);
-            if (NS_FAILED(rv))
-                return rv;
+            MOZ_TRY_VAR(mTimer, NS_NewTimerWithCallback(this, minutes * 60 * 1000,
+                                                        nsITimer::TYPE_REPEATING_SLACK));
         }
     } //first_time
 
     return NS_OK;
 } // nsPref::downloadAutoConfig()
 
 
 
--- a/ipc/glue/BackgroundImpl.cpp
+++ b/ipc/glue/BackgroundImpl.cpp
@@ -1025,19 +1025,18 @@ ParentImpl::CreateBackgroundThread()
     NS_WARNING("Trying to create background thread after shutdown has "
                "already begun!");
     return false;
   }
 
   nsCOMPtr<nsITimer> newShutdownTimer;
 
   if (!sShutdownTimer) {
-    nsresult rv;
-    newShutdownTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
+    newShutdownTimer = NS_NewTimer();
+    if (!newShutdownTimer) {
       return false;
     }
   }
 
   if (!sShutdownObserverRegistered) {
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     if (NS_WARN_IF(!obs)) {
       return false;
--- a/ipc/glue/MessagePump.cpp
+++ b/ipc/glue/MessagePump.cpp
@@ -81,17 +81,17 @@ MessagePump::Run(MessagePump::Delegate* 
   MOZ_ASSERT(keep_running_);
   MOZ_RELEASE_ASSERT(NS_IsMainThread(),
                      "Use mozilla::ipc::MessagePumpForNonMainThreads instead!");
   MOZ_RELEASE_ASSERT(!mEventTarget);
 
   nsIThread* thisThread = NS_GetCurrentThread();
   MOZ_ASSERT(thisThread);
 
-  mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
+  mDelayedWorkTimer = NS_NewTimer();
   MOZ_ASSERT(mDelayedWorkTimer);
 
   base::ScopedNSAutoreleasePool autoReleasePool;
 
   for (;;) {
     autoReleasePool.Recycle();
 
     bool did_work = NS_ProcessNextEvent(thisThread, false) ? true : false;
@@ -157,17 +157,17 @@ void
 MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime)
 {
   // To avoid racing on mDelayedWorkTimer, we need to be on the same thread as
   // ::Run().
   MOZ_RELEASE_ASSERT((!mEventTarget && NS_IsMainThread())
                      || mEventTarget->IsOnCurrentThread());
 
   if (!mDelayedWorkTimer) {
-    mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
+    mDelayedWorkTimer = NS_NewTimer();
     if (!mDelayedWorkTimer) {
         // Called before XPCOM has started up? We can't do this correctly.
         NS_WARNING("Delayed task might not run!");
         delayed_work_time_ = aDelayedTime;
         return;
     }
   }
 
@@ -305,23 +305,19 @@ void
 MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate* aDelegate)
 {
   MOZ_ASSERT(keep_running_);
   MOZ_RELEASE_ASSERT(!NS_IsMainThread(), "Use mozilla::ipc::MessagePump instead!");
 
   nsIThread* thread = NS_GetCurrentThread();
   MOZ_RELEASE_ASSERT(mEventTarget->IsOnCurrentThread());
 
-  mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
+  mDelayedWorkTimer = NS_NewTimer(mEventTarget);
   MOZ_ASSERT(mDelayedWorkTimer);
 
-  if (NS_FAILED(mDelayedWorkTimer->SetTarget(mEventTarget))) {
-    MOZ_CRASH("Failed to set timer target!");
-  }
-
   // Chromium event notifications to be processed will be received by this
   // event loop as a DoWorkRunnables via ScheduleWork. Chromium events that
   // were received before our thread is valid, however, will not generate
   // runnable wrappers. We must process any of these before we enter this
   // loop, or we will forever have unprocessed chromium messages in our queue.
   //
   // Note we would like to request a flush of the chromium event queue
   // using a runnable on the xpcom side, but some thread implementations
--- a/mobile/android/components/build/nsAndroidHistory.cpp
+++ b/mobile/android/components/build/nsAndroidHistory.cpp
@@ -42,17 +42,17 @@ nsAndroidHistory::GetSingleton()
   return sHistory;
 }
 
 nsAndroidHistory::nsAndroidHistory()
   : mHistoryEnabled(true)
 {
   LoadPrefs();
 
-  mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+  mTimer = NS_NewTimer();
 }
 
 NS_IMETHODIMP
 nsAndroidHistory::RegisterVisitedCallback(nsIURI *aURI, Link *aContent)
 {
   if (!aContent || !aURI)
     return NS_OK;
 
--- a/parser/html/nsHtml5StreamParser.cpp
+++ b/parser/html/nsHtml5StreamParser.cpp
@@ -176,17 +176,17 @@ nsHtml5StreamParser::nsHtml5StreamParser
   , mTerminated(false)
   , mInterrupted(false)
   , mTerminatedMutex("nsHtml5StreamParser mTerminatedMutex")
   , mEventTarget(nsHtml5Module::GetStreamParserThread()->SerialEventTarget())
   , mExecutorFlusher(new nsHtml5ExecutorFlusher(aExecutor))
   , mLoadFlusher(new nsHtml5LoadFlusher(aExecutor))
   , mFeedChardet(false)
   , mInitialEncodingWasFromParentFrame(false)
-  , mFlushTimer(do_CreateInstance("@mozilla.org/timer;1"))
+  , mFlushTimer(NS_NewTimer())
   , mFlushTimerMutex("nsHtml5StreamParser mFlushTimerMutex")
   , mFlushTimerArmed(false)
   , mFlushTimerEverFired(false)
   , mMode(aMode)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   mFlushTimer->SetTarget(mEventTarget);
 #ifdef DEBUG
--- a/security/manager/ssl/DataStorage.cpp
+++ b/security/manager/ssl/DataStorage.cpp
@@ -958,18 +958,18 @@ DataStorage::SetTimer()
 {
   MOZ_ASSERT(!NS_IsMainThread());
   MOZ_ASSERT(XRE_IsParentProcess());
 
   MutexAutoLock lock(mMutex);
 
   nsresult rv;
   if (!mTimer) {
-    mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
+    mTimer = NS_NewTimer();
+    if (NS_WARN_IF(!mTimer)) {
       return;
     }
   }
 
   rv = mTimer->InitWithNamedFuncCallback(TimerCallback,
                                          this,
                                          mTimerDelay,
                                          nsITimer::TYPE_ONE_SHOT,
--- a/startupcache/StartupCache.cpp
+++ b/startupcache/StartupCache.cpp
@@ -576,19 +576,19 @@ StartupCache::GetDebugObjectOutputStream
 
   return NS_OK;
 }
 
 nsresult
 StartupCache::ResetStartupWriteTimer()
 {
   mStartupWriteInitiated = false;
-  nsresult rv;
+  nsresult rv = NS_OK;
   if (!mTimer)
-    mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
+    mTimer = NS_NewTimer();
   else
     rv = mTimer->Cancel();
   NS_ENSURE_SUCCESS(rv, rv);
   // Wait for 10 seconds, then write out the cache.
   mTimer->InitWithNamedFuncCallback(StartupCache::WriteTimeout, this, 60000,
                                     nsITimer::TYPE_ONE_SHOT,
                                     "StartupCache::WriteTimeout");
   return NS_OK;
--- a/tools/profiler/gecko/ThreadResponsiveness.cpp
+++ b/tools/profiler/gecko/ThreadResponsiveness.cpp
@@ -57,18 +57,18 @@ public:
 
   // Can only run on the main thread.
   NS_IMETHOD Run() override
   {
     mStartToPrevTracer_us = uint64_t(profiler_time() * 1000.0);
 
     if (!mStop) {
       if (!mTimer) {
-        mTimer = do_CreateInstance("@mozilla.org/timer;1");
-        mTimer->SetTarget(SystemGroup::EventTargetFor(TaskCategory::Other));
+        mTimer = NS_NewTimer(
+          SystemGroup::EventTargetFor(TaskCategory::Other));
       }
       mTimer->InitWithCallback(this, 16, nsITimer::TYPE_ONE_SHOT);
     }
 
     return NS_OK;
   }
 
   // Main thread only
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -4,16 +4,17 @@
  * 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 "base/basictypes.h"
 
 /* This must occur *after* base/basictypes.h to avoid typedefs conflicts. */
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Base64.h"
+#include "mozilla/ResultExtensions.h"
 
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/TabChild.h"
 #include "nsXULAppAPI.h"
 
 #include "nsExternalHelperAppService.h"
 #include "nsCExternalHandlerService.h"
 #include "nsIURI.h"
@@ -2526,22 +2527,17 @@ nsresult nsExternalAppHandler::MaybeClos
     nsCOMPtr<nsPIDOMWindowOuter> opener = window->GetOpener();
 
     if (opener && !opener->Closed()) {
       mContentContext = do_GetInterface(opener);
 
       // Now close the old window.  Do it on a timer so that we don't run
       // into issues trying to close the window before it has fully opened.
       NS_ASSERTION(!mTimer, "mTimer was already initialized once!");
-      mTimer = do_CreateInstance("@mozilla.org/timer;1");
-      if (!mTimer) {
-        return NS_ERROR_FAILURE;
-      }
-
-      mTimer->InitWithCallback(this, 0, nsITimer::TYPE_ONE_SHOT);
+      MOZ_TRY_VAR(mTimer, NS_NewTimerWithCallback(this, 0, nsITimer::TYPE_ONE_SHOT));
       mWindowToClose = window;
     }
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
--- a/xpfe/appshell/nsWebShellWindow.cpp
+++ b/xpfe/appshell/nsWebShellWindow.cpp
@@ -569,17 +569,17 @@ NS_IMPL_ISUPPORTS(WebShellWindowTimerCal
 
 } // namespace mozilla
 
 void
 nsWebShellWindow::SetPersistenceTimer(uint32_t aDirtyFlags)
 {
   MutexAutoLock lock(mSPTimerLock);
   if (!mSPTimer) {
-    mSPTimer = do_CreateInstance("@mozilla.org/timer;1");
+    mSPTimer = NS_NewTimer();
     if (!mSPTimer) {
       NS_WARNING("Couldn't create @mozilla.org/timer;1 instance?");
       return;
     }
   }
 
   RefPtr<WebShellWindowTimerCallback> callback =
     new WebShellWindowTimerCallback(this);
--- a/xpfe/components/directory/nsDirectoryViewer.cpp
+++ b/xpfe/components/directory/nsDirectoryViewer.cpp
@@ -853,67 +853,59 @@ nsHTTPIndex::GetTargets(nsIRDFResource *
 		    {
     		    // add aSource into list of connections to make
 	    	    mConnectionList->AppendElement(aSource, /*weak =*/ false);
 
                 // if we don't have a timer about to fire, create one
                 // which should fire as soon as possible (out-of-band)
             	if (!mTimer)
             	{
-            		mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
+                    rv = NS_NewTimerWithFuncCallback(getter_AddRefs(mTimer),
+                                                     nsHTTPIndex::FireTimer,
+                                                     this,
+                                                     1,
+                                                     nsITimer::TYPE_ONE_SHOT,
+                                                     "nsHTTPIndex::GetTargets");
+                    // Note: don't addref "this" as we'll cancel the
+                    // timer in the httpIndex destructor
             		NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create a timer");
-            		if (NS_SUCCEEDED(rv))
-            		{
-                          mTimer->InitWithNamedFuncCallback(
-                            nsHTTPIndex::FireTimer,
-                            this,
-                            1,
-                            nsITimer::TYPE_ONE_SHOT,
-                            "nsHTTPIndex::GetTargets");
-                          // Note: don't addref "this" as we'll cancel the
-                          // timer in the httpIndex destructor
-                        }
             	}
 	    	}
 		}
 	}
 
 	return(rv);
 }
 
 
 nsresult
 nsHTTPIndex::AddElement(nsIRDFResource *parent, nsIRDFResource *prop, nsIRDFNode *child)
 {
-    nsresult    rv;
 
     if (!mNodeList)
     {
         mNodeList = nsArray::Create();
     }
 
     // order required: parent, prop, then child
     mNodeList->AppendElement(parent, /*weak =*/ false);
     mNodeList->AppendElement(prop, /*weak =*/ false);
     mNodeList->AppendElement(child, /*weak = */ false);
 
 	if (!mTimer)
 	{
-		mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
-		NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create a timer");
-		if (NS_FAILED(rv))  return(rv);
-
-                mTimer->InitWithNamedFuncCallback(nsHTTPIndex::FireTimer,
-                                                  this,
-                                                  1,
-                                                  nsITimer::TYPE_ONE_SHOT,
-                                                  "nsHTTPIndex::AddElement");
-                // Note: don't addref "this" as we'll cancel the
-                // timer in the httpIndex destructor
-        }
+        return NS_NewTimerWithFuncCallback(getter_AddRefs(mTimer),
+                                           nsHTTPIndex::FireTimer,
+                                           this,
+                                           1,
+                                           nsITimer::TYPE_ONE_SHOT,
+                                           "nsHTTPIndex::AddElement");
+        // Note: don't addref "this" as we'll cancel the
+        // timer in the httpIndex destructor
+    }
 
     return(NS_OK);
 }
 
 void
 nsHTTPIndex::FireTimer(nsITimer* aTimer, void* aClosure)
 {
   nsHTTPIndex *httpIndex = static_cast<nsHTTPIndex *>(aClosure);
@@ -1029,27 +1021,24 @@ nsHTTPIndex::FireTimer(nsITimer* aTimer,
   // weak reference back to nsHTTPIndex
   httpIndex->mTimer->Cancel();
   httpIndex->mTimer = nullptr;
 
   // after firing off any/all of the connections be sure
   // to cancel the timer if we don't need to refire it
   if (refireTimer)
   {
-    httpIndex->mTimer = do_CreateInstance("@mozilla.org/timer;1");
-    if (httpIndex->mTimer)
-    {
-      httpIndex->mTimer->InitWithNamedFuncCallback(nsHTTPIndex::FireTimer,
-                                                   aClosure,
-                                                   10,
-                                                   nsITimer::TYPE_ONE_SHOT,
-                                                   "nsHTTPIndex::FireTimer");
-      // Note: don't addref "this" as we'll cancel the
-      // timer in the httpIndex destructor
-    }
+    NS_NewTimerWithFuncCallback(getter_AddRefs(httpIndex->mTimer),
+                                nsHTTPIndex::FireTimer,
+                                aClosure,
+                                10,
+                                nsITimer::TYPE_ONE_SHOT,
+                                "nsHTTPIndex::FireTimer");
+    // Note: don't addref "this" as we'll cancel the
+    // timer in the httpIndex destructor
   }
 }
 
 NS_IMETHODIMP
 nsHTTPIndex::Assert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget,
 			bool aTruthValue)
 {
 	nsresult	rv = NS_ERROR_UNEXPECTED;