xpcom/base/ClearOnShutdown.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set sw=2 ts=8 et ft=cpp : */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_ClearOnShutdown_h
     8 #define mozilla_ClearOnShutdown_h
    10 #include "mozilla/LinkedList.h"
    11 #include "mozilla/StaticPtr.h"
    12 #include "MainThreadUtils.h"
    14 /*
    15  * This header exports one public method in the mozilla namespace:
    16  *
    17  *   template<class SmartPtr>
    18  *   void ClearOnShutdown(SmartPtr *aPtr)
    19  *
    20  * This function takes a pointer to a smart pointer and nulls the smart pointer
    21  * on shutdown.
    22  *
    23  * This is useful if you have a global smart pointer object which you don't
    24  * want to "leak" on shutdown.
    25  *
    26  * Although ClearOnShutdown will work with any smart pointer (i.e., nsCOMPtr,
    27  * nsRefPtr, nsAutoPtr, StaticRefPtr, and StaticAutoPtr), you probably want to
    28  * use it only with StaticRefPtr and StaticAutoPtr.  There is no way to undo a
    29  * call to ClearOnShutdown, so you can call it only on smart pointers which you
    30  * know will live until the program shuts down.  In practice, these are likely
    31  * global variables, which should be Static{Ref,Auto}Ptr.
    32  *
    33  * ClearOnShutdown is currently main-thread only because we don't want to
    34  * accidentally free an object from a different thread than the one it was
    35  * created on.
    36  */
    38 namespace mozilla {
    39 namespace ClearOnShutdown_Internal {
    41 class ShutdownObserver : public LinkedListElement<ShutdownObserver>
    42 {
    43 public:
    44   virtual void Shutdown() = 0;
    45   virtual ~ShutdownObserver() {}
    46 };
    48 template<class SmartPtr>
    49 class PointerClearer : public ShutdownObserver
    50 {
    51 public:
    52   PointerClearer(SmartPtr *aPtr)
    53     : mPtr(aPtr)
    54   {}
    56   virtual void Shutdown()
    57   {
    58     if (mPtr) {
    59       *mPtr = nullptr;
    60     }
    61   }
    63 private:
    64   SmartPtr *mPtr;
    65 };
    67 extern bool sHasShutDown;
    68 extern StaticAutoPtr<LinkedList<ShutdownObserver> > sShutdownObservers;
    70 } // namespace ClearOnShutdown_Internal
    72 template<class SmartPtr>
    73 inline void ClearOnShutdown(SmartPtr *aPtr)
    74 {
    75   using namespace ClearOnShutdown_Internal;
    77   MOZ_ASSERT(NS_IsMainThread());
    78   MOZ_ASSERT(!sHasShutDown);
    80   if (!sShutdownObservers) {
    81     sShutdownObservers = new LinkedList<ShutdownObserver>();
    82   }
    83   sShutdownObservers->insertBack(new PointerClearer<SmartPtr>(aPtr));
    84 }
    86 // Called when XPCOM is shutting down, after all shutdown notifications have
    87 // been sent and after all threads' event loops have been purged.
    88 inline void KillClearOnShutdown()
    89 {
    90   using namespace ClearOnShutdown_Internal;
    92   MOZ_ASSERT(NS_IsMainThread());
    94   if (sShutdownObservers) {
    95     ShutdownObserver *observer;
    96     while ((observer = sShutdownObservers->popFirst())) {
    97       observer->Shutdown();
    98       delete observer;
    99     }
   100   }
   102   sShutdownObservers = nullptr;
   103   sHasShutDown = true;
   104 }
   106 } // namespace mozilla
   108 #endif

mercurial