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