1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_ABILITY_SCHEDULE_PROXY_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_SCHEDULE_PROXY_H
18 
19 #include "ability_scheduler_interface.h"
20 
21 #include <iremote_proxy.h>
22 
23 namespace OHOS {
24 namespace NativeRdb {
25 class AbsSharedResultSet;
26 class DataAbilityPredicates;
27 class ValuesBucket;
28 }  // namespace NativeRdb
29 namespace AppExecFwk {
30 }  // namespace AppExecFwk
31 namespace AAFwk {
32 /**
33  * @class AbilitySchedulerProxy
34  * AbilityScheduler proxy.
35  */
36 class AbilitySchedulerProxy : public IRemoteProxy<IAbilityScheduler> {
37 public:
AbilitySchedulerProxy(const sptr<IRemoteObject> & impl)38     explicit AbilitySchedulerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAbilityScheduler>(impl)
39     {}
40 
~AbilitySchedulerProxy()41     virtual ~AbilitySchedulerProxy()
42     {}
43 
44     /*
45      * ScheduleAbilityTransaction,  schedule ability to transform life state.
46      *
47      * @param Want, Special Want for service type's ability.
48      * @param stateInfo, The lifecycle state to be transformed.
49      */
50     bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo,
51         sptr<SessionInfo> sessionInfo = nullptr) override;
52 
53     /*
54      * ScheduleShareData,  schedule ability to transform life state and share data with orgin ability.
55      *
56      * @param want, special Want for service type's ability.
57      * @param stateInfo, the lifecycle state to be transformed.
58      * @param uniqueId, the Id of origin ability request.
59      */
60     void ScheduleShareData(const int32_t &uniqueId) override;
61 
62     /*
63      * SendResult, Send result to app when ability is terminated with result want.
64      *
65      * @param requestCode, the requestCode of the ability to start.
66      * @param resultCode, the resultCode of the ability to terminate.
67      * @param resultWant, the want of the ability to terminate.
68      */
69     void SendResult(int requestCode, int resultCode, const Want &resultWant) override;
70 
71     /*
72      * ScheduleConnectAbility,  schedule service ability to connect.
73      *
74      * @param Want, Special Want for service type's ability.
75      */
76     void ScheduleConnectAbility(const Want &want) override;
77 
78     /*
79      * ScheduleDisconnectAbility, schedule service ability to disconnect.
80      */
81     void ScheduleDisconnectAbility(const Want &want) override;
82 
83     /*
84      * ScheduleCommandAbility, schedule service ability to command.
85      */
86     void ScheduleCommandAbility(const Want &want, bool restart, int startId) override;
87 
88     void ScheduleCommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo,
89         WindowCommand winCmd) override;
90 
91     /*
92      * SchedulePrepareTerminateAbility, schedule service ability to prepare terminate.
93      */
94     bool SchedulePrepareTerminateAbility() override;
95 
96     /*
97      * ScheduleSaveAbilityState, scheduling save ability state.
98      */
99     void ScheduleSaveAbilityState() override;
100 
101     /*
102      * ScheduleRestoreAbilityState, scheduling restore ability state.
103      */
104     void ScheduleRestoreAbilityState(const PacMap &inState) override;
105 
106     /**
107      * @brief Obtains the MIME types of files supported.
108      *
109      * @param uri Indicates the path of the files to obtain.
110      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
111      *
112      * @return Returns the matched MIME types. If there is no match, null is returned.
113      */
114     virtual std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) override;
115 
116     /**
117      * @brief Opens a file in a specified remote path.
118      *
119      * @param uri Indicates the path of the file to open.
120      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
121      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
122      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
123      *  or "rwt" for read and write access that truncates any existing file.
124      *
125      * @return Returns the file descriptor.
126      */
127     virtual int OpenFile(const Uri &uri, const std::string &mode) override;
128 
129     /**
130      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
131      * inside of their .hap.
132      *
133      * @param uri Indicates the path of the file to open.
134      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
135      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
136      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
137      * data, or "rwt" for read and write access that truncates any existing file.
138      *
139      * @return Returns the RawFileDescriptor object containing file descriptor.
140      */
141     virtual int OpenRawFile(const Uri &uri, const std::string &mode) override;
142 
143     /**
144      * @brief Inserts a single data record into the database.
145      *
146      * @param uri Indicates the path of the data to operate.
147      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
148      *
149      * @return Returns the index of the inserted data record.
150      */
151     virtual int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) override;
152 
153     /**
154      * @brief Calls the method of the Data ability.
155      *
156      * @param uri Indicates the Data ability of the method to call.
157      * @param method Indicates the method to call.
158      * @param arg Indicates the parameter of the String type.
159      * @param pacMap Defines a PacMap object for storing a series of values.
160      *
161      * @return Returns the call result.
162      */
163     virtual std::shared_ptr<AppExecFwk::PacMap> Call(
164         const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap) override;
165 
166     /**
167      * @brief Updates data records in the database.
168      *
169      * @param uri Indicates the path of data to update.
170      * @param value Indicates the data to update. This parameter can be null.
171      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
172      *
173      * @return Returns the number of data records updated.
174      */
175     virtual int Update(const Uri &uri, const NativeRdb::ValuesBucket &value,
176         const NativeRdb::DataAbilityPredicates &predicates) override;
177 
178     /**
179      * @brief Deletes one or more data records from the database.
180      *
181      * @param uri Indicates the path of the data to operate.
182      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
183      *
184      * @return Returns the number of data records deleted.
185      */
186     virtual int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) override;
187 
188     /**
189      * @brief Deletes one or more data records from the database.
190      *
191      * @param uri Indicates the path of data to query.
192      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
193      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
194      *
195      * @return Returns the query result.
196      */
197     virtual std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(
198         const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates) override;
199 
200     /**
201      * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
202      * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
203      *
204      * @param uri Indicates the URI of the data.
205      *
206      * @return Returns the MIME type that matches the data specified by uri.
207      */
208     std::string GetType(const Uri &uri) override;
209 
210     /**
211      * @brief Reloads data in the database.
212      *
213      * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
214      * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
215      * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
216      * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
217      *
218      * @return Returns true if the data is successfully reloaded; returns false otherwise.
219      */
220     bool Reload(const Uri &uri, const PacMap &extras) override;
221 
222     /**
223      * @brief Inserts multiple data records into the database.
224      *
225      * @param uri Indicates the path of the data to operate.
226      * @param values Indicates the data records to insert.
227      *
228      * @return Returns the number of data records inserted.
229      */
230     int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values) override;
231 
232     /**
233      * @brief Registers an observer to DataObsMgr specified by the given Uri.
234      *
235      * @param uri, Indicates the path of the data to operate.
236      * @param dataObserver, Indicates the IDataAbilityObserver object.
237      *
238      * @return Return true if success. otherwise return false.
239      */
240     bool ScheduleRegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) override;
241 
242     /**
243      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
244      *
245      * @param uri, Indicates the path of the data to operate.
246      * @param dataObserver, Indicates the IDataAbilityObserver object.
247      *
248      * @return Return true if success. otherwise return false.
249      */
250     bool ScheduleUnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) override;
251 
252     /**
253      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
254      *
255      * @param uri, Indicates the path of the data to operate.
256      *
257      * @return Return true if success. otherwise return false.
258      */
259     bool ScheduleNotifyChange(const Uri &uri) override;
260 
261     /**
262      * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
263      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
264      * context has changed. If you implement URI normalization for a Data ability, you must also implement
265      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
266      * any method that is called on the Data ability must require normalization verification and denormalization. The
267      * default implementation of this method returns null, indicating that this Data ability does not support URI
268      * normalization.
269      *
270      * @param uri Indicates the Uri object to normalize.
271      *
272      * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
273      */
274     Uri NormalizeUri(const Uri &uri) override;
275 
276     /**
277      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
278      * The default implementation of this method returns the original URI passed to it.
279      *
280      * @param uri uri Indicates the Uri object to denormalize.
281      *
282      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
283      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
284      * in the current environment.
285      */
286     Uri DenormalizeUri(const Uri &uri) override;
287 
288     /**
289      * @brief Performs batch operations on the database.
290      *
291      * @param operations Indicates a list of database operations on the database.
292      * @return Returns the result of each operation, in array.
293      */
294     std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch(
295         const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> &operations) override;
296 
297     /**
298      * ContinueAbility, call ContinueAbility() through proxy project,
299      * Notify continue ability.
300      *
301      * @param deviceId Target deviceId.
302      * @param versionCode Target bundle version.
303      * @return
304      */
305     void ContinueAbility(const std::string& deviceId, uint32_t versionCode) override;
306 
307     /**
308      * NotifyContinuationResult, call NotifyContinuationResult() through proxy project,
309      * Notify continuation result to ability.
310      *
311      * @param The continuation result.
312      * @return
313      */
314     void NotifyContinuationResult(int32_t result) override;
315 
316     /**
317      * Dump Ability Runner info.
318      *
319      * @param
320      * @return Ability Runner info.
321      */
322     void DumpAbilityInfo(const std::vector<std::string> &params, std::vector<std::string> &info) override;
323     void CallRequest() override;
324     int32_t CreateModalUIExtension(const Want &want) override;
325 
326     void OnExecuteIntent(const Want &want) override;
327 
328     /**
329      * @brief Update sessionToken.
330      * @param sessionToken The token of session.
331      */
332     void UpdateSessionToken(sptr<IRemoteObject> sessionToken) override;
333 
334 private:
335     bool WriteInterfaceToken(MessageParcel &data);
336     int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
337 
338 private:
339     static inline BrokerDelegator<AbilitySchedulerProxy> delegator_;
340 };
341 }  // namespace AAFwk
342 }  // namespace OHOS
343 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_SCHEDULE_PROXY_H
344