1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 /* An implementaion of nsIException. */
8 
9 #include "xpcprivate.h"
10 #include "nsError.h"
11 
12 /***************************************************************************/
13 /* Quick and dirty mapping of well known result codes to strings. We only
14  *  call this when building an exception object, so iterating the short array
15  *  is not too bad.
16  *
17  *  It sure would be nice to have exceptions declared in idl and available
18  *  in some more global way at runtime.
19  */
20 
21 static const struct ResultMap {
22   nsresult rv;
23   const char* name;
24   const char* format;
25 } map[] = {
26 #define XPC_MSG_DEF(val, format) {(val), #val, format},
27 #include "xpc.msg"
28 #undef XPC_MSG_DEF
29     {NS_OK, 0, 0}  // sentinel to mark end of array
30 };
31 
32 #define RESULT_COUNT ((sizeof(map) / sizeof(map[0])) - 1)
33 
34 // static
NameAndFormatForNSResult(nsresult rv,const char ** name,const char ** format)35 bool nsXPCException::NameAndFormatForNSResult(nsresult rv, const char** name,
36                                               const char** format) {
37   for (const ResultMap* p = map; p->name; p++) {
38     if (rv == p->rv) {
39       if (name) *name = p->name;
40       if (format) *format = p->format;
41       return true;
42     }
43   }
44   return false;
45 }
46 
47 // static
IterateNSResults(nsresult * rv,const char ** name,const char ** format,const void ** iterp)48 const void* nsXPCException::IterateNSResults(nsresult* rv, const char** name,
49                                              const char** format,
50                                              const void** iterp) {
51   const ResultMap* p = (const ResultMap*)*iterp;
52   if (!p) {
53     p = map;
54   } else {
55     p++;
56   }
57   if (!p->name) {
58     p = nullptr;
59   } else {
60     if (rv) {
61       *rv = p->rv;
62     }
63     if (name) {
64       *name = p->name;
65     }
66     if (format) {
67       *format = p->format;
68     }
69   }
70   *iterp = p;
71   return p;
72 }
73 
74 // static
GetNSResultCount()75 uint32_t nsXPCException::GetNSResultCount() { return RESULT_COUNT; }
76