1 /* 2 * Copyright (c) 2021 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_impl.h" 23 #include "ability.h" 24 #include "ability_local_record.h" 25 #include "context.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 using DummyConfiguration = OHOS::AAFwk::DummyConfiguration; 39 class AbilityImpl; 40 class Ability; 41 class AbilityHandler; 42 class AbilityLocalRecord; 43 class ApplicationImpl; 44 class OHOSApplication; 45 class AbilityHandler; 46 class AbilityThread : public AbilitySchedulerStub { 47 public: 48 /** 49 * @brief Default constructor used to create a AbilityThread instance. 50 */ 51 AbilityThread(); 52 ~AbilityThread(); 53 54 /** 55 * @description: Attach The ability thread to the main process. 56 * @param application Indicates the main process. 57 * @param abilityRecord Indicates the abilityRecord. 58 * @param mainRunner The runner which main_thread holds. 59 */ 60 static void AbilityThreadMain(std::shared_ptr<OHOSApplication> &application, 61 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner); 62 63 /** 64 * @description: Attach The ability thread to the main process. 65 * @param application Indicates the main process. 66 * @param abilityRecord Indicates the abilityRecord. 67 */ 68 static void AbilityThreadMain( 69 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 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); 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 */ 85 void Attach( 86 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 87 88 /** 89 * @description: Provide operating system AbilityTransaction information to the observer 90 * @param want Indicates the structure containing Transaction information about the ability. 91 * @param lifeCycleStateInfo Indicates the lifecycle state. 92 */ 93 void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState); 94 95 /** 96 * @description: Provide operating system ConnectAbility information to the observer 97 * @param want Indicates the structure containing connect information about the ability. 98 */ 99 void ScheduleConnectAbility(const Want &want); 100 101 /** 102 * @description: Provide operating system ConnectAbility information to the observer 103 * @return None 104 */ 105 void ScheduleDisconnectAbility(const Want &want); 106 107 /** 108 * @description: Provide operating system CommandAbility information to the observer 109 * 110 * @param want The Want object to command to. 111 * 112 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 113 * destroyed, and the value false indicates a normal startup. 114 * 115 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 116 * 1 every time the ability is started. For example, if the ability has been started for six times, the value of 117 * startId is 6. 118 */ 119 void ScheduleCommandAbility(const Want &want, bool restart, int startId); 120 121 /** 122 * @description: Provide operating system SaveabilityState information to the observer 123 * @param state Indicates save ability state used to dispatch. 124 */ 125 void ScheduleSaveAbilityState(PacMap &state); 126 127 /** 128 * @description: Provide operating system RestoreAbilityState information to the observer 129 * @param state Indicates resotre ability state used to dispatchRestoreAbilityState. 130 */ 131 void ScheduleRestoreAbilityState(const PacMap &state); 132 133 /** 134 * @brief ScheduleUpdateConfiguration, scheduling update configuration. 135 */ 136 void ScheduleUpdateConfiguration(const DummyConfiguration &config); 137 138 /** 139 * @brief Send the result code and data to be returned by this Page ability to the caller. 140 * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to 141 * receive the result set in the current method. This method can be called only after the ability has been 142 * initialized. 143 * 144 * @param requestCode Indicates the request code for send. 145 * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result 146 * code to identify an error. 147 * @param want Indicates the data returned after the ability is destroyed. You can define the data returned. This 148 * parameter can be null. 149 */ 150 void SendResult(int requestCode, int resultCode, const Want &resultData); 151 152 /** 153 * @brief Obtains the MIME types of files supported. 154 * 155 * @param uri Indicates the path of the files to obtain. 156 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 157 * 158 * @return Returns the matched MIME types. If there is no match, null is returned. 159 */ 160 std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter); 161 162 /** 163 * @brief Opens a file in a specified remote path. 164 * 165 * @param uri Indicates the path of the file to open. 166 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 167 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 168 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 169 * or "rwt" for read and write access that truncates any existing file. 170 * 171 * @return Returns the file descriptor. 172 */ 173 int OpenFile(const Uri &uri, const std::string &mode); 174 175 /** 176 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 177 * inside of their .hap. 178 * 179 * @param uri Indicates the path of the file to open. 180 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 181 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 182 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 183 * data, or "rwt" for read and write access that truncates any existing file. 184 * 185 * @return Returns the RawFileDescriptor object containing file descriptor. 186 */ 187 int OpenRawFile(const Uri &uri, const std::string &mode); 188 189 /** 190 * @brief Inserts a single data record into the database. 191 * 192 * @param uri Indicates the path of the data to operate. 193 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 194 * 195 * @return Returns the index of the inserted data record. 196 */ 197 int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value); 198 199 /** 200 * @brief Updates data records in the database. 201 * 202 * @param uri Indicates the path of data to update. 203 * @param value Indicates the data to update. This parameter can be null. 204 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 205 * 206 * @return Returns the number of data records updated. 207 */ 208 int Update(const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates); 209 210 /** 211 * @brief Deletes one or more data records from the database. 212 * 213 * @param uri Indicates the path of the data to operate. 214 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 215 * 216 * @return Returns the number of data records deleted. 217 */ 218 int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates); 219 220 /** 221 * @brief Deletes one or more data records from the database. 222 * 223 * @param uri Indicates the path of data to query. 224 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 225 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 226 * 227 * @return Returns the query result. 228 */ 229 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query( 230 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates); 231 232 /** 233 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be 234 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. 235 * 236 * @param uri Indicates the URI of the data. 237 * 238 * @return Returns the MIME type that matches the data specified by uri. 239 */ 240 std::string GetType(const Uri &uri); 241 242 /** 243 * @brief Reloads data in the database. 244 * 245 * @param uri Indicates the position where the data is to reload. This parameter is mandatory. 246 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This 247 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across 248 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object. 249 * 250 * @return Returns true if the data is successfully reloaded; returns false otherwise. 251 */ 252 bool Reload(const Uri &uri, const PacMap &extras); 253 254 /** 255 * @brief Inserts multiple data records into the database. 256 * 257 * @param uri Indicates the path of the data to operate. 258 * @param values Indicates the data records to insert. 259 * 260 * @return Returns the number of data records inserted. 261 */ 262 int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values); 263 264 /** 265 * @brief notify multi window mode changed. 266 * 267 * @param winModeKey Indicates ability Window display mode. 268 * @param flag Indicates this ability has been enter this mode. 269 */ 270 void NotifyMultiWinModeChanged(int32_t winModeKey, bool flag); 271 272 /** 273 * @brief notify this ability is top active ability. 274 * 275 * @param flag true: Indicates this ability is top active ability 276 */ 277 void NotifyTopActiveAbilityChanged(bool flag); 278 279 /** 280 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 281 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the 282 * context has changed. If you implement URI normalization for a Data ability, you must also implement 283 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 284 * any method that is called on the Data ability must require normalization verification and denormalization. The 285 * default implementation of this method returns null, indicating that this Data ability does not support URI 286 * normalization. 287 * 288 * @param uri Indicates the Uri object to normalize. 289 * 290 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 291 */ 292 Uri NormalizeUri(const Uri &uri); 293 294 /** 295 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 296 * The default implementation of this method returns the original URI passed to it. 297 * 298 * @param uri uri Indicates the Uri object to denormalize. 299 * 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); 305 306 /** 307 * @brief Registers an observer to DataObsMgr specified by the given Uri. 308 * 309 * @param uri, Indicates the path of the data to operate. 310 * @param dataObserver, Indicates the IDataAbilityObserver object. 311 */ 312 bool HandleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 313 314 315 /** 316 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 317 * 318 * @param uri, Indicates the path of the data to operate. 319 * @param dataObserver, Indicates the IDataAbilityObserver object. 320 */ 321 bool HandleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 322 323 /** 324 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 325 * 326 * @param uri, Indicates the path of the data to operate. 327 */ 328 bool HandleNotifyChange(const Uri &uri); 329 330 /** 331 * @brief Registers an observer to DataObsMgr specified by the given Uri. 332 * 333 * @param uri, Indicates the path of the data to operate. 334 * @param dataObserver, Indicates the IDataAbilityObserver object. 335 */ 336 bool ScheduleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 337 338 /** 339 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 340 * 341 * @param uri, Indicates the path of the data to operate. 342 * @param dataObserver, Indicates the IDataAbilityObserver object. 343 */ 344 bool ScheduleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 345 346 /** 347 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 348 * 349 * @param uri, Indicates the path of the data to operate. 350 */ 351 bool ScheduleNotifyChange(const Uri &uri); 352 353 /** 354 * @brief Access authority verification. 355 * 356 * @return Returns true on success, others on failure. 357 */ 358 bool CheckObsPermission(); 359 360 std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> &operations); 361 private: 362 /** 363 * @description: Create the abilityname. 364 * 365 * @param abilityRecord Indicates the abilityRecord. 366 * 367 * @return Returns the abilityname. 368 * 369 */ 370 std::string CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 371 372 /** 373 * @description: Create and init contextDeal. 374 * 375 * @param application Indicates the main process. 376 * @param abilityRecord Indicates the abilityRecord. 377 * @param abilityObject Indicates the abilityObject. 378 * 379 * @return Returns the contextDeal. 380 * 381 */ 382 std::shared_ptr<ContextDeal> CreateAndInitContextDeal(std::shared_ptr<OHOSApplication> &application, 383 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<Context> &abilityObject); 384 385 /** 386 * @description: Handle the life cycle of Ability. 387 * @param want Indicates the structure containing lifecycle information about the ability. 388 * @param lifeCycleStateInfo Indicates the lifeCycleStateInfo. 389 */ 390 void HandleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo); 391 392 /** 393 * @description: Handle the current connection of Ability. 394 * @param want Indicates the structure containing connection information about the ability. 395 */ 396 void HandleConnectAbility(const Want &want); 397 398 /** 399 * @description: Handle the current disconnection of Ability. 400 */ 401 void HandleDisconnectAbility(const Want &want); 402 403 /** 404 * @brief Handle the current commadn of Ability. 405 * 406 * @param want The Want object to command to. 407 * 408 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 409 * destroyed, and the value false indicates a normal startup. 410 * 411 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 412 * 1 every time the ability is started. For example, if the ability has been started for six times, the value of 413 * startId is 6. 414 */ 415 void HandleCommandAbility(const Want &want, bool restart, int startId); 416 417 /** 418 * @description: Handle the SaveAbility state. 419 * @param state Indicates save ability state used to dispatchSaveAbilityState. 420 */ 421 void HandleSaveAbilityState(PacMap &state); 422 423 /** 424 * @description: Handle the restoreAbility state. 425 * @param state Indicates save ability state used to dispatchRestoreAbilityState. 426 */ 427 void HandleRestoreAbilityState(const PacMap &state); 428 429 /* 430 * @brief Handle the scheduling update configuration. 431 */ 432 void HandleUpdateConfiguration(const DummyConfiguration &config); 433 434 std::shared_ptr<AbilityImpl> abilityImpl_ = nullptr; 435 sptr<IRemoteObject> token_; 436 std::shared_ptr<Ability> currentAbility_ = nullptr; 437 std::shared_ptr<AbilityHandler> abilityHandler_ = nullptr; 438 std::shared_ptr<EventRunner> runner_ = nullptr; 439 }; 440 } // namespace AppExecFwk 441 } // namespace OHOS 442 #endif // FOUNDATION_APPEXECFWK_ABILITY_THREAD_H