1 /*
2 * Copyright (c) 2024 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 "native_child_callback.h"
17 #include "hilog_tag_wrapper.h"
18 #include "ipc_inner_object.h"
19 #include "child_process_manager_error_utils.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23
NativeChildCallback(OH_Ability_OnNativeChildProcessStarted cb)24 NativeChildCallback::NativeChildCallback(OH_Ability_OnNativeChildProcessStarted cb)
25 : NativeChildNotifyStub(), callback_(cb)
26 {
27 }
28
OnNativeChildStarted(const sptr<IRemoteObject> & nativeChild)29 void NativeChildCallback::OnNativeChildStarted(const sptr<IRemoteObject> &nativeChild)
30 {
31 if (callback_ == nullptr) {
32 TAG_LOGE(AAFwkTag::PROCESSMGR, "null callback_");
33 return;
34 }
35
36 if (!nativeChild) {
37 TAG_LOGE(AAFwkTag::PROCESSMGR, "null nativeChild");
38 return;
39 }
40
41 TAG_LOGI(AAFwkTag::PROCESSMGR, "Native child process started");
42 sptr<IRemoteObject> ipcRemote = nativeChild;
43 OHIPCRemoteProxy *ipcProxy = CreateIPCRemoteProxy(ipcRemote);
44 if (ipcProxy == nullptr) {
45 TAG_LOGE(AAFwkTag::PROCESSMGR, "null ipcProxy");
46 callback_(NCP_ERR_INTERNAL, nullptr);
47 return;
48 }
49
50 callback_(static_cast<int32_t>(ChildProcessManagerErrorCode::ERR_OK), ipcProxy);
51 callback_ = nullptr;
52
53 ChildCallbackManager::GetInstance().RemoveRemoteObject(this);
54 }
55
OnError(int32_t errCode)56 void NativeChildCallback::OnError(int32_t errCode)
57 {
58 if (callback_ == nullptr) {
59 TAG_LOGE(AAFwkTag::PROCESSMGR, "null callback_");
60 return;
61 }
62
63 TAG_LOGI(AAFwkTag::PROCESSMGR, "Native child process start err %{public}d", errCode);
64 callback_(errCode, nullptr);
65
66 callback_ = nullptr;
67 ChildCallbackManager::GetInstance().RemoveRemoteObject(this);
68 }
69
OnNativeChildExit(int32_t pid,int32_t signal)70 int32_t NativeChildCallback::OnNativeChildExit(int32_t pid, int32_t signal)
71 {
72 auto exitCallbacks = GetExitCallbacks();
73 for (const auto &exitCallback : exitCallbacks) {
74 TAG_LOGI(AAFwkTag::PROCESSMGR,
75 "native child process exit, pid:%{public}d, signal:%{public}d", pid, signal);
76 exitCallback(pid, signal);
77 }
78 return NCP_NO_ERROR;
79 }
80
AddExitCallback(OH_Ability_OnNativeChildProcessExit callback)81 void NativeChildCallback::AddExitCallback(OH_Ability_OnNativeChildProcessExit callback)
82 {
83 std::lock_guard lock(exitCallbackListMutex_);
84 for (const auto &cb : exitCallbacks_) {
85 if (cb == callback) {
86 TAG_LOGI(AAFwkTag::PROCESSMGR, "repeated add exit callback");
87 return;
88 }
89 }
90 exitCallbacks_.emplace_back(callback);
91 }
92
RemoveExitCallback(OH_Ability_OnNativeChildProcessExit callback)93 int32_t NativeChildCallback::RemoveExitCallback(OH_Ability_OnNativeChildProcessExit callback)
94 {
95 std::lock_guard lock(exitCallbackListMutex_);
96 auto it = std::find(exitCallbacks_.begin(), exitCallbacks_.end(), callback);
97 if (it == exitCallbacks_.end()) {
98 TAG_LOGE(AAFwkTag::PROCESSMGR, "native child exit callback not exist");
99 return NCP_ERR_CALLBACK_NOT_EXIST;
100 }
101 exitCallbacks_.erase(it);
102 return NCP_NO_ERROR;
103 }
104
IsCallbacksEmpty()105 bool NativeChildCallback::IsCallbacksEmpty()
106 {
107 std::lock_guard lock(exitCallbackListMutex_);
108 return exitCallbacks_.empty();
109 }
110
GetExitCallbacks()111 std::list<OH_Ability_OnNativeChildProcessExit> NativeChildCallback::GetExitCallbacks()
112 {
113 std::lock_guard lock(exitCallbackListMutex_);
114 return exitCallbacks_;
115 }
116 } // namespace AbilityRuntime
117 } // namespace OHOS
118