1 /*
2 * Copyright (c) 2022 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 "ability_manager_privacy_client.h"
16 #include "accesstoken_log.h"
17 #include "iservice_registry.h"
18 #include "privacy_error.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "AbilityManagerPrivacyClient"
27 };
28 } // namespace
29
GetInstance()30 AbilityManagerPrivacyClient& AbilityManagerPrivacyClient::GetInstance()
31 {
32 static AbilityManagerPrivacyClient instance;
33 return instance;
34 }
35
AbilityManagerPrivacyClient()36 AbilityManagerPrivacyClient::AbilityManagerPrivacyClient()
37 {}
38
~AbilityManagerPrivacyClient()39 AbilityManagerPrivacyClient::~AbilityManagerPrivacyClient()
40 {}
41
StartAbility(const AAFwk::Want & want,const sptr<IRemoteObject> & callerToken,int requestCode,int32_t userId)42 int32_t AbilityManagerPrivacyClient::StartAbility(
43 const AAFwk::Want &want, const sptr<IRemoteObject> &callerToken, int requestCode, int32_t userId)
44 {
45 auto proxy = GetProxy();
46 if (proxy == nullptr) {
47 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
48 return PrivacyError::ERR_SERVICE_ABNORMAL;
49 }
50 ACCESSTOKEN_LOG_INFO(LABEL, "Start ability %{public}s, userId:%{public}d",
51 want.GetElement().GetAbilityName().c_str(), userId);
52 return proxy->StartAbility(want, callerToken, userId, requestCode);
53 }
54
InitProxy()55 void AbilityManagerPrivacyClient::InitProxy()
56 {
57 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (sam == nullptr) {
59 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
60 return;
61 }
62 auto abilityManagerSa = sam->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
63 if (abilityManagerSa == nullptr) {
64 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null", ABILITY_MGR_SERVICE_ID);
65 return;
66 }
67
68 auto deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new AbilityManagerPrivacyDeathRecipient());
69 if (deathRecipient_ == nullptr) {
70 ACCESSTOKEN_LOG_ERROR(LABEL, "Create AbilityManagerPrivacyDeathRecipient failed");
71 return;
72 }
73
74 if (!abilityManagerSa->IsProxyObject()) {
75 ACCESSTOKEN_LOG_ERROR(LABEL, "Not proxy object");
76 return;
77 }
78 if (!abilityManagerSa->AddDeathRecipient(deathRecipient_)) {
79 ACCESSTOKEN_LOG_ERROR(LABEL, "Add death recipient failed");
80 return;
81 }
82
83 proxy_ = iface_cast<IAbilityManager>(abilityManagerSa);
84 if (proxy_ == nullptr) {
85 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
86 }
87 }
88
OnRemoteDiedHandle()89 void AbilityManagerPrivacyClient::OnRemoteDiedHandle()
90 {
91 std::lock_guard<std::mutex> lock(proxyMutex_);
92 proxy_ = nullptr;
93 }
94
GetProxy()95 sptr<IAbilityManager> AbilityManagerPrivacyClient::GetProxy()
96 {
97 std::lock_guard<std::mutex> lock(proxyMutex_);
98 if (proxy_ == nullptr) {
99 InitProxy();
100 }
101 return proxy_;
102 }
103 } // namespace AccessToken
104 } // namespace Security
105 } // namespace OHOS
106
107