1 /* 2 * Copyright (c) 2023 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 #ifndef FILE_ACCESS_OBSERVER_COMMON_H 17 #define FILE_ACCESS_OBSERVER_COMMON_H 18 19 #include <string> 20 #include <vector> 21 22 #include "file_access_extension_info.h" 23 #include "hilog_wrapper.h" 24 #include "parcel.h" 25 26 namespace OHOS { 27 namespace FileAccessFwk { 28 constexpr int64_t MAX_COUNTNUM = 1000; 29 enum NotifyType { 30 NOTIFY_ADD = 0, 31 NOTIFY_DELETE, 32 NOTIFY_MOVE_TO, 33 NOTIFY_MOVE_FROM, 34 NOTIFY_MOVE_SELE 35 }; 36 37 struct NotifyMessage : public OHOS::Parcelable { 38 public: 39 NotifyType notifyType_; 40 std::vector<std::string> uris_; 41 NotifyMessage() = default; NotifyMessageNotifyMessage42 NotifyMessage(const NotifyType notifyType, const std::vector<std::string> &uris) 43 : notifyType_(notifyType), uris_(uris) {} 44 ReadFromParcelNotifyMessage45 bool ReadFromParcel(Parcel &parcel) 46 { 47 notifyType_ = NotifyType(parcel.ReadInt32()); 48 auto count = parcel.ReadInt32(); 49 if (count > MAX_COUNTNUM) { 50 HILOG_ERROR("ERROR:Count greater than 1000 ."); 51 return false; 52 } 53 for (int i = 0; i < count; i++) { 54 uris_.push_back(parcel.ReadString()); 55 } 56 return true; 57 } 58 MarshallingNotifyMessage59 virtual bool Marshalling(Parcel &parcel) const override 60 { 61 if (!parcel.WriteInt32(notifyType_)) { 62 HILOG_ERROR("NotifyMessage Unmarshalling error:write deviceType fail."); 63 return false; 64 } 65 if (!parcel.WriteInt32(uris_.size())) { 66 HILOG_ERROR("NotifyMessage Unmarshalling error:write notifyType fail."); 67 return false; 68 } 69 for (unsigned int i = 0; i < uris_.size(); i++) { 70 if (!parcel.WriteString(uris_[i])) { 71 HILOG_ERROR("NotifyMessage Unmarshalling error:write srcUri fail."); 72 return false; 73 } 74 } 75 return true; 76 } 77 UnmarshallingNotifyMessage78 static NotifyMessage *Unmarshalling(Parcel &parcel) 79 { 80 NotifyMessage *message = new (std::nothrow) NotifyMessage(); 81 if (message == nullptr) { 82 HILOG_ERROR("NotifyMessage Unmarshalling error:new object fail."); 83 return nullptr; 84 } 85 86 if (!message->ReadFromParcel(parcel)) { 87 delete message; 88 message = nullptr; 89 } 90 return message; 91 } 92 }; 93 } // namespace FileAccessFwk 94 } // namespace OHOS 95 #endif // FILE_ACCESS_OBSERVER_COMMON_H 96