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
16 #include "app_manager_access_proxy.h"
17 #include "accesstoken_log.h"
18
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AppManagerAccessProxy"};
24 static constexpr int32_t ERROR = -1;
25 constexpr int32_t CYCLE_LIMIT = 1000;
26 }
27
GetAmsMgr()28 sptr<IAmsMgr> AppManagerAccessProxy::GetAmsMgr()
29 {
30 MessageParcel data;
31 MessageParcel reply;
32 MessageOption option;
33 if (!data.WriteInterfaceToken(GetDescriptor())) {
34 return nullptr;
35 }
36 sptr<IRemoteObject> remote = Remote();
37 if (remote == nullptr) {
38 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
39 return nullptr;
40 }
41 int32_t error = remote->SendRequest(
42 static_cast<uint32_t>(IAppMgr::Message::APP_GET_MGR_INSTANCE), data, reply, option);
43 if (error != ERR_NONE) {
44 ACCESSTOKEN_LOG_ERROR(LABEL, "GetAmsMgr failed, error: %{public}d", error);
45 return nullptr;
46 }
47 sptr<IRemoteObject> object = reply.ReadRemoteObject();
48 sptr<IAmsMgr> amsMgr = new AmsManagerAccessProxy(object);
49 if (!amsMgr) {
50 ACCESSTOKEN_LOG_ERROR(LABEL, "Ability manager service instance is nullptr. ");
51 return nullptr;
52 }
53 return amsMgr;
54 }
55
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)56 int32_t AppManagerAccessProxy::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer,
57 const std::vector<std::string>& bundleNameList)
58 {
59 MessageParcel data;
60 MessageParcel reply;
61 MessageOption option;
62 if (!data.WriteInterfaceToken(GetDescriptor())) {
63 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
64 return ERROR;
65 }
66 if (!data.WriteRemoteObject(observer->AsObject())) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "Observer write failed.");
68 return ERROR;
69 }
70 if (!data.WriteStringVector(bundleNameList)) {
71 ACCESSTOKEN_LOG_ERROR(LABEL, "BundleNameList write failed.");
72 return ERROR;
73 }
74 sptr<IRemoteObject> remote = Remote();
75 if (remote == nullptr) {
76 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
77 return ERROR;
78 }
79 int32_t error = remote->SendRequest(
80 static_cast<uint32_t>(IAppMgr::Message::REGISTER_APPLICATION_STATE_OBSERVER), data, reply, option);
81 if (error != ERR_NONE) {
82 ACCESSTOKEN_LOG_ERROR(LABEL, "RegisterAppStatus failed, error: %{public}d", error);
83 return ERROR;
84 }
85 return reply.ReadInt32();
86 }
87
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)88 int32_t AppManagerAccessProxy::UnregisterApplicationStateObserver(
89 const sptr<IApplicationStateObserver>& observer)
90 {
91 MessageParcel data;
92 MessageParcel reply;
93 MessageOption option;
94 if (!data.WriteInterfaceToken(GetDescriptor())) {
95 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
96 return ERROR;
97 }
98 if (!data.WriteRemoteObject(observer->AsObject())) {
99 ACCESSTOKEN_LOG_ERROR(LABEL, "Observer write failed.");
100 return ERROR;
101 }
102 sptr<IRemoteObject> remote = Remote();
103 if (remote == nullptr) {
104 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
105 return ERROR;
106 }
107 int32_t error = remote->SendRequest(
108 static_cast<uint32_t>(IAppMgr::Message::UNREGISTER_APPLICATION_STATE_OBSERVER), data, reply, option);
109 if (error != ERR_NONE) {
110 ACCESSTOKEN_LOG_ERROR(LABEL, "Set microphoneMute failed, error: %d", error);
111 return error;
112 }
113 return reply.ReadInt32();
114 }
115
GetForegroundApplications(std::vector<AppStateData> & list)116 int32_t AppManagerAccessProxy::GetForegroundApplications(std::vector<AppStateData>& list)
117 {
118 MessageParcel data;
119 MessageParcel reply;
120 MessageOption option;
121 if (!data.WriteInterfaceToken(GetDescriptor())) {
122 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
123 return ERROR;
124 }
125 sptr<IRemoteObject> remote = Remote();
126 if (remote == nullptr) {
127 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
128 return ERROR;
129 }
130 int32_t error = remote->SendRequest(
131 static_cast<uint32_t>(IAppMgr::Message::GET_FOREGROUND_APPLICATIONS), data, reply, option);
132 if (error != ERR_NONE) {
133 ACCESSTOKEN_LOG_ERROR(LABEL, "GetForegroundApplications failed, error: %{public}d", error);
134 return error;
135 }
136 uint32_t infoSize = reply.ReadUint32();
137 if (infoSize > CYCLE_LIMIT) {
138 ACCESSTOKEN_LOG_ERROR(LABEL, "InfoSize is too large");
139 return ERROR;
140 }
141 for (uint32_t i = 0; i < infoSize; i++) {
142 std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
143 if (info != nullptr) {
144 list.emplace_back(*info);
145 }
146 }
147 return reply.ReadInt32();
148 }
149 } // namespace AccessToken
150 } // namespace Security
151 } // namespace OHOS
152