• 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 "ability_manager_adapter.h"
17 #include "access_token_error.h"
18 #include "accesstoken_common_log.h"
19 #include <iremote_proxy.h>
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace Security {
26 namespace AccessToken {
27 namespace {
28 const int32_t DEFAULT_INVAL_VALUE = -1;
29 const std::u16string ABILITY_MGR_DESCRIPTOR = u"ohos.aafwk.AbilityManager";
30 constexpr const char* BUNDLE_NAME = "bundleName";
31 constexpr const char* APP_INDEX = "appIndex";
32 constexpr const char* USER_ID = "userId";
33 constexpr const char* CALLER_TOKENID = "callerTokenId";
34 constexpr const char* RESOURCE_KEY = "ohos.sensitive.resource";
35 }
36 using namespace AAFwk;
GetInstance()37 AbilityManagerAdapter& AbilityManagerAdapter::GetInstance()
38 {
39     static AbilityManagerAdapter *instance = new (std::nothrow) AbilityManagerAdapter();
40     return *instance;
41 }
42 
AbilityManagerAdapter()43 AbilityManagerAdapter::AbilityManagerAdapter()
44 {}
45 
~AbilityManagerAdapter()46 AbilityManagerAdapter::~AbilityManagerAdapter()
47 {}
48 
AbilityManagerConvertWant(const InnerWant & innerWant,AAFwk::Want & want)49 static void AbilityManagerConvertWant(const InnerWant &innerWant, AAFwk::Want &want)
50 {
51     if (innerWant.bundleName != std::nullopt && innerWant.abilityName != std::nullopt) {
52         want.SetElementName(innerWant.bundleName.value(), innerWant.abilityName.value());
53     }
54     if (innerWant.hapBundleName != std::nullopt) {
55         want.SetParam(BUNDLE_NAME, innerWant.hapBundleName.value());
56     }
57     if (innerWant.hapAppIndex != std::nullopt) {
58         want.SetParam(APP_INDEX, innerWant.hapAppIndex.value());
59     }
60     if (innerWant.hapUserID != std::nullopt) {
61         want.SetParam(USER_ID, innerWant.hapUserID.value());
62     }
63     if (innerWant.callerTokenId != std::nullopt) {
64         want.SetParam(CALLER_TOKENID, std::to_string(innerWant.callerTokenId.value()));
65     }
66     if (innerWant.resource != std::nullopt) {
67         want.SetParam(RESOURCE_KEY, innerWant.resource.value());
68     }
69 }
70 
StartAbility(const InnerWant & innerWant,const sptr<IRemoteObject> & callerToken)71 int32_t AbilityManagerAdapter::StartAbility(const InnerWant &innerWant, const sptr<IRemoteObject> &callerToken)
72 {
73     auto abms = GetProxy();
74     if (abms == nullptr) {
75         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to GetProxy.");
76         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
77     }
78 
79     AAFwk::Want want;
80     AbilityManagerConvertWant(innerWant, want);
81 
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85 
86     if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
87         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write WriteInterfaceToken.");
88         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
89     }
90 
91     if (!data.WriteParcelable(&want)) {
92         LOGE(ATM_DOMAIN, ATM_TAG, "Want write failed.");
93         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
94     }
95     if (!data.WriteInt32(DEFAULT_INVAL_VALUE)) {
96         LOGE(ATM_DOMAIN, ATM_TAG, "UserId write failed.");
97         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
98     }
99     if (!data.WriteInt32(DEFAULT_INVAL_VALUE)) {
100         LOGE(ATM_DOMAIN, ATM_TAG, "RequestCode write failed.");
101         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
102     }
103     int32_t error = abms->SendRequest(static_cast<uint32_t>(AbilityManagerAdapter::Message::START_ABILITY),
104         data, reply, option);
105     if (error != NO_ERROR) {
106         LOGE(ATM_DOMAIN, ATM_TAG, "SendRequest error: %{public}d", error);
107         return error;
108     }
109     return reply.ReadInt32();
110 }
111 
KillProcessForPermissionUpdate(uint32_t accessTokenId)112 int32_t AbilityManagerAdapter::KillProcessForPermissionUpdate(uint32_t accessTokenId)
113 {
114     auto abms = GetProxy();
115     if (abms == nullptr) {
116         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to GetProxy.");
117         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
118     }
119 
120     MessageParcel data;
121     MessageParcel reply;
122     MessageOption option;
123 
124     if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
125         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write WriteInterfaceToken.");
126         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
127     }
128     if (!data.WriteUint32(accessTokenId)) {
129         LOGE(ATM_DOMAIN, ATM_TAG, "WriteUint32 failed.");
130         return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
131     }
132     int32_t error = abms->SendRequest(static_cast<uint32_t>(
133         AbilityManagerAdapter::Message::KILL_PROCESS_FOR_PERMISSION_UPDATE), data, reply, option);
134     if (error != NO_ERROR) {
135         LOGE(ATM_DOMAIN, ATM_TAG, "SendRequest error: %{public}d", error);
136         return error;
137     }
138     return reply.ReadInt32();
139 }
140 
InitProxy()141 void AbilityManagerAdapter::InitProxy()
142 {
143     if (proxy_ != nullptr && (!proxy_->IsObjectDead())) {
144         return;
145     }
146     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
147     if (systemManager == nullptr) {
148         LOGE(ATM_DOMAIN, ATM_TAG, "Fail to get system ability registry.");
149         return;
150     }
151     sptr<IRemoteObject> remoteObj = systemManager->CheckSystemAbility(ABILITY_MGR_SERVICE_ID);
152     if (remoteObj == nullptr) {
153         LOGE(ATM_DOMAIN, ATM_TAG, "Fail to connect ability manager service.");
154         return;
155     }
156 
157     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) AbilityMgrDeathRecipient());
158     if (deathRecipient_ == nullptr) {
159         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to create AbilityMgrDeathRecipient!");
160         return;
161     }
162     if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(deathRecipient_))) {
163         LOGE(ATM_DOMAIN, ATM_TAG, "Add death recipient to AbilityManagerService failed.");
164         return;
165     }
166     proxy_ = remoteObj;
167 }
168 
GetProxy()169 sptr<IRemoteObject> AbilityManagerAdapter::GetProxy()
170 {
171     std::lock_guard<std::mutex> lock(proxyMutex_);
172     if (proxy_ == nullptr || proxy_->IsObjectDead()) {
173         InitProxy();
174     }
175     return proxy_;
176 }
177 
ReleaseProxy(const wptr<IRemoteObject> & remote)178 void AbilityManagerAdapter::ReleaseProxy(const wptr<IRemoteObject>& remote)
179 {
180     std::lock_guard<std::mutex> lock(proxyMutex_);
181     if ((proxy_ != nullptr) && (proxy_ == remote.promote())) {
182         proxy_->RemoveDeathRecipient(deathRecipient_);
183         proxy_ = nullptr;
184         deathRecipient_ = nullptr;
185     }
186 }
187 
OnRemoteDied(const wptr<IRemoteObject> & remote)188 void AbilityManagerAdapter::AbilityMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
189 {
190     LOGE(ATM_DOMAIN, ATM_TAG, "AbilityMgrDeathRecipient handle remote died.");
191     AbilityManagerAdapter::GetInstance().ReleaseProxy(remote);
192 }
193 } // namespace AccessToken
194 } // namespace Security
195 } // namespace OHOS
196