• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "accesstoken_common_log.h"
17 #include "iremote_object.h"
18 #include "proxy_death_handler.h"
19 #include "proxy_death_recipient.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 
AddProxyStub(const sptr<IRemoteObject> & proxyStub,std::shared_ptr<ProxyDeathParam> & param)25 void ProxyDeathHandler::AddProxyStub(const sptr<IRemoteObject>& proxyStub,
26     std::shared_ptr<ProxyDeathParam>& param)
27 {
28     std::lock_guard lock(proxyLock_);
29     if (proxyStub == nullptr || param == nullptr) {
30         return;
31     }
32     if (proxyStubAndRecipientMap_.find(proxyStub) != proxyStubAndRecipientMap_.end()) {
33         LOGI(ATM_DOMAIN, ATM_TAG, "Proxy is found.");
34         return;
35     }
36     auto proxyDeathRecipient = sptr<ProxyDeathRecipient>::MakeSptr(this);
37     if (proxyDeathRecipient == nullptr) {
38         LOGE(ATM_DOMAIN, ATM_TAG, "Create proxy death recipient failed.");
39         return;
40     }
41     if (proxyStub->IsObjectDead()) {
42         LOGE(ATM_DOMAIN, ATM_TAG, "Remote stub is dead.");
43         return;
44     }
45     proxyStub->AddDeathRecipient(proxyDeathRecipient);
46     RecipientAndParam cur(proxyDeathRecipient, param);
47     proxyStubAndRecipientMap_.emplace(proxyStub, cur);
48     if (proxyStub->IsObjectDead()) {
49         proxyStubAndRecipientMap_.erase(proxyStub);
50         return;
51     }
52 }
53 
HandleRemoteDied(const sptr<IRemoteObject> & object)54 void ProxyDeathHandler::HandleRemoteDied(const sptr<IRemoteObject>& object)
55 {
56     if (object == nullptr) {
57         return;
58     }
59     std::lock_guard lock(proxyLock_);
60     auto iter = proxyStubAndRecipientMap_.find(object);
61     if (iter == proxyStubAndRecipientMap_.end()) {
62         LOGE(ATM_DOMAIN, ATM_TAG, "Cannot find object in map");
63         return;
64     }
65     object->RemoveDeathRecipient(iter->second.first);
66     ProcessProxyData(iter->second.second);
67     proxyStubAndRecipientMap_.erase(iter);
68 }
69 
ProcessProxyData(const std::shared_ptr<ProxyDeathParam> & param)70 void ProxyDeathHandler::ProcessProxyData(const std::shared_ptr<ProxyDeathParam>& param)
71 {
72     LOGI(ATM_DOMAIN, ATM_TAG, "called");
73     param->ProcessParam();
74 }
75 
ReleaseProxies()76 void ProxyDeathHandler::ReleaseProxies()
77 {
78     std::lock_guard lock(proxyLock_);
79     for (auto iter: proxyStubAndRecipientMap_) {
80         auto object = iter.first;
81         if (object != nullptr && !object->IsObjectDead()) {
82             object->RemoveDeathRecipient(iter.second.first);
83         }
84     }
85 }
86 
ReleaseProxyByParam(const std::shared_ptr<ProxyDeathParam> & param)87 void ProxyDeathHandler::ReleaseProxyByParam(const std::shared_ptr<ProxyDeathParam>& param)
88 {
89     std::lock_guard lock(proxyLock_);
90     for (auto iter = proxyStubAndRecipientMap_.begin(); iter != proxyStubAndRecipientMap_.end();) {
91         if (iter->second.second == nullptr || param == nullptr) {
92             ++iter;
93             continue;
94         }
95         if (iter->second.second->IsEqual(param.get())) {
96             auto object = iter->first;
97             if (object != nullptr && !object->IsObjectDead()) {
98                 object->RemoveDeathRecipient(iter->second.first);
99             }
100             iter = proxyStubAndRecipientMap_.erase(iter);
101         } else {
102             ++iter;
103         }
104     }
105 }
106 }  // namespace AccessToken
107 } // namespace Security
108 }  // namespace OHOS
109