2014-04-03 04:58:00 -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: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2011-07-09 20:21:16 -07:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2014-06-06 15:39:40 -07:00
|
|
|
#include "jsfriendapi.h"
|
2011-07-09 20:21:16 -07:00
|
|
|
|
2012-02-13 05:10:04 -08:00
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
|
2011-07-09 20:21:16 -07:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
|
2013-09-20 02:22:59 -07:00
|
|
|
using namespace JS;
|
2011-07-09 20:21:16 -07:00
|
|
|
using namespace mozilla::scache;
|
|
|
|
|
2012-02-13 05:10:04 -08:00
|
|
|
// We only serialize scripts with system principals. So we don't serialize the
|
|
|
|
// principals when writing a script. Instead, when reading it back, we set the
|
|
|
|
// principals to the system principals.
|
2011-07-09 20:21:16 -07:00
|
|
|
nsresult
|
2012-02-13 05:10:04 -08:00
|
|
|
ReadCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx,
|
2013-09-20 02:22:59 -07:00
|
|
|
nsIPrincipal *systemPrincipal, MutableHandleScript scriptp)
|
2011-07-09 20:21:16 -07:00
|
|
|
{
|
|
|
|
nsAutoArrayPtr<char> buf;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2012-02-13 05:10:04 -08:00
|
|
|
nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(),
|
|
|
|
getter_Transfers(buf), &len);
|
2012-02-20 02:58:00 -08:00
|
|
|
if (NS_FAILED(rv))
|
2011-07-09 20:21:16 -07:00
|
|
|
return rv; // don't warn since NOT_AVAILABLE is an ok error
|
|
|
|
|
2014-09-29 01:44:30 -07:00
|
|
|
scriptp.set(JS_DecodeScript(cx, buf, len));
|
2013-09-20 02:22:59 -07:00
|
|
|
if (!scriptp)
|
2012-02-13 05:10:04 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-02-20 02:58:00 -08:00
|
|
|
return NS_OK;
|
2011-07-09 20:21:16 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
nsresult
|
|
|
|
ReadCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx,
|
|
|
|
nsIPrincipal *systemPrincipal, JSFunction **functionp)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
/* This doesn't actually work ...
|
|
|
|
nsAutoArrayPtr<char> buf;
|
|
|
|
uint32_t len;
|
|
|
|
nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(),
|
|
|
|
getter_Transfers(buf), &len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv; // don't warn since NOT_AVAILABLE is an ok error
|
|
|
|
|
|
|
|
JSObject *obj = JS_DecodeInterpretedFunction(cx, buf, len, nsJSPrincipals::get(systemPrincipal), nullptr);
|
|
|
|
if (!obj)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
JSFunction* function = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(obj));
|
|
|
|
*functionp = function;
|
|
|
|
return NS_OK;*/
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:21:16 -07:00
|
|
|
nsresult
|
2012-02-13 05:10:04 -08:00
|
|
|
WriteCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx,
|
2013-09-20 02:22:59 -07:00
|
|
|
nsIPrincipal *systemPrincipal, HandleScript script)
|
2011-07-09 20:21:16 -07:00
|
|
|
{
|
2012-02-13 05:10:04 -08:00
|
|
|
MOZ_ASSERT(JS_GetScriptPrincipals(script) == nsJSPrincipals::get(systemPrincipal));
|
2011-07-09 20:21:16 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
uint32_t size;
|
|
|
|
void *data = JS_EncodeScript(cx, script, &size);
|
|
|
|
if (!data)
|
2012-02-13 05:10:04 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2011-07-09 20:21:16 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
MOZ_ASSERT(size);
|
|
|
|
nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size);
|
|
|
|
js_free(data);
|
2011-07-09 20:21:16 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2012-10-31 09:13:28 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
WriteCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx,
|
|
|
|
nsIPrincipal *systemPrincipal, JSFunction *function)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
/* This doesn't actually work ...
|
|
|
|
uint32_t size;
|
|
|
|
void *data =
|
|
|
|
JS_EncodeInterpretedFunction(cx, JS_GetFunctionObject(function), &size);
|
|
|
|
if (!data)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
MOZ_ASSERT(size);
|
|
|
|
nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size);
|
|
|
|
js_free(data);
|
|
|
|
return rv;*/
|
|
|
|
}
|