1 /* 2 * Copyright (c) 2022 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 "continuous_task_param.h" 17 18 #include "string_ex.h" 19 20 #include "continuous_task_log.h" 21 22 namespace OHOS { 23 namespace BackgroundTaskMgr { ReadFromParcel(Parcel & parcel)24bool ContinuousTaskParam::ReadFromParcel(Parcel &parcel) 25 { 26 if (!parcel.ReadBool(isNewApi_)) { 27 BGTASK_LOGE("Failed to read the flag which indicate whether is called from newApi"); 28 return false; 29 } 30 31 if (!parcel.ReadUint32(bgModeId_)) { 32 BGTASK_LOGE("Failed to read request background mode info"); 33 return false; 34 } 35 bool valid = parcel.ReadBool(); 36 if (valid) { 37 wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>( 38 parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>()); 39 if (!wantAgent_) { 40 BGTASK_LOGE("Failed to read wantAgent"); 41 return false; 42 } 43 } 44 45 std::u16string u16AbilityName; 46 if (!parcel.ReadString16(u16AbilityName)) { 47 BGTASK_LOGE("Failed to read ability name"); 48 return false; 49 } 50 abilityName_ = Str16ToStr8(u16AbilityName); 51 52 std::u16string u16AppName; 53 if (!parcel.ReadString16(u16AppName)) { 54 BGTASK_LOGE("Failed to read app name"); 55 return false; 56 } 57 appName_ = Str16ToStr8(u16AppName); 58 59 if (!parcel.ReadBool(isBatchApi_)) { 60 BGTASK_LOGE("Failed to read the flag isBatchApi"); 61 return false; 62 } 63 if (isBatchApi_) { 64 if (!parcel.ReadUInt32Vector(&bgModeIds_)) { 65 BGTASK_LOGE("read parce bgmodes error"); 66 return false; 67 } 68 BGTASK_LOGD("read parce bgmodes_ size %{public}d", static_cast<uint32_t>(bgModeIds_.size())); 69 } 70 if (!parcel.ReadInt32(abilityId_)) { 71 BGTASK_LOGE("Failed to read the abilityId"); 72 return false; 73 } 74 return true; 75 } 76 ReadFromParcel(Parcel & parcel)77bool ContinuousTaskParamForInner::ReadFromParcel(Parcel &parcel) 78 { 79 if (!parcel.ReadBool(isStart_)) { 80 BGTASK_LOGE("Failed to read the flag which indicate keep or stop running background"); 81 return false; 82 } 83 84 if (!parcel.ReadUint32(bgModeId_)) { 85 BGTASK_LOGE("Failed to read request background mode info"); 86 return false; 87 } 88 89 if (!parcel.ReadInt32(uid_)) { 90 BGTASK_LOGE("Failed to read uid info"); 91 return false; 92 } 93 94 if (!parcel.ReadInt32(abilityId_)) { 95 BGTASK_LOGE("Failed to read the abilityId"); 96 return false; 97 } 98 99 if (!parcel.ReadUint64(tokenId_)) { 100 BGTASK_LOGE("Failed to read the tokenId"); 101 return false; 102 } 103 104 if (!parcel.ReadInt32(pid_)) { 105 BGTASK_LOGE("Failed to read the pid"); 106 return false; 107 } 108 return true; 109 } 110 Unmarshalling(Parcel & parcel)111ContinuousTaskParam *ContinuousTaskParam::Unmarshalling(Parcel &parcel) 112 { 113 ContinuousTaskParam *param = new (std::nothrow) ContinuousTaskParam(); 114 if (param && !param->ReadFromParcel(parcel)) { 115 BGTASK_LOGE("read from parcel failed"); 116 delete param; 117 param = nullptr; 118 } 119 return param; 120 } 121 Unmarshalling(Parcel & parcel)122ContinuousTaskParamForInner *ContinuousTaskParamForInner::Unmarshalling(Parcel &parcel) 123 { 124 ContinuousTaskParamForInner *param = new (std::nothrow) ContinuousTaskParamForInner(); 125 if (param && !param->ReadFromParcel(parcel)) { 126 BGTASK_LOGE("read from parcel failed"); 127 delete param; 128 param = nullptr; 129 } 130 return param; 131 } 132 Marshalling(Parcel & parcel) const133bool ContinuousTaskParam::Marshalling(Parcel &parcel) const 134 { 135 if (!parcel.WriteBool(isNewApi_)) { 136 BGTASK_LOGE("Failed to write the flag which indicate whether is called from newApi"); 137 return false; 138 } 139 140 if (!parcel.WriteUint32(bgModeId_)) { 141 BGTASK_LOGE("Failed to write request background mode info"); 142 return false; 143 } 144 bool valid = wantAgent_ != nullptr; 145 if (!parcel.WriteBool(valid)) { 146 BGTASK_LOGE("Failed to write the flag which indicate whether wantAgent is null"); 147 return false; 148 } 149 if (valid) { 150 if (!parcel.WriteParcelable(wantAgent_.get())) { 151 BGTASK_LOGE("Failed to write wantAgent"); 152 return false; 153 } 154 } 155 156 std::u16string u16AbilityName = Str8ToStr16(abilityName_); 157 if (!parcel.WriteString16(u16AbilityName)) { 158 BGTASK_LOGE("Failed to write abilityName"); 159 return false; 160 } 161 std::u16string u16AppName = Str8ToStr16(appName_); 162 if (!parcel.WriteString16(u16AppName)) { 163 BGTASK_LOGE("Failed to write appName"); 164 return false; 165 } 166 if (!parcel.WriteBool(isBatchApi_)) { 167 BGTASK_LOGE("Failed to write the isBatchApi"); 168 return false; 169 } 170 171 if (isBatchApi_) { 172 BGTASK_LOGD("write modes %{public}u", static_cast<uint32_t>(bgModeIds_.size())); 173 if (!parcel.WriteUInt32Vector(bgModeIds_)) { 174 BGTASK_LOGE("Failed to write bgModeIds"); 175 return false; 176 } 177 } 178 179 if (!parcel.WriteInt32(abilityId_)) { 180 BGTASK_LOGE("Failed to write the abilityId"); 181 return false; 182 } 183 return true; 184 } 185 Marshalling(Parcel & parcel) const186bool ContinuousTaskParamForInner::Marshalling(Parcel &parcel) const 187 { 188 if (!parcel.WriteBool(isStart_)) { 189 BGTASK_LOGE("Failed to write the flag which indicate keep or stop running background"); 190 return false; 191 } 192 193 if (!parcel.WriteUint32(bgModeId_)) { 194 BGTASK_LOGE("Failed to write request background mode info"); 195 return false; 196 } 197 198 if (!parcel.WriteInt32(uid_)) { 199 BGTASK_LOGE("Failed to write uid info"); 200 return false; 201 } 202 203 if (!parcel.WriteInt32(abilityId_)) { 204 BGTASK_LOGE("Failed to write the abilityId"); 205 return false; 206 } 207 if (!parcel.WriteUint64(tokenId_)) { 208 BGTASK_LOGE("Failed to write tokenId_"); 209 return false; 210 } 211 if (!parcel.WriteInt32(pid_)) { 212 BGTASK_LOGE("Failed to write pid_"); 213 return false; 214 } 215 return true; 216 } 217 SetPid(const int32_t pid)218void ContinuousTaskParamForInner::SetPid(const int32_t pid) 219 { 220 pid_ = pid; 221 } 222 GetPid() const223int32_t ContinuousTaskParamForInner::GetPid() const 224 { 225 return pid_; 226 } 227 } // namespace BackgroundTaskMgr 228 } // namespace OHOS