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 #ifndef STORAGE_SERVICES_CMD_RESPONSE_H 16 #define STORAGE_SERVICES_CMD_RESPONSE_H 17 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include "file_info.h" 23 #include "log.h" 24 #include "parcel.h" 25 namespace OHOS { 26 namespace FileManagerService { 27 class CmdResponse : public Parcelable { 28 public: 29 CmdResponse() = default; CmdResponse(int & err,std::string & uri,std::vector<std::shared_ptr<FileInfo>> & fileInfoList)30 CmdResponse(int &err, std::string &uri, std::vector<std::shared_ptr<FileInfo>> &fileInfoList) 31 : err_(err), uri_(uri), vecFileInfo_(fileInfoList) 32 {} 33 ~CmdResponse() = default; 34 SetErr(const int err)35 void SetErr(const int err) 36 { 37 err_ = err; 38 } 39 GetErr()40 int GetErr() const 41 { 42 return err_; 43 } 44 SetUri(const std::string & uri)45 void SetUri(const std::string &uri) 46 { 47 uri_ = uri; 48 } 49 GetUri()50 std::string GetUri() const 51 { 52 return uri_; 53 } 54 SetFileInfoList(std::vector<std::shared_ptr<FileInfo>> & fileInfoList)55 void SetFileInfoList(std::vector<std::shared_ptr<FileInfo>> &fileInfoList) 56 { 57 vecFileInfo_ = fileInfoList; 58 } 59 GetFileInfoList()60 std::vector<std::shared_ptr<FileInfo>> GetFileInfoList() 61 { 62 return vecFileInfo_; 63 } 64 Marshalling(Parcel & parcel)65 virtual bool Marshalling(Parcel &parcel) const override 66 { 67 parcel.WriteInt32(err_); 68 parcel.WriteString(uri_); 69 size_t fileCount = vecFileInfo_.size(); 70 parcel.WriteUint64(fileCount); 71 for (size_t i = 0; i < fileCount; i++) { 72 if (parcel.WriteParcelable(vecFileInfo_[i].get()) != true) { 73 ERR_LOG("Marshalling FileInfo fails!"); 74 return false; 75 } 76 } 77 return true; 78 } 79 Unmarshalling(Parcel & parcel)80 static CmdResponse* Unmarshalling(Parcel &parcel) 81 { 82 auto *obj = new (std::nothrow) CmdResponse(); 83 if (obj == nullptr) { 84 return nullptr; 85 } 86 obj->err_ = parcel.ReadInt32(); 87 obj->uri_ = parcel.ReadString(); 88 size_t fileCount = parcel.ReadUint64(); 89 for (size_t i = 0; i < fileCount; i++) { 90 std::shared_ptr<FileInfo> file(parcel.ReadParcelable<FileInfo>()); 91 obj->vecFileInfo_.emplace_back(file); 92 } 93 return obj; 94 } 95 private: 96 int err_; 97 std::string uri_; 98 std::vector<std::shared_ptr<FileInfo>> vecFileInfo_; 99 }; 100 } // FileManagerService 101 } // namespace OHOS 102 #endif // STORAGE_SERVICES_CMD_RESPONSE_H