1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBE_INFO_H
17 #define FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBE_INFO_H
18 
19 #include "matching_skills.h"
20 
21 namespace OHOS {
22 namespace EventFwk {
23 class CommonEventSubscribeInfo : public Parcelable {
24 public:
25     enum ThreadMode {
26         HANDLER,     // the main thread of this ability.
27         POST,        // the event dispatch thread.
28         ASYNC,       // an asynchronous thread.
29         BACKGROUND,  // the background thread.
30         COMMON,      // common listening thread of a process.
31     };
32 
33     /**
34      * A constructor used to create a CommonEventSubscribeInfo instance
35      * with the matchingSkills parameters passed.
36      *
37      * @param matchingSkills Indicates the matching skills.
38      */
39     explicit CommonEventSubscribeInfo(const MatchingSkills &matchingSkills);
40 
41     CommonEventSubscribeInfo();
42 
43     /**
44      * A constructor used to create a CommonEventSubscribeInfo instance by
45      * copying parameters from an existing one.
46      *
47      *  @param commonEventSubscribeInfo Indicates the commonEventSubscribeInfo.
48      */
49     explicit CommonEventSubscribeInfo(const CommonEventSubscribeInfo &commonEventSubscribeInfo);
50 
51     virtual ~CommonEventSubscribeInfo();
52 
53     /**
54      * Sets the subscriber priority for this CommonEventSubscribeInfo object.
55      *
56      * @param priority Indicates the subscriber priority.
57      */
58     void SetPriority(const int32_t &priority);
59 
60     /**
61      * Obtains the subscriber priority of this CommonEventSubscribeInfo object.
62      *
63      * @return Returns the subscriber priority.
64      */
65     int32_t GetPriority() const;
66 
67     /**
68      * Sets the subscriber userId for this CommonEventSubscribeInfo object.
69      *
70      * @param userId Indicates the user ID of the subscriber.
71      */
72     void SetUserId(const int32_t &userId);
73 
74     /**
75      * Obtains the subscriber userId of this CommonEventSubscribeInfo object.
76      *
77      * @return Returns the user ID of the subscriber.
78      */
79     int32_t GetUserId() const;
80 
81     /**
82      * Sets the permission that the publisher must have in order to send
83      * a common event to this subscriber.
84      *
85      * @param permission Indicates the subscriber permission.
86      */
87     void SetPermission(const std::string &permission);
88 
89     /**
90      * Obtains the publisher permission of this CommonEventSubscribeInfo object.
91      *
92      * @return Returns the subscriber permission.
93      */
94     std::string GetPermission() const;
95 
96     /**
97      * Obtains the thread mode of this CommonEventSubscribeInfo object.
98      *
99      * @return Returns the thread mode.
100      */
101     CommonEventSubscribeInfo::ThreadMode GetThreadMode() const;
102 
103     /**
104      * Sets the thread mode of this CommonEventSubscribeInfo object.
105      *
106      * @param threadMode Indicates the thread mode to be set, which is specified in ThreadMode. Currently, only the
107      * HANDLER mode is supported.
108      */
109     void SetThreadMode(CommonEventSubscribeInfo::ThreadMode threadMode);
110 
111     /**
112      * Sets the device ID for this CommonEventSubscribeInfo object.
113      * Your application will only receive common events sent from the specified device.
114      *
115      * @param deviceId Indicates the device ID. The value must be an existing device ID on the same ohos network.
116      * Otherwise, it is invalid.
117      */
118     void SetDeviceId(const std::string &deviceId);
119 
120     /**
121      * Obtains the device ID in this CommonEventSubscribeInfo object.
122      *
123      * @return Returns the device ID.
124      */
125     std::string GetDeviceId() const;
126 
127     /**
128      * Obtains the MatchingSkills object carried in this CommonEventSubscribeInfo object.
129      *
130      * @return Returns the matchingSkills object.
131      */
132     const MatchingSkills &GetMatchingSkills() const;
133 
134     /**
135      * Sets the publisher bundle name for this CommonEventSubscribeInfo object.
136      *
137      * @param publisherBundleName Indicates the bundle name of the publisher.
138      */
139     void SetPublisherBundleName(const std::string &publisherBundleName);
140 
141     /**
142      * Obtains the publisher bundle name of this CommonEventSubscribeInfo object.
143      *
144      * @return Returns the bundle name of the publisher.
145      */
146     std::string GetPublisherBundleName() const;
147 
148     /**
149      * Sets the publisher uid for this CommonEventSubscribeInfo object.
150      *
151      * @param publisherUid Indicates the uid of the publisher.
152      */
153     void SetPublisherUid(int32_t publisherUid);
154 
155     /**
156      * Obtains the publisher uid of this CommonEventSubscribeInfo object.
157      *
158      * @return Returns the uid of the publisher.
159      */
160     int32_t GetPublisherUid() const;
161 
162     /**
163      * Marshals a subscriber info object into a Parcel.
164      *
165      * @param parcel Indicates specified Parcel object.
166      * @return Returns true if success; false otherwise.
167      */
168     virtual bool Marshalling(Parcel &parcel) const override;
169 
170     /**
171      * Unmarshals a Parcel object into a subscriber info.
172      *
173      * @return Returns the subscriber info.
174      */
175     static CommonEventSubscribeInfo *Unmarshalling(Parcel &parcel);
176 
177 private:
178     /**
179      * Read a subscriber object from a Parcel.
180      *
181      * @param parcel Indicates specified Parcel object.
182      * @return Returns true if success; false otherwise.
183      */
184     bool ReadFromParcel(Parcel &parcel);
185 
186 private:
187     MatchingSkills matchingSkills_;
188     int32_t priority_;
189     int32_t userId_;
190     std::string permission_;
191     std::string deviceId_;
192     CommonEventSubscribeInfo::ThreadMode threadMode_;
193     std::string publisherBundleName_;
194     int32_t publisherUid_;
195 };
196 }  // namespace EventFwk
197 }  // namespace OHOS
198 
199 #endif  // FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBE_INFO_H
200