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_THREAD_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_THREAD_H
18 
19 #include "ability_scheduler_stub.h"
20 #include "context.h"
21 #include "ohos_application.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 using AbilitySchedulerStub = OHOS::AAFwk::AbilitySchedulerStub;
26 class AbilityHandler;
27 class AbilityLocalRecord;
28 class AbilityThread : public AbilitySchedulerStub {
29 public:
30     /**
31      * @brief Default constructor used to create a AbilityThread instance.
32      */
33     AbilityThread() = default;
34     virtual ~AbilityThread() = default;
35 
36     /**
37      * @brief Attach The ability thread to the main process.
38      * @param application Indicates the main process.
39      * @param abilityRecord Indicates the abilityRecord.
40      * @param mainRunner The runner which main_thread holds.
41      * @param appContext the AbilityRuntime context
42      */
43     static void AbilityThreadMain(const std::shared_ptr<OHOSApplication> &application,
44         const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner,
45         const std::shared_ptr<AbilityRuntime::Context> &appContext);
46 
47     /**
48      * @brief Attach The ability thread to the main process.
49      * @param application Indicates the main process.
50      * @param abilityRecord Indicates the abilityRecord.
51      * @param appContext the AbilityRuntime context
52      */
53     static void AbilityThreadMain(const std::shared_ptr<OHOSApplication> &application,
54         const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
55         const std::shared_ptr<AbilityRuntime::Context> &appContext);
56 
57     /**
58      * @brief Attach The ability thread to the main process.
59      * @param application Indicates the main process.
60      * @param abilityRecord Indicates the abilityRecord.
61      * @param mainRunner The runner which main_thread holds.
62      * @param appContext the AbilityRuntime context
63      */
64     virtual void Attach(const std::shared_ptr<OHOSApplication> &application,
65         const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner,
66         const std::shared_ptr<AbilityRuntime::Context> &appContext) = 0;
67 
68     /**
69      * @brief Attach The ability thread to the main process.
70      * @param application Indicates the main process.
71      * @param abilityRecord Indicates the abilityRecord.
72      * @param appContext the AbilityRuntime context
73      */
74     virtual void Attach(const std::shared_ptr<OHOSApplication> &application,
75         const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
76         const std::shared_ptr<AbilityRuntime::Context> &appContext) = 0;
77 
78     /**
79      * @brief ScheduleUpdateConfiguration, scheduling update configuration.
80      * @param config Indicates the updated configuration information
81      */
82     virtual void ScheduleUpdateConfiguration(const Configuration &config) = 0;
83 
84     /**
85      * @brief notify this ability current memory level.
86      * @param level Current memory level
87      */
88     virtual void NotifyMemoryLevel(int32_t level) = 0;
89 
90     /**
91      * @brief  Provide operating system AbilityTransaction information to the observer
92      * @param want Indicates the structure containing Transaction information about the ability.
93      * @param targetState Indicates the lifecycle state.
94      * @param sessionInfo Indicates the session info.
95      */
96     bool ScheduleAbilityTransaction(
97         const Want &want, const LifeCycleStateInfo &targetState, sptr<SessionInfo> sessionInfo = nullptr) override;
98 
99     /**
100      * @brief Provide operating system ShareData information to the observer
101      * @param requestCode Indicates the Ability request code.
102      */
103     void ScheduleShareData(const int32_t &requestCode) override;
104 
105     /**
106      * @brief Provide operating system ConnectAbility information to the observer
107      * @param want Indicates the structure containing connect information about the ability.
108      */
109     void ScheduleConnectAbility(const Want &want) override;
110 
111     /**
112      * @brief Provide operating system DisconnectAbility information to the observer
113      * @param want Indicates the structure containing connect information about the ability.
114      */
115     void ScheduleDisconnectAbility(const Want &want) override;
116 
117     /**
118      * @brief Provide operating system CommandAbility information to the observer
119      * @param want Indicates the structure containing connect information about the ability.
120      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
121      * destroyed, and the value false indicates a normal startup.
122      * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by
123      * 1 every time the ability is started. For example, if the ability has been started for six times, the value of
124      * startId is 6.
125      */
126     void ScheduleCommandAbility(const Want &want, bool restart, int startId) override;
127 
128     /**
129      * @brief Schedule Command AbilityWindow
130      * @param want The Want object to command to.
131      * @param sessionInfo Indicates the session info.
132      * @param winCmd Indicates the WindowCommand of winCmd
133      */
134     void ScheduleCommandAbilityWindow(
135         const Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd) override;
136 
137     /**
138      * @brief Provide operating system PrepareTerminateAbility information to the observer
139      */
140     bool SchedulePrepareTerminateAbility() override;
141 
142     /**
143      * @brief Provide operating system SaveabilityState information to the observer
144      */
145     void ScheduleSaveAbilityState() override;
146 
147     /**
148      * @brief Provide operating system RestoreAbilityState information to the observer
149      * @param state Indicates resotre ability state used to dispatchRestoreAbilityState.
150      */
151     void ScheduleRestoreAbilityState(const PacMap &state) override;
152 
153     /**
154      * @brief Send the result code and data to be returned by this Page ability to the caller.
155      * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to
156      * receive the result set in the current method. This method can be called only after the ability has been
157      * initialized.
158      * @param requestCode Indicates the request code for send.
159      * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result
160      * code to identify an error.
161      * @param resultData Indicates the data returned after the ability is destroyed. You can define the data returned.
162      * This parameter can be null.
163      */
164     void SendResult(int requestCode, int resultCode, const Want &resultData) override;
165 
166     /**
167      * @brief Obtains the MIME types of files supported.
168      * @param uri Indicates the path of the files to obtain.
169      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
170      * @return Returns the matched MIME types. If there is no match, null is returned.
171      */
172     std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) override;
173 
174     /**
175      * @brief Opens a file in a specified remote path.
176      * @param uri Indicates the path of the file to open.
177      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
178      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
179      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
180      * or "rwt" for read and write access that truncates any existing file.
181      * @return Returns the file descriptor.
182      */
183     int OpenFile(const Uri &uri, const std::string &mode) override;
184 
185     /**
186      * @brief This is like openFile, open a file that need to be able to return sub-sections of files that often assets
187      * inside of their .hap.
188      * @param uri Indicates the path of the file to open.
189      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
190      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
191      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
192      * data, or "rwt" for read and write access that truncates any existing file.
193      * @return Returns the RawFileDescriptor object containing file descriptor.
194      */
195     int OpenRawFile(const Uri &uri, const std::string &mode) override;
196 
197     /**
198      * @brief Inserts a single data record into the database.
199      * @param uri Indicates the path of the data to operate.
200      * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
201      * @return Returns the index of the inserted data record.
202      */
203     int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) override;
204 
205     /**
206      * @brief Calls the method of the Data ability.
207      * @param uri Indicates the path of the data to operate.
208      * @param method Indicates the method to call
209      * @param arg Indicates the parameter of the String type.
210      * @param pacMap Defines a PacMap object for storing a series of values.
211      * @return Returns the call result.
212      */
213     std::shared_ptr<AppExecFwk::PacMap> Call(
214         const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap) override;
215 
216     /**
217      * @brief Updates data records in the database.
218      * @param uri Indicates the path of data to update.
219      * @param value Indicates the data to update. This parameter can be null.
220      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
221      * @return Returns the number of data records updated.
222      */
223     int Update(const Uri &uri, const NativeRdb::ValuesBucket &value,
224         const NativeRdb::DataAbilityPredicates &predicates) override;
225 
226     /**
227      * @brief Deletes one or more data records from the database.
228      * @param uri Indicates the path of the data to operate.
229      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
230      * @return Returns the number of data records deleted.
231      */
232     int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) override;
233 
234     /**
235      * @brief Deletes one or more data records from the database.
236      * @param uri Indicates the path of data to query.
237      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
238      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
239      * @return Returns the query result.
240      */
241     std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(
242         const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates) override;
243 
244     /**
245      * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
246      * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
247      * @param uri Indicates the URI of the data.
248      * @return Returns the MIME type that matches the data specified by uri.
249      */
250     std::string GetType(const Uri &uri) override;
251 
252     /**
253      * @brief Reloads data in the database.
254      * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
255      * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
256      * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
257      * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
258      * @return Returns true if the data is successfully reloaded; returns false otherwise.
259      */
260     bool Reload(const Uri &uri, const PacMap &extras) override;
261 
262     /**
263      * @brief Inserts multiple data records into the database.
264      * @param uri Indicates the path of the data to operate.
265      * @param values Indicates the data records to insert.
266      * @return Returns the number of data records inserted.
267      */
268     int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values) override;
269 
270     /**
271      * @brief continue ability to target device.
272      * @param deviceId target deviceId
273      * @param versionCode Target bundle version.
274      */
275     void ContinueAbility(const std::string &deviceId, uint32_t versionCode) override;
276 
277     /**
278      * @brief notify this ability continuation result.
279      * @param result: Continuation result
280      */
281     void NotifyContinuationResult(int32_t result) override;
282 
283     /**
284      * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
285      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
286      * context has changed. If you implement URI normalization for a Data ability, you must also implement
287      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
288      * any method that is called on the Data ability must require normalization verification and denormalization. The
289      * default implementation of this method returns null, indicating that this Data ability does not support URI
290      * normalization.
291      * @param uri Indicates the Uri object to normalize.
292      * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
293      */
294     Uri NormalizeUri(const Uri &uri) override;
295 
296     /**
297      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
298      * The default implementation of this method returns the original URI passed to it.
299      * @param uri uri Indicates the Uri object to denormalize.
300      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
301      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
302      * in the current environment.
303      */
304     Uri DenormalizeUri(const Uri &uri) override;
305 
306     /**
307      * @brief Registers an observer to DataObsMgr specified by the given Uri.
308      * @param uri Indicates the path of the data to operate.
309      * @param dataObserver Indicates the IDataAbilityObserver object.
310      */
311     bool ScheduleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) override;
312 
313     /**
314      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
315      * @param uri Indicates the path of the data to operate.
316      * @param dataObserver Indicates the IDataAbilityObserver object.
317      */
318     bool ScheduleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) override;
319 
320     /**
321      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
322      * @param uri Indicates the path of the data to operate.
323      */
324     bool ScheduleNotifyChange(const Uri &uri) override;
325 
326     /**
327      * @brief Dump ability runner info.
328      * @param params Indicates the params
329      * @param info ability runner info.
330      */
331     void DumpAbilityInfo(const std::vector<std::string> &params, std::vector<std::string> &info) override;
332 
333     /**
334      * @brief Call Request
335      */
336     void CallRequest() override;
337 
338     void OnExecuteIntent(const Want &want) override;
339 
340     /**
341      * @brief Execute Batch
342      * @param operations Indicates the operations
343      */
344     std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch(
345         const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> &operations) override;
346 
347     /**
348      * @brief Dump ability runner info.
349      * @param want Create modalUIExtension with want object.
350      */
351     int CreateModalUIExtension(const Want &want) override;
352 
353     /**
354      * @brief Update sessionToken.
355      * @param sessionToken The token of session.
356      */
357     void UpdateSessionToken(sptr<IRemoteObject> sessionToken) override;
358 
359     sptr<IRemoteObject> token_;
360     std::shared_ptr<AbilityHandler> abilityHandler_ = nullptr;
361     std::shared_ptr<EventRunner> runner_ = nullptr;
362 };
363 } // namespace AppExecFwk
364 } // namespace OHOS
365 #endif // OHOS_ABILITY_RUNTIME_ABILITY_THREAD_H
366