1 /* 2 * Copyright (c) 2025 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 #ifndef OHOS_APP_NATIVE_SPAWN_MANAGER_H 17 #define OHOS_APP_NATIVE_SPAWN_MANAGER_H 18 19 #include <mutex> 20 #include <string> 21 #include "nocopyable.h" 22 #include "app_running_manager.h" 23 #include "native_child_notify_interface.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 28 /** 29 * @class AppNativeSpawnManager 30 * provides native spawn exit. 31 */ 32 class AppNativeSpawnManager { 33 public: 34 /** 35 * GetInstance, get an instance of AppNativeSpawnManager. 36 * 37 * @return An instance of AppNativeSpawnManager. 38 */ 39 static AppNativeSpawnManager &GetInstance(); 40 41 /** 42 * AppNativeSpawnManager, destructor. 43 * 44 */ 45 ~AppNativeSpawnManager(); 46 47 int32_t RegisterNativeChildExitNotify(const sptr<INativeChildNotify> &callback); 48 49 int32_t UnregisterNativeChildExitNotify(const sptr<INativeChildNotify> &callback); 50 51 // pid is parent pid 52 sptr<INativeChildNotify> GetNativeChildCallbackByPid(int32_t pid); 53 54 // pid is parent pid 55 void RemoveNativeChildCallbackByPid(int32_t pid); 56 57 void InitNativeSpawnMsgPipe(std::shared_ptr<AppRunningManager> appRunningManager); 58 GetNRfd()59 int GetNRfd() const 60 { 61 return nrFd_; 62 } 63 GetNWfd()64 int GetNWfd() const 65 { 66 return nwFd_; 67 } 68 69 void NotifyChildProcessExitTask(int32_t pid, int32_t signal, const std::string &bundleName); 70 71 int32_t GetChildRelation(int32_t childPid); 72 73 void AddChildRelation(int32_t childPid, int32_t parentPid); 74 75 void RemoveChildRelation(int32_t childPid); 76 private: 77 /** 78 * AppUtils, private constructor. 79 * 80 */ 81 AppNativeSpawnManager(); 82 83 //native spawn use 84 int nrFd_ = -1; 85 int nwFd_ = -1; 86 std::mutex nativeChildCallbackLock_; 87 std::map<int32_t, sptr<INativeChildNotify>> nativeChildCallbackMap_; 88 89 // child pid -> parent pid 90 std::mutex childRelationLock_; 91 std::map<int32_t, int32_t> childRelationMap_; 92 std::shared_ptr<AppRunningManager> appRunningManager_ = nullptr; 93 DISALLOW_COPY_AND_MOVE(AppNativeSpawnManager); 94 }; 95 } 96 } 97 #endif // OHOS_APP_NATIVE_SPAWN_MANAGER_H