Bug 573039 - Construct nsCUPSShim statically and avoid calling PR_UnloadLibrary on libcups after it has been initialized. r=karlt a=blocking2.0
authorMatthew Gregan <kinetik@flim.org>
Thu, 23 Dec 2010 13:59:41 +1300
changeset 59628 6f38fc526313
parent 59627 7dbd04a58fa0
child 59629 7161698099c0
push id17701
push usermgregan@mozilla.com
push date2010-12-23 02:46 +0000
treeherdermozilla-central@6f38fc526313 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt, blocking2
bugs573039
milestone2.0b9pre
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 573039 - Construct nsCUPSShim statically and avoid calling PR_UnloadLibrary on libcups after it has been initialized. r=karlt a=blocking2.0
widget/src/gtk2/nsCUPSShim.cpp
widget/src/gtk2/nsCUPSShim.h
widget/src/gtk2/nsPSPrinters.cpp
widget/src/gtk2/nsPSPrinters.h
--- a/widget/src/gtk2/nsCUPSShim.cpp
+++ b/widget/src/gtk2/nsCUPSShim.cpp
@@ -83,14 +83,8 @@ nsCUPSShim::Init()
 #endif
             PR_UnloadLibrary(mCupsLib);
             mCupsLib = nsnull;
             return PR_FALSE;
         }
     }
     return PR_TRUE;
 }
-
-nsCUPSShim::~nsCUPSShim()
-{
-    if (mCupsLib)
-        PR_UnloadLibrary(mCupsLib);
-}
--- a/widget/src/gtk2/nsCUPSShim.h
+++ b/widget/src/gtk2/nsCUPSShim.h
@@ -76,21 +76,19 @@ typedef int (PR_CALLBACK *CupsTempFdType
                                           int   length);
 typedef int (PR_CALLBACK *CupsAddOptionType)(const char    *name,
                                              const char    *value,
                                              int           num_options,
                                              cups_option_t **options);
 
 struct PRLibrary;
 
+/* Note: this class relies on static initialization. */
 class nsCUPSShim {
     public:
-        nsCUPSShim() : mCupsLib(nsnull) { }
-        ~nsCUPSShim();
-
         /**
          * Initialize this object. Attempt to load the CUPS shared
          * library and find function pointers for the supported
          * functions (see below).
          * @return PR_FALSE if the shared library could not be loaded, or if
          *                  any of the functions could not be found.
          *         PR_TRUE  for successful initialization.
          */
--- a/widget/src/gtk2/nsPSPrinters.cpp
+++ b/widget/src/gtk2/nsPSPrinters.cpp
@@ -40,43 +40,46 @@
 #include "nsCUPSShim.h"
 #include "nsIDeviceContext.h"       // GFX error codes
 #include "nsIPrefBranch.h"
 #include "nsIPrefService.h"
 #include "nsIServiceManager.h"
 #include "nsPrintfCString.h"
 #include "nsPSPrinters.h"
 #include "nsReadableUtils.h"        // StringBeginsWith()
+#include "nsCUPSShim.h"
 
 #include "prlink.h"
 #include "prenv.h"
 #include "plstr.h"
 
 #define NS_CUPS_PRINTER "CUPS/"
 #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
 
 /* dummy printer name for the gfx/src/ps driver */
 #define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
 
+nsCUPSShim gCupsShim;
+
 /* Initialize the printer manager object */
 nsresult
 nsPSPrinterList::Init()
 {
     nsresult rv;
 
     mPrefSvc = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
     if (NS_SUCCEEDED(rv))
         rv = mPrefSvc->GetBranch("print.", getter_AddRefs(mPref));
     NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_INITIALIZED);
 
     // Should we try cups?
     PRBool useCups = PR_TRUE;
     rv = mPref->GetBoolPref("postscript.cups.enabled", &useCups);
-    if (useCups)
-        mCups.Init();
+    if (useCups && !gCupsShim.IsInitialized())
+        gCupsShim.Init();
     return NS_OK;
 }
 
 
 /* Check whether the PostScript module has been disabled at runtime */
 PRBool
 nsPSPrinterList::Enabled()
 {
@@ -94,35 +97,35 @@ nsPSPrinterList::Enabled()
 /* Fetch a list of printers handled by the PostsScript module */
 void
 nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
 {
     aList.Clear();
 
     // Query CUPS for a printer list. The default printer goes to the
     // head of the output list; others are appended.
-    if (mCups.IsInitialized()) {
+    if (gCupsShim.IsInitialized()) {
         cups_dest_t *dests;
 
-        int num_dests = (mCups.mCupsGetDests)(&dests);
+        int num_dests = (gCupsShim.mCupsGetDests)(&dests);
         if (num_dests) {
             for (int i = 0; i < num_dests; i++) {
                 nsCAutoString fullName(NS_CUPS_PRINTER);
                 fullName.Append(dests[i].name);
                 if (dests[i].instance != NULL) {
                     fullName.Append("/");
                     fullName.Append(dests[i].instance);
                 }
                 if (dests[i].is_default)
                     aList.InsertElementAt(0, fullName);
                 else
                     aList.AppendElement(fullName);
             }
         }
-        (mCups.mCupsFreeDests)(num_dests, dests);
+        (gCupsShim.mCupsFreeDests)(num_dests, dests);
     }
 
     // Build the "classic" list of printers -- those accessed by running
     // an opaque command. This list always contains a printer named "default".
     // In addition, we look for either an environment variable
     // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
     // print.printer_list, which contains a space-separated list of printer
     // names.
--- a/widget/src/gtk2/nsPSPrinters.h
+++ b/widget/src/gtk2/nsPSPrinters.h
@@ -37,17 +37,16 @@
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef nsPSPrinters_h___
 #define nsPSPrinters_h___
 
 #include "nsString.h"
 #include "nsTArray.h"
 #include "prtypes.h"
-#include "nsCUPSShim.h"
 
 class nsIPrefService;
 class nsIPrefBranch;
 class nsCUPSShim;
 
 class nsPSPrinterList {
     public:
         /**
@@ -90,12 +89,11 @@ class nsPSPrinterList {
          *              the <type> portion as described for GetPrinterList().
          * @return The PrinterType value for this name.
          */
         static PrinterType GetPrinterType(const nsACString& aName);
 
     private:
         nsCOMPtr<nsIPrefService> mPrefSvc;
         nsCOMPtr<nsIPrefBranch> mPref;
-        nsCUPSShim mCups;
 };
 
 #endif /* nsPSPrinters_h___ */