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 "app_manager_privacy_client.h"
16 #include <unistd.h>
17
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "AppManagerPrivacyClient"
28 };
29 } // namespace
30
GetInstance()31 AppManagerPrivacyClient& AppManagerPrivacyClient::GetInstance()
32 {
33 static AppManagerPrivacyClient instance;
34 return instance;
35 }
36
AppManagerPrivacyClient()37 AppManagerPrivacyClient::AppManagerPrivacyClient()
38 {}
39
~AppManagerPrivacyClient()40 AppManagerPrivacyClient::~AppManagerPrivacyClient()
41 {}
42
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)43 int32_t AppManagerPrivacyClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer)
44 {
45 if (observer == nullptr) {
46 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: callback is nullptr");
47 return -1;
48 }
49 auto proxy = GetProxy();
50 if (proxy == nullptr) {
51 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
52 return -1;
53 }
54 std::vector<std::string> bundleNameList;
55 return proxy->RegisterApplicationStateObserver(observer, bundleNameList);
56 }
57
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)58 int32_t AppManagerPrivacyClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
59 {
60 if (observer == nullptr) {
61 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: callback is nullptr");
62 return -1;
63 }
64 auto proxy = GetProxy();
65 if (proxy == nullptr) {
66 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
67 return -1;
68 }
69 return proxy->UnregisterApplicationStateObserver(observer);
70 }
71
GetForegroundApplications(std::vector<AppStateData> & list)72 int32_t AppManagerPrivacyClient::GetForegroundApplications(std::vector<AppStateData>& list)
73 {
74 auto proxy = GetProxy();
75 if (proxy == nullptr) {
76 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
77 return -1;
78 }
79 return proxy->GetForegroundApplications(list);
80 }
81
InitProxy()82 void AppManagerPrivacyClient::InitProxy()
83 {
84 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85 if (sam == nullptr) {
86 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
87 return;
88 }
89 auto appManagerSa = sam->GetSystemAbility(APP_MGR_SERVICE_ID);
90 if (appManagerSa == nullptr) {
91 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
92 AUDIO_POLICY_SERVICE_ID);
93 return;
94 }
95
96 serviceDeathObserver_ = new (std::nothrow) AppMgrDeathRecipient();
97 if (serviceDeathObserver_ != nullptr) {
98 appManagerSa->AddDeathRecipient(serviceDeathObserver_);
99 }
100
101 proxy_ = iface_cast<IAppMgr>(appManagerSa);
102 if (proxy_ == nullptr) {
103 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
104 }
105 }
106
OnRemoteDiedHandle()107 void AppManagerPrivacyClient::OnRemoteDiedHandle()
108 {
109 std::lock_guard<std::mutex> lock(proxyMutex_);
110 proxy_ = nullptr;
111 }
112
GetProxy()113 sptr<IAppMgr> AppManagerPrivacyClient::GetProxy()
114 {
115 std::lock_guard<std::mutex> lock(proxyMutex_);
116 if (proxy_ == nullptr) {
117 InitProxy();
118 }
119 return proxy_;
120 }
121 } // namespace AccessToken
122 } // namespace Security
123 } // namespace OHOS
124
125