1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 "napi_observer.h"
17 
18 #include "datashare_js_utils.h"
19 #include "datashare_log.h"
20 
21 namespace OHOS {
22 namespace DataShare {
NapiObserver(napi_env env,napi_value callback)23 NapiObserver::NapiObserver(napi_env env, napi_value callback) : env_(env)
24 {
25     napi_create_reference(env, callback, 1, &ref_);
26     napi_get_uv_event_loop(env, &loop_);
27 }
28 
CallbackFunc(uv_work_t * work,int status)29 void NapiObserver::CallbackFunc(uv_work_t *work, int status)
30 {
31     LOG_DEBUG("RdbObsCallbackFunc start");
32     std::shared_ptr<ObserverWorker> innerWorker(reinterpret_cast<ObserverWorker *>(work->data));
33     std::shared_ptr<NapiObserver> observer = innerWorker->observer_.lock();
34     if (observer == nullptr || observer->ref_ == nullptr) {
35         delete work;
36         LOG_ERROR("rdbObserver->ref_ is nullptr");
37         return;
38     }
39     napi_handle_scope scope = nullptr;
40     napi_open_handle_scope(observer->env_, &scope);
41     if (scope == nullptr) {
42         delete work;
43         return;
44     }
45     napi_value callback = nullptr;
46     napi_value param[2];
47     napi_value global = nullptr;
48     napi_value result;
49     napi_get_reference_value(observer->env_, observer->ref_, &callback);
50     napi_get_global(observer->env_, &global);
51     napi_get_undefined(observer->env_, &param[0]);
52     param[1] = innerWorker->getParam(observer->env_);
53     napi_status callStatus = napi_call_function(observer->env_, global, callback, 2, param, &result);
54     napi_close_handle_scope(observer->env_, scope);
55     if (callStatus != napi_ok) {
56         LOG_ERROR("napi_call_function failed status : %{public}d", callStatus);
57     }
58     delete work;
59 }
60 
~NapiObserver()61 NapiObserver::~NapiObserver()
62 {
63     if (ref_ != nullptr) {
64         napi_delete_reference(env_, ref_);
65         ref_ = nullptr;
66     }
67 }
68 
operator ==(const NapiObserver & rhs) const69 bool NapiObserver::operator==(const NapiObserver &rhs) const
70 {
71     if (ref_ == nullptr) {
72         return (rhs.ref_ == nullptr);
73     }
74 
75     napi_value value1 = nullptr;
76     napi_get_reference_value(env_, ref_, &value1);
77 
78     napi_value value2 = nullptr;
79     napi_get_reference_value(env_, rhs.ref_, &value2);
80 
81     bool isEqual = false;
82     napi_strict_equals(env_, value1, value2, &isEqual);
83     return isEqual;
84 }
85 
operator !=(const NapiObserver & rhs) const86 bool NapiObserver::operator!=(const NapiObserver &rhs) const
87 {
88     return !(rhs == *this);
89 }
90 
OnChange(const RdbChangeNode & changeNode)91 void NapiRdbObserver::OnChange(const RdbChangeNode &changeNode)
92 {
93     LOG_DEBUG("NapiRdbObserver onchange Start");
94     if (ref_ == nullptr) {
95         LOG_ERROR("ref_ is nullptr");
96         return;
97     }
98     ObserverWorker *observerWorker = new (std::nothrow) ObserverWorker(shared_from_this());
99     if (observerWorker == nullptr) {
100         LOG_ERROR("Failed to create observerWorker");
101         return;
102     }
103     observerWorker->getParam = [changeNode](napi_env env) {
104         return DataShareJSUtils::Convert2JSValue(env, changeNode);
105     };
106 
107     uv_work_t *work = new (std::nothrow) uv_work_t();
108     if (work == nullptr) {
109         delete observerWorker;
110         LOG_ERROR("Failed to create uv work");
111         return;
112     }
113     work->data = observerWorker;
114     int ret = uv_queue_work(
115         loop_, work, [](uv_work_t *work) {}, CallbackFunc);
116     if (ret != 0) {
117         LOG_ERROR("uv_queue_work failed");
118         delete observerWorker;
119         delete work;
120     }
121 }
122 
OnChange(PublishedDataChangeNode & changeNode)123 void NapiPublishedObserver::OnChange(PublishedDataChangeNode &changeNode)
124 {
125     LOG_DEBUG("NapiPublishedObserver onchange Start");
126     if (ref_ == nullptr) {
127         LOG_ERROR("ref_ is nullptr");
128         return;
129     }
130     ObserverWorker *observerWorker = new (std::nothrow) ObserverWorker(shared_from_this());
131     if (observerWorker == nullptr) {
132         LOG_ERROR("Failed to create observerWorker");
133         return;
134     }
135     std::shared_ptr<PublishedDataChangeNode> node = std::make_shared<PublishedDataChangeNode>(std::move(changeNode));
136     observerWorker->getParam = [node](napi_env env) {
137         return DataShareJSUtils::Convert2JSValue(env, *node);
138     };
139 
140     uv_work_t *work = new (std::nothrow) uv_work_t();
141     if (work == nullptr) {
142         delete observerWorker;
143         LOG_ERROR("Failed to create uv work");
144         return;
145     }
146     work->data = observerWorker;
147     int ret = uv_queue_work(
148         loop_, work, [](uv_work_t *work) {}, CallbackFunc);
149     if (ret != 0) {
150         LOG_ERROR("uv_queue_work failed");
151         delete observerWorker;
152         delete work;
153     }
154 }
155 } // namespace DataShare
156 } // namespace OHOS
157