sbLibraryLoaderUtils.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SB_LIBRARYLOADERUTILS__
28 #define __SB_LIBRARYLOADERUTILS__
29 
30 #include <nsIFile.h>
31 #include <nsIProperties.h>
32 #include <nsStringGlue.h>
33 #include <nsServiceManagerUtils.h>
34 #include <nsILineInputStream.h>
35 #include <nsNetUtil.h>
36 #include <prerror.h>
37 #include <prlog.h>
38 #include <prprf.h>
39 
40 #ifdef XP_MACOSX
41 #include <mach-o/dyld.h>
42 #endif
43 
44 // We give this a different name (not LOG) because it's in a header, we don't
45 // want to step in LOG() in other source files.
46 #define LOAD_LIBRARIES_LOG(args) PR_LOG(loadLibrariesLog, PR_LOG_WARN, args)
47 
48 static nsresult
49 SB_LoadLibraries(nsIFile* aManifest)
50 {
51  NS_ENSURE_ARG_POINTER(aManifest);
52 
53  nsresult rv;
54 
55 #ifdef PR_LOGGING
56  PRLogModuleInfo* loadLibrariesLog = PR_NewLogModule("sbLoadLibraries");
57 #endif
58 
59  nsCOMPtr<nsIProperties> directorySvc =
60  do_GetService("@mozilla.org/file/directory_service;1", &rv);
61  NS_ENSURE_SUCCESS(rv, rv);
62 
63  nsCOMPtr<nsIFile> appDir;
64  rv = directorySvc->Get("resource:app",
65  NS_GET_IID(nsIFile),
66  getter_AddRefs(appDir));
67  NS_ENSURE_SUCCESS(rv, rv);
68 
69  nsCOMPtr<nsIInputStream> fis;
70  rv = NS_NewLocalFileInputStream(getter_AddRefs(fis), aManifest);
71  NS_ENSURE_SUCCESS(rv, rv);
72 
73  nsCOMPtr<nsILineInputStream> lis = do_QueryInterface(fis, &rv);
74  NS_ENSURE_SUCCESS(rv, rv);
75 
76  nsCString line;
77  PRBool moreLines;
78  do {
79  rv = lis->ReadLine(line, &moreLines);
80  NS_ENSURE_SUCCESS(rv, rv);
81 
82  if (!line.IsEmpty() && line.CharAt(0) != '#') {
83 
84  // If the line starts with @, then consider it a relative path from the
85  // application directory. Otherwise, it is relative to the location of
86  // the manifest
87  nsCOMPtr<nsILocalFile> libLocal;
88  if (line.CharAt(0) == '@') {
89  nsCOMPtr<nsIFile> clone;
90  rv = appDir->Clone(getter_AddRefs(clone));
91  NS_ENSURE_SUCCESS(rv, rv);
92 
93  libLocal = do_QueryInterface(clone, &rv);
94  NS_ENSURE_SUCCESS(rv, rv);
95 
96  nsDependentCSubstring path(line.BeginReading() + 1, line.Length() - 1);
97  rv = libLocal->AppendRelativeNativePath(path);
98  NS_ENSURE_SUCCESS(rv, rv);
99  }
100  else {
101  nsCOMPtr<nsIFile> parent;
102  rv = aManifest->GetParent(getter_AddRefs(parent));
103  NS_ENSURE_SUCCESS(rv, rv);
104 
105  while (StringBeginsWith(line, NS_LITERAL_CSTRING(".."))) {
106  nsCOMPtr<nsIFile> newParent;
107 
108  // remove .. + delimiter from the path to append
109  line.Cut(0, 3);
110 
111  rv = parent->GetParent(getter_AddRefs(newParent));
112  NS_ENSURE_SUCCESS(rv, rv);
113 
114  newParent.swap(parent);
115  }
116 
117  libLocal = do_QueryInterface(parent, &rv);
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  rv = libLocal->AppendRelativePath(NS_ConvertASCIItoUTF16(line));
121  NS_ENSURE_SUCCESS(rv, rv);
122  }
123 
124  PRBool success;
125 
126 #ifdef PR_LOGGING
127  {
128  nsCString _path;
129  libLocal->GetNativePath(_path);
130  LOAD_LIBRARIES_LOG(("SB_LoadLibraries: loading '%s'\n",
131  _path.BeginReading()));
132  }
133 #endif
134 
135 #if XP_MACOSX
136  nsCString filePath;
137  libLocal->GetNativePath(filePath);
138  const struct mach_header* header = NSAddImage(filePath.get(),
139  NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME |
140  NSADDIMAGE_OPTION_WITH_SEARCHING);
141 
142  if (!header) {
143  NSLinkEditErrors linkEditError;
144  int errorNum;
145  const char *errorString;
146  const char *fileName;
147 
148  NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString);
149  char *message = PR_smprintf(
150  "SB_LoadLibraries: ERROR %d:%d for file %s\n%s",
151  linkEditError, errorNum, fileName, errorString);
152 
153  NS_WARNING(message);
154  PR_smprintf_free(message);
155  }
156 
157  success = (header != nsnull);
158 #else
159  PRLibrary* library;
160  rv = libLocal->Load(&library);
161  // Leaking the library, we don't care
162  success = NS_SUCCEEDED(rv);
163 #endif
164 
165  if (!success) {
166  nsCString libPath;
167  libLocal->GetNativePath(libPath);
168  char* message = PR_smprintf("SB_LoadLibraries: Error loading library: %s, OS error was: %i",
169  libPath.get(), PR_GetOSError());
170  NS_WARNING(message);
171  PR_smprintf_free(message);
172  }
173  }
174  }
175  while (moreLines);
176 
177  rv = fis->Close();
178  NS_ENSURE_SUCCESS(rv, rv);
179 
180  return NS_OK;
181 }
182 
183 #endif /* __SB_LIBRARYLOADERUTILS__ */
return NS_OK
static nsresult SB_LoadLibraries(nsIFile *aManifest)
var header
Definition: FeedWriter.js:953
GstMessage * message
#define LOAD_LIBRARIES_LOG(args)