• 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_remote_object_holder.h"
17 
18 #include <uv.h>
19 #include <string_ex.h>
20 #include "ipc_debug.h"
21 #include "log_tags.h"
22 #include "native_engine/native_value.h"
23 
24 #if (defined(HIVIEWDFX_BACKTRACE_SUPPORT) && defined(FFRT_SUPPORT))
25 #include <sstream>
26 #include "backtrace_local.h"
27 #include "ffrt.h"
28 #include "ipc_thread_skeleton.h"
29 #include "native_engine/native_engine.h"
30 #endif
31 namespace OHOS {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_NAPI, "napi_remoteObject_holder" };
33 
OnEnvCleanUp(void * data)34 static void OnEnvCleanUp(void *data)
35 {
36     if (data == nullptr) {
37         ZLOGE(LOG_LABEL, "data is null");
38         return;
39     }
40     NAPIRemoteObjectHolder *holder = reinterpret_cast<NAPIRemoteObjectHolder *>(data);
41     // js env has been destrcted, clear saved env info, and check befor use it
42     holder->CleanJsEnv();
43 }
44 
NAPIRemoteObjectHolder(napi_env env,const std::u16string & descriptor,napi_value thisVar)45 NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar)
46     : env_(env), jsThreadId_(std::this_thread::get_id()), descriptor_(descriptor), sptrCachedObject_(nullptr),
47       wptrCachedObject_(nullptr), localInterfaceRef_(nullptr), attachCount_(1), jsObjectRef_(nullptr)
48 {
49     DetectCreatedInIPCThread();
50     // create weak ref, need call napi_delete_reference to release memory,
51     // increase ref count when the JS object will transfer to another thread or process.
52     napi_create_reference(env, thisVar, 0, &jsObjectRef_);
53 
54     // register listener for env destruction
55     napi_status status = napi_add_env_cleanup_hook(env, OnEnvCleanUp, this);
56     if (status != napi_ok) {
57         ZLOGE(LOG_LABEL, "add cleanup hook failed");
58     }
59 }
60 
DeleteJsObjectRefInUvWork()61 void NAPIRemoteObjectHolder::DeleteJsObjectRefInUvWork()
62 {
63     if (env_ == nullptr) {
64         ZLOGE(LOG_LABEL, "js env has been destructed");
65         return;
66     }
67     OperateJsRefParam *param = new (std::nothrow) OperateJsRefParam {
68         .env = env_,
69         .thisVarRef = jsObjectRef_
70     };
71     NAPI_ASSERT_RETURN_VOID(env_, param != nullptr, "new OperateJsRefParam failed");
72 
73     auto task = [param]() {
74         napi_handle_scope scope = nullptr;
75         napi_open_handle_scope(param->env, &scope);
76         napi_status napiStatus = napi_delete_reference(param->env, param->thisVarRef);
77         if (napiStatus != napi_ok) {
78             ZLOGE(LOG_LABEL, "failed to delete ref");
79         }
80         napi_close_handle_scope(param->env, scope);
81         delete param;
82     };
83     napi_status sendRet = napi_send_event(env_, task, napi_eprio_high);
84     if (sendRet != napi_ok) {
85         ZLOGE(LOG_LABEL, "napi_send_event failed, ret:%{public}d", sendRet);
86         delete param;
87     }
88 }
89 
~NAPIRemoteObjectHolder()90 NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
91 {
92     if (env_ == nullptr) {
93         ZLOGE(LOG_LABEL, "js env has been destructed");
94         return;
95     }
96 
97     napi_status status = napi_remove_env_cleanup_hook(env_, OnEnvCleanUp, this);
98     if (status != napi_ok) {
99         ZLOGE(LOG_LABEL, "remove cleanup hook failed");
100     }
101 
102     if (localInterfaceRef_ != nullptr) {
103         status = napi_delete_reference(env_, localInterfaceRef_);
104         if (status != napi_ok) {
105             ZLOGE(LOG_LABEL, "failed to delete ref");
106         }
107     }
108 
109     if (jsObjectRef_ != nullptr) {
110         if (jsThreadId_ == std::this_thread::get_id()) {
111             status = napi_delete_reference(env_, jsObjectRef_);
112             if (status != napi_ok) {
113                 ZLOGE(LOG_LABEL, "failed to delete ref");
114             }
115         } else {
116             DeleteJsObjectRefInUvWork();
117         }
118     }
119 }
120 
Get()121 sptr<IRemoteObject> NAPIRemoteObjectHolder::Get()
122 {
123     std::lock_guard<std::mutex> lockGuard(mutex_);
124     // grab an strong reference to the object,
125     // so it will not be freed util this reference released.
126     if (sptrCachedObject_ != nullptr) {
127         return sptrCachedObject_;
128     }
129 
130     sptr<IRemoteObject> tmp = wptrCachedObject_.promote();
131     if (tmp == nullptr && env_ != nullptr) {
132         tmp = new (std::nothrow) NAPIRemoteObject(jsThreadId_, env_, jsObjectRef_, descriptor_);
133         if (tmp == nullptr) {
134             ZLOGE(LOG_LABEL, "new NAPIRemoteObject failed");
135             return nullptr;
136         }
137         wptrCachedObject_ = tmp;
138     }
139     return tmp;
140 }
141 
Set(sptr<IRemoteObject> object)142 void NAPIRemoteObjectHolder::Set(sptr<IRemoteObject> object)
143 {
144     std::lock_guard<std::mutex> lockGuard(mutex_);
145     IPCObjectStub *tmp = static_cast<IPCObjectStub *>(object.GetRefPtr());
146     if (tmp->GetObjectType() == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT) {
147         wptrCachedObject_ = object;
148     } else {
149         sptrCachedObject_ = object;
150     }
151 }
152 
GetJsObjectRef() const153 napi_ref NAPIRemoteObjectHolder::GetJsObjectRef() const
154 {
155     return jsObjectRef_;
156 }
157 
GetJsObjectEnv() const158 napi_env NAPIRemoteObjectHolder::GetJsObjectEnv() const
159 {
160     return env_;
161 }
162 
CleanJsEnv()163 void NAPIRemoteObjectHolder::CleanJsEnv()
164 {
165     env_ = nullptr;
166     jsObjectRef_ = nullptr;
167     sptr<IRemoteObject> tmp = wptrCachedObject_.promote();
168     if (tmp != nullptr) {
169         NAPIRemoteObject *object = static_cast<NAPIRemoteObject *>(tmp.GetRefPtr());
170         ZLOGI(LOG_LABEL, "reset env and napi_ref");
171         object->ResetJsEnv();
172     }
173 }
174 
attachLocalInterface(napi_value localInterface,std::string & descriptor)175 void NAPIRemoteObjectHolder::attachLocalInterface(napi_value localInterface, std::string &descriptor)
176 {
177     if (env_ == nullptr) {
178         ZLOGE(LOG_LABEL, "Js env has been destructed");
179         return;
180     }
181     if (localInterfaceRef_ != nullptr) {
182         napi_delete_reference(env_, localInterfaceRef_);
183     }
184     napi_create_reference(env_, localInterface, 0, &localInterfaceRef_);
185     descriptor_ = Str8ToStr16(descriptor);
186 }
187 
queryLocalInterface(std::string & descriptor)188 napi_value NAPIRemoteObjectHolder::queryLocalInterface(std::string &descriptor)
189 {
190     if (env_ == nullptr) {
191         ZLOGE(LOG_LABEL, "Js env has been destructed");
192         return nullptr;
193     }
194     if (!descriptor_.empty() && strcmp(Str16ToStr8(descriptor_).c_str(), descriptor.c_str()) == 0) {
195         napi_value ret = nullptr;
196         napi_get_reference_value(env_, localInterfaceRef_, &ret);
197         return ret;
198     }
199     napi_value result = nullptr;
200     napi_get_null(env_, &result);
201     return result;
202 }
203 
DetectCreatedInIPCThread()204 void NAPIRemoteObjectHolder::DetectCreatedInIPCThread()
205 {
206 #if (defined(HIVIEWDFX_BACKTRACE_SUPPORT) && defined(FFRT_SUPPORT))
207     if (IPCThreadSkeleton::GetThreadType() == ThreadType::IPC_THREAD && descriptor_.length() > 0 &&
208         descriptor_.find(u"ServiceCallbackStubImpl") != std::string::npos) {
209         std::string backtrace;
210         if (!HiviewDFX::GetBacktrace(backtrace, false)) {
211             ZLOGW(LOG_LABEL, "GetBacktrace failed");
212         } else {
213             ZLOGW(LOG_LABEL, "GetBacktrace info:\n%{public}s", backtrace.c_str());
214         }
215         uint64_t curTime = static_cast<uint64_t>(
216             std::chrono::duration_cast<std::chrono::nanoseconds>(
217             std::chrono::steady_clock::now().time_since_epoch()).count());
218         std::ostringstream stream;
219         stream << jsThreadId_;
220         ZLOGW(LOG_LABEL, "jsThreadId_:%{public}s, NativeEngine GetSysTid: %{public}u, "
221             "ffrt id:%{public}" PRIu64 ", GetCurSysTid:%{public}u, time:%{public}" PRIu64,
222             stream.str().c_str(), (reinterpret_cast<NativeEngine *>(env_))->GetSysTid(),
223             ffrt_this_task_get_id(), NativeEngine::GetCurSysTid(), curTime);
224     }
225 #endif
226 }
227 } // namespace OHOS