Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / ext / call_credentials.h
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #ifndef GRPC_NODE_CALL_CREDENTIALS_H_
20 #define GRPC_NODE_CALL_CREDENTIALS_H_
21
22 #include <queue>
23
24 #include <nan.h>
25 #include <node.h>
26 #include <uv.h>
27 #include "grpc/grpc_security.h"
28
29 namespace grpc {
30 namespace node {
31
32 class CallCredentials : public Nan::ObjectWrap {
33  public:
34   static void Init(v8::Local<v8::Object> exports);
35   static bool HasInstance(v8::Local<v8::Value> val);
36   /* Wrap a grpc_call_credentials struct in a javascript object */
37   static v8::Local<v8::Value> WrapStruct(grpc_call_credentials *credentials);
38
39   /* Returns the grpc_call_credentials struct that this object wraps */
40   grpc_call_credentials *GetWrappedCredentials();
41
42  private:
43   explicit CallCredentials(grpc_call_credentials *credentials);
44   ~CallCredentials();
45
46   // Prevent copying
47   CallCredentials(const CallCredentials &);
48   CallCredentials &operator=(const CallCredentials &);
49
50   static NAN_METHOD(New);
51   static NAN_METHOD(CreateSsl);
52   static NAN_METHOD(CreateFromPlugin);
53
54   static NAN_METHOD(Compose);
55   static Nan::Callback *constructor;
56   // Used for typechecking instances of this javascript class
57   static Nan::Persistent<v8::FunctionTemplate> fun_tpl;
58
59   grpc_call_credentials *wrapped_credentials;
60 };
61
62 /* Auth metadata plugin functionality */
63
64 typedef struct plugin_callback_data {
65   plugin_callback_data(const char *service_url_,
66                        grpc_credentials_plugin_metadata_cb cb_,
67                        void *user_data_)
68       : service_url(service_url_),
69         cb(cb_),
70         user_data(user_data_),
71         async_resource(NULL) {
72     Nan::HandleScope scope;
73     async_resource = new Nan::AsyncResource("grpc:plugin_callback_data");
74   }
75   ~plugin_callback_data() {
76     delete async_resource;
77   }
78
79   const char *service_url;
80   grpc_credentials_plugin_metadata_cb cb;
81   void *user_data;
82   Nan::AsyncResource *async_resource;
83 } plugin_callback_data;
84
85 typedef struct plugin_state {
86   Nan::Callback *callback;
87   std::queue<plugin_callback_data *> *pending_callbacks;
88   uv_mutex_t plugin_mutex;
89   // async.data == this
90   uv_async_t plugin_async;
91 } plugin_state;
92
93 int plugin_get_metadata(
94     void *state, grpc_auth_metadata_context context,
95     grpc_credentials_plugin_metadata_cb cb,
96     void *user_data,
97     grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
98     size_t *num_creds_md, grpc_status_code *status,
99     const char **error_details);
100
101 void plugin_destroy_state(void *state);
102
103 NAN_METHOD(PluginCallback);
104
105 NAUV_WORK_CB(SendPluginCallback);
106
107 }  // namespace node
108 }  // namepsace grpc
109
110 #endif  // GRPC_NODE_CALL_CREDENTIALS_H_