sbComponentLoader.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //=BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2009 POTI, Inc.
9 // http://www.songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the GPL).
13 //
14 // Software distributed under the License is distributed
15 // on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 //=END SONGBIRD GPL
25 */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // C++ source for the Mozilla XPCOM component loader services.
31 //
32 // Based on Benjamin Smedberg's loader at
33 // http://developer.mozilla.org/en/docs/
34 // Using_Dependent_Libraries_In_Extension_Components.
35 //
36 //------------------------------------------------------------------------------
37 //------------------------------------------------------------------------------
38 
44 //------------------------------------------------------------------------------
45 //
46 // Component loader services imported services.
47 //
48 //------------------------------------------------------------------------------
49 
50 // Mozilla imports.
51 #include <nsCOMPtr.h>
52 #include <nsILocalFile.h>
53 #include <nsModule.h>
54 #include <nsStringAPI.h>
55 
56 // Mac OS/X imports.
57 #ifdef XP_MACOSX
58 #include <mach-o/dyld.h>
59 #endif
60 
61 
62 //------------------------------------------------------------------------------
63 //
64 // Component loader services configuration.
65 //
66 //------------------------------------------------------------------------------
67 
68 //
69 // CLModuleLib Component module library to load.
70 // CLLibList List of dependent libraries to load.
71 //
72 
73 #if defined(XP_MACOSX)
74 
75 #ifdef DEBUG
76 static const char *CLModuleLib = "sbIPDDevice_d.dylib";
77 #else
78 static const char *CLModuleLib = "sbIPDDevice.dylib";
79 #endif
80 
81 static const char *CLLibList[] =
82 {
83  "libiconv.dylib",
84  "libintl.dylib",
85  "libglib-2.0.dylib",
86  "libgobject-2.0.dylib",
87  "libgpod.dylib"
88 };
89 
90 #elif defined(linux)
91 
92 #ifdef DEBUG
93 static const char *CLModuleLib = "sbIPDDevice_d.so";
94 #else
95 static const char *CLModuleLib = "sbIPDDevice.so";
96 #endif
97 
98 static const char *CLLibList[] =
99 {
100  "libgpod.so"
101 };
102 
103 #elif defined(XP_WIN)
104 
105 #ifdef DEBUG
106 static const char *CLModuleLib = "sbIPDDevice_d.dll";
107 #else
108 static const char *CLModuleLib = "sbIPDDevice.dll";
109 #endif
110 
111 static const char *CLLibList[] =
112 {
113  "iconv.dll",
114  "intl.dll",
115  "libglib-2.0-0.dll",
116  "libgobject-2.0-0.dll",
117  "libgpod.dll"
118 };
119 
120 #else
121 
122 #error "Unsupported OS."
123 
124 #endif
125 
126 
127 //------------------------------------------------------------------------------
128 //
129 // Component loader services functions.
130 //
131 //------------------------------------------------------------------------------
132 
133 extern "C" {
134 
135 static nsresult LoadLibrary(nsCOMPtr<nsIFile> aLibDir,
136  nsString aLibPath);
137 
138 
149 NS_EXPORT nsresult
150 NSGetModule(nsIComponentManager* aComponentManager,
151  nsIFile* aComponentFile,
152  nsIModule** aComponentModule)
153 {
154  nsresult rv;
155 
156  // Get the libraries directory.
157  nsCOMPtr<nsIFile> pLibDir;
158  nsCOMPtr<nsIFile> _pLibDir;
159  rv = aComponentFile->GetParent(getter_AddRefs(pLibDir));
160  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
161  _pLibDir = pLibDir;
162  rv = _pLibDir->GetParent(getter_AddRefs(pLibDir));
163  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
164  rv = pLibDir->Append(NS_LITERAL_STRING("libraries"));
165 
166  // Load the dependent libraries.
167  PRUint32 libCount = sizeof (CLLibList) / sizeof (CLLibList[0]);
168  for (PRUint32 i = 0; i < libCount; i++) {
169  rv = LoadLibrary(pLibDir, NS_ConvertUTF8toUTF16(CLLibList[i]));
170  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
171  }
172 
173  // Produce the library file.
174  nsCOMPtr<nsILocalFile> pLibFile;
175  nsCOMPtr<nsIFile> _pLibFile;
176  rv = pLibDir->Clone(getter_AddRefs(_pLibFile));
177  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
178  pLibFile = do_QueryInterface(_pLibFile, &rv);
179  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
180  rv = pLibFile->Append(NS_ConvertUTF8toUTF16(CLModuleLib));
181  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
182 
183  // Load the component library.
184  PRLibrary *pLibrary;
185  rv = pLibFile->Load(&pLibrary);
186  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
187 
188  // Get the component get module function.
189  nsGetModuleProc getModuleProc = (nsGetModuleProc)
190  PR_FindFunctionSymbol(pLibrary,
191  NS_GET_MODULE_SYMBOL);
192  NS_ENSURE_TRUE(getModuleProc, NS_ERROR_NOT_AVAILABLE);
193 
194  // Call the component get module function.
195  rv = getModuleProc(aComponentManager,
196  aComponentFile,
197  aComponentModule);
198  NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
199 
200  return NS_OK;
201 }
202 
203 
212 nsresult
213 LoadLibrary(nsCOMPtr<nsIFile> aLibDir,
214  nsString aLibPath)
215 {
216  nsresult rv;
217 
218  // Produce the library file.
219  nsCOMPtr<nsILocalFile> pLibFile;
220  nsCOMPtr<nsIFile> _pLibFile;
221  rv = aLibDir->Clone(getter_AddRefs(_pLibFile));
222  NS_ENSURE_SUCCESS(rv, rv);
223  pLibFile = do_QueryInterface(_pLibFile, &rv);
224  NS_ENSURE_SUCCESS(rv, rv);
225  rv = pLibFile->Append(aLibPath);
226  NS_ENSURE_SUCCESS(rv, rv);
227 
228  // Load the library.
229 #ifndef XP_MACOSX
230  PRLibrary *pLibrary;
231  rv = pLibFile->Load(&pLibrary);
232  NS_ENSURE_SUCCESS(rv, rv);
233 #else
234  nsCString filePath;
235  rv = pLibFile->GetNativePath(filePath);
236  NS_ENSURE_SUCCESS(rv, rv);
237  NS_ENSURE_TRUE(NSAddImage(filePath.get(),
238  NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME),
239  NS_ERROR_NOT_AVAILABLE);
240 #endif
241 
242  return NS_OK;
243 }
244 
245 }; // extern "C"
246 
247 
return NS_OK
static nsresult LoadLibrary(nsCOMPtr< nsIFile > aLibDir, nsString aLibPath)
NS_EXPORT nsresult NSGetModule(nsIComponentManager *aComponentManager, nsIFile *aComponentFile, nsIModule **aComponentModule)
_getSelectedPageStyle s i