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 OHOS_ABILITY_BASE_OPERATION_H
17 #define OHOS_ABILITY_BASE_OPERATION_H
18 
19 #include <string>
20 #include "uri.h"
21 #include "parcel.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 class OperationBuilder;
27 
28 class Operation : public Parcelable {
29     friend class OperationBuilder;
30     friend class Want;
31 
32 public:
33     Operation();
34     ~Operation();
35     Operation(const Operation &other);
36     /**
37      * @description: Obtains the value of the abilityName attribute included in this Operation.
38      * @return Returns the ability name included in this Operation.
39      */
40     std::string GetAbilityName() const;
41 
42     /**
43      * @description: Obtains the value of the action attribute included in this Operation.
44      * @return Returns the action included in this Operation.
45      */
46     std::string GetAction() const;
47 
48     /**
49      * @description: Obtains the value of the bundleName attribute included in this Operation.
50      * @return Returns the bundle name included in this Operation.
51      */
52     std::string GetBundleName() const;
53 
54     /**
55      * @description: Obtains the value of the deviceId attribute included in this Operation.
56      * @return Returns the device ID included in this Operation.
57      */
58     std::string GetDeviceId() const;
59 
60     /**
61      * @description: Obtains the value of the entities attribute included in this Operation.
62      * @return Returns the entities included in this Operation.
63      */
64     const std::vector<std::string> &GetEntities() const;
65 
66     /**
67      * @description: Obtains the value of the flags attribute included in this Operation.
68      * @return Returns the flags included in this Operation.
69      */
70     unsigned int GetFlags() const;
71 
72     /**
73      * @description: Obtains the value of the uri attribute included in this Operation.
74      * @return Returns the URI included in this Operation.
75      */
76     OHOS::Uri GetUri() const;
77 
78     /**
79      * @description: Obtains the description of the ModuleName object in the Operation.
80      * @return Returns the ModuleName description in the Operation.
81      */
82     std::string GetModuleName() const;
83 
84     bool operator==(const Operation &other) const;
85     Operation &operator=(const Operation &other);
86 
87     bool Marshalling(Parcel &parcel) const;
88     static Operation *Unmarshalling(Parcel &parcel);
89 
90     void DumpInfo(int level) const;
91 
92 private:
93     /**
94      * @description: Sets a flag in a Want.
95      * @param flags Indicates the flag to set.
96      * @return Returns this Want object containing the flag.
97      */
98     void SetFlags(unsigned int flags);
99     /**
100      * @description: Adds a flag to a Want.
101      * @param flags Indicates the flag to add.
102      * @return Returns the Want object with the added flag.
103      */
104     void AddFlags(unsigned int flags);
105     /**
106      * @description: Removes the description of a flag from a Want.
107      * @param flags Indicates the flag to remove.
108      * @return Removes the description of a flag from a Want.
109      */
110     void RemoveFlags(unsigned int flags);
111 
112     /**
113      * @description: Adds the description of an entity to a Want
114      * @param entity Indicates the entity description to add
115      * @return Returns this Want object containing the entity.
116      */
117     void AddEntity(const std::string &entity);
118 
119     /**
120      * @description: Removes the description of an entity from a Want
121      * @param entity Indicates the entity description to remove.
122      * @return void
123      */
124     void RemoveEntity(const std::string &entity);
125 
126     /**
127      * @description: Checks whether a Want contains the given entity
128      * @param entity Indicates the entity to check
129      * @return Returns true if the given entity is contained; returns false otherwise
130      */
131     bool HasEntity(const std::string &entity) const;
132 
133     /**
134      * @description: Obtains the number of entities in a Want
135      * @return Returns the entity quantity
136      */
137     int CountEntities() const;
138 
139     /**
140      * @description: Sets a bundle name in this Want.
141      * If a bundle name is specified in a Want, the Want will match only
142      * the abilities in the specified bundle. You cannot use this method and
143      * setPicker(ohos.aafwk.content.Want) on the same Want.
144      * @param bundleName Indicates the bundle name to set.
145      * @return Returns a Want object containing the specified bundle name.
146      */
147     void SetBundleName(const std::string &bundleName);
148 
149     /**
150      * @description: Sets a uri in this operation.
151      * @param uri Indicates uri object to set.
152      * @return -
153      */
154     void SetUri(const Uri &uri);
155 
156     /**
157      * @description: Gets a uri in this operation.
158      * @param uri Indicates uri object to set.
159      * @return Returns a uri in this operation.
160      */
161     Uri &GetUri(const Uri &uri);
162 
163     /**
164      * @description: Sets the value of the abilityName attribute included in this Operation.
165      * @return Returns the ability name included in this Operation.
166      */
167     void SetAbilityName(const std::string &abilityname);
168 
169     /**
170      * @description: Sets the value of the deviceId attribute included in this Operation.
171      * @param deviceid Indicates deviceid object to set.
172      * @return -
173      */
174     void SetDeviceId(const std::string &deviceid);
175 
176     /**
177      * @description: Sets the value of the action attribute included in this Operation.
178      * @param deviceid Indicates deviceid object to set.
179      * @return -
180      */
181     void SetAction(const std::string &action);
182 
183     /**
184      * @description: Sets the entities of this Operation.
185      * @param entities Indicates entities to set.
186      * @return -
187      */
188     void SetEntities(const std::vector<std::string> &entities);
189 
190     /**
191      * @description: Sets an ModuleName object in the Operation.
192      * @param moduleName Indicates the ModuleName description.
193      * @return Returns this Operation object containing the ModuleName.
194      */
195     void SetModuleName(const std::string &moduleName);
196 
197 private:
198     std::string abilityName_;
199     std::string action_;
200     std::string bundleName_;
201     std::string deviceId_;
202     std::string moduleName_;
203     std::vector<std::string> entities_;
204     unsigned int flags_;
205     Uri uri_;
206     friend class OperationBuilder;
207 
208     // no object in parcel
209     static constexpr int VALUE_NULL = -1;
210     // object exist in parcel
211     static constexpr int VALUE_OBJECT = 1;
212 
213     bool ReadFromParcel(Parcel &parcel);
214 };
215 }  // namespace AAFwk
216 }  // namespace OHOS
217 
218 #endif  // OHOS_ABILITY_BASE_OPERATION_H