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 #include "napi_datashare_observer.h"
16
17 #include <memory>
18 #include "datashare_log.h"
19
20 namespace OHOS {
21 namespace DataShare {
NAPIInnerObserver(napi_env env,napi_value callback)22 NAPIInnerObserver::NAPIInnerObserver(napi_env env, napi_value callback)
23 : env_(env)
24 {
25 napi_create_reference(env, callback, 1, &ref_);
26 napi_get_uv_event_loop(env, &loop_);
27 }
28
OnChange()29 void NAPIInnerObserver::OnChange()
30 {
31 LOG_DEBUG("Start");
32 if (ref_ == nullptr) {
33 LOG_ERROR("ref_ is nullptr");
34 return;
35 }
36 ObserverWorker *observerWorker = new (std::nothrow)ObserverWorker(shared_from_this());
37 if (observerWorker == nullptr) {
38 LOG_ERROR("Failed to create observerWorker");
39 return;
40 }
41 uv_work_t *work = new (std::nothrow)uv_work_t();
42 if (work == nullptr) {
43 delete observerWorker;
44 LOG_ERROR("Failed to create uv work");
45 return;
46 }
47 work->data = observerWorker;
48 int ret = uv_queue_work(loop_, work,
49 [](uv_work_t *work) {},
50 [](uv_work_t *work, int status) {
51 LOG_DEBUG("uv_queue_work start");
52 std::shared_ptr<ObserverWorker> innerWorker(reinterpret_cast<ObserverWorker *>(work->data));
53 auto observer = innerWorker->observer_.lock();
54 if (observer == nullptr || observer->ref_ == nullptr) {
55 delete work;
56 LOG_ERROR("innerWorker->observer_->ref_ is nullptr");
57 return;
58 }
59 napi_handle_scope scope = nullptr;
60 napi_open_handle_scope(observer->env_, &scope);
61 if (scope == nullptr) {
62 delete work;
63 return;
64 }
65 napi_value callback = nullptr;
66 napi_value args[2] = {0};
67 napi_value global = nullptr;
68 napi_value result;
69 napi_get_reference_value(observer->env_, observer->ref_, &callback);
70 napi_get_global(observer->env_, &global);
71 napi_status callStatus = napi_call_function(observer->env_, global, callback, 2, args, &result);
72 napi_close_handle_scope(observer->env_, scope);
73 if (callStatus != napi_ok) {
74 LOG_ERROR("napi_call_function failed status : %{public}d", callStatus);
75 }
76 delete work;
77 });
78 if (ret != 0) {
79 LOG_ERROR("uv_queue_work failed");
80 delete observerWorker;
81 delete work;
82 }
83 }
84
DeleteReference()85 void NAPIInnerObserver::DeleteReference()
86 {
87 if (ref_ != nullptr) {
88 napi_delete_reference(env_, ref_);
89 ref_ = nullptr;
90 }
91 }
92
GetCallback()93 napi_ref NAPIInnerObserver::GetCallback()
94 {
95 return ref_;
96 }
97 } // namespace DataShare
98 } // namespace OHOS
99