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 return true; 59 } 60 Unmarshalling(Parcel & parcel)61ContinuousTaskParam *ContinuousTaskParam::Unmarshalling(Parcel &parcel) 62 { 63 ContinuousTaskParam *param = new (std::nothrow) ContinuousTaskParam(); 64 if (param && !param->ReadFromParcel(parcel)) { 65 BGTASK_LOGE("read from parcel failed"); 66 delete param; 67 param = nullptr; 68 } 69 return param; 70 } 71 Marshalling(Parcel & parcel) const72bool ContinuousTaskParam::Marshalling(Parcel &parcel) const 73 { 74 if (!parcel.WriteBool(isNewApi_)) { 75 BGTASK_LOGE("Failed to write the flag which indicate whether is called from newApi"); 76 return false; 77 } 78 79 if (!parcel.WriteUint32(bgModeId_)) { 80 BGTASK_LOGE("Failed to write request background mode info"); 81 return false; 82 } 83 bool valid = wantAgent_ != nullptr; 84 if (!parcel.WriteBool(valid)) { 85 BGTASK_LOGE("Failed to write the flag which indicate whether wantAgent is null"); 86 return false; 87 } 88 if (valid) { 89 if (!parcel.WriteParcelable(wantAgent_.get())) { 90 BGTASK_LOGE("Failed to write wantAgent"); 91 return false; 92 } 93 } 94 95 std::u16string u16AbilityName = Str8ToStr16(abilityName_); 96 if (!parcel.WriteString16(u16AbilityName)) { 97 BGTASK_LOGE("Failed to write abilityName"); 98 return false; 99 } 100 std::u16string u16AppName = Str8ToStr16(appName_); 101 if (!parcel.WriteString16(u16AppName)) { 102 BGTASK_LOGE("Failed to write appName"); 103 return false; 104 } 105 return true; 106 } 107 } // namespace BackgroundTaskMgr 108 } // namespace OHOS