Bug 1414441 - Stop caching an XPConnect pointer in storage/. r=mak
authorAndrew McCreight <continuation@gmail.com>
Fri, 03 Nov 2017 15:50:29 -0700
changeset 695778 b98366a27c05e617771746cd10f1f685a1b82dff
parent 695777 a74c645411e7a93fd48ec1d4e17c4c771a2339a2
child 695779 4aa421a32de9a1bb122062ebec128cea5789baa8
push id88535
push userbmo:eoger@fastmail.com
push dateThu, 09 Nov 2017 20:23:39 +0000
reviewersmak
bugs1414441
milestone58.0a1
Bug 1414441 - Stop caching an XPConnect pointer in storage/. r=mak Services.h has a built in cache now MozReview-Commit-ID: 362qIrzf13F
storage/mozStorageService.cpp
storage/mozStorageService.h
storage/mozStorageStatementJSHelper.cpp
--- a/storage/mozStorageService.cpp
+++ b/storage/mozStorageService.cpp
@@ -226,36 +226,16 @@ Service::getSingleton()
   if (NS_FAILED(service->initialize())) {
     gService = nullptr;
     return nullptr;
   }
 
   return service.forget();
 }
 
-nsIXPConnect *Service::sXPConnect = nullptr;
-
-// static
-already_AddRefed<nsIXPConnect>
-Service::getXPConnect()
-{
-  NS_PRECONDITION(NS_IsMainThread(),
-                  "Must only get XPConnect on the main thread!");
-  NS_PRECONDITION(gService,
-                  "Can not get XPConnect without an instance of our service!");
-
-  // If we've been shutdown, sXPConnect will be null.  To prevent leaks, we do
-  // not cache the service after this point.
-  nsCOMPtr<nsIXPConnect> xpc(sXPConnect);
-  if (!xpc)
-    xpc = do_GetService(nsIXPConnect::GetCID());
-  NS_ASSERTION(xpc, "Could not get XPConnect!");
-  return xpc.forget();
-}
-
 int32_t Service::sSynchronousPref;
 
 // static
 int32_t
 Service::getSynchronousPref()
 {
   return sSynchronousPref;
 }
@@ -274,18 +254,16 @@ Service::~Service()
 {
   mozilla::UnregisterWeakMemoryReporter(this);
   mozilla::UnregisterStorageSQLiteDistinguishedAmount();
 
   int rc = sqlite3_vfs_unregister(mSqliteVFS);
   if (rc != SQLITE_OK)
     NS_WARNING("Failed to unregister sqlite vfs wrapper.");
 
-  shutdown(); // To release sXPConnect.
-
   gService = nullptr;
   delete mSqliteVFS;
   mSqliteVFS = nullptr;
 }
 
 void
 Service::registerConnection(Connection *aConnection)
 {
@@ -390,28 +368,21 @@ Service::minimizeMemory()
         NewRunnableMethod<const nsCString>(
           "Connection::ExecuteSimpleSQL",
           conn, &Connection::ExecuteSimpleSQL, shrinkPragma);
       Unused << conn->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
     }
   }
 }
 
