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_datashare_observer.h"
17
18 #include <memory>
19 #include <chrono>
20 #include <cinttypes>
21 #include "datashare_log.h"
22 #include "napi_datashare_values_bucket.h"
23
24 namespace OHOS {
25 namespace DataShare {
26 using namespace std::chrono;
NAPIInnerObserver(napi_env env,napi_value callback)27 NAPIInnerObserver::NAPIInnerObserver(napi_env env, napi_value callback)
28 : env_(env)
29 {
30 napi_create_reference(env, callback, 1, &ref_);
31 napi_get_uv_event_loop(env, &loop_);
32 }
33
OnComplete(uv_work_t * work,int status)34 void NAPIInnerObserver::OnComplete(uv_work_t *work, int status)
35 {
36 LOG_DEBUG("uv_queue_work start");
37 std::shared_ptr<ObserverWorker> innerWorker(reinterpret_cast<ObserverWorker *>(work->data));
38 auto observer = innerWorker->observer_.lock();
39 if (observer == nullptr || observer->ref_ == nullptr) {
40 delete work;
41 LOG_ERROR("innerWorker->observer_->ref_ is nullptr");
42 return;
43 }
44 napi_handle_scope scope = nullptr;
45 napi_open_handle_scope(observer->env_, &scope);
46 if (scope == nullptr) {
47 delete work;
48 return;
49 }
50 napi_value callback = nullptr;
51 napi_value args[2] = {0};
52 napi_value global = nullptr;
53 napi_get_reference_value(observer->env_, observer->ref_, &callback);
54 napi_get_global(observer->env_, &global);
55 napi_get_undefined(observer->env_, &args[0]);
56 if (innerWorker->isNotifyDetails_) {
57 args[1] = DataShareJSUtils::Convert2JSValue(observer->env_, innerWorker->result_);
58 }
59 napi_status callStatus = napi_call_function(observer->env_, global, callback, 2, args, nullptr);
60 napi_close_handle_scope(observer->env_, scope);
61 if (callStatus != napi_ok) {
62 LOG_ERROR("napi_call_function failed status : %{public}d", callStatus);
63 }
64 delete work;
65 }
66
OnChange(const DataShareObserver::ChangeInfo & changeInfo,bool isNotifyDetails)67 void NAPIInnerObserver::OnChange(const DataShareObserver::ChangeInfo& changeInfo, bool isNotifyDetails)
68 {
69 auto time =
70 static_cast<uint64_t>(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());
71 LOG_INFO("NAPIInnerObserver datashare callback start, times %{public}" PRIu64 ".", time);
72 if (ref_ == nullptr) {
73 LOG_ERROR("ref_ is nullptr");
74 return;
75 }
76
77 uv_work_t* work = new (std::nothrow) uv_work_t();
78 if (work == nullptr) {
79 LOG_ERROR("Failed to create uv work");
80 return;
81 }
82
83 ObserverWorker* observerWorker = nullptr;
84 observerWorker = new (std::nothrow) ObserverWorker(shared_from_this(), changeInfo);
85 if (observerWorker == nullptr) {
86 delete work;
87 LOG_ERROR("Failed to create observerWorker");
88 return;
89 }
90 observerWorker->isNotifyDetails_ = isNotifyDetails;
91 work->data = observerWorker;
92 int ret = uv_queue_work_with_qos(loop_, work, [](uv_work_t *work) {},
93 NAPIInnerObserver::OnComplete, uv_qos_user_initiated);
94 if (ret != 0) {
95 LOG_ERROR("uv_queue_work failed");
96 delete observerWorker;
97 delete work;
98 }
99 }
100
DeleteReference()101 void NAPIInnerObserver::DeleteReference()
102 {
103 if (ref_ != nullptr) {
104 napi_delete_reference(env_, ref_);
105 ref_ = nullptr;
106 }
107 }
108
GetCallback()109 napi_ref NAPIInnerObserver::GetCallback()
110 {
111 return ref_;
112 }
113 } // namespace DataShare
114 } // namespace OHOS