Bug 763848 - Expose the path to libxul at OS.Constants.Sys. r=khuey

This commit is contained in:
David Rajchenbach-Teller 2012-06-28 21:56:30 -04:00
Родитель 1129aada2a
Коммит d10793c683
4 изменённых файлов: 132 добавлений и 1 удалений

Просмотреть файл

@ -29,6 +29,9 @@ ifdef MOZ_B2G_RIL
DIRS += gonk DIRS += gonk
endif endif
DEFINES += -DDLL_PREFIX=\"$(DLL_PREFIX)\" -DDLL_SUFFIX=\"$(DLL_SUFFIX)\"
CPPSRCS = \ CPPSRCS = \
nsDeviceSensors.cpp \ nsDeviceSensors.cpp \
OSFileConstants.cpp \ OSFileConstants.cpp \

Просмотреть файл

@ -5,6 +5,8 @@
#include "fcntl.h" #include "fcntl.h"
#include "errno.h" #include "errno.h"
#include "prsystem.h"
#if defined(XP_UNIX) #if defined(XP_UNIX)
#include "unistd.h" #include "unistd.h"
#endif // defined(XP_UNIX) #endif // defined(XP_UNIX)
@ -23,6 +25,8 @@
// Used to provide information on the OS // Used to provide information on the OS
#include "nsThreadUtils.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIXULRuntime.h" #include "nsIXULRuntime.h"
#include "nsXPCOMCIDInternal.h" #include "nsXPCOMCIDInternal.h"
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
@ -35,8 +39,65 @@
* etc.) used by OS.File and possibly other OS-bound JavaScript libraries. * etc.) used by OS.File and possibly other OS-bound JavaScript libraries.
*/ */
namespace mozilla { namespace mozilla {
// Use an anonymous namespace to hide the symbols and avoid any collision
// with, for instance, |extern bool gInitialized;|
namespace {
/**
* |true| if this module has been initialized, |false| otherwise
*/
bool gInitialized = false;
/**
* The name of the directory holding all the libraries (libxpcom, libnss, etc.)
*/
nsString* gLibDirectory;
}
/**
* Perform the part of initialization that can only be
* executed on the main thread.
*/
nsresult InitOSFileConstants()
{
MOZ_ASSERT(NS_IsMainThread());
if (gInitialized) {
return NS_ERROR_ALREADY_INITIALIZED;
}
gInitialized = true;
// Initialize gLibDirectory
nsCOMPtr<nsIFile> xpcomLib;
nsresult rv = NS_GetSpecialDirectory("XpcomLib", getter_AddRefs(xpcomLib));
if (NS_FAILED(rv) || !xpcomLib) {
return rv;
}
nsCOMPtr<nsIFile> libDir;
rv = xpcomLib->GetParent(getter_AddRefs(libDir));
if (NS_FAILED(rv)) {
return rv;
}
gLibDirectory = new nsString();
return libDir->GetPath(*gLibDirectory);
}
nsresult CleanupOSFileConstants()
{
MOZ_ASSERT(NS_IsMainThread());
if (!gInitialized) {
return NS_ERROR_NOT_INITIALIZED;
}
delete gLibDirectory;
return NS_OK;
}
/** /**
* Define a simple read-only property holding an integer. * Define a simple read-only property holding an integer.
* *
@ -321,6 +382,8 @@ JSObject *GetOrCreateObjectProperty(JSContext *cx, JSObject *aObject,
*/ */
bool DefineOSFileConstants(JSContext *cx, JSObject *global) bool DefineOSFileConstants(JSContext *cx, JSObject *global)
{ {
MOZ_ASSERT(gInitialized);
JSObject *objOS; JSObject *objOS;
if (!(objOS = GetOrCreateObjectProperty(cx, global, "OS"))) { if (!(objOS = GetOrCreateObjectProperty(cx, global, "OS"))) {
return false; return false;
@ -329,6 +392,9 @@ bool DefineOSFileConstants(JSContext *cx, JSObject *global)
if (!(objConstants = GetOrCreateObjectProperty(cx, objOS, "Constants"))) { if (!(objConstants = GetOrCreateObjectProperty(cx, objOS, "Constants"))) {
return false; return false;
} }
// Build OS.Constants.libc
JSObject *objLibc; JSObject *objLibc;
if (!(objLibc = GetOrCreateObjectProperty(cx, objConstants, "libc"))) { if (!(objLibc = GetOrCreateObjectProperty(cx, objConstants, "libc"))) {
return false; return false;
@ -336,7 +402,10 @@ bool DefineOSFileConstants(JSContext *cx, JSObject *global)
if (!dom::DefineConstants(cx, objLibc, gLibcProperties)) { if (!dom::DefineConstants(cx, objLibc, gLibcProperties)) {
return false; return false;
} }
#if defined(XP_WIN) #if defined(XP_WIN)
// Build OS.Constants.Win
JSObject *objWin; JSObject *objWin;
if (!(objWin = GetOrCreateObjectProperty(cx, objConstants, "Win"))) { if (!(objWin = GetOrCreateObjectProperty(cx, objConstants, "Win"))) {
return false; return false;
@ -345,6 +414,9 @@ bool DefineOSFileConstants(JSContext *cx, JSObject *global)
return false; return false;
} }
#endif // defined(XP_WIN) #endif // defined(XP_WIN)
// Build OS.Constants.Sys
JSObject *objSys; JSObject *objSys;
if (!(objSys = GetOrCreateObjectProperty(cx, objConstants, "Sys"))) { if (!(objSys = GetOrCreateObjectProperty(cx, objConstants, "Sys"))) {
return false; return false;
@ -353,7 +425,7 @@ bool DefineOSFileConstants(JSContext *cx, JSObject *global)
nsCOMPtr<nsIXULRuntime> runtime = do_GetService(XULRUNTIME_SERVICE_CONTRACTID); nsCOMPtr<nsIXULRuntime> runtime = do_GetService(XULRUNTIME_SERVICE_CONTRACTID);
if (runtime) { if (runtime) {
nsCAutoString os; nsCAutoString os;
nsresult rv = runtime->GetOS(os); DebugOnly<nsresult> rv = runtime->GetOS(os);
MOZ_ASSERT(NS_SUCCEEDED(rv)); MOZ_ASSERT(NS_SUCCEEDED(rv));
JSString* strVersion = JS_NewStringCopyZ(cx, os.get()); JSString* strVersion = JS_NewStringCopyZ(cx, os.get());
@ -367,6 +439,30 @@ bool DefineOSFileConstants(JSContext *cx, JSObject *global)
} }
} }
// Locate libxul
{
nsAutoString xulPath(*gLibDirectory);
xulPath.Append(PR_GetDirectorySeparator());
#if defined(XP_MACOSX)
// Under MacOS X, for some reason, libxul is called simply "XUL"
xulPath.Append(NS_LITERAL_STRING("XUL"));
#else
// On other platforms, libxul is a library "xul" with regular
// library prefix/suffix
xulPath.Append(NS_LITERAL_STRING(DLL_PREFIX));
xulPath.Append(NS_LITERAL_STRING("xul"));
xulPath.Append(NS_LITERAL_STRING(DLL_SUFFIX));
#endif // defined(XP_MACOSX)
JSString* strPathToLibXUL = JS_NewUCStringCopyZ(cx, xulPath.get());
jsval valXul = STRING_TO_JSVAL(strPathToLibXUL);
if (!JS_SetProperty(cx, objSys, "libxulpath", &valXul)) {
return false;
}
}
return true; return true;
} }

Просмотреть файл

@ -9,6 +9,26 @@
namespace mozilla { namespace mozilla {
/**
* Perform initialization of this module.
*
* This function _must_ be called:
* - from the main thread;
* - only once;
* - before any Chrome Worker is created.
*/
nsresult InitOSFileConstants();
/**
* Perform cleanup of this module.
*
* This function _must_ be called:
* - from the main thread;
* - only once;
* - after all Chrome Workers are dead.
*/
nsresult CleanupOSFileConstants();
/** /**
* Define OS-specific constants. * Define OS-specific constants.
* *

Просмотреть файл

@ -36,6 +36,8 @@
#include "Worker.h" #include "Worker.h"
#include "WorkerPrivate.h" #include "WorkerPrivate.h"
#include "OSFileConstants.h"
using namespace mozilla; using namespace mozilla;
using namespace mozilla::dom; using namespace mozilla::dom;
@ -929,6 +931,11 @@ RuntimeService::Init()
mSystemCharset); mSystemCharset);
} }
rv = InitOSFileConstants();
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK; return NS_OK;
} }
@ -1043,6 +1050,11 @@ RuntimeService::Cleanup()
mObserved = NS_FAILED(rv); mObserved = NS_FAILED(rv);
} }
} }
nsresult rv = CleanupOSFileConstants();
if (NS_FAILED(rv)) {
MOZ_NOT_REACHED("Could not cleanup OSFileConstants");
}
} }
// static // static