-void
-Service::shutdown()
-{
-  NS_IF_RELEASE(sXPConnect);
-}
-
 sqlite3_vfs *ConstructTelemetryVFS();
 const char *GetVFSName();
 
 static const char* sObserverTopics[] = {
   "memory-pressure",
-  "xpcom-shutdown",
   "xpcom-shutdown-threads"
 };
 
 nsresult
 Service::initialize()
 {
   MOZ_ASSERT(NS_IsMainThread(), "Must be initialized on the main thread");
 
@@ -423,32 +394,26 @@ Service::initialize()
   if (mSqliteVFS) {
     rc = sqlite3_vfs_register(mSqliteVFS, 0);
     if (rc != SQLITE_OK)
       return convertResultCode(rc);
   } else {
     NS_WARNING("Failed to register telemetry VFS");
   }
 
-  // Register for xpcom-shutdown so we can cleanup after ourselves.  The
-  // observer service can only be used on the main thread.
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   NS_ENSURE_TRUE(os, NS_ERROR_FAILURE);
 
   for (size_t i = 0; i < ArrayLength(sObserverTopics); ++i) {
     nsresult rv = os->AddObserver(this, sObserverTopics[i], false);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
-  // We cache XPConnect for our language helpers.  XPConnect can only be
-  // used on the main thread.
-  (void)CallGetService(nsIXPConnect::GetCID(), &sXPConnect);
-
   // We need to obtain the toolkit.storage.synchronous preferences on the main
   // thread because the preference service can only be accessed there.  This
   // is cached in the service for all future Open[Unshared]Database calls.
   sSynchronousPref =
     Preferences::GetInt(PREF_TS_SYNCHRONOUS, PREF_TS_SYNCHRONOUS_DEFAULT);
 
   // We need to obtain the toolkit.storage.pageSize preferences on the main
   // thread because the preference service can only be accessed there.  This
@@ -799,18 +764,16 @@ Service::BackupDatabaseFile(nsIFile *aDB
 ////////////////////////////////////////////////////////////////////////////////
 //// nsIObserver
 
 NS_IMETHODIMP
 Service::Observe(nsISupports *, const char *aTopic, const char16_t *)
 {
   if (strcmp(aTopic, "memory-pressure") == 0) {
     minimizeMemory();
-  } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
-    shutdown();
   } else if (strcmp(aTopic, "xpcom-shutdown-threads") == 0) {
     // The Service is kept alive by our strong observer references and
     // references held by Connection instances.  Since we're about to remove the
     // former and then wait for the latter ones to go away, it behooves us to
     // hold a strong reference to ourselves so our calls to getConnections() do
     // not happen on a deleted object.
     RefPtr<Service> kungFuDeathGrip = this;
 
--- a/storage/mozStorageService.h
+++ b/storage/mozStorageService.h
@@ -13,17 +13,16 @@
 #include "nsIMemoryReporter.h"
 #include "nsIObserver.h"
 #include "nsTArray.h"
 #include "mozilla/Mutex.h"
 
 #include "mozIStorageService.h"
 
 class nsIMemoryReporter;
-class nsIXPConnect;
 struct sqlite3_vfs;
 
 namespace mozilla {
 namespace storage {
 
 class Connection;
 class Service : public mozIStorageService
               , public nsIObserver
@@ -55,22 +54,16 @@ public:
   static already_AddRefed<Service> getSingleton();
 
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_MOZISTORAGESERVICE
   NS_DECL_NSIOBSERVER
   NS_DECL_NSIMEMORYREPORTER
 
   /**
-   * Obtains an already AddRefed pointer to XPConnect.  This is used by
-   * language helpers.
-   */
-  static already_AddRefed<nsIXPConnect> getXPConnect();
-
-  /**
    * Obtains the cached data for the toolkit.storage.synchronous preference.
    */
   static int32_t getSynchronousPref();
 
   /**
    * Obtains the default page size for this platform. The default value is
    * specified in the SQLite makefile (SQLITE_DEFAULT_PAGE_SIZE) but it may be
    * overriden with the PREF_TS_PAGESIZE hidden preference.
@@ -152,21 +145,16 @@ private:
 
   /**
    * Frees as much heap memory as possible from all of the known open
    * connections.
    */
   void minimizeMemory();
 
   /**
-   * Shuts down the storage service, freeing all of the acquired resources.
-   */
-  void shutdown();
-
-  /**
    * Lazily creates and returns a collation created from the application's
    * locale that all statements of all Connections of this Service may use.
    * Since the collation's lifetime is that of the Service and no statement may
    * execute outside the lifetime of the Service, this method returns a raw
    * pointer.
    */
   nsICollation *getLocaleCollation();
 
@@ -180,18 +168,16 @@ private:
   nsCOMPtr<nsICollation> mLocaleCollation;
 
   nsCOMPtr<nsIFile> mProfileStorageFile;
 
   nsCOMPtr<nsIMemoryReporter> mStorageSQLiteReporter;
 
   static Service *gService;
 
-  static nsIXPConnect *sXPConnect;
-
   static int32_t sSynchronousPref;
   static int32_t sDefaultPageSize;
 };
 
 } // namespace storage
 } // namespace mozilla
 
 #endif /* MOZSTORAGESERVICE_H */
--- a/storage/mozStorageStatementJSHelper.cpp
+++ b/storage/mozStorageStatementJSHelper.cpp
@@ -16,29 +16,31 @@
 
 #include "mozStorageStatementRow.h"
 #include "mozStorageStatementParams.h"
 
 #include "jsapi.h"
 
 #include "xpc_make_class.h"
 
+#include "mozilla/Services.h"
+
 namespace mozilla {
 namespace storage {
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Global Functions
 
 static
 bool
 stepFunc(JSContext *aCtx,
          uint32_t,
          JS::Value *_vp)
 {
-  nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
+  nsCOMPtr<nsIXPConnect> xpc(mozilla::services::GetXPConnect());
   nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
   JSObject *obj = JS_THIS_OBJECT(aCtx, _vp);
   if (!obj) {
     return false;
   }
 
   nsresult rv =
     xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper));