1 /*
2  * Copyright (c) 2021-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 "process_info.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "string_ex.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
ProcessInfo(const std::string & name,const pid_t & pid)23 ProcessInfo::ProcessInfo(const std::string &name, const pid_t &pid) : processName_(name), pid_(pid)
24 {}
25 
26 /**
27  * @brief read this Sequenceable object from a Parcel.
28  *
29  * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
30  * @return Returns true if read successed; returns false otherwise.
31  */
ReadFromParcel(Parcel & parcel)32 bool ProcessInfo::ReadFromParcel(Parcel &parcel)
33 {
34     processName_ = Str16ToStr8(parcel.ReadString16());
35     pid_ = parcel.ReadInt32();
36     processType_ = ProcessType(parcel.ReadInt32());
37     return true;
38 }
39 
40 /**
41  * @brief Unmarshals this Sequenceable object from a Parcel.
42  *
43  * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
44  */
Unmarshalling(Parcel & parcel)45 ProcessInfo *ProcessInfo::Unmarshalling(Parcel &parcel)
46 {
47     ProcessInfo *processInfo = new (std::nothrow) ProcessInfo();
48     if (processInfo && !processInfo->ReadFromParcel(parcel)) {
49         TAG_LOGE(AAFwkTag::APPMGR, "ProcessInfo::Unmarshalling ReadFromParcel failed");
50         delete processInfo;
51         processInfo = nullptr;
52     }
53     return processInfo;
54 }
55 
56 /**
57  * @brief Marshals this Sequenceable object into a Parcel.
58  *
59  * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
60  */
Marshalling(Parcel & parcel) const61 bool ProcessInfo::Marshalling(Parcel &parcel) const
62 {
63     return (parcel.WriteString16(Str8ToStr16(processName_)) && parcel.WriteInt32(pid_)
64         && parcel.WriteInt32(static_cast<int32_t>(processType_)));
65 }
66 
SetProcessType(const ProcessType & processType)67 void ProcessInfo::SetProcessType(const ProcessType &processType)
68 {
69     processType_ = processType;
70 }
71 
GetProcessType() const72 ProcessType ProcessInfo::GetProcessType() const
73 {
74     return processType_;
75 }
76 }  // namespace AppExecFwk
77 }  // namespace OHOS
78