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