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 tokenId_ = parcel.ReadUint64(); 99 return true; 100 } 101 Unmarshalling(Parcel &parcel)102ContinuousTaskParam *ContinuousTaskParam::Unmarshalling(Parcel &parcel) 103 { 104 ContinuousTaskParam *param = new (std::nothrow) ContinuousTaskParam(); 105 if (param && !param->ReadFromParcel(parcel)) { 106 BGTASK_LOGE("read from parcel failed"); 107 delete param; 108 param = nullptr; 109 } 110 return param; 111 } 112 Unmarshalling(Parcel &parcel)113ContinuousTaskParamForInner *ContinuousTaskParamForInner::Unmarshalling(Parcel &parcel) 114 { 115 ContinuousTaskParamForInner *param = new (std::nothrow) ContinuousTaskParamForInner(); 116 if (param && !param->ReadFromParcel(parcel)) { 117 BGTASK_LOGE("read from parcel failed"); 118 delete param; 119 param = nullptr; 120 } 121 return param; 122 } 123 Marshalling(Parcel &parcel) const124bool ContinuousTaskParam::Marshalling(Parcel &parcel) const 125 { 126 if (!parcel.WriteBool(isNewApi_)) { 127 BGTASK_LOGE("Failed to write the flag which indicate whether is called from newApi"); 128 return false; 129 } 130 131 if (!parcel.WriteUint32(bgModeId_)) { 132 BGTASK_LOGE("Failed to write request background mode info"); 133 return false; 134 } 135 bool valid = wantAgent_ != nullptr; 136 if (!parcel.WriteBool(valid)) { 137 BGTASK_LOGE("Failed to write the flag which indicate whether wantAgent is null"); 138 return false; 139 } 140 if (valid) { 141 if (!parcel.WriteParcelable(wantAgent_.get())) { 142 BGTASK_LOGE("Failed to write wantAgent"); 143 return false; 144 } 145 } 146 147 std::u16string u16AbilityName = Str8ToStr16(abilityName_); 148 if (!parcel.WriteString16(u16AbilityName)) { 149 BGTASK_LOGE("Failed to write abilityName"); 150 return false; 151 } 152 std::u16string u16AppName = Str8ToStr16(appName_); 153 if (!parcel.WriteString16(u16AppName)) { 154 BGTASK_LOGE("Failed to write appName"); 155 return false; 156 } 157 if (!parcel.WriteBool(isBatchApi_)) { 158 BGTASK_LOGE("Failed to write the isBatchApi"); 159 return false; 160 } 161 162 if (isBatchApi_) { 163 BGTASK_LOGD("write modes %{public}u", static_cast<uint32_t>(bgModeIds_.size())); 164 if (!parcel.WriteUInt32Vector(bgModeIds_)) { 165 BGTASK_LOGE("Failed to write bgModeIds"); 166 return false; 167 } 168 } 169 170 if (!parcel.WriteInt32(abilityId_)) { 171 BGTASK_LOGE("Failed to write the abilityId"); 172 return false; 173 } 174 return true; 175 } 176 Marshalling(Parcel &parcel) const177bool ContinuousTaskParamForInner::Marshalling(Parcel &parcel) const 178 { 179 if (!parcel.WriteBool(isStart_)) { 180 BGTASK_LOGE("Failed to write the flag which indicate keep or stop running background"); 181 return false; 182 } 183 184 if (!parcel.WriteUint32(bgModeId_)) { 185 BGTASK_LOGE("Failed to write request background mode info"); 186 return false; 187 } 188 189 if (!parcel.WriteInt32(uid_)) { 190 BGTASK_LOGE("Failed to write uid info"); 191 return false; 192 } 193 194 if (!parcel.WriteInt32(abilityId_)) { 195 BGTASK_LOGE("Failed to write the abilityId"); 196 return false; 197 } 198 if (!parcel.WriteUint64(tokenId_)) { 199 BGTASK_LOGE("Failed to write tokenId_"); 200 return false; 201 } 202 return true; 203 } 204 } // namespace BackgroundTaskMgr 205 } // namespace OHOS