Bug 1265621 - Use StaticRefPtr in Omnijar.cpp; r=froydnj
authorJim Chen <nchen@mozilla.com>
Thu, 28 Apr 2016 16:07:02 -0400
changeset 295326 499b7bf811b3b4d307ea14dae1f3660b219432a5
parent 295325 b083e01f57a13f614bb2de9b0413409905f59385
child 295327 370e94b2ac056add0be2153201bdf30386438351
push id75877
push usernchen@mozilla.com
push dateThu, 28 Apr 2016 20:07:23 +0000
treeherdermozilla-inbound@ce3035337782 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1265621
milestone49.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 1265621 - Use StaticRefPtr in Omnijar.cpp; r=froydnj Use StaticRefPtr instead of raw pointers in Omnijar.cpp for convenience and safety.
xpcom/build/Omnijar.cpp
xpcom/build/Omnijar.h
--- a/xpcom/build/Omnijar.cpp
+++ b/xpcom/build/Omnijar.cpp
@@ -9,37 +9,36 @@
 #include "nsDirectoryService.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsIFile.h"
 #include "nsZipArchive.h"
 #include "nsNetUtil.h"
 
 namespace mozilla {
 
-nsIFile* Omnijar::sPath[2] = { nullptr, nullptr };
-nsZipArchive* Omnijar::sReader[2] = { nullptr, nullptr };
+StaticRefPtr<nsIFile> Omnijar::sPath[2];
+StaticRefPtr<nsZipArchive> Omnijar::sReader[2];
 bool Omnijar::sInitialized = false;
-static bool sIsUnified = false;
-static bool sIsNested[2] = { false, false };
+bool Omnijar::sIsUnified = false;
+bool Omnijar::sIsNested[2] = { false, false };
 
 static const char* sProp[2] = {
   NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR
 };
 
 #define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP])
 
 void
 Omnijar::CleanUpOne(Type aType)
 {
   if (sReader[aType]) {
     sReader[aType]->CloseArchive();
-    NS_IF_RELEASE(sReader[aType]);
+    sReader[aType] = nullptr;
   }
-  sReader[aType] = nullptr;
-  NS_IF_RELEASE(sPath[aType]);
+  sPath[aType] = nullptr;
 }
 
 void
 Omnijar::InitOne(nsIFile* aPath, Type aType)
 {
   nsCOMPtr<nsIFile> file;
   if (aPath) {
     file = aPath;
@@ -92,19 +91,17 @@ Omnijar::InitOne(nsIFile* aPath, Type aT
     if (NS_FAILED(zipReader->OpenArchive(handle))) {
       return;
     }
     sIsNested[aType] = true;
   }
 
   CleanUpOne(aType);
   sReader[aType] = zipReader;
-  NS_IF_ADDREF(sReader[aType]);
   sPath[aType] = file;
-  NS_IF_ADDREF(sPath[aType]);
 }
 
 void
 Omnijar::Init(nsIFile* aGrePath, nsIFile* aAppPath)
 {
   InitOne(aGrePath, GRE);
   InitOne(aAppPath, APP);
   sInitialized = true;
--- a/xpcom/build/Omnijar.h
+++ b/xpcom/build/Omnijar.h
@@ -8,39 +8,48 @@
 #define mozilla_Omnijar_h
 
 #include "nscore.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsIFile.h"
 #include "nsZipArchive.h"
 
+#include "mozilla/StaticPtr.h"
+
 namespace mozilla {
 
 class Omnijar
 {
 private:
   /**
    * Store an nsIFile for an omni.jar. We can store two paths here, one
    * for GRE (corresponding to resource://gre/) and one for APP
    * (corresponding to resource:/// and resource://app/), but only
    * store one when both point to the same location (unified).
    */
-  static nsIFile* sPath[2];
+  static StaticRefPtr<nsIFile> sPath[2];
 
   /**
    * Cached nsZipArchives for the corresponding sPath
    */
-  static nsZipArchive* sReader[2];
+  static StaticRefPtr<nsZipArchive> sReader[2];
 
   /**
    * Has Omnijar::Init() been called?
    */
   static bool sInitialized;
 
+  /**
+   * Is using unified GRE/APP jar?
+   */
+  static bool sIsUnified;
+
+  static bool sIsNested[2];
+
 public:
   enum Type
   {
     GRE = 0,
     APP = 1
   };
 
   /**
@@ -66,17 +75,17 @@ public:
   /**
    * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
    * Returns nullptr when there is no corresponding omni.jar.
    * Also returns nullptr for APP in the unified case.
    */
   static inline already_AddRefed<nsIFile> GetPath(Type aType)
   {
     MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
-    nsCOMPtr<nsIFile> path = sPath[aType];
+    nsCOMPtr<nsIFile> path = sPath[aType].get();
     return path.forget();
   }
 
   /**
    * Returns whether GRE or APP use an omni.jar. Returns PR_False for
    * APP when using an omni.jar in the unified case.
    */
   static inline bool HasOmnijar(Type aType)
@@ -87,17 +96,17 @@ public:
 
   /**
    * Returns a nsZipArchive pointer for the omni.jar file for GRE or
    * APP. Returns nullptr in the same cases GetPath() would.
    */
   static inline already_AddRefed<nsZipArchive> GetReader(Type aType)
   {
     MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
-    RefPtr<nsZipArchive> reader = sReader[aType];
+    RefPtr<nsZipArchive> reader = sReader[aType].get();
     return reader.forget();
   }
 
   /**
    * Returns a nsZipArchive pointer for the given path IAOI the given
    * path is the omni.jar for either GRE or APP.
    */
   static already_AddRefed<nsZipArchive> GetReader(nsIFile* aPath);