1 /* 2 * Copyright (c) 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_EXTENSION_RECORD_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H 18 19 #include <atomic> 20 #include <map> 21 #include <memory> 22 #include <set> 23 #include <tuple> 24 25 #include "ability_record.h" 26 #include "extension_record.h" 27 #include "ui_extension_host_info.h" 28 #include "ui_extension_session_info.h" 29 30 namespace OHOS { 31 namespace AbilityRuntime { 32 class ExtensionRecordManager : public std::enable_shared_from_this<ExtensionRecordManager> { 33 public: 34 using ExtensionAbilityRecordMap = std::map<int32_t, std::shared_ptr<ExtensionRecord>>; 35 using PreLoadUIExtensionMapKey = std::tuple<std::string, std::string, std::string, std::string>; 36 using PreLoadUIExtensionMapType = 37 std::map<PreLoadUIExtensionMapKey, std::vector<std::shared_ptr<ExtensionRecord>>>; 38 explicit ExtensionRecordManager(const int32_t userId); 39 virtual ~ExtensionRecordManager(); 40 41 /** 42 * @brief Generate extension record id, if input id didn't exist, return it, else assign one. 43 * 44 * @param extensionRecordId Input extension record id. 45 * @return int32_t Generated extension record id. 46 */ 47 int32_t GenerateExtensionRecordId(const int32_t extensionRecordId); 48 49 /** 50 * @brief Add extension record by id, if record exist, replace it. 51 * 52 * @param extensionRecordId extension record id. 53 * @param record extension record. 54 */ 55 void AddExtensionRecord(const int32_t extensionRecordId, const std::shared_ptr<ExtensionRecord> &record); 56 57 /** 58 * @brief Remove extension record by id 59 * 60 * @param extensionRecordId extension record id. 61 */ 62 void RemoveExtensionRecord(const int32_t extensionRecordId); 63 64 /** 65 * @brief Add extension record to terminate list by id 66 * 67 * @param extensionRecordId extension record id. 68 */ 69 void AddExtensionRecordToTerminatedList(const int32_t extensionRecordId); 70 71 static bool IsBelongToManager(const AppExecFwk::AbilityInfo &abilityInfo); 72 73 /** 74 * @brief Get extensionList by pid. 75 * @param pid Process id. 76 * @param extensionList UIExtensionAbility name list. 77 */ 78 int32_t GetActiveUIExtensionList(const int32_t pid, std::vector<std::string> &extensionList); 79 80 /** 81 * @brief Get extensionList by bundleName. 82 * @param bundleName The application bundle name. 83 * @param extensionList UIExtensionAbility name list. 84 */ 85 int32_t GetActiveUIExtensionList(const std::string &bundleName, std::vector<std::string> &extensionList); 86 87 int32_t StartAbility(const AAFwk::AbilityRequest &abilityRequest); 88 89 int32_t CreateExtensionRecord(const AAFwk::AbilityRequest &abilityRequest, const std::string &hostBundleName, 90 std::shared_ptr<ExtensionRecord> &extensionRecord, int32_t &extensionRecordId); 91 92 bool IsPreloadExtensionRecord(const AAFwk::AbilityRequest &abilityRequest, 93 const std::string &hostBundleName, std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 94 95 int32_t AddPreloadUIExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> abilityRecord); 96 97 void RemoveAllPreloadUIExtensionRecord(PreLoadUIExtensionMapKey &preLoadUIExtensionInfo); 98 99 bool RemovePreloadUIExtensionRecord( 100 const std::tuple<std::string, std::string, std::string, std::string> extensionRecordMapKey); 101 102 bool RemovePreloadUIExtensionRecordById( 103 const std::tuple<std::string, std::string, std::string, std::string> extensionRecordMapKey, 104 int32_t extensionRecordId); 105 106 int32_t GetOrCreateExtensionRecord(const AAFwk::AbilityRequest &abilityRequest, const std::string &hostBundleName, 107 std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, bool &isLoaded); 108 109 std::shared_ptr<AAFwk::AbilityRecord> GetAbilityRecordBySessionInfo(const sptr<AAFwk::SessionInfo> &sessionInfo); 110 111 std::shared_ptr<AAFwk::AbilityRecord> GetUIExtensionRootHostInfo(const sptr<IRemoteObject> token); 112 113 int32_t GetUIExtensionSessionInfo(const sptr<IRemoteObject> token, UIExtensionSessionInfo &uiExtensionSessionInfo); 114 115 bool IsFocused( 116 int32_t extensionRecordId, const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &focusToken); 117 118 void LoadTimeout(int32_t extensionRecordId); 119 void ForegroundTimeout(int32_t extensionRecordId); 120 void BackgroundTimeout(int32_t extensionRecordId); 121 void TerminateTimeout(int32_t extensionRecordId); 122 123 int32_t GetHostBundleNameForExtensionId(int32_t extensionRecordId, std::string& hostBundleName); 124 void GetCallerTokenList(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, 125 std::list<sptr<IRemoteObject>> &callerList); 126 127 private: 128 inline std::shared_ptr<ExtensionRecord> GetExtensionRecordById(int32_t extensionRecordId); 129 130 private: 131 int32_t userId_; 132 static std::atomic_int32_t extensionRecordId_; 133 std::mutex mutex_; 134 std::set<int32_t> extensionRecordIdSet_; 135 ExtensionAbilityRecordMap extensionRecords_; 136 ExtensionAbilityRecordMap terminateRecords_; 137 std::mutex preloadUIExtensionMapMutex_; 138 PreLoadUIExtensionMapType preloadUIExtensionMap_; 139 140 void SetCachedFocusedCallerToken(int32_t extensionRecordId, sptr<IRemoteObject> &focusedCallerToken); 141 sptr<IRemoteObject> GetCachedFocusedCallerToken(int32_t extensionRecordId) const; 142 sptr<IRemoteObject> GetRootCallerTokenLocked( 143 int32_t extensionRecordId, const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord); 144 145 int32_t GetOrCreateExtensionRecordInner(const AAFwk::AbilityRequest &abilityRequest, 146 const std::string &hostBundleName, std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 147 148 int32_t GetExtensionRecord(const int32_t extensionRecordId, const std::string &hostBundleName, 149 std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 150 151 int32_t UpdateProcessName(const AAFwk::AbilityRequest &abilityRequest, std::shared_ptr<ExtensionRecord> &record); 152 153 bool IsHostSpecifiedProcessValid(const AAFwk::AbilityRequest &abilityRequest, 154 std::shared_ptr<ExtensionRecord> &record, const std::string &process); 155 }; 156 } // namespace AbilityRuntime 157 } // namespace OHOS 158 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H 159