1 /*
2 * Copyright (c) 2023-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 #include "free_install_observer_manager.h"
16
17 #include "ability_manager_service.h"
18 #include "ability_util.h"
19
20 namespace OHOS {
21 namespace AAFwk {
FreeInstallObserverManager()22 FreeInstallObserverManager::FreeInstallObserverManager()
23 {}
24
~FreeInstallObserverManager()25 FreeInstallObserverManager::~FreeInstallObserverManager()
26 {}
27
AddObserver(int32_t recordId,const sptr<IFreeInstallObserver> & observer)28 int32_t FreeInstallObserverManager::AddObserver(int32_t recordId, const sptr<IFreeInstallObserver> &observer)
29 {
30 TAG_LOGD(AAFwkTag::FREE_INSTALL, "begin");
31 if (observer == nullptr) {
32 TAG_LOGE(AAFwkTag::FREE_INSTALL, "null observer");
33 return ERR_INVALID_VALUE;
34 }
35
36 {
37 std::lock_guard<ffrt::mutex> lock(observerLock_);
38 observerMap_[recordId] = observer;
39 }
40
41 if (!deathRecipient_) {
42 std::weak_ptr<FreeInstallObserverManager> thisWeakPtr(shared_from_this());
43 // add death recipient
44 deathRecipient_ =
45 new FreeInstallObserverRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
46 auto freeInstallObserverManager = thisWeakPtr.lock();
47 if (freeInstallObserverManager) {
48 freeInstallObserverManager->OnObserverDied(remote);
49 }
50 });
51 }
52
53 auto observerObj = observer->AsObject();
54 if (!observerObj || !observerObj->AddDeathRecipient(deathRecipient_)) {
55 TAG_LOGE(AAFwkTag::FREE_INSTALL, "AddDeathRecipient failed");
56 }
57
58 return ERR_OK;
59 }
60
RemoveObserver(const sptr<IFreeInstallObserver> & observer)61 int32_t FreeInstallObserverManager::RemoveObserver(const sptr<IFreeInstallObserver> &observer)
62 {
63 TAG_LOGD(AAFwkTag::FREE_INSTALL, "begin");
64 if (observer == nullptr) {
65 TAG_LOGE(AAFwkTag::FREE_INSTALL, "null observer");
66 return ERR_INVALID_VALUE;
67 }
68 std::lock_guard<ffrt::mutex> lock(observerLock_);
69 for (auto &item : observerMap_) {
70 if (item.second && item.second->AsObject() == observer->AsObject()) {
71 observerMap_.erase(item.first);
72 return ERR_OK;
73 }
74 }
75 TAG_LOGE(AAFwkTag::FREE_INSTALL, "observer null or removed");
76 return ERR_INVALID_VALUE;
77 }
78
OnInstallFinished(int32_t recordId,const std::string & bundleName,const std::string & abilityName,const std::string & startTime,const int & resultCode)79 void FreeInstallObserverManager::OnInstallFinished(int32_t recordId, const std::string &bundleName,
80 const std::string &abilityName, const std::string &startTime, const int &resultCode)
81 {
82 auto task = [weak = weak_from_this(), recordId, bundleName, abilityName, startTime, resultCode]() {
83 auto self = weak.lock();
84 if (self == nullptr) {
85 TAG_LOGE(AAFwkTag::FREE_INSTALL, "null self");
86 return;
87 }
88 TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnInstallFinished come");
89 self->HandleOnInstallFinished(recordId, bundleName, abilityName, startTime, resultCode);
90 };
91
92 auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
93 CHECK_POINTER_LOG(handler, "Fail to get Ability task handler.");
94 handler->SubmitTask(task);
95 }
96
OnInstallFinishedByUrl(int32_t recordId,const std::string & startTime,const std::string & url,const int & resultCode)97 void FreeInstallObserverManager::OnInstallFinishedByUrl(int32_t recordId, const std::string &startTime,
98 const std::string &url, const int &resultCode)
99 {
100 auto task = [weak = weak_from_this(), recordId, startTime, url, resultCode]() {
101 auto self = weak.lock();
102 if (self == nullptr) {
103 TAG_LOGE(AAFwkTag::FREE_INSTALL, "null self");
104 return;
105 }
106 TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnInstallFinishedByUrl come");
107 self->HandleOnInstallFinishedByUrl(recordId, startTime, url, resultCode);
108 };
109
110 auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
111 CHECK_POINTER_LOG(handler, "ability task handler get failed");
112 handler->SubmitTask(task);
113 }
114
HandleOnInstallFinished(int32_t recordId,const std::string & bundleName,const std::string & abilityName,const std::string & startTime,const int & resultCode)115 void FreeInstallObserverManager::HandleOnInstallFinished(int32_t recordId, const std::string &bundleName,
116 const std::string &abilityName, const std::string &startTime, const int &resultCode)
117 {
118 TAG_LOGD(AAFwkTag::FREE_INSTALL, "begin");
119 std::lock_guard<ffrt::mutex> lock(observerLock_);
120 auto iter = observerMap_.find(recordId);
121 if (iter != observerMap_.end() && iter->second != nullptr) {
122 (iter->second)->OnInstallFinished(bundleName, abilityName, startTime, resultCode);
123 }
124 }
125
HandleOnInstallFinishedByUrl(int32_t recordId,const std::string & startTime,const std::string & url,const int & resultCode)126 void FreeInstallObserverManager::HandleOnInstallFinishedByUrl(int32_t recordId, const std::string &startTime,
127 const std::string &url, const int &resultCode)
128 {
129 TAG_LOGD(AAFwkTag::FREE_INSTALL, "begin");
130 std::lock_guard<ffrt::mutex> lock(observerLock_);
131 auto iter = observerMap_.find(recordId);
132 if (iter != observerMap_.end() && iter->second != nullptr) {
133 (iter->second)->OnInstallFinishedByUrl(startTime, url, resultCode);
134 }
135 }
136
OnObserverDied(const wptr<IRemoteObject> & remote)137 void FreeInstallObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote)
138 {
139 TAG_LOGI(AAFwkTag::FREE_INSTALL, "called");
140 auto remoteObj = remote.promote();
141 if (remoteObj == nullptr) {
142 TAG_LOGE(AAFwkTag::FREE_INSTALL, "null observer");
143 return;
144 }
145 remoteObj->RemoveDeathRecipient(deathRecipient_);
146
147 std::lock_guard<ffrt::mutex> lock(observerLock_);
148 for (auto &item : observerMap_) {
149 if (item.second && item.second->AsObject() == remoteObj) {
150 observerMap_.erase(item.first);
151 return;
152 }
153 }
154 }
155
FreeInstallObserverRecipient(RemoteDiedHandler handler)156 FreeInstallObserverRecipient::FreeInstallObserverRecipient(RemoteDiedHandler handler) : handler_(handler)
157 {}
158
~FreeInstallObserverRecipient()159 FreeInstallObserverRecipient::~FreeInstallObserverRecipient()
160 {}
161
OnRemoteDied(const wptr<IRemoteObject> & remote)162 void FreeInstallObserverRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
163 {
164 TAG_LOGI(AAFwkTag::FREE_INSTALL, "called");
165 if (handler_) {
166 handler_(remote);
167 }
168 }
169 } // namespace AAFwk
170 } // namespace OHOS