1 /*
2 * Copyright (c) 2021-2024 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 #include "ability_record_mgr.h"
17 #include "hilog_tag_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
21 /**
22 * @brief Get the token witch is set to the AbilityRecordMgr.
23 *
24 * @return Returns the token which is set to the AbilityRecordMgr.
25 */
GetToken() const26 sptr<IRemoteObject> AbilityRecordMgr::GetToken() const
27 {
28 return tokens_;
29 }
30
31 /**
32 * @brief Set the token witch the app launched.
33 *
34 * @param token The token which the is launched by app.
35 */
SetToken(const sptr<IRemoteObject> & token)36 void AbilityRecordMgr::SetToken(const sptr<IRemoteObject> &token)
37 {
38 if (token == nullptr) {
39 TAG_LOGE(AAFwkTag::APPKIT, "application is nullptr");
40 return;
41 }
42 tokens_ = token;
43 }
44
45 /**
46 * @brief Set the eventRunner of abilitythread to the AbilityRecordMgr.
47 *
48 * @param eventRunner The runner of the abilitythread.
49 *
50 */
SetEventRunner(const std::shared_ptr<EventRunner> & eventRunner)51 void AbilityRecordMgr::SetEventRunner(const std::shared_ptr<EventRunner> &eventRunner)
52 {
53 if (eventRunner == nullptr) {
54 TAG_LOGE(AAFwkTag::APPKIT, "eventRunner is nullptr");
55 return;
56 }
57 sptr<IRemoteObject> token = GetToken();
58 if (token == nullptr) {
59 TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
60 return;
61 }
62
63 std::shared_ptr<AbilityLocalRecord> abilityInstance = GetAbilityItem(token);
64 if (abilityInstance != nullptr) {
65 abilityInstance->SetEventRunner(eventRunner);
66 } else {
67 TAG_LOGW(AAFwkTag::APPKIT, "ability record not exist");
68 }
69 }
70
71 /**
72 * @brief Save the token and abilityRecord to the AbilityRecordMgr.
73 *
74 * @param token The token which the abilityRecord belongs to.
75 * @param abilityRecord the abilityRecord witch contains the context info belong the the ability.
76 *
77 */
AddAbilityRecord(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityLocalRecord> & abilityRecord)78 void AbilityRecordMgr::AddAbilityRecord(
79 const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
80 {
81 if (token == nullptr) {
82 TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
83 return;
84 }
85
86 if (abilityRecord == nullptr) {
87 TAG_LOGE(AAFwkTag::APPKIT, "abilityRecord is nullptr");
88 return;
89 }
90
91 abilityRecords_[token] = abilityRecord;
92 }
93
94 /**
95 * @brief Remove the abilityRecord by token.
96 *
97 * @param token The token which the abilityRecord belongs to.
98 *
99 */
RemoveAbilityRecord(const sptr<IRemoteObject> & token)100 void AbilityRecordMgr::RemoveAbilityRecord(const sptr<IRemoteObject> &token)
101 {
102 if (token == nullptr) {
103 TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
104 return;
105 }
106 abilityRecords_.erase(token);
107 }
108
109 /**
110 * @brief Get the number of abilityRecords which the AbilityRecordMgr saved.
111 *
112 * @return Return the number of abilityRecords which the AbilityRecordMgr saved.
113 *
114 */
GetRecordCount() const115 int AbilityRecordMgr::GetRecordCount() const
116 {
117 return abilityRecords_.size();
118 }
119
120 /**
121 * @brief Get the abilityRecord by token.
122 *
123 * @param token The token which the abilityRecord belongs to.
124 *
125 */
GetAbilityItem(const sptr<IRemoteObject> & token) const126 std::shared_ptr<AbilityLocalRecord> AbilityRecordMgr::GetAbilityItem(const sptr<IRemoteObject> &token) const
127 {
128 if (token == nullptr) {
129 TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
130 return nullptr;
131 }
132
133 const auto &iter = abilityRecords_.find(token);
134 if (iter != abilityRecords_.end()) {
135 return iter->second;
136 }
137 TAG_LOGW(AAFwkTag::APPKIT, "the ability not found");
138 return nullptr;
139 }
140
141 /**
142 * @brief Get the all tokens in the abilityRecordMgr.
143 *
144 * @return all tokens in the abilityRecordMgr.
145 *
146 */
GetAllTokens()147 std::vector<sptr<IRemoteObject>> AbilityRecordMgr::GetAllTokens()
148 {
149 std::vector<sptr<IRemoteObject>> tokens;
150 for (std::map<sptr<IRemoteObject>, std::shared_ptr<AbilityLocalRecord>>::iterator it = abilityRecords_.begin();
151 it != abilityRecords_.end();
152 ++it) {
153 sptr<IRemoteObject> token = it->first;
154 tokens.emplace_back(token);
155 }
156 return tokens;
157 }
158 } // namespace AppExecFwk
159 } // namespace OHOS
160