| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can | |
| 4 * be found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 // Lookup table types for method dispatching. | |
| 8 | |
| 9 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_METHOD_MAP_H | |
| 10 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_METHOD_MAP_H | |
| 11 | |
| 12 #include <limits.h> | |
| 13 #include <map> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "native_client/src/include/nacl_macros.h" | |
| 17 #include "native_client/src/include/portability_string.h" | |
| 18 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
| 19 | |
| 20 namespace plugin { | |
| 21 | |
| 22 class Plugin; | |
| 23 | |
| 24 bool InitSrpcArgArray(NaClSrpcArg* arr, int size); | |
| 25 void FreeSrpcArg(NaClSrpcArg* arg); | |
| 26 | |
| 27 // A utility class that builds and deletes parameter vectors used in rpcs. | |
| 28 class SrpcParams { | |
| 29 public: | |
| 30 SrpcParams() : exception_string_(NULL) { | |
| 31 memset(ins_, 0, sizeof(ins_)); | |
| 32 memset(outs_, 0, sizeof(outs_)); | |
| 33 } | |
| 34 | |
| 35 SrpcParams(const char* in_types, const char* out_types) | |
| 36 : exception_string_(NULL) { | |
| 37 if (!Init(in_types, out_types)) { | |
| 38 FreeAll(); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 ~SrpcParams() { | |
| 43 FreeAll(); | |
| 44 free(exception_string_); | |
| 45 } | |
| 46 | |
| 47 bool Init(const char* in_types, const char* out_types); | |
| 48 uint32_t SignatureLength() const; | |
| 49 uint32_t InputLength() const; | |
| 50 uint32_t OutputLength() const; | |
| 51 | |
| 52 NaClSrpcArg** ins() const { return const_cast<NaClSrpcArg**>(ins_); } | |
| 53 NaClSrpcArg** outs() const { return const_cast<NaClSrpcArg**>(outs_); } | |
| 54 | |
| 55 char* exception_string() const { return exception_string_; } | |
| 56 void set_exception_string(const char* msg) { | |
| 57 exception_string_ = STRDUP(msg); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 NACL_DISALLOW_COPY_AND_ASSIGN(SrpcParams); | |
| 62 void FreeAll(); | |
| 63 bool FillVec(NaClSrpcArg* vec[], const char* types); | |
| 64 void FreeArguments(NaClSrpcArg* vec[]); | |
| 65 // The ins_ and outs_ arrays contain one more element, to hold a NULL pointer | |
| 66 // to indicate the end of the list. | |
| 67 NaClSrpcArg* ins_[NACL_SRPC_MAX_ARGS + 1]; | |
| 68 NaClSrpcArg* outs_[NACL_SRPC_MAX_ARGS + 1]; | |
| 69 char* exception_string_; | |
| 70 }; | |
| 71 | |
| 72 typedef bool (*RpcFunction)(void* obj, SrpcParams* params); | |
| 73 | |
| 74 // MethodInfo records the method names and type signatures of an SRPC server. | |
| 75 class MethodInfo { | |
| 76 public: | |
| 77 // statically defined method - called through a pointer | |
| 78 MethodInfo(const RpcFunction function_ptr, | |
| 79 const char* name, | |
| 80 const char* ins, | |
| 81 const char* outs, | |
| 82 // index is set to UINT_MAX for methods implemented by the plugin, | |
| 83 // All methods implemented by nacl modules have indexes | |
| 84 // that are lower than UINT_MAX. | |
| 85 const uint32_t index = UINT_MAX) : | |
| 86 function_ptr_(function_ptr), | |
| 87 name_(STRDUP(name)), | |
| 88 ins_(STRDUP(ins)), | |
| 89 outs_(STRDUP(outs)), | |
| 90 index_(index) { } | |
| 91 ~MethodInfo(); | |
| 92 | |
| 93 RpcFunction function_ptr() const { return function_ptr_; } | |
| 94 char* name() const { return name_; } | |
| 95 char* ins() const { return ins_; } | |
| 96 char* outs() const { return outs_; } | |
| 97 uint32_t index() const { return index_; } | |
| 98 | |
| 99 private: | |
| 100 NACL_DISALLOW_COPY_AND_ASSIGN(MethodInfo); | |
| 101 RpcFunction function_ptr_; | |
| 102 char* name_; | |
| 103 char* ins_; | |
| 104 char* outs_; | |
| 105 uint32_t index_; | |
| 106 }; | |
| 107 | |
| 108 class MethodMap { | |
| 109 public: | |
| 110 MethodMap() {} | |
| 111 ~MethodMap(); | |
| 112 MethodInfo* GetMethod(uintptr_t method_id); | |
| 113 void AddMethod(uintptr_t method_id, MethodInfo* info); | |
| 114 | |
| 115 typedef std::vector<uintptr_t> MethodMapKeys; | |
| 116 MethodMapKeys* Keys() { return &method_map_keys_; } | |
| 117 | |
| 118 private: | |
| 119 NACL_DISALLOW_COPY_AND_ASSIGN(MethodMap); | |
| 120 typedef std::map<uintptr_t, MethodInfo*> MethodMapStorage; | |
| 121 MethodMapStorage method_map_; | |
| 122 MethodMapKeys method_map_keys_; | |
| 123 }; | |
| 124 | |
| 125 } // namespace plugin | |
| 126 | |
| 127 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_METHOD_MAP_H | |
| OLD | NEW |