Bug 1432285 - Implement an nsPrintingPromptService::GetSingleton method and make XPCOM use that. r=bobowen draft
authorJonathan Watt <jwatt@jwatt.org>
Mon, 22 Jan 2018 22:54:54 +0000
changeset 723333 f52b99d9c6550e1702ca26798cbf6ba422325f0d
parent 723105 87002575130154a3afa0acf44ccabe85b711e371
child 746832 b7567344c04fbd60361c8430e461822a5db000ad
push id96397
push userjwatt@jwatt.org
push dateMon, 22 Jan 2018 22:56:02 +0000
reviewersbobowen
bugs1432285
milestone59.0a1
Bug 1432285 - Implement an nsPrintingPromptService::GetSingleton method and make XPCOM use that. r=bobowen MozReview-Commit-ID: 62zz0IKiXq2
toolkit/components/printingui/nsPrintingPromptService.cpp
toolkit/components/printingui/nsPrintingPromptService.h
toolkit/xre/nsEmbeddingModule.cpp
--- a/toolkit/components/printingui/nsPrintingPromptService.cpp
+++ b/toolkit/components/printingui/nsPrintingPromptService.cpp
@@ -1,37 +1,68 @@
 /* -*- Mode: C++; tab-width: 2; 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 "nsPrintingPromptService.h"
 
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/RefPtr.h"
+#include "mozilla/StaticPtr.h"
 #include "nsIDOMWindow.h"
 #include "nsIServiceManager.h"
 #include "nsISupportsUtils.h"
 #include "nsString.h"
 #include "nsIPrintDialogService.h"
 #include "nsPIDOMWindow.h"
+#include "nsXULAppAPI.h"
 
 // Printing Progress Includes
 #if !defined(XP_MACOSX)
 #include "nsPrintProgress.h"
 #include "nsPrintProgressParams.h"
 
 static const char* kPrintProgressDialogURL =
   "chrome://global/content/printProgress.xul";
 static const char* kPrtPrvProgressDialogURL =
   "chrome://global/content/printPreviewProgress.xul";
 #endif
 
+using namespace mozilla;
+
 NS_IMPL_ISUPPORTS(nsPrintingPromptService,
                   nsIPrintingPromptService,
                   nsIWebProgressListener)
 
+
+StaticRefPtr<nsPrintingPromptService> sSingleton;
+
+/* static */ nsPrintingPromptService*
+nsPrintingPromptService::GetSingleton()
+{
+  MOZ_ASSERT(XRE_IsParentProcess(),
+             "The content process must use nsPrintingProxy");
+
+  if (!sSingleton) {
+    sSingleton = new nsPrintingPromptService();
+    sSingleton->Init();
+    ClearOnShutdown(&sSingleton);
+  }
+
+  return sSingleton;
+}
+
+/* static */ already_AddRefed<nsPrintingPromptService>
+nsPrintingPromptService::GetAddRefedSingleton()
+{
+  return do_AddRef(GetSingleton());
+}
+
+
 nsPrintingPromptService::nsPrintingPromptService() = default;
 
 nsPrintingPromptService::~nsPrintingPromptService() = default;
 
 nsresult
 nsPrintingPromptService::Init()
 {
 #if !defined(XP_MACOSX)
--- a/toolkit/components/printingui/nsPrintingPromptService.h
+++ b/toolkit/components/printingui/nsPrintingPromptService.h
@@ -26,32 +26,33 @@
 #include "nsIWebProgressListener.h"
 #if !defined(XP_MACOSX)
 #include "nsPrintProgress.h"
 
 class nsIDOMWindow;
 class nsIDialogParamBlock;
 #endif
 
-class nsPrintingPromptService
+class nsPrintingPromptService final
   : public nsIPrintingPromptService
   , public nsIWebProgressListener
 {
 
 public:
-  nsPrintingPromptService();
-
-  nsresult Init();
+  static nsPrintingPromptService* GetSingleton();
+  static already_AddRefed<nsPrintingPromptService> GetAddRefedSingleton();
 
   NS_DECL_NSIPRINTINGPROMPTSERVICE
   NS_DECL_NSIWEBPROGRESSLISTENER
   NS_DECL_ISUPPORTS
 
 protected:
+  nsPrintingPromptService();
   virtual ~nsPrintingPromptService();
+  nsresult Init();
 
 private:
 #if !defined(XP_MACOSX)
   nsCOMPtr<nsIWindowWatcher> mWatcher;
   nsCOMPtr<nsIPrintProgress> mPrintProgress;
   nsCOMPtr<nsIWebProgressListener> mWebProgressListener;
 #endif
 };
--- a/toolkit/xre/nsEmbeddingModule.cpp
+++ b/toolkit/xre/nsEmbeddingModule.cpp
@@ -33,17 +33,18 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsControl
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCommandManager)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCommandParams)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsControllerCommandGroup)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBaseCommandController)
 
 #ifdef MOZ_XUL
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDialogParamBlock)
 #ifdef NS_PRINTING
-NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintingPromptService, Init)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsPrintingPromptService,
+                                         nsPrintingPromptService::GetAddRefedSingleton)
 #ifdef PROXY_PRINTING
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsPrintingProxy,
                                          nsPrintingProxy::GetInstance)
 #endif
 #endif
 #endif
 
 #ifdef MOZ_XUL