• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_APPEXECFWK_ABILITY_THREAD_H
17 #define FOUNDATION_APPEXECFWK_ABILITY_THREAD_H
18 
19 #include "want.h"
20 #include "ability_manager_client.h"
21 #include "ability_manager_interface.h"
22 #include "ability.h"
23 #include "ability_local_record.h"
24 #include "context.h"
25 #include "extension_impl.h"
26 #include "ohos_application.h"
27 #include "ability_scheduler_stub.h"
28 #include "pac_map.h"
29 #include "ohos/aafwk/base/ipc_singleton.h"
30 #include "dummy_values_bucket.h"
31 #include "dummy_data_ability_predicates.h"
32 #include "dummy_result_set.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 using AbilitySchedulerStub = OHOS::AAFwk::AbilitySchedulerStub;
37 using LifeCycleStateInfo = OHOS::AAFwk::LifeCycleStateInfo;
38 class AbilityImpl;
39 class Ability;
40 class AbilityHandler;
41 class AbilityLocalRecord;
42 class ApplicationImpl;
43 class OHOSApplication;
44 class AbilityHandler;
45 class AbilityThread : public AbilitySchedulerStub {
46 public:
47     /**
48      * @brief Default constructor used to create a AbilityThread instance.
49      */
50     AbilityThread();
51     ~AbilityThread();
52 
53     /**
54      * @description: Attach The ability thread to the main process.
55      * @param application Indicates the main process.
56      * @param abilityRecord Indicates the abilityRecord.
57      * @param mainRunner The runner which main_thread holds.
58      * @param appContext the AbilityRuntime context
59      */
60     static void AbilityThreadMain(std::shared_ptr<OHOSApplication> &application,
61         const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner,
62         const std::shared_ptr<AbilityRuntime::Context> &appContext);
63 
64     /**
65      * @description: Attach The ability thread to the main process.
66      * @param application Indicates the main process.
67      * @param abilityRecord Indicates the abilityRecord.
68      * @param appContext the AbilityRuntime context
69      */
70     static void AbilityThreadMain(
71         std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
72         const std::shared_ptr<AbilityRuntime::Context> &appContext);
73 
74     /**
75      * @description: Attach The ability thread to the main process.
76      * @param application Indicates the main process.
77      * @param abilityRecord Indicates the abilityRecord.
78      * @param mainRunner The runner which main_thread holds.
79      */
80     void Attach(std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
81         const std::shared_ptr<EventRunner> &mainRunner, const std::shared_ptr<AbilityRuntime::Context> &appContext);
82 
83     /**
84      * @description: Attach The ability thread to the main process.
85      * @param application Indicates the main process.
86      * @param abilityRecord Indicates the abilityRecord.
87      * @param mainRunner The runner which main_thread holds.
88      */
89     void AttachExtension(std::shared_ptr<OHOSApplication> &application,
90         const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
91         const std::shared_ptr<EventRunner> &mainRunner);
92 
93     /**
94      * @description: Attach The ability thread to the main process.
95      * @param application Indicates the main process.
96      * @param abilityRecord Indicates the abilityRecord.
97      * @param mainRunner The runner which main_thread holds.
98      */
99     void AttachExtension(std::shared_ptr<OHOSApplication> &application,
100         const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
101 
102     void InitExtensionFlag(const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
103 
104     /**
105      * @description: Attach The ability thread to the main process.
106      * @param application Indicates the main process.
107      * @param abilityRecord Indicates the abilityRecord.
108      */
109     void Attach(
110         std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
111         const std::shared_ptr<AbilityRuntime::Context> &appContext);
112 
113     /**
114      * @description:  Provide operating system AbilityTransaction information to the observer
115      * @param want Indicates the structure containing Transaction information about the ability.
116      * @param lifeCycleStateInfo Indicates the lifecycle state.
117      */
118     void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState);
119 
120     /**
121      * @description:  Provide operating system ConnectAbility information to the observer
122      * @param  want Indicates the structure containing connect information about the ability.
123      */
124     void ScheduleConnectAbility(const Want &want);
125 
126     /**
127      * @description: Provide operating system ConnectAbility information to the observer
128      * @return  None
129      */
130     void ScheduleDisconnectAbility(const Want &want);
131 
132     /**
133      * @description: Provide operating system CommandAbility information to the observer
134      *
135      * @param want The Want object to command to.
136      *
137      * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
138      * destroyed, and the value false indicates a normal startup.
139      *
140      * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by
141      * 1 every time the ability is started. For example, if the ability has been started for six times, the value of
142      * startId is 6.
143      */
144     void ScheduleCommandAbility(const Want &want, bool restart, int startId);
145 
146     /**
147      * @description: Provide operating system SaveabilityState information to the observer
148      */
149     void ScheduleSaveAbilityState();
150 
151     /**
152      * @description:  Provide operating system RestoreAbilityState information to the observer
153      * @param state Indicates resotre ability state used to dispatchRestoreAbilityState.
154      */
155     void ScheduleRestoreAbilityState(const PacMap &state);
156 
157     /**
158      * @brief ScheduleUpdateConfiguration, scheduling update configuration.
159      */
160     void ScheduleUpdateConfiguration(const Configuration &config);
161 
162     /**
163      * @brief Send the result code and data to be returned by this Page ability to the caller.
164      * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to
165      * receive the result set in the current method. This method can be called only after the ability has been
166      * initialized.
167      *
168      * @param requestCode Indicates the request code for send.
169      * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result
170      * code to identify an error.
171      * @param want Indicates the data returned after the ability is destroyed. You can define the data returned. This
172      * parameter can be null.
173      */
174     void SendResult(int requestCode, int resultCode, const Want &resultData);
175 
176     /**
177      * @brief Obtains the MIME types of files supported.
178      *
179      * @param uri Indicates the path of the files to obtain.
180      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
181      *
182      * @return Returns the matched MIME types. If there is no match, null is returned.
183      */
184     std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter);
185 
186     /**
187      * @brief Opens a file in a specified remote path.
188      *
189      * @param uri Indicates the path of the file to open.
190      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
191      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
192      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
193      *  or "rwt" for read and write access that truncates any existing file.
194      *
195      * @return Returns the file descriptor.
196      */
197     int OpenFile(const Uri &uri, const std::string &mode);
198 
199     /**
200      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
201      * inside of their .hap.
202      *
203      * @param uri Indicates the path of the file to open.
204      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
205      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
206      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
207      * data, or "rwt" for read and write access that truncates any existing file.
208      *
209      * @return Returns the RawFileDescriptor object containing file descriptor.
210      */
211     int OpenRawFile(const Uri &uri, const std::string &mode);
212 
213     /**
214      * @brief Inserts a single data record into the database.
215      *
216      * @param uri Indicates the path of the data to operate.
217      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
218      *
219      * @return Returns the index of the inserted data record.
220      */
221     int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value);
222 
223     std::shared_ptr<AppExecFwk::PacMap> Call(
224         const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap);
225 
226     /**
227      * @brief Updates data records in the database.
228      *
229      * @param uri Indicates the path of data to update.
230      * @param value Indicates the data to update. This parameter can be null.
231      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
232      *
233      * @return Returns the number of data records updated.
234      */
235     int Update(const Uri &uri, const NativeRdb::ValuesBucket &value,
236         const NativeRdb::DataAbilityPredicates &predicates);
237 
238     /**
239      * @brief Deletes one or more data records from the database.
240      *
241      * @param uri Indicates the path of the data to operate.
242      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
243      *
244      * @return Returns the number of data records deleted.
245      */
246     int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates);
247 
248     /**
249      * @brief Deletes one or more data records from the database.
250      *
251      * @param uri Indicates the path of data to query.
252      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
253      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
254      *
255      * @return Returns the query result.
256      */
257     std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(
258         const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates);
259 
260     /**
261      * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
262      * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
263      *
264      * @param uri Indicates the URI of the data.
265      *
266      * @return Returns the MIME type that matches the data specified by uri.
267      */
268     std::string GetType(const Uri &uri);
269 
270     /**
271      * @brief Reloads data in the database.
272      *
273      * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
274      * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
275      * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
276      * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
277      *
278      * @return Returns true if the data is successfully reloaded; returns false otherwise.
279      */
280     bool Reload(const Uri &uri, const PacMap &extras);
281 
282     /**
283      * @brief Inserts multiple data records into the database.
284      *
285      * @param uri Indicates the path of the data to operate.
286      * @param values Indicates the data records to insert.
287      *
288      * @return Returns the number of data records inserted.
289      */
290     int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values);
291 
292 #ifdef SUPPORT_GRAPHICS
293     /**
294      * @brief notify multi window mode changed.
295      *
296      * @param winModeKey Indicates ability Window display mode.
297      * @param flag Indicates this ability has been enter this mode.
298      */
299     void NotifyMultiWinModeChanged(int32_t winModeKey, bool flag);
300 
301     /**
302      * @brief notify this ability is top active ability.
303      *
304      * @param flag true: Indicates this ability is top active ability
305      */
306     void NotifyTopActiveAbilityChanged(bool flag);
307 #endif
308 
309     /**
310      * @brief continue ability to target device.
311      *
312      * @param deviceId: target deviceId
313      */
314     void ContinueAbility(const std::string& deviceId);
315 
316     /**
317      * @brief notify this ability continuation result.
318      *
319      * @param result: Continuation result
320      */
321     void NotifyContinuationResult(int32_t result);
322 
323     /**
324      * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
325      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
326      * context has changed. If you implement URI normalization for a Data ability, you must also implement
327      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
328      * any method that is called on the Data ability must require normalization verification and denormalization. The
329      * default implementation of this method returns null, indicating that this Data ability does not support URI
330      * normalization.
331      *
332      * @param uri Indicates the Uri object to normalize.
333      *
334      * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
335      */
336     Uri NormalizeUri(const Uri &uri);
337 
338     /**
339      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
340      * The default implementation of this method returns the original URI passed to it.
341      *
342      * @param uri uri Indicates the Uri object to denormalize.
343      *
344      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
345      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
346      * in the current environment.
347      */
348     Uri DenormalizeUri(const Uri &uri);
349 
350     /**
351      * @brief Registers an observer to DataObsMgr specified by the given Uri.
352      *
353      * @param uri, Indicates the path of the data to operate.
354      * @param dataObserver, Indicates the IDataAbilityObserver object.
355      */
356     bool HandleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
357 
358     /**
359      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
360      *
361      * @param uri, Indicates the path of the data to operate.
362      * @param dataObserver, Indicates the IDataAbilityObserver object.
363      */
364     bool HandleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
365 
366     /**
367      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
368      *
369      * @param uri, Indicates the path of the data to operate.
370      */
371     bool HandleNotifyChange(const Uri &uri);
372 
373     /**
374      * @brief Registers an observer to DataObsMgr specified by the given Uri.
375      *
376      * @param uri, Indicates the path of the data to operate.
377      * @param dataObserver, Indicates the IDataAbilityObserver object.
378      */
379     bool ScheduleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
380 
381     /**
382      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
383      *
384      * @param uri, Indicates the path of the data to operate.
385      * @param dataObserver, Indicates the IDataAbilityObserver object.
386      */
387     bool ScheduleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
388 
389     /**
390      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
391      *
392      * @param uri, Indicates the path of the data to operate.
393      */
394     bool ScheduleNotifyChange(const Uri &uri);
395 
396     /**
397      * @brief Access authority verification.
398      *
399      * @return Returns true on success, others on failure.
400      */
401     bool CheckObsPermission();
402 
403 	/**
404      * @brief Dump ability runner info.
405      *
406      * @param runnerInfo ability runner info.
407      */
408     void DumpAbilityInfo(const std::vector<std::string> &params, std::vector<std::string> &info);
409 
410     sptr<IRemoteObject> CallRequest();
411 
412     std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch(
413         const std::vector<std::shared_ptr<DataAbilityOperation>> &operations);
414 private:
415     void DumpAbilityInfoInner(const std::vector<std::string> &params, std::vector<std::string> &info);
416     void DumpOtherInfo(std::vector<std::string> &info);
417     /**
418      * @description: Create the abilityname.
419      *
420      * @param abilityRecord Indicates the abilityRecord.
421      *
422      * @return Returns the abilityname.
423      *
424      */
425     std::string CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
426 
427     /**
428      * @description: Create and init contextDeal.
429      *
430      * @param application Indicates the main process.
431      * @param abilityRecord Indicates the abilityRecord.
432      * @param abilityObject Indicates the abilityObject.
433      *
434      * @return Returns the contextDeal.
435      *
436      */
437     std::shared_ptr<ContextDeal> CreateAndInitContextDeal(std::shared_ptr<OHOSApplication> &application,
438         const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<Context> &abilityObject);
439 
440     /**
441      * @description:  Handle the life cycle of Ability.
442      * @param want  Indicates the structure containing lifecycle information about the ability.
443      * @param lifeCycleStateInfo  Indicates the lifeCycleStateInfo.
444      */
445     void HandleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo);
446 
447     /**
448      * @description:  Handle the life cycle of Extension.
449      * @param want  Indicates the structure containing lifecycle information about the extension.
450      * @param lifeCycleStateInfo  Indicates the lifeCycleStateInfo.
451      */
452     void HandleExtensionTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo);
453 
454     /**
455      * @description:  Handle the current connection of Ability.
456      * @param want  Indicates the structure containing connection information about the ability.
457      */
458     void HandleConnectAbility(const Want &want);
459 
460     /**
461      * @description:  Handle the current disconnection of Ability.
462      */
463     void HandleDisconnectAbility(const Want &want);
464 
465     /**
466      * @brief Handle the current command of Ability.
467      *
468      * @param want The Want object to command to.
469      *
470      * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
471      * destroyed, and the value false indicates a normal startup.
472      *
473      * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by
474      * 1 every time the ability is started. For example, if the ability has been started for six times, the value of
475      * startId is 6.
476      */
477     void HandleCommandAbility(const Want &want, bool restart, int startId);
478 
479     /**
480      * @description:  Handle the current connection of Extension.
481      * @param want  Indicates the structure containing connection information about the extension.
482      */
483     void HandleConnectExtension(const Want &want);
484 
485     /**
486      * @description:  Handle the current disconnection of Extension.
487      */
488     void HandleDisconnectExtension(const Want &want);
489 
490     /**
491      * @brief Handle the current command of Extension.
492      *
493      * @param want The Want object to command to.
494      *
495      * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
496      * destroyed, and the value false indicates a normal startup.
497      *
498      * @param startId Indicates the number of times the Service extension has been started. The startId is incremented
499      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
500      * value of startId is 6.
501      */
502     void HandleCommandExtension(const Want &want, bool restart, int startId);
503 
504     /**
505      * @description: Handle the restoreAbility state.
506      * @param state  Indicates save ability state used to dispatchRestoreAbilityState.
507      */
508     void HandleRestoreAbilityState(const PacMap &state);
509 
510     /*
511      * @brief Handle the scheduling update configuration.
512      */
513     void HandleUpdateConfiguration(const Configuration &config);
514 
515     /**
516      * @brief Handle the scheduling update configuration of extension.
517      *
518      * @param config Configuration
519      */
520     void HandleExtensionUpdateConfiguration(const Configuration &config);
521 
522     std::shared_ptr<AbilityRuntime::AbilityContext> BuildAbilityContext(
523         const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
524         const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityRuntime::Context> &stageContext);
525 
526     std::shared_ptr<AbilityImpl> abilityImpl_ = nullptr;
527     sptr<IRemoteObject> token_;
528     std::shared_ptr<Ability> currentAbility_ = nullptr;
529     std::shared_ptr<AbilityRuntime::ExtensionImpl> extensionImpl_ = nullptr;
530     std::shared_ptr<AbilityRuntime::Extension> currentExtension_ = nullptr;
531     std::shared_ptr<AbilityHandler> abilityHandler_ = nullptr;
532     std::shared_ptr<EventRunner> runner_ = nullptr;
533     bool isExtension_ = false;
534 };
535 }  // namespace AppExecFwk
536 }  // namespace OHOS
537 #endif  // FOUNDATION_APPEXECFWK_ABILITY_THREAD_H
538