Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / iotjs_handlewrap.c
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "iotjs_def.h"
17 #include "iotjs_handlewrap.h"
18
19
20 void iotjs_handlewrap_initialize(iotjs_handlewrap_t* handlewrap,
21                                  const iotjs_jval_t* jobject,
22                                  uv_handle_t* handle,
23                                  JFreeHandlerType jfreehandler) {
24   IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_handlewrap_t, handlewrap);
25
26   // Increase ref count of Javascript object to guarantee it is alive until the
27   // handle has closed.
28   iotjs_jval_t jobjectref = iotjs_jval_create_copied(jobject);
29   iotjs_jobjectwrap_initialize(&_this->jobjectwrap, &jobjectref, jfreehandler);
30
31   _this->handle = handle;
32   _this->on_close_cb = NULL;
33
34   handle->data = handlewrap;
35
36   iotjs_handlewrap_validate(handlewrap);
37 }
38
39
40 void iotjs_handlewrap_destroy(iotjs_handlewrap_t* handlewrap) {
41   IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_handlewrap_t, handlewrap);
42
43   // Handle should have been release before this.
44   IOTJS_ASSERT(_this->handle == NULL);
45
46   iotjs_jobjectwrap_destroy(&_this->jobjectwrap);
47 }
48
49
50 iotjs_handlewrap_t* iotjs_handlewrap_from_handle(uv_handle_t* handle) {
51   iotjs_handlewrap_t* handlewrap = (iotjs_handlewrap_t*)(handle->data);
52   iotjs_handlewrap_validate(handlewrap);
53   return handlewrap;
54 }
55
56
57 iotjs_handlewrap_t* iotjs_handlewrap_from_jobject(const iotjs_jval_t* jobject) {
58   iotjs_handlewrap_t* handlewrap =
59       (iotjs_handlewrap_t*)(iotjs_jval_get_object_native_handle(jobject));
60   iotjs_handlewrap_validate(handlewrap);
61   return handlewrap;
62 }
63
64
65 uv_handle_t* iotjs_handlewrap_get_uv_handle(iotjs_handlewrap_t* handlewrap) {
66   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
67   iotjs_handlewrap_validate(handlewrap);
68   return _this->handle;
69 }
70
71
72 iotjs_jval_t* iotjs_handlewrap_jobject(iotjs_handlewrap_t* handlewrap) {
73   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
74   iotjs_handlewrap_validate(handlewrap);
75   return iotjs_jobjectwrap_jobject(&_this->jobjectwrap);
76 }
77
78
79 static void iotjs_handlewrap_on_close(iotjs_handlewrap_t* handlewrap) {
80   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
81
82   // The handle closed.
83   // Calls registered close handler function.
84   if (_this->on_close_cb) {
85     _this->on_close_cb(_this->handle);
86   }
87
88   // Set handle null.
89   _this->handle = NULL;
90
91   // Decrease ref count of Javascript object. From now the object can be
92   // reclaimed.
93   iotjs_jval_destroy(iotjs_jobjectwrap_jobject(&_this->jobjectwrap));
94 }
95
96
97 static void iotjs_on_handle_closed(uv_handle_t* handle) {
98   iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_handle(handle);
99   iotjs_handlewrap_on_close(handlewrap);
100 }
101
102
103 void iotjs_handlewrap_close(iotjs_handlewrap_t* handlewrap,
104                             OnCloseHandler on_close_cb) {
105   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
106
107   if (_this->handle != NULL && !uv_is_closing(_this->handle)) {
108     _this->on_close_cb = on_close_cb;
109     uv_close(_this->handle, iotjs_on_handle_closed);
110   } else {
111     DDLOG("Attempt to close uninitialized or already closed handle");
112   }
113 }
114
115
116 void iotjs_handlewrap_validate(iotjs_handlewrap_t* handlewrap) {
117   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
118
119   IOTJS_ASSERT((iotjs_handlewrap_t*)_this == handlewrap);
120   IOTJS_ASSERT((iotjs_jobjectwrap_t*)_this == &_this->jobjectwrap);
121   IOTJS_ASSERT((void*)_this == _this->handle->data);
122   IOTJS_ASSERT((uintptr_t)_this ==
123                iotjs_jval_get_object_native_handle(
124                    iotjs_jobjectwrap_jobject(&_this->jobjectwrap)));
125 }