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     NOTIFY_DEVICE_ONLINE,
36     NOTIFY_DEVICE_OFFLINE
37 };
38 
39 struct NotifyMessage : public OHOS::Parcelable {
40 public:
41     NotifyType notifyType_;
42     std::vector<std::string> uris_;
43     NotifyMessage() = default;
NotifyMessageNotifyMessage44     NotifyMessage(const NotifyType notifyType, const std::vector<std::string> &uris)
45         : notifyType_(notifyType), uris_(uris) {}
46 
ReadFromParcelNotifyMessage47     bool ReadFromParcel(Parcel &parcel)
48     {
49         notifyType_ = NotifyType(parcel.ReadInt32());
50         auto count = parcel.ReadInt32();
51         if (count > MAX_COUNTNUM) {
52             HILOG_ERROR("ERROR:Count greater than 1000 .");
53             return false;
54         }
55         for (int i = 0; i < count; i++) {
56             uris_.push_back(parcel.ReadString());
57         }
58         return true;
59     }
60 
MarshallingNotifyMessage61     virtual bool Marshalling(Parcel &parcel) const override
62     {
63         if (!parcel.WriteInt32(notifyType_)) {
64             HILOG_ERROR("NotifyMessage Unmarshalling error:write deviceType fail.");
65             return false;
66         }
67         if (!parcel.WriteInt32(uris_.size())) {
68             HILOG_ERROR("NotifyMessage Unmarshalling error:write notifyType fail.");
69             return false;
70         }
71         for (unsigned int i = 0; i < uris_.size(); i++) {
72             if (!parcel.WriteString(uris_[i])) {
73                 HILOG_ERROR("NotifyMessage Unmarshalling error:write srcUri fail.");
74                 return false;
75             }
76         }
77         return true;
78     }
79 
UnmarshallingNotifyMessage80     static NotifyMessage *Unmarshalling(Parcel &parcel)
81     {
82         NotifyMessage *message = new (std::nothrow) NotifyMessage();
83         if (message == nullptr) {
84             HILOG_ERROR("NotifyMessage Unmarshalling error:new object fail.");
85             return nullptr;
86         }
87 
88         if (!message->ReadFromParcel(parcel)) {
89             delete message;
90             message = nullptr;
91         }
92         return message;
93     }
94 };
95 } // namespace FileAccessFwk
96 } // namespace OHOS
97 #endif // FILE_ACCESS_OBSERVER_COMMON_H
98