js/xpconnect/src/XPCThrower.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 /* Code for throwing errors into JavaScript. */
michael@0 8
michael@0 9 #include "xpcprivate.h"
michael@0 10 #include "XPCWrapper.h"
michael@0 11 #include "jsprf.h"
michael@0 12 #include "mozilla/dom/BindingUtils.h"
michael@0 13 #include "mozilla/dom/Exceptions.h"
michael@0 14
michael@0 15 using namespace mozilla;
michael@0 16 using namespace mozilla::dom;
michael@0 17
michael@0 18 bool XPCThrower::sVerbose = true;
michael@0 19
michael@0 20 // static
michael@0 21 void
michael@0 22 XPCThrower::Throw(nsresult rv, JSContext* cx)
michael@0 23 {
michael@0 24 const char* format;
michael@0 25 if (JS_IsExceptionPending(cx))
michael@0 26 return;
michael@0 27 if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
michael@0 28 format = "";
michael@0 29 dom::Throw(cx, rv, format);
michael@0 30 }
michael@0 31
michael@0 32 namespace xpc {
michael@0 33
michael@0 34 bool
michael@0 35 Throw(JSContext *cx, nsresult rv)
michael@0 36 {
michael@0 37 XPCThrower::Throw(rv, cx);
michael@0 38 return false;
michael@0 39 }
michael@0 40
michael@0 41 } // namespace xpc
michael@0 42
michael@0 43 /*
michael@0 44 * If there has already been an exception thrown, see if we're throwing the
michael@0 45 * same sort of exception, and if we are, don't clobber the old one. ccx
michael@0 46 * should be the current call context.
michael@0 47 */
michael@0 48 // static
michael@0 49 bool
michael@0 50 XPCThrower::CheckForPendingException(nsresult result, JSContext *cx)
michael@0 51 {
michael@0 52 nsCOMPtr<nsIException> e = XPCJSRuntime::Get()->GetPendingException();
michael@0 53 if (!e)
michael@0 54 return false;
michael@0 55 XPCJSRuntime::Get()->SetPendingException(nullptr);
michael@0 56
michael@0 57 nsresult e_result;
michael@0 58 if (NS_FAILED(e->GetResult(&e_result)) || e_result != result)
michael@0 59 return false;
michael@0 60
michael@0 61 if (!ThrowExceptionObject(cx, e))
michael@0 62 JS_ReportOutOfMemory(cx);
michael@0 63 return true;
michael@0 64 }
michael@0 65
michael@0 66 // static
michael@0 67 void
michael@0 68 XPCThrower::Throw(nsresult rv, XPCCallContext& ccx)
michael@0 69 {
michael@0 70 char* sz;
michael@0 71 const char* format;
michael@0 72
michael@0 73 if (CheckForPendingException(rv, ccx))
michael@0 74 return;
michael@0 75
michael@0 76 if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
michael@0 77 format = "";
michael@0 78
michael@0 79 sz = (char*) format;
michael@0 80
michael@0 81 if (sz && sVerbose)
michael@0 82 Verbosify(ccx, &sz, false);
michael@0 83
michael@0 84 dom::Throw(ccx, rv, sz);
michael@0 85
michael@0 86 if (sz && sz != format)
michael@0 87 JS_smprintf_free(sz);
michael@0 88 }
michael@0 89
michael@0 90
michael@0 91 // static
michael@0 92 void
michael@0 93 XPCThrower::ThrowBadResult(nsresult rv, nsresult result, XPCCallContext& ccx)
michael@0 94 {
michael@0 95 char* sz;
michael@0 96 const char* format;
michael@0 97 const char* name;
michael@0 98
michael@0 99 /*
michael@0 100 * If there is a pending exception when the native call returns and
michael@0 101 * it has the same error result as returned by the native call, then
michael@0 102 * the native call may be passing through an error from a previous JS
michael@0 103 * call. So we'll just throw that exception into our JS.
michael@0 104 */
michael@0 105
michael@0 106 if (CheckForPendingException(result, ccx))
michael@0 107 return;
michael@0 108
michael@0 109 // else...
michael@0 110
michael@0 111 if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format) || !format)
michael@0 112 format = "";
michael@0 113
michael@0 114 if (nsXPCException::NameAndFormatForNSResult(result, &name, nullptr) && name)
michael@0 115 sz = JS_smprintf("%s 0x%x (%s)", format, result, name);
michael@0 116 else
michael@0 117 sz = JS_smprintf("%s 0x%x", format, result);
michael@0 118
michael@0 119 if (sz && sVerbose)
michael@0 120 Verbosify(ccx, &sz, true);
michael@0 121
michael@0 122 dom::Throw(ccx, result, sz);
michael@0 123
michael@0 124 if (sz)
michael@0 125 JS_smprintf_free(sz);
michael@0 126 }
michael@0 127
michael@0 128 // static
michael@0 129 void
michael@0 130 XPCThrower::ThrowBadParam(nsresult rv, unsigned paramNum, XPCCallContext& ccx)
michael@0 131 {
michael@0 132 char* sz;
michael@0 133 const char* format;
michael@0 134
michael@0 135 if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
michael@0 136 format = "";
michael@0 137
michael@0 138 sz = JS_smprintf("%s arg %d", format, paramNum);
michael@0 139
michael@0 140 if (sz && sVerbose)
michael@0 141 Verbosify(ccx, &sz, true);
michael@0 142
michael@0 143 dom::Throw(ccx, rv, sz);
michael@0 144
michael@0 145 if (sz)
michael@0 146 JS_smprintf_free(sz);
michael@0 147 }
michael@0 148
michael@0 149
michael@0 150 // static
michael@0 151 void
michael@0 152 XPCThrower::Verbosify(XPCCallContext& ccx,
michael@0 153 char** psz, bool own)
michael@0 154 {
michael@0 155 char* sz = nullptr;
michael@0 156
michael@0 157 if (ccx.HasInterfaceAndMember()) {
michael@0 158 XPCNativeInterface* iface = ccx.GetInterface();
michael@0 159 jsid id = ccx.GetMember()->GetName();
michael@0 160 JSAutoByteString bytes;
michael@0 161 const char *name = JSID_IS_VOID(id) ? "Unknown" : bytes.encodeLatin1(ccx, JSID_TO_STRING(id));
michael@0 162 if (!name) {
michael@0 163 name = "";
michael@0 164 }
michael@0 165 sz = JS_smprintf("%s [%s.%s]", *psz, iface->GetNameString(), name);
michael@0 166 }
michael@0 167
michael@0 168 if (sz) {
michael@0 169 if (own)
michael@0 170 JS_smprintf_free(*psz);
michael@0 171 *psz = sz;
michael@0 172 }
michael@0 173 }

mercurial