• 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 #include "child_process_record.h"
16 
17 #include <filesystem>
18 
19 #include "app_running_record.h"
20 #include "hilog_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
ChildProcessRecord(pid_t hostPid,const std::string & srcEntry,const std::shared_ptr<AppRunningRecord> hostRecord)24 ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const std::string &srcEntry,
25     const std::shared_ptr<AppRunningRecord> hostRecord)
26     : hostPid_(hostPid), srcEntry_(srcEntry), hostRecord_(hostRecord)
27 {
28     MakeProcessName(hostRecord);
29 }
30 
~ChildProcessRecord()31 ChildProcessRecord::~ChildProcessRecord()
32 {
33     HILOG_DEBUG("Called.");
34 }
35 
CreateChildProcessRecord(pid_t hostPid,const std::string & srcEntry,const std::shared_ptr<AppRunningRecord> hostRecord)36 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateChildProcessRecord(pid_t hostPid,
37     const std::string &srcEntry, const std::shared_ptr<AppRunningRecord> hostRecord)
38 {
39     HILOG_DEBUG("hostPid: %{public}d, srcEntry: %{public}s", hostPid, srcEntry.c_str());
40     if (hostPid <= 0 || srcEntry.empty() || !hostRecord) {
41         HILOG_ERROR("Invalid parameter.");
42         return nullptr;
43     }
44     return std::make_shared<ChildProcessRecord>(hostPid, srcEntry, hostRecord);
45 }
46 
SetPid(pid_t pid)47 void ChildProcessRecord::SetPid(pid_t pid)
48 {
49     pid_ = pid;
50 }
51 
GetPid() const52 pid_t ChildProcessRecord::GetPid() const
53 {
54     return pid_;
55 }
56 
GetHostPid() const57 pid_t ChildProcessRecord::GetHostPid() const
58 {
59     return hostPid_;
60 }
61 
SetUid(int32_t uid)62 void ChildProcessRecord::SetUid(int32_t uid)
63 {
64     uid_ = uid;
65 }
66 
GetUid() const67 int32_t ChildProcessRecord::GetUid() const
68 {
69     return uid_;
70 }
71 
GetProcessName() const72 std::string ChildProcessRecord::GetProcessName() const
73 {
74     return processName_;
75 }
76 
GetSrcEntry() const77 std::string ChildProcessRecord::GetSrcEntry() const
78 {
79     return srcEntry_;
80 }
81 
GetHostRecord() const82 std::shared_ptr<AppRunningRecord> ChildProcessRecord::GetHostRecord() const
83 {
84     return hostRecord_.lock();
85 }
86 
SetScheduler(const sptr<IChildScheduler> & scheduler)87 void ChildProcessRecord::SetScheduler(const sptr<IChildScheduler> &scheduler)
88 {
89     scheduler_ = scheduler;
90 }
91 
GetScheduler() const92 sptr<IChildScheduler> ChildProcessRecord::GetScheduler() const
93 {
94     return scheduler_;
95 }
96 
SetDeathRecipient(const sptr<AppDeathRecipient> recipient)97 void ChildProcessRecord::SetDeathRecipient(const sptr<AppDeathRecipient> recipient)
98 {
99     deathRecipient_ = recipient;
100 }
101 
RegisterDeathRecipient()102 void ChildProcessRecord::RegisterDeathRecipient()
103 {
104     if (scheduler_ == nullptr || deathRecipient_ == nullptr) {
105         HILOG_ERROR("scheduler_ or deathRecipient_ is null.");
106         return;
107     }
108     auto obj = scheduler_->AsObject();
109     if (!obj || !obj->AddDeathRecipient(deathRecipient_)) {
110         HILOG_ERROR("AddDeathRecipient failed.");
111     }
112 }
113 
RemoveDeathRecipient()114 void ChildProcessRecord::RemoveDeathRecipient()
115 {
116     if (!scheduler_) {
117         HILOG_ERROR("scheduler_ is null.");
118         return;
119     }
120     auto object = scheduler_->AsObject();
121     if (object) {
122         object->RemoveDeathRecipient(deathRecipient_);
123     }
124 }
125 
ScheduleExitProcessSafely()126 void ChildProcessRecord::ScheduleExitProcessSafely()
127 {
128     if (!scheduler_) {
129         HILOG_ERROR("scheduler_ is null.");
130         return;
131     }
132     scheduler_->ScheduleExitProcessSafely();
133 }
134 
MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)135 void ChildProcessRecord::MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)
136 {
137     if (!hostRecord) {
138         HILOG_WARN("hostRecord empty.");
139         return;
140     }
141     processName_ = hostRecord->GetBundleName();
142     if (srcEntry_.empty()) {
143         HILOG_WARN("srcEntry empty.");
144         return;
145     }
146     std::string filename = std::filesystem::path(srcEntry_).stem();
147     if (!filename.empty()) {
148         processName_.append(":");
149         processName_.append(filename);
150     }
151 }
152 }  // namespace AppExecFwk
153 }  // namespace OHOS
154