/src/mozilla-central/js/xpconnect/loader/mozJSLoaderUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | |
8 | | #include "mozilla/scache/StartupCache.h" |
9 | | |
10 | | #include "jsapi.h" |
11 | | #include "jsfriendapi.h" |
12 | | |
13 | | #include "nsJSPrincipals.h" |
14 | | |
15 | | using namespace JS; |
16 | | using namespace mozilla::scache; |
17 | | using mozilla::UniquePtr; |
18 | | |
19 | | // We only serialize scripts with system principals. So we don't serialize the |
20 | | // principals when writing a script. Instead, when reading it back, we set the |
21 | | // principals to the system principals. |
22 | | nsresult |
23 | | ReadCachedScript(StartupCache* cache, nsACString& uri, JSContext* cx, |
24 | | MutableHandleScript scriptp) |
25 | 5 | { |
26 | 5 | UniquePtr<char[]> buf; |
27 | 5 | uint32_t len; |
28 | 5 | nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), &buf, &len); |
29 | 5 | if (NS_FAILED(rv)) { |
30 | 5 | return rv; // don't warn since NOT_AVAILABLE is an ok error |
31 | 5 | } |
32 | 0 | |
33 | 0 | JS::TranscodeBuffer buffer; |
34 | 0 | buffer.replaceRawBuffer(reinterpret_cast<uint8_t*>(buf.release()), len); |
35 | 0 | JS::TranscodeResult code = JS::DecodeScript(cx, buffer, scriptp); |
36 | 0 | if (code == JS::TranscodeResult_Ok) { |
37 | 0 | return NS_OK; |
38 | 0 | } |
39 | 0 | |
40 | 0 | if ((code & JS::TranscodeResult_Failure) != 0) { |
41 | 0 | return NS_ERROR_FAILURE; |
42 | 0 | } |
43 | 0 | |
44 | 0 | MOZ_ASSERT((code & JS::TranscodeResult_Throw) != 0); |
45 | 0 | JS_ClearPendingException(cx); |
46 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
47 | 0 | } |
48 | | |
49 | | nsresult |
50 | | WriteCachedScript(StartupCache* cache, nsACString& uri, JSContext* cx, |
51 | | HandleScript script) |
52 | 5 | { |
53 | 5 | MOZ_ASSERT(nsJSPrincipals::get(JS_GetScriptPrincipals(script))->GetIsSystemPrincipal()); |
54 | 5 | |
55 | 5 | JS::TranscodeBuffer buffer; |
56 | 5 | JS::TranscodeResult code = JS::EncodeScript(cx, buffer, script); |
57 | 5 | if (code != JS::TranscodeResult_Ok) { |
58 | 0 | if ((code & JS::TranscodeResult_Failure) != 0) { |
59 | 0 | return NS_ERROR_FAILURE; |
60 | 0 | } |
61 | 0 | MOZ_ASSERT((code & JS::TranscodeResult_Throw) != 0); |
62 | 0 | JS_ClearPendingException(cx); |
63 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
64 | 0 | } |
65 | 5 | |
66 | 5 | size_t size = buffer.length(); |
67 | 5 | if (size > UINT32_MAX) { |
68 | 0 | return NS_ERROR_FAILURE; |
69 | 0 | } |
70 | 5 | |
71 | 5 | // Move the vector buffer into a unique pointer buffer. |
72 | 5 | UniquePtr<char[]> buf(reinterpret_cast<char*>(buffer.extractOrCopyRawBuffer())); |
73 | 5 | nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), |
74 | 5 | std::move(buf), |
75 | 5 | size); |
76 | 5 | return rv; |
77 | 5 | } |