Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / toolkit / xre / nsEmbedFunctions.cpp
blob2cd9f4f60bfaa931723c648cf88b10564d64c1fc
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla libXUL embedding.
16 * The Initial Developer of the Original Code is
17 * Benjamin Smedberg <benjamin@smedbergs.us>
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Mozilla Foundation. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsXULAppAPI.h"
40 #include <stdlib.h>
42 #include "nsIAppStartupNotifier.h"
43 #include "nsIDirectoryService.h"
44 #include "nsILocalFile.h"
45 #include "nsIToolkitChromeRegistry.h"
47 #include "nsAppDirectoryServiceDefs.h"
48 #include "nsAppRunner.h"
49 #include "nsDirectoryServiceDefs.h"
50 #include "nsStaticComponents.h"
51 #include "nsString.h"
52 #include "nsXREDirProvider.h"
53 #include "nsIToolkitProfile.h"
55 void
56 XRE_GetStaticComponents(nsStaticModuleInfo const **aStaticComponents,
57 PRUint32 *aComponentCount)
59 *aStaticComponents = kPStaticModules;
60 *aComponentCount = kStaticModuleCount;
63 nsresult
64 XRE_LockProfileDirectory(nsILocalFile* aDirectory,
65 nsISupports* *aLockObject)
67 nsCOMPtr<nsIProfileLock> lock;
69 nsresult rv = NS_LockProfilePath(aDirectory, nsnull, nsnull,
70 getter_AddRefs(lock));
71 if (NS_SUCCEEDED(rv))
72 NS_ADDREF(*aLockObject = lock);
74 return rv;
77 static nsStaticModuleInfo *sCombined;
78 static PRInt32 sInitCounter;
80 nsresult
81 XRE_InitEmbedding(nsILocalFile *aLibXULDirectory,
82 nsILocalFile *aAppDirectory,
83 nsIDirectoryServiceProvider *aAppDirProvider,
84 nsStaticModuleInfo const *aStaticComponents,
85 PRUint32 aStaticComponentCount)
87 // Initialize some globals to make nsXREDirProvider happy
88 static char* kNullCommandLine[] = { nsnull };
89 gArgv = kNullCommandLine;
90 gArgc = 0;
92 NS_ENSURE_ARG(aLibXULDirectory);
94 if (++sInitCounter > 1) // XXXbsmedberg is this really the right solution?
95 return NS_OK;
97 if (!aAppDirectory)
98 aAppDirectory = aLibXULDirectory;
100 nsresult rv;
102 new nsXREDirProvider; // This sets gDirServiceProvider
103 if (!gDirServiceProvider)
104 return NS_ERROR_OUT_OF_MEMORY;
106 rv = gDirServiceProvider->Initialize(aAppDirectory, aLibXULDirectory,
107 aAppDirProvider);
108 if (NS_FAILED(rv))
109 return rv;
111 // Combine the toolkit static components and the app components.
112 PRUint32 combinedCount = kStaticModuleCount + aStaticComponentCount;
114 sCombined = new nsStaticModuleInfo[combinedCount];
115 if (!sCombined)
116 return NS_ERROR_OUT_OF_MEMORY;
118 memcpy(sCombined, kPStaticModules,
119 sizeof(nsStaticModuleInfo) * kStaticModuleCount);
120 memcpy(sCombined + kStaticModuleCount, aStaticComponents,
121 sizeof(nsStaticModuleInfo) * aStaticComponentCount);
123 rv = NS_InitXPCOM3(nsnull, aAppDirectory, gDirServiceProvider,
124 sCombined, combinedCount);
125 if (NS_FAILED(rv))
126 return rv;
128 // We do not need to autoregister components here. The CheckUpdateFile()
129 // bits in NS_InitXPCOM3 check for an .autoreg file. If the app wants
130 // to autoregister every time (for instance, if it's debug), it can do
131 // so after we return from this function.
133 nsCOMPtr<nsIObserver> startupNotifier
134 (do_CreateInstance(NS_APPSTARTUPNOTIFIER_CONTRACTID));
135 if (!startupNotifier)
136 return NS_ERROR_FAILURE;
138 startupNotifier->Observe(nsnull, APPSTARTUP_TOPIC, nsnull);
140 return NS_OK;
143 void
144 XRE_NotifyProfile()
146 NS_ASSERTION(gDirServiceProvider, "XRE_InitEmbedding was not called!");
147 gDirServiceProvider->DoStartup();
150 void
151 XRE_TermEmbedding()
153 if (--sInitCounter != 0)
154 return;
156 NS_ASSERTION(gDirServiceProvider,
157 "XRE_TermEmbedding without XRE_InitEmbedding");
159 gDirServiceProvider->DoShutdown();
160 NS_ShutdownXPCOM(nsnull);
161 delete [] sCombined;
162 delete gDirServiceProvider;