OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <node.h> |
| 35 #include <nan.h> |
| 36 #include <uv.h> |
| 37 |
| 38 #include "grpc/grpc.h" |
| 39 #include "grpc/grpc_security.h" |
| 40 #include "grpc/support/log.h" |
| 41 #include "call_credentials.h" |
| 42 #include "call.h" |
| 43 |
| 44 namespace grpc { |
| 45 namespace node { |
| 46 |
| 47 using Nan::Callback; |
| 48 using Nan::EscapableHandleScope; |
| 49 using Nan::HandleScope; |
| 50 using Nan::Maybe; |
| 51 using Nan::MaybeLocal; |
| 52 using Nan::ObjectWrap; |
| 53 using Nan::Persistent; |
| 54 using Nan::Utf8String; |
| 55 |
| 56 using v8::Exception; |
| 57 using v8::External; |
| 58 using v8::Function; |
| 59 using v8::FunctionTemplate; |
| 60 using v8::Integer; |
| 61 using v8::Local; |
| 62 using v8::Object; |
| 63 using v8::ObjectTemplate; |
| 64 using v8::Value; |
| 65 |
| 66 Nan::Callback *CallCredentials::constructor; |
| 67 Persistent<FunctionTemplate> CallCredentials::fun_tpl; |
| 68 |
| 69 CallCredentials::CallCredentials(grpc_call_credentials *credentials) |
| 70 : wrapped_credentials(credentials) {} |
| 71 |
| 72 CallCredentials::~CallCredentials() { |
| 73 grpc_call_credentials_release(wrapped_credentials); |
| 74 } |
| 75 |
| 76 void CallCredentials::Init(Local<Object> exports) { |
| 77 HandleScope scope; |
| 78 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
| 79 tpl->SetClassName(Nan::New("CallCredentials").ToLocalChecked()); |
| 80 tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 81 Nan::SetPrototypeMethod(tpl, "compose", Compose); |
| 82 fun_tpl.Reset(tpl); |
| 83 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); |
| 84 Nan::Set(ctr, Nan::New("createFromPlugin").ToLocalChecked(), |
| 85 Nan::GetFunction( |
| 86 Nan::New<FunctionTemplate>(CreateFromPlugin)).ToLocalChecked()); |
| 87 Nan::Set(exports, Nan::New("CallCredentials").ToLocalChecked(), ctr); |
| 88 constructor = new Nan::Callback(ctr); |
| 89 } |
| 90 |
| 91 bool CallCredentials::HasInstance(Local<Value> val) { |
| 92 HandleScope scope; |
| 93 return Nan::New(fun_tpl)->HasInstance(val); |
| 94 } |
| 95 |
| 96 Local<Value> CallCredentials::WrapStruct(grpc_call_credentials *credentials) { |
| 97 EscapableHandleScope scope; |
| 98 const int argc = 1; |
| 99 if (credentials == NULL) { |
| 100 return scope.Escape(Nan::Null()); |
| 101 } |
| 102 Local<Value> argv[argc] = { |
| 103 Nan::New<External>(reinterpret_cast<void *>(credentials))}; |
| 104 MaybeLocal<Object> maybe_instance = Nan::NewInstance( |
| 105 constructor->GetFunction(), argc, argv); |
| 106 if (maybe_instance.IsEmpty()) { |
| 107 return scope.Escape(Nan::Null()); |
| 108 } else { |
| 109 return scope.Escape(maybe_instance.ToLocalChecked()); |
| 110 } |
| 111 } |
| 112 |
| 113 grpc_call_credentials *CallCredentials::GetWrappedCredentials() { |
| 114 return wrapped_credentials; |
| 115 } |
| 116 |
| 117 NAN_METHOD(CallCredentials::New) { |
| 118 if (info.IsConstructCall()) { |
| 119 if (!info[0]->IsExternal()) { |
| 120 return Nan::ThrowTypeError( |
| 121 "CallCredentials can only be created with the provided functions"); |
| 122 } |
| 123 Local<External> ext = info[0].As<External>(); |
| 124 grpc_call_credentials *creds_value = |
| 125 reinterpret_cast<grpc_call_credentials *>(ext->Value()); |
| 126 CallCredentials *credentials = new CallCredentials(creds_value); |
| 127 credentials->Wrap(info.This()); |
| 128 info.GetReturnValue().Set(info.This()); |
| 129 return; |
| 130 } else { |
| 131 // This should never be called directly |
| 132 return Nan::ThrowTypeError( |
| 133 "CallCredentials can only be created with the provided functions"); |
| 134 } |
| 135 } |
| 136 |
| 137 NAN_METHOD(CallCredentials::Compose) { |
| 138 if (!CallCredentials::HasInstance(info.This())) { |
| 139 return Nan::ThrowTypeError( |
| 140 "compose can only be called on CallCredentials objects"); |
| 141 } |
| 142 if (!CallCredentials::HasInstance(info[0])) { |
| 143 return Nan::ThrowTypeError( |
| 144 "compose's first argument must be a CallCredentials object"); |
| 145 } |
| 146 CallCredentials *self = ObjectWrap::Unwrap<CallCredentials>(info.This()); |
| 147 CallCredentials *other = ObjectWrap::Unwrap<CallCredentials>( |
| 148 Nan::To<Object>(info[0]).ToLocalChecked()); |
| 149 grpc_call_credentials *creds = grpc_composite_call_credentials_create( |
| 150 self->wrapped_credentials, other->wrapped_credentials, NULL); |
| 151 info.GetReturnValue().Set(WrapStruct(creds)); |
| 152 } |
| 153 |
| 154 |
| 155 |
| 156 NAN_METHOD(CallCredentials::CreateFromPlugin) { |
| 157 if (!info[0]->IsFunction()) { |
| 158 return Nan::ThrowTypeError( |
| 159 "createFromPlugin's argument must be a function"); |
| 160 } |
| 161 grpc_metadata_credentials_plugin plugin; |
| 162 plugin_state *state = new plugin_state; |
| 163 state->callback = new Nan::Callback(info[0].As<Function>()); |
| 164 plugin.get_metadata = plugin_get_metadata; |
| 165 plugin.destroy = plugin_destroy_state; |
| 166 plugin.state = reinterpret_cast<void*>(state); |
| 167 plugin.type = ""; |
| 168 grpc_call_credentials *creds = grpc_metadata_credentials_create_from_plugin( |
| 169 plugin, NULL); |
| 170 info.GetReturnValue().Set(WrapStruct(creds)); |
| 171 } |
| 172 |
| 173 NAN_METHOD(PluginCallback) { |
| 174 // Arguments: status code, error details, metadata |
| 175 if (!info[0]->IsUint32()) { |
| 176 return Nan::ThrowTypeError( |
| 177 "The callback's first argument must be a status code"); |
| 178 } |
| 179 if (!info[1]->IsString()) { |
| 180 return Nan::ThrowTypeError( |
| 181 "The callback's second argument must be a string"); |
| 182 } |
| 183 if (!info[2]->IsObject()) { |
| 184 return Nan::ThrowTypeError( |
| 185 "The callback's third argument must be an object"); |
| 186 } |
| 187 shared_ptr<Resources> resources(new Resources); |
| 188 grpc_status_code code = static_cast<grpc_status_code>( |
| 189 Nan::To<uint32_t>(info[0]).FromJust()); |
| 190 Utf8String details_utf8_str(info[1]); |
| 191 char *details = *details_utf8_str; |
| 192 grpc_metadata_array array; |
| 193 if (!CreateMetadataArray(Nan::To<Object>(info[2]).ToLocalChecked(), |
| 194 &array, resources)){ |
| 195 return Nan::ThrowError("Failed to parse metadata"); |
| 196 } |
| 197 grpc_credentials_plugin_metadata_cb cb = |
| 198 reinterpret_cast<grpc_credentials_plugin_metadata_cb>( |
| 199 Nan::Get(info.Callee(), |
| 200 Nan::New("cb").ToLocalChecked() |
| 201 ).ToLocalChecked().As<External>()->Value()); |
| 202 void *user_data = |
| 203 Nan::Get(info.Callee(), |
| 204 Nan::New("user_data").ToLocalChecked() |
| 205 ).ToLocalChecked().As<External>()->Value(); |
| 206 cb(user_data, array.metadata, array.count, code, details); |
| 207 } |
| 208 |
| 209 NAUV_WORK_CB(SendPluginCallback) { |
| 210 Nan::HandleScope scope; |
| 211 plugin_callback_data *data = reinterpret_cast<plugin_callback_data*>( |
| 212 async->data); |
| 213 // Attach cb and user_data to plugin_callback so that it can access them later |
| 214 v8::Local<v8::Function> plugin_callback = Nan::GetFunction( |
| 215 Nan::New<v8::FunctionTemplate>(PluginCallback)).ToLocalChecked(); |
| 216 Nan::Set(plugin_callback, Nan::New("cb").ToLocalChecked(), |
| 217 Nan::New<v8::External>(reinterpret_cast<void*>(data->cb))); |
| 218 Nan::Set(plugin_callback, Nan::New("user_data").ToLocalChecked(), |
| 219 Nan::New<v8::External>(data->user_data)); |
| 220 const int argc = 2; |
| 221 v8::Local<v8::Value> argv[argc] = { |
| 222 Nan::New(data->service_url).ToLocalChecked(), |
| 223 plugin_callback |
| 224 }; |
| 225 Nan::Callback *callback = data->state->callback; |
| 226 callback->Call(argc, argv); |
| 227 delete data; |
| 228 uv_unref((uv_handle_t *)async); |
| 229 uv_close((uv_handle_t *)async, (uv_close_cb)free); |
| 230 } |
| 231 |
| 232 void plugin_get_metadata(void *state, grpc_auth_metadata_context context, |
| 233 grpc_credentials_plugin_metadata_cb cb, |
| 234 void *user_data) { |
| 235 uv_async_t *async = static_cast<uv_async_t*>(malloc(sizeof(uv_async_t))); |
| 236 uv_async_init(uv_default_loop(), |
| 237 async, |
| 238 SendPluginCallback); |
| 239 plugin_callback_data *data = new plugin_callback_data; |
| 240 data->state = reinterpret_cast<plugin_state*>(state); |
| 241 data->service_url = context.service_url; |
| 242 data->cb = cb; |
| 243 data->user_data = user_data; |
| 244 async->data = data; |
| 245 /* libuv says that it will coalesce calls to uv_async_send. If there is ever a |
| 246 * problem with a callback not getting called, that is probably the reason */ |
| 247 uv_async_send(async); |
| 248 } |
| 249 |
| 250 void plugin_destroy_state(void *ptr) { |
| 251 plugin_state *state = reinterpret_cast<plugin_state *>(ptr); |
| 252 delete state->callback; |
| 253 } |
| 254 |
| 255 } // namespace node |
| 256 } // namespace grpc |
OLD | NEW |