1686862fbSopenharmony_ci/* 2686862fbSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4686862fbSopenharmony_ci * you may not use this file except in compliance with the License. 5686862fbSopenharmony_ci * You may obtain a copy of the License at 6686862fbSopenharmony_ci * 7686862fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8686862fbSopenharmony_ci * 9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12686862fbSopenharmony_ci * See the License for the specific language governing permissions and 13686862fbSopenharmony_ci * limitations under the License. 14686862fbSopenharmony_ci */ 15686862fbSopenharmony_ci 16686862fbSopenharmony_ci#include "continuation_result.h" 17686862fbSopenharmony_ci 18686862fbSopenharmony_ci#include <cstddef> 19686862fbSopenharmony_ci#include <iosfwd> 20686862fbSopenharmony_ci#include <new> 21686862fbSopenharmony_ci#include <vector> 22686862fbSopenharmony_ci 23686862fbSopenharmony_ci#include "base/continuationmgr_log.h" 24686862fbSopenharmony_ci#include "string_ex.h" 25686862fbSopenharmony_ci 26686862fbSopenharmony_cinamespace OHOS { 27686862fbSopenharmony_cinamespace DistributedSchedule { 28686862fbSopenharmony_cinamespace { 29686862fbSopenharmony_ciconst std::string TAG = "ContinuationResult"; 30686862fbSopenharmony_ciconstexpr int32_t VALUE_NULL = -1; // no object in parcel 31686862fbSopenharmony_ciconstexpr int32_t VALUE_OBJECT = 1; // object exist in parcel 32686862fbSopenharmony_ci} 33686862fbSopenharmony_ci 34686862fbSopenharmony_cibool ContinuationResult::ReadFromParcel(Parcel &parcel) 35686862fbSopenharmony_ci{ 36686862fbSopenharmony_ci SetDeviceId(Str16ToStr8(parcel.ReadString16())); 37686862fbSopenharmony_ci SetDeviceType(Str16ToStr8(parcel.ReadString16())); 38686862fbSopenharmony_ci SetDeviceName(Str16ToStr8(parcel.ReadString16())); 39686862fbSopenharmony_ci return true; 40686862fbSopenharmony_ci} 41686862fbSopenharmony_ci 42686862fbSopenharmony_ciContinuationResult *ContinuationResult::Unmarshalling(Parcel &parcel) 43686862fbSopenharmony_ci{ 44686862fbSopenharmony_ci ContinuationResult *continuationResult = new (std::nothrow) ContinuationResult(); 45686862fbSopenharmony_ci if (continuationResult == nullptr) { 46686862fbSopenharmony_ci return nullptr; 47686862fbSopenharmony_ci } 48686862fbSopenharmony_ci 49686862fbSopenharmony_ci if (!continuationResult->ReadFromParcel(parcel)) { 50686862fbSopenharmony_ci delete continuationResult; 51686862fbSopenharmony_ci continuationResult = nullptr; 52686862fbSopenharmony_ci } 53686862fbSopenharmony_ci 54686862fbSopenharmony_ci return continuationResult; 55686862fbSopenharmony_ci} 56686862fbSopenharmony_ci 57686862fbSopenharmony_cibool ContinuationResult::Marshalling(Parcel &parcel) const 58686862fbSopenharmony_ci{ 59686862fbSopenharmony_ci parcel.WriteString16(Str8ToStr16(GetDeviceId())); 60686862fbSopenharmony_ci parcel.WriteString16(Str8ToStr16(GetDeviceType())); 61686862fbSopenharmony_ci parcel.WriteString16(Str8ToStr16(GetDeviceName())); 62686862fbSopenharmony_ci return true; 63686862fbSopenharmony_ci} 64686862fbSopenharmony_ci 65686862fbSopenharmony_civoid ContinuationResult::SetDeviceId(std::string deviceId) 66686862fbSopenharmony_ci{ 67686862fbSopenharmony_ci deviceId_ = deviceId; 68686862fbSopenharmony_ci} 69686862fbSopenharmony_ci 70686862fbSopenharmony_cistd::string ContinuationResult::GetDeviceId() const 71686862fbSopenharmony_ci{ 72686862fbSopenharmony_ci return deviceId_; 73686862fbSopenharmony_ci} 74686862fbSopenharmony_ci 75686862fbSopenharmony_civoid ContinuationResult::SetDeviceType(std::string deviceType) 76686862fbSopenharmony_ci{ 77686862fbSopenharmony_ci deviceType_ = deviceType; 78686862fbSopenharmony_ci} 79686862fbSopenharmony_ci 80686862fbSopenharmony_cistd::string ContinuationResult::GetDeviceType() const 81686862fbSopenharmony_ci{ 82686862fbSopenharmony_ci return deviceType_; 83686862fbSopenharmony_ci} 84686862fbSopenharmony_ci 85686862fbSopenharmony_civoid ContinuationResult::SetDeviceName(std::string deviceName) 86686862fbSopenharmony_ci{ 87686862fbSopenharmony_ci deviceName_ = deviceName; 88686862fbSopenharmony_ci} 89686862fbSopenharmony_ci 90686862fbSopenharmony_cistd::string ContinuationResult::GetDeviceName() const 91686862fbSopenharmony_ci{ 92686862fbSopenharmony_ci return deviceName_; 93686862fbSopenharmony_ci} 94686862fbSopenharmony_ci 95686862fbSopenharmony_cibool ContinuationResult::ReadContinuationResultsFromParcel(Parcel& parcel, 96686862fbSopenharmony_ci std::vector<ContinuationResult>& continuationResults) 97686862fbSopenharmony_ci{ 98686862fbSopenharmony_ci continuationResults.clear(); 99686862fbSopenharmony_ci int32_t empty = parcel.ReadInt32(); 100686862fbSopenharmony_ci if (empty == VALUE_OBJECT) { 101686862fbSopenharmony_ci int32_t len = parcel.ReadInt32(); 102686862fbSopenharmony_ci HILOGD("read size: %{public}d", len); 103686862fbSopenharmony_ci if (len < 0) { 104686862fbSopenharmony_ci HILOGE("size Unmarshalling failed"); 105686862fbSopenharmony_ci return false; 106686862fbSopenharmony_ci } 107686862fbSopenharmony_ci size_t size = static_cast<size_t>(len); 108686862fbSopenharmony_ci if ((size > parcel.GetReadableBytes()) || (continuationResults.max_size() < size)) { 109686862fbSopenharmony_ci HILOGE("size convert failed, size = %{public}zu", size); 110686862fbSopenharmony_ci return false; 111686862fbSopenharmony_ci } 112686862fbSopenharmony_ci for (size_t i = 0; i < size; i++) { 113686862fbSopenharmony_ci ContinuationResult* continuationResult = parcel.ReadParcelable<ContinuationResult>(); 114686862fbSopenharmony_ci if (continuationResult == nullptr) { 115686862fbSopenharmony_ci HILOGE("ContinuationResult Unmarshalling failed"); 116686862fbSopenharmony_ci return false; 117686862fbSopenharmony_ci } 118686862fbSopenharmony_ci continuationResults.emplace_back(*continuationResult); 119686862fbSopenharmony_ci delete continuationResult; 120686862fbSopenharmony_ci } 121686862fbSopenharmony_ci } 122686862fbSopenharmony_ci return true; 123686862fbSopenharmony_ci} 124686862fbSopenharmony_ci 125686862fbSopenharmony_cibool ContinuationResult::WriteContinuationResultsToParcel(Parcel& parcel, 126686862fbSopenharmony_ci const std::vector<ContinuationResult>& continuationResults) 127686862fbSopenharmony_ci{ 128686862fbSopenharmony_ci size_t size = continuationResults.size(); 129686862fbSopenharmony_ci if (size == 0) { 130686862fbSopenharmony_ci return parcel.WriteInt32(VALUE_NULL); 131686862fbSopenharmony_ci } 132686862fbSopenharmony_ci if (!parcel.WriteInt32(VALUE_OBJECT)) { 133686862fbSopenharmony_ci return false; 134686862fbSopenharmony_ci } 135686862fbSopenharmony_ci if (!parcel.WriteInt32(static_cast<int32_t>(size))) { 136686862fbSopenharmony_ci return false; 137686862fbSopenharmony_ci } 138686862fbSopenharmony_ci for (auto& continuationResult : continuationResults) { 139686862fbSopenharmony_ci if (!parcel.WriteParcelable(&continuationResult)) { 140686862fbSopenharmony_ci return false; 141686862fbSopenharmony_ci } 142686862fbSopenharmony_ci } 143686862fbSopenharmony_ci return true; 144686862fbSopenharmony_ci} 145686862fbSopenharmony_ci} // namespace DistributedSchedule 146686862fbSopenharmony_ci} // namespace OHOS