Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsAutoPtr.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "jsapi.h" |
michael@0 | 10 | #include "js/OldDebugAPI.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsJSPrincipals.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "mozilla/scache/StartupCache.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace JS; |
michael@0 | 17 | using namespace mozilla::scache; |
michael@0 | 18 | |
michael@0 | 19 | // We only serialize scripts with system principals. So we don't serialize the |
michael@0 | 20 | // principals when writing a script. Instead, when reading it back, we set the |
michael@0 | 21 | // principals to the system principals. |
michael@0 | 22 | nsresult |
michael@0 | 23 | ReadCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, |
michael@0 | 24 | nsIPrincipal *systemPrincipal, MutableHandleScript scriptp) |
michael@0 | 25 | { |
michael@0 | 26 | nsAutoArrayPtr<char> buf; |
michael@0 | 27 | uint32_t len; |
michael@0 | 28 | nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), |
michael@0 | 29 | getter_Transfers(buf), &len); |
michael@0 | 30 | if (NS_FAILED(rv)) |
michael@0 | 31 | return rv; // don't warn since NOT_AVAILABLE is an ok error |
michael@0 | 32 | |
michael@0 | 33 | scriptp.set(JS_DecodeScript(cx, buf, len, nullptr)); |
michael@0 | 34 | if (!scriptp) |
michael@0 | 35 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 36 | return NS_OK; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | nsresult |
michael@0 | 40 | ReadCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, |
michael@0 | 41 | nsIPrincipal *systemPrincipal, JSFunction **functionp) |
michael@0 | 42 | { |
michael@0 | 43 | return NS_ERROR_FAILURE; |
michael@0 | 44 | /* This doesn't actually work ... |
michael@0 | 45 | nsAutoArrayPtr<char> buf; |
michael@0 | 46 | uint32_t len; |
michael@0 | 47 | nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), |
michael@0 | 48 | getter_Transfers(buf), &len); |
michael@0 | 49 | if (NS_FAILED(rv)) |
michael@0 | 50 | return rv; // don't warn since NOT_AVAILABLE is an ok error |
michael@0 | 51 | |
michael@0 | 52 | JSObject *obj = JS_DecodeInterpretedFunction(cx, buf, len, nsJSPrincipals::get(systemPrincipal), nullptr); |
michael@0 | 53 | if (!obj) |
michael@0 | 54 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 55 | JSFunction* function = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(obj)); |
michael@0 | 56 | *functionp = function; |
michael@0 | 57 | return NS_OK;*/ |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | nsresult |
michael@0 | 61 | WriteCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, |
michael@0 | 62 | nsIPrincipal *systemPrincipal, HandleScript script) |
michael@0 | 63 | { |
michael@0 | 64 | MOZ_ASSERT(JS_GetScriptPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); |
michael@0 | 65 | MOZ_ASSERT(JS_GetScriptOriginPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); |
michael@0 | 66 | |
michael@0 | 67 | uint32_t size; |
michael@0 | 68 | void *data = JS_EncodeScript(cx, script, &size); |
michael@0 | 69 | if (!data) |
michael@0 | 70 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 71 | |
michael@0 | 72 | MOZ_ASSERT(size); |
michael@0 | 73 | nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size); |
michael@0 | 74 | js_free(data); |
michael@0 | 75 | return rv; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | nsresult |
michael@0 | 79 | WriteCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, |
michael@0 | 80 | nsIPrincipal *systemPrincipal, JSFunction *function) |
michael@0 | 81 | { |
michael@0 | 82 | return NS_ERROR_FAILURE; |
michael@0 | 83 | /* This doesn't actually work ... |
michael@0 | 84 | uint32_t size; |
michael@0 | 85 | void *data = |
michael@0 | 86 | JS_EncodeInterpretedFunction(cx, JS_GetFunctionObject(function), &size); |
michael@0 | 87 | if (!data) |
michael@0 | 88 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 89 | |
michael@0 | 90 | MOZ_ASSERT(size); |
michael@0 | 91 | nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size); |
michael@0 | 92 | js_free(data); |
michael@0 | 93 | return rv;*/ |
michael@0 | 94 | } |