2013-06-12 00:00:09 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 04:12:37 -07:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-16 11:42:07 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <cstdarg>
|
2007-05-15 16:27:40 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prlog.h"
|
2011-11-09 16:27:08 -08:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
2012-08-06 13:41:40 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-16 20:36:37 -07:00
|
|
|
#include "jsapi.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2010-06-10 11:11:11 -07:00
|
|
|
#include "mozilla/Module.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "mozJSComponentLoader.h"
|
2011-07-09 20:21:16 -07:00
|
|
|
#include "mozJSLoaderUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2007-06-10 14:13:18 -07:00
|
|
|
#include "nsIFileURL.h"
|
2010-06-15 12:38:46 -07:00
|
|
|
#include "nsIJARURI.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNetUtil.h"
|
2012-12-11 13:21:03 -08:00
|
|
|
#include "nsDOMBlobBuilder.h"
|
2007-05-15 16:27:40 -07:00
|
|
|
#include "jsprf.h"
|
2012-03-09 01:48:50 -08:00
|
|
|
#include "nsJSPrincipals.h"
|
2010-11-15 17:39:28 -08:00
|
|
|
#include "xpcprivate.h"
|
2011-12-28 00:13:38 -08:00
|
|
|
#include "xpcpublic.h"
|
2012-07-23 07:47:18 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-07-23 07:47:18 -07:00
|
|
|
#include "WrapperFactory.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-12 12:37:52 -07:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
#include "mozilla/scache/StartupCacheUtils.h"
|
2012-06-06 19:52:14 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-08-12 12:37:52 -07:00
|
|
|
|
2013-08-26 19:05:20 -07:00
|
|
|
#include "js/OldDebugAPI.h"
|
2010-05-19 16:22:19 -07:00
|
|
|
|
2011-11-08 09:10:51 -08:00
|
|
|
using namespace mozilla;
|
2011-07-20 00:39:09 -07:00
|
|
|
using namespace mozilla::scache;
|
2012-07-23 07:47:18 -07:00
|
|
|
using namespace xpc;
|
2013-04-23 08:48:05 -07:00
|
|
|
using namespace JS;
|
2011-07-20 00:39:09 -07:00
|
|
|
|
2012-11-26 14:41:59 -08:00
|
|
|
// This JSClass exists to trick silly code that expects toString()ing the
|
|
|
|
// global in a component scope to return something with "BackstagePass" in it
|
|
|
|
// to continue working.
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass kFakeBackstagePassJSClass =
|
2012-11-26 14:41:59 -08:00
|
|
|
{
|
|
|
|
"FakeBackstagePass",
|
|
|
|
0,
|
|
|
|
JS_PropertyStub,
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_DeletePropertyStub,
|
2012-11-26 14:41:59 -08:00
|
|
|
JS_PropertyStub,
|
|
|
|
JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub,
|
|
|
|
JS_ResolveStub,
|
|
|
|
JS_ConvertStub
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char kJSRuntimeServiceContractID[] = "@mozilla.org/js/xpc/RuntimeService;1";
|
|
|
|
static const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
|
|
|
|
static const char kObserverServiceContractID[] = "@mozilla.org/observer-service;1";
|
2011-06-15 21:54:47 -07:00
|
|
|
static const char kJSCachePrefix[] = "jsloader";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Some platforms don't have an implementation of PR_MemMap(). */
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#ifndef XP_OS2
|
2007-03-22 10:30:00 -07:00
|
|
|
#define HAVE_PR_MEMMAP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffer sizes for serialization and deserialization of scripts.
|
2008-04-01 23:41:23 -07:00
|
|
|
* FIXME: bug #411579 (tune this macro!) Last updated: Jan 2008
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
#define XPC_SERIALIZATION_BUFFER_SIZE (64 * 1024)
|
2008-04-01 23:41:23 -07:00
|
|
|
#define XPC_DESERIALIZATION_BUFFER_SIZE (12 * 8192)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
// NSPR_LOG_MODULES=JSComponentLoader:5
|
|
|
|
static PRLogModuleInfo *gJSCLLog;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LOG(args) PR_LOG(gJSCLLog, PR_LOG_DEBUG, args)
|
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
// Components.utils.import error messages
|
|
|
|
#define ERROR_SCOPE_OBJ "%s - Second argument must be an object."
|
|
|
|
#define ERROR_NOT_PRESENT "%s - EXPORTED_SYMBOLS is not present."
|
|
|
|
#define ERROR_NOT_AN_ARRAY "%s - EXPORTED_SYMBOLS is not an array."
|
|
|
|
#define ERROR_GETTING_ARRAY_LENGTH "%s - Error getting array length of EXPORTED_SYMBOLS."
|
|
|
|
#define ERROR_ARRAY_ELEMENT "%s - EXPORTED_SYMBOLS[%d] is not a string."
|
|
|
|
#define ERROR_GETTING_SYMBOL "%s - Could not get symbol '%s'."
|
|
|
|
#define ERROR_SETTING_SYMBOL "%s - Could not set symbol '%s' on target object."
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-11-16 04:31:36 -08:00
|
|
|
Dump(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-16 04:31:36 -08:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (args.length() == 0)
|
2011-11-26 02:05:59 -08:00
|
|
|
return true;
|
2010-03-23 14:08:18 -07:00
|
|
|
|
2013-11-16 04:31:36 -08:00
|
|
|
JSString *str = JS::ToString(cx, args[0]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
|
|
|
|
if (!chars)
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2010-12-03 00:24:17 -08:00
|
|
|
|
2014-01-25 20:27:11 -08:00
|
|
|
NS_ConvertUTF16toUTF8 utf8str(reinterpret_cast<const char16_t*>(chars),
|
|
|
|
length);
|
2011-11-09 16:27:08 -08:00
|
|
|
#ifdef ANDROID
|
2012-06-28 07:14:11 -07:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", utf8str.get());
|
2012-08-06 13:41:40 -07:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (IsDebuggerPresent()) {
|
2013-12-05 01:56:22 -08:00
|
|
|
OutputDebugStringW(reinterpret_cast<const wchar_t*>(chars));
|
2012-08-06 13:41:40 -07:00
|
|
|
}
|
2011-11-09 16:27:08 -08:00
|
|
|
#endif
|
|
|
|
fputs(utf8str.get(), stdout);
|
2011-10-19 07:01:24 -07:00
|
|
|
fflush(stdout);
|
2011-11-26 02:05:59 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Debug(JSContext *cx, unsigned argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-08-16 12:35:04 -07:00
|
|
|
return Dump(cx, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2011-11-26 02:05:59 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-05-04 10:25:15 -07:00
|
|
|
File(JSContext *cx, unsigned argc, Value *vp)
|
2011-07-06 09:34:26 -07:00
|
|
|
{
|
2013-05-04 10:25:15 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2011-07-06 09:34:26 -07:00
|
|
|
|
2013-05-04 10:25:15 -07:00
|
|
|
if (args.length() == 0) {
|
2011-07-06 09:34:26 -07:00
|
|
|
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2011-07-06 09:34:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> native;
|
2013-05-04 10:25:15 -07:00
|
|
|
nsresult rv = nsDOMMultipartFile::NewFile(getter_AddRefs(native));
|
2011-07-06 09:34:26 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2011-07-06 09:34:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(initializer);
|
2011-07-06 09:34:26 -07:00
|
|
|
|
2013-05-04 10:25:15 -07:00
|
|
|
rv = initializer->Initialize(nullptr, cx, nullptr, args);
|
2011-07-06 09:34:26 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2011-07-06 09:34:26 -07:00
|
|
|
}
|
|
|
|
|
2014-01-22 09:50:32 -08:00
|
|
|
nsXPConnect *xpc = nsXPConnect::XPConnect();
|
|
|
|
JSObject *glob = CurrentGlobalOrNull(cx);
|
2011-07-06 09:34:26 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = xpc->WrapNativeToJSVal(cx, glob, native, nullptr,
|
2011-07-06 09:34:26 -07:00
|
|
|
&NS_GET_IID(nsISupports),
|
2014-01-09 09:39:36 -08:00
|
|
|
true, args.rval());
|
2011-07-06 09:34:26 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 02:05:59 -08:00
|
|
|
return false;
|
2011-07-06 09:34:26 -07:00
|
|
|
}
|
2011-11-26 02:05:59 -08:00
|
|
|
return true;
|
2011-07-06 09:34:26 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-05-04 10:25:15 -07:00
|
|
|
Blob(JSContext *cx, unsigned argc, Value *vp)
|
2013-01-03 23:53:25 -08:00
|
|
|
{
|
2013-05-04 10:25:15 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-01-03 23:53:25 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> native;
|
2013-05-04 10:25:15 -07:00
|
|
|
nsresult rv = nsDOMMultipartFile::NewBlob(getter_AddRefs(native));
|
2013-01-03 23:53:25 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(initializer);
|
2013-01-03 23:53:25 -08:00
|
|
|
|
2013-05-04 10:25:15 -07:00
|
|
|
rv = initializer->Initialize(nullptr, cx, nullptr, args);
|
2013-01-03 23:53:25 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-22 09:50:32 -08:00
|
|
|
nsXPConnect *xpc = nsXPConnect::XPConnect();
|
|
|
|
JSObject *glob = CurrentGlobalOrNull(cx);
|
2013-01-03 23:53:25 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
|
|
|
rv = xpc->WrapNativeToJSVal(cx, glob, native, nullptr,
|
|
|
|
&NS_GET_IID(nsISupports),
|
2014-01-09 09:39:36 -08:00
|
|
|
true, args.rval());
|
2013-01-03 23:53:25 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec gGlobalFun[] = {
|
2012-08-07 22:26:19 -07:00
|
|
|
JS_FS("dump", Dump, 1,0),
|
|
|
|
JS_FS("debug", Debug, 1,0),
|
|
|
|
JS_FS("atob", Atob, 1,0),
|
|
|
|
JS_FS("btoa", Btoa, 1,0),
|
|
|
|
JS_FS("File", File, 1,JSFUN_CONSTRUCTOR),
|
2013-01-03 23:53:25 -08:00
|
|
|
JS_FS("Blob", Blob, 2,JSFUN_CONSTRUCTOR),
|
2012-08-07 22:26:19 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-04-29 11:16:19 -07:00
|
|
|
class MOZ_STACK_CLASS JSCLContextHelper
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-29 11:16:19 -07:00
|
|
|
JSCLContextHelper(JSContext* aCx);
|
2011-05-13 08:56:26 -07:00
|
|
|
~JSCLContextHelper();
|
2009-02-17 20:11:09 -08:00
|
|
|
|
2011-05-13 08:56:26 -07:00
|
|
|
void reportErrorAfterPop(char *buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
operator JSContext*() const {return mContext;}
|
|
|
|
|
|
|
|
private:
|
2013-04-29 11:16:19 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext* mContext;
|
2013-04-29 11:16:19 -07:00
|
|
|
nsCxPusher mPusher;
|
2011-05-13 08:56:26 -07:00
|
|
|
char* mBuf;
|
2010-03-11 22:50:10 -08:00
|
|
|
|
|
|
|
// prevent copying and assignment
|
2011-12-16 11:42:07 -08:00
|
|
|
JSCLContextHelper(const JSCLContextHelper &) MOZ_DELETE;
|
|
|
|
const JSCLContextHelper& operator=(const JSCLContextHelper &) MOZ_DELETE;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class JSCLAutoErrorReporterSetter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JSCLAutoErrorReporterSetter(JSContext* cx, JSErrorReporter reporter)
|
|
|
|
{mContext = cx; mOldReporter = JS_SetErrorReporter(cx, reporter);}
|
|
|
|
~JSCLAutoErrorReporterSetter()
|
2011-10-14 10:52:47 -07:00
|
|
|
{JS_SetErrorReporter(mContext, mOldReporter);}
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
JSContext* mContext;
|
|
|
|
JSErrorReporter mOldReporter;
|
2011-12-16 11:42:07 -08:00
|
|
|
|
|
|
|
JSCLAutoErrorReporterSetter(const JSCLAutoErrorReporterSetter &) MOZ_DELETE;
|
|
|
|
const JSCLAutoErrorReporterSetter& operator=(const JSCLAutoErrorReporterSetter &) MOZ_DELETE;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-02-17 20:11:09 -08:00
|
|
|
static nsresult
|
2011-12-18 02:09:16 -08:00
|
|
|
ReportOnCaller(JSContext *callerContext,
|
2009-02-17 20:11:09 -08:00
|
|
|
const char *format, ...) {
|
2011-12-18 02:09:16 -08:00
|
|
|
if (!callerContext) {
|
2009-02-17 20:11:09 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2009-02-17 20:11:09 -08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2011-05-13 08:56:26 -07:00
|
|
|
char *buf = JS_vsmprintf(format, ap);
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportError(callerContext, buf);
|
|
|
|
JS_smprintf_free(buf);
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ReportOnCaller(JSCLContextHelper &helper,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2011-05-13 08:56:26 -07:00
|
|
|
char *buf = JS_vsmprintf(format, ap);
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|
|
|
|
|
2011-05-13 08:56:26 -07:00
|
|
|
helper.reportErrorAfterPop(buf);
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mozJSComponentLoader::mozJSComponentLoader()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mRuntime(nullptr),
|
|
|
|
mContext(nullptr),
|
2013-09-02 01:41:57 -07:00
|
|
|
mModules(32),
|
|
|
|
mImports(32),
|
|
|
|
mInProgressImports(32),
|
|
|
|
mThisObjects(32),
|
2012-10-31 09:13:28 -07:00
|
|
|
mInitialized(false),
|
|
|
|
mReuseLoaderGlobal(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(!sSelf, "mozJSComponentLoader should be a singleton");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gJSCLLog) {
|
|
|
|
gJSCLLog = PR_NewLogModule("JSComponentLoader");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sSelf = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozJSComponentLoader::~mozJSComponentLoader()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
NS_ERROR("'xpcom-shutdown-loaders' was not fired before cleaning up mozJSComponentLoader");
|
|
|
|
UnloadModules();
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
sSelf = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mozJSComponentLoader*
|
|
|
|
mozJSComponentLoader::sSelf;
|
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_IMPL_ISUPPORTS3(mozJSComponentLoader,
|
2010-06-10 11:11:11 -07:00
|
|
|
mozilla::ModuleLoader,
|
2007-05-15 16:27:40 -07:00
|
|
|
xpcIJSModuleLoader,
|
|
|
|
nsIObserver)
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
mozJSComponentLoader::ReallyInit()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
mReuseLoaderGlobal = Preferences::GetBool("jsloader.reuseGlobal");
|
2010-08-12 12:37:52 -07:00
|
|
|
|
2013-05-07 11:02:31 -07:00
|
|
|
// XXXkhuey B2G child processes have some sort of preferences race that
|
|
|
|
// results in getting the wrong value.
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
mReuseLoaderGlobal = true;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Get the JSRuntime from the runtime svc, if possible.
|
|
|
|
* We keep a reference around, because it's a Bad Thing if the runtime
|
|
|
|
* service gets shut down before we're done. Bad!
|
|
|
|
*/
|
|
|
|
|
|
|
|
mRuntimeService = do_GetService(kJSRuntimeServiceContractID, &rv);
|
|
|
|
if (NS_FAILED(rv) ||
|
|
|
|
NS_FAILED(rv = mRuntimeService->GetRuntime(&mRuntime)))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Create our compilation context.
|
|
|
|
mContext = JS_NewContext(mRuntime, 256);
|
|
|
|
if (!mContext)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-10-14 10:52:47 -07:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
2007-03-22 10:30:00 -07:00
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
|
|
|
if (!secman)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
|
|
|
|
if (NS_FAILED(rv) || !mSystemPrincipal)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
|
|
|
do_GetService(kObserverServiceContractID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = obsSvc->AddObserver(this, "xpcom-shutdown-loaders", false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
const mozilla::Module*
|
2011-11-08 09:10:51 -08:00
|
|
|
mozJSComponentLoader::LoadModule(FileLocation &aFile)
|
2010-06-15 12:38:46 -07:00
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = aFile.GetBaseFile();
|
2010-06-15 12:38:46 -07:00
|
|
|
|
2011-11-08 09:10:51 -08:00
|
|
|
nsCString spec;
|
|
|
|
aFile.GetURIString(spec);
|
2010-06-15 12:38:46 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2011-11-08 09:10:51 -08:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), spec);
|
2010-06-21 08:41:42 -07:00
|
|
|
if (NS_FAILED(rv))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2010-06-15 12:38:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
|
|
|
if (NS_FAILED(rv))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ModuleEntry* mod;
|
2011-11-08 09:10:51 -08:00
|
|
|
if (mModules.Get(spec, &mod))
|
2012-11-26 14:41:59 -08:00
|
|
|
return mod;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoPtr<ModuleEntry> entry(new ModuleEntry);
|
|
|
|
|
2013-06-20 11:05:34 -07:00
|
|
|
JSAutoRequest ar(mContext);
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedValue dummy(mContext);
|
2013-12-19 12:49:52 -08:00
|
|
|
rv = ObjectForLocation(file, uri, &entry->obj, &entry->thisObjectKey,
|
2013-04-23 08:48:05 -07:00
|
|
|
&entry->location, false, &dummy);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID,
|
|
|
|
&rv);
|
|
|
|
if (NS_FAILED(rv))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIComponentManager> cm;
|
|
|
|
rv = NS_GetComponentManager(getter_AddRefs(cm));
|
|
|
|
if (NS_FAILED(rv))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-29 11:16:19 -07:00
|
|
|
JSCLContextHelper cx(mContext);
|
2012-10-31 09:13:28 -07:00
|
|
|
JSAutoCompartment ac(cx, entry->obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> cm_holder;
|
2012-10-31 09:13:28 -07:00
|
|
|
rv = xpc->WrapNative(cx, entry->obj, cm,
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_GET_IID(nsIComponentManager),
|
|
|
|
getter_AddRefs(cm_holder));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
JSObject* cm_jsobj = cm_holder->GetJSObject();
|
|
|
|
if (!cm_jsobj) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> file_holder;
|
2014-01-31 03:11:23 -08:00
|
|
|
rv = xpc->WrapNative(cx, entry->obj, file,
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(file_holder));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
JSObject* file_jsobj = file_holder->GetJSObject();
|
|
|
|
if (!file_jsobj) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-16 20:38:44 -07:00
|
|
|
JSCLAutoErrorReporterSetter aers(cx, xpc::SystemErrorReporter);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedValue NSGetFactory_val(cx);
|
2014-01-31 03:11:23 -08:00
|
|
|
if (!JS_GetProperty(cx, entry->obj, "NSGetFactory", &NSGetFactory_val) ||
|
2010-06-10 11:11:11 -07:00
|
|
|
JSVAL_IS_VOID(NSGetFactory_val)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
if (JS_TypeOfValue(cx, NSGetFactory_val) != JSTYPE_FUNCTION) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2011-11-08 09:10:51 -08:00
|
|
|
uri->GetSpec(spec);
|
2010-06-10 11:11:11 -07:00
|
|
|
JS_ReportError(cx, "%s has NSGetFactory property that is not a function",
|
2010-06-21 08:41:42 -07:00
|
|
|
spec.get());
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedObject jsGetFactoryObj(cx);
|
2013-09-20 02:22:59 -07:00
|
|
|
if (!JS_ValueToObject(cx, NSGetFactory_val, &jsGetFactoryObj) ||
|
2010-06-10 11:11:11 -07:00
|
|
|
!jsGetFactoryObj) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* XXX report error properly */
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
rv = xpc->WrapJS(cx, jsGetFactoryObj,
|
2010-06-11 13:13:26 -07:00
|
|
|
NS_GET_IID(xpcIJSGetFactory), getter_AddRefs(entry->getfactoryobj));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
/* XXX report error properly */
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mJCL: couldn't get nsIModule from jsval\n");
|
|
|
|
#endif
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this module for later
|
2012-05-18 10:30:49 -07:00
|
|
|
mModules.Put(spec, entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-16 10:42:10 -07:00
|
|
|
// Set the location information for the new global, so that tools like
|
|
|
|
// about:memory may use that information
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!mReuseLoaderGlobal) {
|
2014-01-31 03:11:23 -08:00
|
|
|
xpc::SetLocationForGlobal(entry->obj, spec);
|
2012-10-31 09:13:28 -07:00
|
|
|
}
|
2012-05-16 10:42:10 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// The hash owns the ModuleEntry now, forget about it
|
2010-06-11 13:13:26 -07:00
|
|
|
return entry.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-13 11:13:27 -08:00
|
|
|
nsresult
|
|
|
|
mozJSComponentLoader::FindTargetObject(JSContext* aCx,
|
2013-10-14 04:42:16 -07:00
|
|
|
MutableHandleObject aTargetObject)
|
2012-11-13 11:13:27 -08:00
|
|
|
{
|
2013-04-23 08:48:05 -07:00
|
|
|
aTargetObject.set(nullptr);
|
2012-11-13 11:13:27 -08:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedObject targetObject(aCx);
|
2012-11-13 11:13:27 -08:00
|
|
|
if (mReuseLoaderGlobal) {
|
|
|
|
JSScript* script =
|
|
|
|
js::GetOutermostEnclosingFunctionOfScriptedCaller(aCx);
|
|
|
|
if (script) {
|
|
|
|
targetObject = mThisObjects.Get(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The above could fail, even if mReuseLoaderGlobal, if the scripted
|
|
|
|
// caller is not a component/JSM (it could be a DOM scope, for
|
|
|
|
// instance).
|
|
|
|
if (!targetObject) {
|
|
|
|
// Our targetObject is the caller's global object. Let's get it.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIXPConnect> xpc =
|
|
|
|
do_GetService(kXPConnectServiceContractID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAXPCNativeCallContext *cc = nullptr;
|
|
|
|
rv = xpc->GetCurrentNativeCallContext(&cc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wn;
|
|
|
|
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
targetObject = wn->GetJSObject();
|
2012-11-13 11:13:27 -08:00
|
|
|
if (!targetObject) {
|
|
|
|
NS_ERROR("null calling object");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
targetObject = JS_GetGlobalForObject(aCx, targetObject);
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
aTargetObject.set(targetObject);
|
2012-11-13 11:13:27 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-26 14:41:55 -08:00
|
|
|
void
|
2013-04-26 10:50:18 -07:00
|
|
|
mozJSComponentLoader::NoteSubScript(HandleScript aScript, HandleObject aThisObject)
|
2012-11-26 14:41:55 -08:00
|
|
|
{
|
|
|
|
if (!mInitialized && NS_FAILED(ReallyInit())) {
|
2013-06-12 00:00:09 -07:00
|
|
|
MOZ_CRASH();
|
2012-11-26 14:41:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mThisObjects.Put(aScript, aThisObject);
|
|
|
|
}
|
|
|
|
|
2013-12-05 13:54:30 -08:00
|
|
|
/* static */ size_t
|
|
|
|
mozJSComponentLoader::DataEntrySizeOfExcludingThis(const nsACString& aKey,
|
|
|
|
ModuleEntry* const& aData,
|
|
|
|
MallocSizeOf aMallocSizeOf, void*)
|
|
|
|
{
|
|
|
|
return aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
|
|
|
aData->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ size_t
|
|
|
|
mozJSComponentLoader::ClassEntrySizeOfExcludingThis(const nsACString& aKey,
|
|
|
|
const nsAutoPtr<ModuleEntry>& aData,
|
|
|
|
MallocSizeOf aMallocSizeOf, void*)
|
|
|
|
{
|
|
|
|
return aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
|
|
|
aData->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
mozJSComponentLoader::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
amount += mModules.SizeOfExcludingThis(DataEntrySizeOfExcludingThis, aMallocSizeOf);
|
|
|
|
amount += mImports.SizeOfExcludingThis(ClassEntrySizeOfExcludingThis, aMallocSizeOf);
|
|
|
|
amount += mInProgressImports.SizeOfExcludingThis(DataEntrySizeOfExcludingThis, aMallocSizeOf);
|
|
|
|
amount += mThisObjects.SizeOfExcludingThis(nullptr, aMallocSizeOf);
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Some stack based classes for cleaning up on early return
|
|
|
|
#ifdef HAVE_PR_MEMMAP
|
|
|
|
class FileAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FileAutoCloser(PRFileDesc *file) : mFile(file) {}
|
|
|
|
~FileAutoCloser() { PR_Close(mFile); }
|
|
|
|
private:
|
|
|
|
PRFileDesc *mFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileMapAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FileMapAutoCloser(PRFileMap *map) : mMap(map) {}
|
|
|
|
~FileMapAutoCloser() { PR_CloseFileMap(mMap); }
|
|
|
|
private:
|
|
|
|
PRFileMap *mMap;
|
|
|
|
};
|
2011-12-16 11:08:59 -08:00
|
|
|
#else
|
|
|
|
class ANSIFileAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ANSIFileAutoCloser(FILE *file) : mFile(file) {}
|
|
|
|
~ANSIFileAutoCloser() { fclose(mFile); }
|
|
|
|
private:
|
|
|
|
FILE *mFile;
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
JSObject*
|
|
|
|
mozJSComponentLoader::PrepareObjectForLocation(JSCLContextHelper& aCx,
|
|
|
|
nsIFile *aComponentFile,
|
|
|
|
nsIURI *aURI,
|
|
|
|
bool aReuseLoaderGlobal,
|
|
|
|
bool *aRealFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-10-31 09:13:28 -07:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
|
|
|
if (aReuseLoaderGlobal) {
|
|
|
|
holder = mLoaderGlobal;
|
|
|
|
}
|
2012-10-31 01:35:11 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
nsresult rv = NS_OK;
|
2012-10-31 01:35:11 -07:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc =
|
|
|
|
do_GetService(kXPConnectServiceContractID, &rv);
|
2012-10-31 09:13:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
if (!mLoaderGlobal) {
|
2013-04-04 02:27:38 -07:00
|
|
|
nsRefPtr<BackstagePass> backstagePass;
|
|
|
|
rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
|
2012-10-31 09:13:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2013-10-14 04:42:16 -07:00
|
|
|
CompartmentOptions options;
|
|
|
|
options.setZone(SystemZone)
|
2013-06-29 08:11:18 -07:00
|
|
|
.setVersion(JSVERSION_LATEST);
|
2013-04-04 02:27:38 -07:00
|
|
|
rv = xpc->InitClassesWithNewWrappedGlobal(aCx,
|
|
|
|
static_cast<nsIGlobalObject *>(backstagePass),
|
2012-10-31 09:13:28 -07:00
|
|
|
mSystemPrincipal,
|
|
|
|
0,
|
2013-06-29 08:11:17 -07:00
|
|
|
options,
|
2012-10-31 09:13:28 -07:00
|
|
|
getter_AddRefs(holder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
RootedObject global(aCx, holder->GetJSObject());
|
|
|
|
NS_ENSURE_TRUE(global, nullptr);
|
2012-10-31 09:13:28 -07:00
|
|
|
|
2013-04-04 02:27:38 -07:00
|
|
|
backstagePass->SetGlobalObject(global);
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
if (!JS_DefineFunctions(aCx, global, gGlobalFun) ||
|
|
|
|
!JS_DefineProfilingFunctions(aCx, global)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-10-30 08:26:12 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (aReuseLoaderGlobal) {
|
|
|
|
mLoaderGlobal = holder;
|
|
|
|
}
|
|
|
|
}
|
2012-10-30 12:28:11 -07:00
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
RootedObject obj(aCx, holder->GetJSObject());
|
|
|
|
NS_ENSURE_TRUE(obj, nullptr);
|
2012-10-30 12:28:11 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
JSAutoCompartment ac(aCx, obj);
|
|
|
|
|
|
|
|
if (aReuseLoaderGlobal) {
|
|
|
|
// If we're reusing the loader global, we don't actually use the
|
|
|
|
// global, but rather we use a different object as the 'this' object.
|
2014-01-16 09:48:58 -08:00
|
|
|
obj = JS_NewObject(aCx, &kFakeBackstagePassJSClass, NullPtr(), NullPtr());
|
2013-04-23 08:48:05 -07:00
|
|
|
NS_ENSURE_TRUE(obj, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
*aRealFile = false;
|
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
// need to be extra careful checking for URIs pointing to files
|
|
|
|
// EnsureFile may not always get called, especially on resource URIs
|
|
|
|
// so we need to call GetFile to make sure this is a valid file
|
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
|
|
|
|
nsCOMPtr<nsIFile> testFile;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
fileURL->GetFile(getter_AddRefs(testFile));
|
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
if (testFile) {
|
2012-10-31 09:13:28 -07:00
|
|
|
*aRealFile = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> locationHolder;
|
2012-10-31 09:13:28 -07:00
|
|
|
rv = xpc->WrapNative(aCx, obj, aComponentFile,
|
2012-06-05 19:08:30 -07:00
|
|
|
NS_GET_IID(nsIFile),
|
2010-06-15 12:38:46 -07:00
|
|
|
getter_AddRefs(locationHolder));
|
2012-10-31 09:13:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
RootedObject locationObj(aCx, locationHolder->GetJSObject());
|
|
|
|
NS_ENSURE_TRUE(locationObj, nullptr);
|
2010-06-15 12:38:46 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!JS_DefineProperty(aCx, obj, "__LOCATION__",
|
2013-10-14 04:42:16 -07:00
|
|
|
ObjectValue(*locationObj),
|
2012-10-31 09:13:28 -07:00
|
|
|
nullptr, nullptr, 0)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString nativePath;
|
2010-06-15 12:38:46 -07:00
|
|
|
rv = aURI->GetSpec(nativePath);
|
2012-10-31 09:13:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-13 15:10:13 -07:00
|
|
|
// Expose the URI from which the script was imported through a special
|
|
|
|
// variable that we insert into the JSM.
|
2012-10-31 09:13:28 -07:00
|
|
|
JSString *exposedUri = JS_NewStringCopyN(aCx, nativePath.get(),
|
|
|
|
nativePath.Length());
|
|
|
|
if (!JS_DefineProperty(aCx, obj, "__URI__",
|
|
|
|
STRING_TO_JSVAL(exposedUri), nullptr, nullptr, 0)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-10-30 12:28:11 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
mozJSComponentLoader::ObjectForLocation(nsIFile *aComponentFile,
|
|
|
|
nsIURI *aURI,
|
|
|
|
JSObject **aObject,
|
2013-12-19 12:49:52 -08:00
|
|
|
JSScript **aTableScript,
|
2012-10-31 09:13:28 -07:00
|
|
|
char **aLocation,
|
2013-04-23 08:48:05 -07:00
|
|
|
bool aPropagateExceptions,
|
2013-10-14 04:42:16 -07:00
|
|
|
MutableHandleValue aException)
|
2012-10-31 09:13:28 -07:00
|
|
|
{
|
2013-04-29 11:16:19 -07:00
|
|
|
JSCLContextHelper cx(mContext);
|
2012-10-31 09:13:28 -07:00
|
|
|
|
|
|
|
JS_AbortIfWrongThread(JS_GetRuntime(cx));
|
|
|
|
|
2013-07-16 20:38:44 -07:00
|
|
|
JSCLAutoErrorReporterSetter aers(cx, xpc::SystemErrorReporter);
|
2012-10-31 09:13:28 -07:00
|
|
|
|
|
|
|
bool realFile = false;
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedObject obj(cx, PrepareObjectForLocation(cx, aComponentFile, aURI,
|
|
|
|
mReuseLoaderGlobal, &realFile));
|
2012-10-31 09:13:28 -07:00
|
|
|
NS_ENSURE_TRUE(obj, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-26 10:50:18 -07:00
|
|
|
RootedScript script(cx);
|
|
|
|
RootedFunction function(cx);
|
2012-10-31 09:13:28 -07:00
|
|
|
|
|
|
|
nsAutoCString nativePath;
|
|
|
|
nsresult rv = aURI->GetSpec(nativePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-08-12 12:37:52 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Before compiling the script, first check to see if we have it in
|
2010-08-12 12:37:52 -07:00
|
|
|
// the startupcache. Note: as a rule, startupcache errors are not fatal
|
2007-03-22 10:30:00 -07:00
|
|
|
// to loading the script, since we can always slow-load.
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool writeToCache = false;
|
2010-08-12 12:37:52 -07:00
|
|
|
StartupCache* cache = StartupCache::GetSingleton();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString cachePath(kJSCachePrefix);
|
2011-07-20 00:39:09 -07:00
|
|
|
rv = PathifyURI(aURI, cachePath);
|
2011-07-09 20:21:16 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-08-12 12:37:52 -07:00
|
|
|
if (cache) {
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!mReuseLoaderGlobal) {
|
2013-09-20 02:22:59 -07:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
|
2012-10-31 09:13:28 -07:00
|
|
|
} else {
|
|
|
|
rv = ReadCachedFunction(cache, cachePath, cx, mSystemPrincipal,
|
2013-04-26 10:50:18 -07:00
|
|
|
function.address());
|
2012-10-31 09:13:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-08-12 12:37:52 -07:00
|
|
|
LOG(("Successfully loaded %s from startupcache\n", nativePath.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-12 12:37:52 -07:00
|
|
|
// This is ok, it just means the script is not yet in the
|
|
|
|
// cache. Could mean that the cache was corrupted and got removed,
|
|
|
|
// but either way we're going to write this out.
|
2011-10-17 07:59:28 -07:00
|
|
|
writeToCache = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!script && !function) {
|
2010-08-12 12:37:52 -07:00
|
|
|
// The script wasn't in the cache , so compile it now.
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("Slow loading %s\n", nativePath.get()));
|
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
// If aPropagateExceptions is true, then our caller wants us to propagate
|
2008-08-04 16:54:16 -07:00
|
|
|
// any exceptions out to our caller. Ensure that the engine doesn't
|
2008-08-04 10:03:34 -07:00
|
|
|
// eagerly report the exception.
|
2013-10-14 04:42:16 -07:00
|
|
|
AutoSaveContextOptions asco(cx);
|
2013-04-23 08:48:05 -07:00
|
|
|
if (aPropagateExceptions)
|
2013-10-14 04:42:16 -07:00
|
|
|
ContextOptionsRef(cx).setDontReportUncaught(true);
|
2013-04-23 08:48:05 -07:00
|
|
|
|
2013-10-14 04:42:16 -07:00
|
|
|
CompileOptions options(cx);
|
2012-08-07 10:39:47 -07:00
|
|
|
options.setPrincipals(nsJSPrincipals::get(mSystemPrincipal))
|
2012-10-31 09:13:28 -07:00
|
|
|
.setNoScriptRval(mReuseLoaderGlobal ? false : true)
|
2012-08-07 10:39:47 -07:00
|
|
|
.setVersion(JSVERSION_LATEST)
|
|
|
|
.setFileAndLine(nativePath.get(), 1)
|
2012-10-31 09:13:28 -07:00
|
|
|
.setSourcePolicy(mReuseLoaderGlobal ?
|
2013-10-14 04:42:16 -07:00
|
|
|
CompileOptions::NO_SOURCE :
|
|
|
|
CompileOptions::LAZY_SOURCE);
|
2008-08-04 10:03:34 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
if (realFile) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef HAVE_PR_MEMMAP
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t fileSize;
|
2010-06-15 12:38:46 -07:00
|
|
|
rv = aComponentFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-25 16:25:57 -07:00
|
|
|
int64_t maxSize = UINT32_MAX;
|
2012-10-03 07:13:19 -07:00
|
|
|
if (fileSize > maxSize) {
|
2010-06-15 12:38:46 -07:00
|
|
|
NS_ERROR("file too large");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
PRFileDesc *fileHandle;
|
|
|
|
rv = aComponentFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fileHandle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
// Make sure the file is closed, no matter how we return.
|
|
|
|
FileAutoCloser fileCloser(fileHandle);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
PRFileMap *map = PR_CreateFileMap(fileHandle, fileSize,
|
|
|
|
PR_PROT_READONLY);
|
|
|
|
if (!map) {
|
|
|
|
NS_ERROR("Failed to create file map");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
// Make sure the file map is closed, no matter how we return.
|
|
|
|
FileMapAutoCloser mapCloser(map);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-12 10:29:11 -07:00
|
|
|
uint32_t fileSize32 = fileSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
char *buf = static_cast<char*>(PR_MemMap(map, 0, fileSize32));
|
|
|
|
if (!buf) {
|
|
|
|
NS_WARNING("Failed to map file");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!mReuseLoaderGlobal) {
|
2013-10-14 04:42:16 -07:00
|
|
|
script = Compile(cx, obj, options, buf,
|
2012-10-31 09:13:28 -07:00
|
|
|
fileSize32);
|
|
|
|
} else {
|
2013-10-14 04:42:16 -07:00
|
|
|
function = CompileFunction(cx, obj, options,
|
2012-10-31 09:13:28 -07:00
|
|
|
nullptr, 0, nullptr,
|
|
|
|
buf, fileSize32);
|
|
|
|
}
|
2010-11-01 17:33:22 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
PR_MemUnmap(buf, fileSize32);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#else /* HAVE_PR_MEMMAP */
|
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
/**
|
2011-12-16 11:08:59 -08:00
|
|
|
* No memmap implementation, so fall back to
|
|
|
|
* reading in the file
|
2010-06-15 12:38:46 -07:00
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
FILE *fileHandle;
|
|
|
|
rv = aComponentFile->OpenANSIFileDesc("r", &fileHandle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-16 11:08:59 -08:00
|
|
|
// Ensure file fclose
|
|
|
|
ANSIFileAutoCloser fileCloser(fileHandle);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t len;
|
2011-12-16 11:08:59 -08:00
|
|
|
rv = aComponentFile->GetFileSize(&len);
|
|
|
|
if (NS_FAILED(rv) || len < 0) {
|
|
|
|
NS_WARNING("Failed to get file size");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = (char *) malloc(len * sizeof(char));
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rlen = fread(buf, 1, len, fileHandle);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (rlen != (uint64_t)len) {
|
2011-12-16 11:08:59 -08:00
|
|
|
free(buf);
|
|
|
|
NS_WARNING("Failed to read file");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-10-31 09:13:28 -07:00
|
|
|
|
|
|
|
if (!mReuseLoaderGlobal) {
|
2013-10-14 04:42:16 -07:00
|
|
|
script = Compile(cx, obj, options, buf,
|
2012-10-31 09:13:28 -07:00
|
|
|
fileSize32);
|
|
|
|
} else {
|
2013-10-14 04:42:16 -07:00
|
|
|
function = CompileFunction(cx, obj, options,
|
|
|
|
nullptr, 0, nullptr,
|
|
|
|
buf, fileSize32);
|
2012-10-31 09:13:28 -07:00
|
|
|
}
|
2011-12-16 11:08:59 -08:00
|
|
|
|
|
|
|
free(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-04 11:12:51 -07:00
|
|
|
#endif /* HAVE_PR_MEMMAP */
|
2010-06-15 12:38:46 -07:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(aURI, getter_AddRefs(scriptChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> scriptStream;
|
|
|
|
rv = scriptChannel->Open(getter_AddRefs(scriptStream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-08-04 10:03:34 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t len64;
|
|
|
|
uint32_t bytesRead;
|
2010-06-15 12:38:46 -07:00
|
|
|
|
2012-08-10 19:44:11 -07:00
|
|
|
rv = scriptStream->Available(&len64);
|
2010-06-15 12:38:46 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ENSURE_TRUE(len64 < UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2012-08-10 19:44:11 -07:00
|
|
|
if (!len64)
|
2010-06-15 12:38:46 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = (uint32_t)len64;
|
2010-06-15 12:38:46 -07:00
|
|
|
|
|
|
|
/* malloc an internal buf the size of the file */
|
|
|
|
nsAutoArrayPtr<char> buf(new char[len + 1]);
|
|
|
|
if (!buf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* read the file in one swoop */
|
|
|
|
rv = scriptStream->Read(buf, len, &bytesRead);
|
|
|
|
if (bytesRead != len)
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!mReuseLoaderGlobal) {
|
2013-10-14 04:42:16 -07:00
|
|
|
script = Compile(cx, obj, options, buf, bytesRead);
|
2012-10-31 09:13:28 -07:00
|
|
|
} else {
|
2013-10-14 04:42:16 -07:00
|
|
|
function = CompileFunction(cx, obj, options,
|
2012-10-31 09:13:28 -07:00
|
|
|
nullptr, 0, nullptr,
|
|
|
|
buf, bytesRead);
|
|
|
|
}
|
2010-06-15 12:38:46 -07:00
|
|
|
}
|
2008-08-04 10:03:34 -07:00
|
|
|
// Propagate the exception, if one exists. Also, don't leave the stale
|
|
|
|
// exception on this context.
|
2013-04-23 08:48:05 -07:00
|
|
|
if (!script && !function && aPropagateExceptions) {
|
2013-09-19 00:54:01 -07:00
|
|
|
JS_GetPendingException(cx, aException);
|
2011-03-25 14:31:14 -07:00
|
|
|
JS_ClearPendingException(cx);
|
2008-08-04 10:03:34 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!script && !function) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-08-12 12:37:52 -07:00
|
|
|
if (writeToCache) {
|
2011-10-14 10:52:47 -07:00
|
|
|
// We successfully compiled the script, so cache it.
|
2012-10-31 09:13:28 -07:00
|
|
|
if (script) {
|
|
|
|
rv = WriteCachedScript(cache, cachePath, cx, mSystemPrincipal,
|
|
|
|
script);
|
|
|
|
} else {
|
|
|
|
rv = WriteCachedFunction(cache, cachePath, cx, mSystemPrincipal,
|
|
|
|
function);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Don't treat failure to write as fatal, since we might be working
|
2010-08-12 12:37:52 -07:00
|
|
|
// with a read-only cache.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-08-12 12:37:52 -07:00
|
|
|
LOG(("Successfully wrote to cache\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-12 12:37:52 -07:00
|
|
|
LOG(("Failed to write to cache\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
// Assign aObject here so that it's available to recursive imports.
|
2007-06-19 23:29:49 -07:00
|
|
|
// See bug 384168.
|
2012-10-31 09:13:28 -07:00
|
|
|
*aObject = obj;
|
2007-06-19 23:29:49 -07:00
|
|
|
|
2013-12-19 12:49:52 -08:00
|
|
|
RootedScript tableScript(cx, script);
|
2012-11-13 11:13:27 -08:00
|
|
|
if (!tableScript) {
|
|
|
|
tableScript = JS_GetFunctionScript(cx, function);
|
|
|
|
MOZ_ASSERT(tableScript);
|
|
|
|
}
|
|
|
|
|
2013-12-19 12:49:52 -08:00
|
|
|
*aTableScript = tableScript;
|
|
|
|
|
|
|
|
// tableScript stays in the table until shutdown. To avoid it being
|
|
|
|
// collected and another script getting the same address, we root
|
|
|
|
// tableScript lower down in this function.
|
2012-11-13 11:13:27 -08:00
|
|
|
mThisObjects.Put(tableScript, obj);
|
2012-10-31 09:13:28 -07:00
|
|
|
bool ok = false;
|
2013-10-14 04:42:16 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
AutoSaveContextOptions asco(cx);
|
|
|
|
if (aPropagateExceptions)
|
|
|
|
ContextOptionsRef(cx).setDontReportUncaught(true);
|
|
|
|
if (script) {
|
2013-10-23 13:36:09 -07:00
|
|
|
ok = JS_ExecuteScriptVersion(cx, obj, script, nullptr, JSVERSION_LATEST);
|
2013-10-14 04:42:16 -07:00
|
|
|
} else {
|
2014-01-14 18:46:44 -08:00
|
|
|
RootedValue rval(cx);
|
|
|
|
ok = JS_CallFunction(cx, obj, function, 0, nullptr, rval.address());
|
2013-10-14 04:42:16 -07:00
|
|
|
}
|
|
|
|
}
|
2011-12-28 08:55:11 -08:00
|
|
|
|
|
|
|
if (!ok) {
|
2013-04-23 08:48:05 -07:00
|
|
|
if (aPropagateExceptions) {
|
2013-09-19 00:54:01 -07:00
|
|
|
JS_GetPendingException(cx, aException);
|
2011-12-28 08:55:11 -08:00
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
}
|
2012-10-31 09:13:28 -07:00
|
|
|
*aObject = nullptr;
|
2013-12-19 12:49:52 -08:00
|
|
|
*aTableScript = nullptr;
|
|
|
|
mThisObjects.Remove(tableScript);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Freed when we remove from the table. */
|
2010-06-15 12:38:46 -07:00
|
|
|
*aLocation = ToNewCString(nativePath);
|
2007-06-19 23:29:49 -07:00
|
|
|
if (!*aLocation) {
|
2012-10-31 09:13:28 -07:00
|
|
|
*aObject = nullptr;
|
2013-12-19 12:49:52 -08:00
|
|
|
*aTableScript = nullptr;
|
|
|
|
mThisObjects.Remove(tableScript);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-06-19 23:29:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
JS_AddNamedObjectRoot(cx, aObject, *aLocation);
|
2013-12-19 12:49:52 -08:00
|
|
|
JS_AddNamedScriptRoot(cx, aTableScript, *aLocation);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-11 13:13:26 -07:00
|
|
|
/* static */ PLDHashOperator
|
2011-11-08 09:08:49 -08:00
|
|
|
mozJSComponentLoader::ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx)
|
2010-06-11 13:13:26 -07:00
|
|
|
{
|
|
|
|
entry->Clear();
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
mozJSComponentLoader::UnloadModules()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
if (mLoaderGlobal) {
|
|
|
|
MOZ_ASSERT(mReuseLoaderGlobal, "How did this happen?");
|
|
|
|
|
2013-06-19 18:09:48 -07:00
|
|
|
JSAutoRequest ar(mContext);
|
2013-05-20 05:44:18 -07:00
|
|
|
RootedObject global(mContext, mLoaderGlobal->GetJSObject());
|
|
|
|
if (global) {
|
2013-07-17 14:28:54 -07:00
|
|
|
JSAutoCompartment ac(mContext, global);
|
2012-10-31 09:13:28 -07:00
|
|
|
JS_SetAllNonReservedSlotsToUndefined(mContext, global);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Going to leak!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoaderGlobal = nullptr;
|
|
|
|
}
|
|
|
|
|
2007-06-19 23:29:49 -07:00
|
|
|
mInProgressImports.Clear();
|
2007-05-15 16:27:40 -07:00
|
|
|
mImports.Clear();
|
2012-11-13 11:13:27 -08:00
|
|
|
mThisObjects.Clear();
|
2010-06-11 13:13:26 -07:00
|
|
|
|
2013-10-10 13:35:53 -07:00
|
|
|
mModules.Enumerate(ClearModules, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-25 08:42:40 -07:00
|
|
|
JS_DestroyContextNoGC(mContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mRuntimeService = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_IMETHODIMP
|
2011-12-18 02:09:16 -08:00
|
|
|
mozJSComponentLoader::Import(const nsACString& registryLocation,
|
2014-01-09 09:39:36 -08:00
|
|
|
HandleValue targetValArg,
|
|
|
|
JSContext *cx,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t optionalArgc,
|
2014-01-09 09:39:36 -08:00
|
|
|
MutableHandleValue retval)
|
2007-05-15 16:27:40 -07:00
|
|
|
{
|
2012-10-25 16:10:53 -07:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
|
2013-04-23 08:48:05 -07:00
|
|
|
|
|
|
|
RootedValue targetVal(cx, targetValArg);
|
|
|
|
RootedObject targetObject(cx, nullptr);
|
2011-12-18 02:09:16 -08:00
|
|
|
if (optionalArgc) {
|
2007-05-15 16:27:40 -07:00
|
|
|
// The caller passed in the optional second argument. Get it.
|
2012-07-23 07:47:18 -07:00
|
|
|
if (targetVal.isObject()) {
|
|
|
|
// If we're passing in something like a content DOM window, chances
|
|
|
|
// are the caller expects the properties to end up on the object
|
|
|
|
// proper and not on the Xray holder. This is dubious, but can be used
|
|
|
|
// during testing. Given that dumb callers can already leak JSMs into
|
|
|
|
// content by passing a raw content JS object (where Xrays aren't
|
|
|
|
// possible), we aim for consistency here. Waive xray.
|
|
|
|
if (WrapperFactory::IsXrayWrapper(&targetVal.toObject()) &&
|
2013-10-26 09:19:05 -07:00
|
|
|
!WrapperFactory::WaiveXrayAndWrap(cx, &targetVal))
|
2012-07-23 07:47:18 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
targetObject = &targetVal.toObject();
|
|
|
|
} else if (!targetVal.isNull()) {
|
|
|
|
// If targetVal isNull(), we actually want to leave targetObject null.
|
|
|
|
// Not doing so breaks |make package|.
|
2011-12-18 02:09:16 -08:00
|
|
|
return ReportOnCaller(cx, ERROR_SCOPE_OBJ,
|
2012-07-23 07:47:18 -07:00
|
|
|
PromiseFlatCString(registryLocation).get());
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
|
|
|
} else {
|
2012-11-13 11:13:27 -08:00
|
|
|
nsresult rv = FindTargetObject(cx, &targetObject);
|
2011-12-18 02:09:16 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (targetObject) {
|
|
|
|
ac.construct(cx, targetObject);
|
2010-10-10 15:42:04 -07:00
|
|
|
}
|
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedObject global(cx);
|
|
|
|
nsresult rv = ImportInto(registryLocation, targetObject, cx, &global);
|
2010-10-10 15:42:04 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
if (global) {
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &global)) {
|
2013-04-23 08:48:05 -07:00
|
|
|
NS_ERROR("can't wrap return value");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-05-15 16:27:40 -07:00
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
retval.setObject(*global);
|
2013-04-23 08:48:05 -07:00
|
|
|
}
|
2007-05-15 16:27:40 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] JSObjectPtr importInto(in AUTF8String registryLocation,
|
|
|
|
in JSObjectPtr targetObj); */
|
|
|
|
NS_IMETHODIMP
|
2014-01-22 09:50:32 -08:00
|
|
|
mozJSComponentLoader::ImportInto(const nsACString &aLocation,
|
2013-04-23 08:48:05 -07:00
|
|
|
JSObject *aTargetObj,
|
2014-01-22 09:50:32 -08:00
|
|
|
nsAXPCNativeCallContext *cc,
|
2013-04-23 08:48:05 -07:00
|
|
|
JSObject **_retval)
|
2011-12-18 02:09:16 -08:00
|
|
|
{
|
|
|
|
JSContext *callercx;
|
|
|
|
nsresult rv = cc->GetJSContext(&callercx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-04-23 08:48:05 -07:00
|
|
|
|
|
|
|
RootedObject targetObject(callercx, aTargetObj);
|
|
|
|
RootedObject global(callercx);
|
|
|
|
rv = ImportInto(aLocation, targetObject, callercx, &global);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*_retval = global;
|
|
|
|
return NS_OK;
|
2011-12-18 02:09:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-23 08:48:05 -07:00
|
|
|
mozJSComponentLoader::ImportInto(const nsACString &aLocation,
|
2013-10-14 04:42:16 -07:00
|
|
|
HandleObject targetObj,
|
2013-04-23 08:48:05 -07:00
|
|
|
JSContext *callercx,
|
2013-10-14 04:42:16 -07:00
|
|
|
MutableHandleObject vp)
|
2007-05-15 16:27:40 -07:00
|
|
|
{
|
2013-04-23 08:48:05 -07:00
|
|
|
vp.set(nullptr);
|
2007-05-15 16:27:40 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
nsresult rv;
|
2007-05-15 16:27:40 -07:00
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2007-06-10 14:13:18 -07:00
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-10 14:13:18 -07:00
|
|
|
|
2007-11-30 22:45:04 -08:00
|
|
|
// Get the URI.
|
2007-06-10 14:13:18 -07:00
|
|
|
nsCOMPtr<nsIURI> resURI;
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = ioService->NewURI(aLocation, nullptr, nullptr, getter_AddRefs(resURI));
|
2010-06-15 12:38:46 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-10 14:13:18 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
// figure out the resolved URI
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(resURI, getter_AddRefs(scriptChannel));
|
2007-11-30 22:45:04 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG);
|
2007-06-10 14:13:18 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
rv = scriptChannel->GetURI(getter_AddRefs(resolvedURI));
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
// get the JAR if there is one
|
|
|
|
nsCOMPtr<nsIJARURI> jarURI;
|
|
|
|
jarURI = do_QueryInterface(resolvedURI, &rv);
|
|
|
|
nsCOMPtr<nsIFileURL> baseFileURL;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
2011-12-08 02:03:36 -08:00
|
|
|
while (jarURI) {
|
|
|
|
jarURI->GetJARFile(getter_AddRefs(baseURI));
|
|
|
|
jarURI = do_QueryInterface(baseURI, &rv);
|
|
|
|
}
|
2010-06-15 12:38:46 -07:00
|
|
|
baseFileURL = do_QueryInterface(baseURI, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
baseFileURL = do_QueryInterface(resolvedURI, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
nsCOMPtr<nsIFile> sourceFile;
|
|
|
|
rv = baseFileURL->GetFile(getter_AddRefs(sourceFile));
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-15 12:38:46 -07:00
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> sourceLocalFile;
|
2010-06-15 12:38:46 -07:00
|
|
|
sourceLocalFile = do_QueryInterface(sourceFile, &rv);
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString key;
|
2011-11-08 09:08:49 -08:00
|
|
|
rv = resolvedURI->GetSpec(key);
|
2010-06-15 12:38:46 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 16:27:40 -07:00
|
|
|
|
|
|
|
ModuleEntry* mod;
|
|
|
|
nsAutoPtr<ModuleEntry> newEntry;
|
2010-06-15 12:38:46 -07:00
|
|
|
if (!mImports.Get(key, &mod) && !mInProgressImports.Get(key, &mod)) {
|
2007-05-15 16:27:40 -07:00
|
|
|
newEntry = new ModuleEntry;
|
2012-05-18 10:30:49 -07:00
|
|
|
if (!newEntry)
|
2007-05-15 16:27:40 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-05-18 10:30:49 -07:00
|
|
|
mInProgressImports.Put(key, newEntry);
|
2007-06-19 23:29:49 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedValue exception(callercx);
|
2012-10-31 09:13:28 -07:00
|
|
|
rv = ObjectForLocation(sourceLocalFile, resURI, &newEntry->obj,
|
2013-12-19 12:49:52 -08:00
|
|
|
&newEntry->thisObjectKey,
|
2013-04-23 08:48:05 -07:00
|
|
|
&newEntry->location, true, &exception);
|
2007-06-19 23:29:49 -07:00
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
mInProgressImports.Remove(key);
|
2007-06-19 23:29:49 -07:00
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-04-23 08:48:05 -07:00
|
|
|
if (!exception.isUndefined()) {
|
2008-08-04 10:03:34 -07:00
|
|
|
// An exception was thrown during compilation. Propagate it
|
|
|
|
// out to our caller so they can report it.
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(callercx, &exception))
|
2012-02-23 13:50:01 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-23 08:48:05 -07:00
|
|
|
JS_SetPendingException(callercx, exception);
|
2008-08-04 10:03:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something failed, but we don't know what it is, guess.
|
2008-08-04 18:04:19 -07:00
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
2007-06-19 23:29:49 -07:00
|
|
|
|
2012-05-16 10:42:10 -07:00
|
|
|
// Set the location information for the new global, so that tools like
|
|
|
|
// about:memory may use that information
|
2012-10-31 09:13:28 -07:00
|
|
|
if (!mReuseLoaderGlobal) {
|
|
|
|
xpc::SetLocationForGlobal(newEntry->obj, aLocation);
|
|
|
|
}
|
2012-05-16 10:42:10 -07:00
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
mod = newEntry;
|
|
|
|
}
|
|
|
|
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(mod->obj, "Import table contains entry with no object");
|
2013-04-23 08:48:05 -07:00
|
|
|
vp.set(mod->obj);
|
2007-05-15 16:27:40 -07:00
|
|
|
|
|
|
|
if (targetObj) {
|
2013-04-29 11:16:19 -07:00
|
|
|
JSCLContextHelper cxhelper(mContext);
|
2012-10-31 09:13:28 -07:00
|
|
|
JSAutoCompartment ac(mContext, mod->obj);
|
2007-09-18 17:26:39 -07:00
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedValue symbols(mContext);
|
2014-01-31 03:11:23 -08:00
|
|
|
if (!JS_GetProperty(mContext, mod->obj,
|
2013-07-26 02:00:38 -07:00
|
|
|
"EXPORTED_SYMBOLS", &symbols)) {
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_NOT_PRESENT,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
|
|
|
|
2014-01-29 02:00:40 -08:00
|
|
|
if (!JS_IsArrayObject(mContext, symbols)) {
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_NOT_AN_ARRAY,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedObject symbolsObj(mContext, &symbols.toObject());
|
2012-05-11 08:46:26 -07:00
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
// Iterate over symbols array, installing symbols on targetObj:
|
|
|
|
|
2012-03-06 15:52:55 -08:00
|
|
|
uint32_t symbolCount = 0;
|
2007-05-15 16:27:40 -07:00
|
|
|
if (!JS_GetArrayLength(mContext, symbolsObj, &symbolCount)) {
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_GETTING_ARRAY_LENGTH,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
2007-06-18 08:36:16 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString logBuffer;
|
2007-06-18 08:36:16 -07:00
|
|
|
#endif
|
|
|
|
|
2013-04-23 08:48:05 -07:00
|
|
|
RootedValue value(mContext);
|
|
|
|
RootedId symbolId(mContext);
|
2012-03-06 15:52:55 -08:00
|
|
|
for (uint32_t i = 0; i < symbolCount; ++i) {
|
2013-08-05 06:02:47 -07:00
|
|
|
if (!JS_GetElement(mContext, symbolsObj, i, &value) ||
|
2013-04-23 08:48:05 -07:00
|
|
|
!value.isString() ||
|
2014-01-17 10:08:51 -08:00
|
|
|
!JS_ValueToId(mContext, value, &symbolId)) {
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_ARRAY_ELEMENT,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get(), i);
|
|
|
|
}
|
|
|
|
|
2014-01-31 03:11:23 -08:00
|
|
|
if (!JS_GetPropertyById(mContext, mod->obj, symbolId, &value)) {
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!bytes)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_GETTING_SYMBOL,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get(),
|
2010-11-11 12:40:29 -08:00
|
|
|
bytes.ptr());
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment target_ac(mContext, targetObj);
|
2010-09-29 10:00:52 -07:00
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(mContext, &value) ||
|
2013-07-26 02:00:38 -07:00
|
|
|
!JS_SetPropertyById(mContext, targetObj, symbolId, value)) {
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!bytes)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-02-17 20:11:09 -08:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_SETTING_SYMBOL,
|
2007-05-15 16:27:40 -07:00
|
|
|
PromiseFlatCString(aLocation).get(),
|
2010-11-11 12:40:29 -08:00
|
|
|
bytes.ptr());
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (i == 0) {
|
2007-06-18 08:36:16 -07:00
|
|
|
logBuffer.AssignLiteral("Installing symbols [ ");
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!!bytes)
|
|
|
|
logBuffer.Append(bytes.ptr());
|
2007-06-18 08:36:16 -07:00
|
|
|
logBuffer.AppendLiteral(" ");
|
2007-05-15 16:27:40 -07:00
|
|
|
if (i == symbolCount - 1) {
|
2011-08-15 15:56:46 -07:00
|
|
|
LOG(("%s] from %s\n", logBuffer.get(),
|
2011-10-14 10:52:48 -07:00
|
|
|
PromiseFlatCString(aLocation).get()));
|
2007-05-15 16:27:40 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this module for later
|
|
|
|
if (newEntry) {
|
2012-05-18 10:30:49 -07:00
|
|
|
mImports.Put(key, newEntry);
|
2007-05-15 16:27:40 -07:00
|
|
|
newEntry.forget();
|
|
|
|
}
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:08:43 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSComponentLoader::Unload(const nsACString & aLocation)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get the URI.
|
|
|
|
nsCOMPtr<nsIURI> resURI;
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = ioService->NewURI(aLocation, nullptr, nullptr, getter_AddRefs(resURI));
|
2011-06-15 11:08:43 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// figure out the resolved URI
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(resURI, getter_AddRefs(scriptChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
rv = scriptChannel->GetURI(getter_AddRefs(resolvedURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString key;
|
2011-11-08 09:08:49 -08:00
|
|
|
rv = resolvedURI->GetSpec(key);
|
2011-06-15 11:08:43 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ModuleEntry* mod;
|
|
|
|
if (mImports.Get(key, &mod)) {
|
|
|
|
mImports.Remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSComponentLoader::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *data)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-12 12:37:52 -07:00
|
|
|
if (!strcmp(topic, "xpcom-shutdown-loaders")) {
|
2007-03-22 10:30:00 -07:00
|
|
|
UnloadModules();
|
2011-10-14 10:52:48 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ERROR("Unexpected observer topic.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 13:54:30 -08:00
|
|
|
size_t
|
|
|
|
mozJSComponentLoader::ModuleEntry::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += aMallocSizeOf(location);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2010-06-11 13:13:26 -07:00
|
|
|
/* static */ already_AddRefed<nsIFactory>
|
|
|
|
mozJSComponentLoader::ModuleEntry::GetFactory(const mozilla::Module& module,
|
|
|
|
const mozilla::Module::CIDEntry& entry)
|
|
|
|
{
|
|
|
|
const ModuleEntry& self = static_cast<const ModuleEntry&>(module);
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(self.getfactoryobj, "Handing out an uninitialized module?");
|
2010-06-11 13:13:26 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFactory> f;
|
|
|
|
nsresult rv = self.getfactoryobj->Get(*entry.cid, getter_AddRefs(f));
|
|
|
|
if (NS_FAILED(rv))
|
2013-06-12 00:00:09 -07:00
|
|
|
return nullptr;
|
2010-06-11 13:13:26 -07:00
|
|
|
|
|
|
|
return f.forget();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-04-29 11:16:19 -07:00
|
|
|
JSCLContextHelper::JSCLContextHelper(JSContext* aCx)
|
|
|
|
: mContext(aCx)
|
|
|
|
, mBuf(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-29 11:16:19 -07:00
|
|
|
mPusher.Push(mContext);
|
2011-07-18 14:54:48 -07:00
|
|
|
JS_BeginRequest(mContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-13 08:56:26 -07:00
|
|
|
JSCLContextHelper::~JSCLContextHelper()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-29 11:16:19 -07:00
|
|
|
JS_EndRequest(mContext);
|
|
|
|
mPusher.Pop();
|
|
|
|
JSContext *restoredCx = nsContentUtils::GetCurrentJSContext();
|
|
|
|
if (restoredCx && mBuf) {
|
|
|
|
JS_ReportError(restoredCx, mBuf);
|
2011-05-13 08:56:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mBuf) {
|
|
|
|
JS_smprintf_free(mBuf);
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|
2011-05-13 08:56:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSCLContextHelper::reportErrorAfterPop(char *buf)
|
|
|
|
{
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(!mBuf, "Already called reportErrorAfterPop");
|
2011-05-13 08:56:26 -07:00
|
|
|
mBuf = buf;
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|