1 /*
2 * Copyright (c) 2021 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 "running_process_info.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20
21 #include "app_log_wrapper.h"
22 #include "parcel_macro.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26
27 namespace {
28
29 const std::string JSON_KEY_PROCESSNAME = "processName";
30 const std::string JSON_KEY_PID = "pid";
31 const std::string JSON_KEY_STATE = "state";
32 } // namespace
33
ReadFromParcel(Parcel & parcel)34 bool RunningProcessInfo::ReadFromParcel(Parcel &parcel)
35 {
36 processName_ = Str16ToStr8(parcel.ReadString16());
37 int32_t typeData;
38 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, typeData);
39 pid_ = static_cast<int32_t>(typeData);
40 int32_t uidData;
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uidData);
42 uid_ = static_cast<int32_t>(uidData);
43 int32_t stateData;
44 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, stateData);
45 state_ = static_cast<AppProcessState>(stateData);
46 return true;
47 }
48
Unmarshalling(Parcel & parcel)49 RunningProcessInfo *RunningProcessInfo::Unmarshalling(Parcel &parcel)
50 {
51 RunningProcessInfo *info = new (std::nothrow) RunningProcessInfo();
52 if (info && !info->ReadFromParcel(parcel)) {
53 APP_LOGW("read from parcel failed");
54 delete info;
55 info = nullptr;
56 }
57 return info;
58 }
59
Marshalling(Parcel & parcel) const60 bool RunningProcessInfo::Marshalling(Parcel &parcel) const
61 {
62 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName_));
63 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid_));
64 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uid_));
65 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(state_));
66 return true;
67 }
68
69 } // namespace AppExecFwk
70 } // namespace OHOS
71