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/ui_extension_host_info.h" 28 #include "ui_extension/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 int32_t hostPid = AAFwk::DEFAULT_INVAL_VALUE); 92 93 bool IsPreloadExtensionRecord(const AAFwk::AbilityRequest &abilityRequest, 94 const std::string &hostBundleName, std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 95 96 int32_t AddPreloadUIExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> abilityRecord); 97 98 void RemoveAllPreloadUIExtensionRecord(PreLoadUIExtensionMapKey &preLoadUIExtensionInfo); 99 100 bool RemovePreloadUIExtensionRecord( 101 const std::tuple<std::string, std::string, std::string, std::string> extensionRecordMapKey); 102 103 bool RemovePreloadUIExtensionRecordById( 104 const std::tuple<std::string, std::string, std::string, std::string> &extensionRecordMapKey, 105 int32_t extensionRecordId); 106 107 int32_t GetOrCreateExtensionRecord(const AAFwk::AbilityRequest &abilityRequest, const std::string &hostBundleName, 108 std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, bool &isLoaded); 109 110 std::shared_ptr<AAFwk::AbilityRecord> GetAbilityRecordBySessionInfo(const sptr<AAFwk::SessionInfo> &sessionInfo); 111 112 std::shared_ptr<AAFwk::AbilityRecord> GetUIExtensionRootHostInfo(const sptr<IRemoteObject> token); 113 114 int32_t GetUIExtensionSessionInfo(const sptr<IRemoteObject> token, UIExtensionSessionInfo &uiExtensionSessionInfo); 115 116 bool IsFocused( 117 int32_t extensionRecordId, const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &focusToken); 118 119 void LoadTimeout(int32_t extensionRecordId); 120 void ForegroundTimeout(int32_t extensionRecordId); 121 void BackgroundTimeout(int32_t extensionRecordId); 122 void TerminateTimeout(int32_t extensionRecordId); 123 124 int32_t GetHostBundleNameForExtensionId(int32_t extensionRecordId, std::string& hostBundleName); 125 void GetCallerTokenList(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, 126 std::list<sptr<IRemoteObject>> &callerList); 127 128 /** 129 * Query preload uiextension record. 130 * 131 * @param element, The uiextension ElementName. 132 * @param moduleName, The uiextension moduleName. 133 * @param hostBundleName, The uiextension caller hostBundleName. 134 * @param recordNum, The returned count of uiextension. 135 * @return Returns ERR_OK on success, others on failure. 136 */ 137 int32_t QueryPreLoadUIExtensionRecord(const AppExecFwk::ElementName &element, 138 const std::string &moduleName, 139 const std::string &hostBundleName, 140 int32_t &recordNum); 141 142 private: 143 inline std::shared_ptr<ExtensionRecord> GetExtensionRecordById(int32_t extensionRecordId); 144 145 private: 146 int32_t userId_; 147 static std::atomic_int32_t extensionRecordId_; 148 std::set<int32_t> extensionRecordIdSet_; 149 std::mutex mutex_; 150 std::mutex preloadUIExtensionMapMutex_; 151 ExtensionAbilityRecordMap extensionRecords_; 152 ExtensionAbilityRecordMap terminateRecords_; 153 PreLoadUIExtensionMapType preloadUIExtensionMap_; 154 155 void SetCachedFocusedCallerToken(int32_t extensionRecordId, sptr<IRemoteObject> &focusedCallerToken); 156 sptr<IRemoteObject> GetCachedFocusedCallerToken(int32_t extensionRecordId) const; 157 sptr<IRemoteObject> GetRootCallerTokenLocked( 158 int32_t extensionRecordId, const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord); 159 160 int32_t GetOrCreateExtensionRecordInner(const AAFwk::AbilityRequest &abilityRequest, 161 const std::string &hostBundleName, std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 162 163 int32_t GetExtensionRecord(const int32_t extensionRecordId, const std::string &hostBundleName, 164 std::shared_ptr<ExtensionRecord> &extensionRecord, bool &isLoaded); 165 166 int32_t UpdateProcessName(const AAFwk::AbilityRequest &abilityRequest, std::shared_ptr<ExtensionRecord> &record); 167 168 bool IsHostSpecifiedProcessValid(const AAFwk::AbilityRequest &abilityRequest, 169 std::shared_ptr<ExtensionRecord> &record, const std::string &process); 170 }; 171 } // namespace AbilityRuntime 172 } // namespace OHOS 173 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H 174