1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "iotjs_def.h"
17 #include "iotjs_handlewrap.h"
20 void iotjs_handlewrap_initialize(iotjs_handlewrap_t* handlewrap,
21 const iotjs_jval_t* jobject,
23 JFreeHandlerType jfreehandler) {
24 IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_handlewrap_t, handlewrap);
26 // Increase ref count of Javascript object to guarantee it is alive until the
28 iotjs_jval_t jobjectref = iotjs_jval_create_copied(jobject);
29 iotjs_jobjectwrap_initialize(&_this->jobjectwrap, &jobjectref, jfreehandler);
31 _this->handle = handle;
32 _this->on_close_cb = NULL;
34 handle->data = handlewrap;
36 iotjs_handlewrap_validate(handlewrap);
40 void iotjs_handlewrap_destroy(iotjs_handlewrap_t* handlewrap) {
41 IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_handlewrap_t, handlewrap);
43 // Handle should have been release before this.
44 IOTJS_ASSERT(_this->handle == NULL);
46 iotjs_jobjectwrap_destroy(&_this->jobjectwrap);
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);
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);
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);
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);
79 static void iotjs_handlewrap_on_close(iotjs_handlewrap_t* handlewrap) {
80 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
83 // Calls registered close handler function.
84 if (_this->on_close_cb) {
85 _this->on_close_cb(_this->handle);
91 // Decrease ref count of Javascript object. From now the object can be
93 iotjs_jval_destroy(iotjs_jobjectwrap_jobject(&_this->jobjectwrap));
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);
103 void iotjs_handlewrap_close(iotjs_handlewrap_t* handlewrap,
104 OnCloseHandler on_close_cb) {
105 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
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);
111 DDLOG("Attempt to close uninitialized or already closed handle");
116 void iotjs_handlewrap_validate(iotjs_handlewrap_t* handlewrap) {
117 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_handlewrap_t, handlewrap);
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)));