1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
6 
7 #include "nsErrorService.h"
8 #include "nsCRTGlue.h"
9 #include "mozilla/StaticPtr.h"
10 #include "mozilla/ClearOnShutdown.h"
11 
12 namespace {
13 
14 mozilla::StaticRefPtr<nsErrorService> gSingleton;
15 
16 }
17 
NS_IMPL_ISUPPORTS(nsErrorService,nsIErrorService)18 NS_IMPL_ISUPPORTS(nsErrorService, nsIErrorService)
19 
20 // static
21 already_AddRefed<nsIErrorService> nsErrorService::GetOrCreate() {
22   // Be careful to not recreate the service for a second time if GetOrCreate is
23   // called super late during shutdown.
24   static bool serviceCreated = false;
25   RefPtr<nsErrorService> svc;
26   if (gSingleton) {
27     svc = gSingleton;
28   } else if (!serviceCreated) {
29     gSingleton = new nsErrorService();
30     mozilla::ClearOnShutdown(&gSingleton);
31     svc = gSingleton;
32     serviceCreated = true;
33   }
34 
35   return svc.forget();
36 }
37 
38 NS_IMETHODIMP
RegisterErrorStringBundle(int16_t aErrorModule,const char * aStringBundleURL)39 nsErrorService::RegisterErrorStringBundle(int16_t aErrorModule,
40                                           const char* aStringBundleURL) {
41   mErrorStringBundleURLMap.InsertOrUpdate(
42       aErrorModule, MakeUnique<nsCString>(aStringBundleURL));
43   return NS_OK;
44 }
45 
46 NS_IMETHODIMP
UnregisterErrorStringBundle(int16_t aErrorModule)47 nsErrorService::UnregisterErrorStringBundle(int16_t aErrorModule) {
48   mErrorStringBundleURLMap.Remove(aErrorModule);
49   return NS_OK;
50 }
51 
52 NS_IMETHODIMP
GetErrorStringBundle(int16_t aErrorModule,char ** aResult)53 nsErrorService::GetErrorStringBundle(int16_t aErrorModule, char** aResult) {
54   nsCString* bundleURL = mErrorStringBundleURLMap.Get(aErrorModule);
55   if (!bundleURL) {
56     return NS_ERROR_FAILURE;
57   }
58   *aResult = ToNewCString(*bundleURL);
59   return NS_OK;
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63