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_privacy_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_PRIVACY, "AppManagerPrivacyProxy"};
24 static constexpr int32_t ERROR = -1;
25 constexpr int32_t CYCLE_LIMIT = 1000;
26 }
27
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)28 int32_t AppManagerPrivacyProxy::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer,
29 const std::vector<std::string>& bundleNameList)
30 {
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
36 return ERROR;
37 }
38 if (!data.WriteRemoteObject(observer->AsObject())) {
39 ACCESSTOKEN_LOG_ERROR(LABEL, "observer write failed.");
40 return ERROR;
41 }
42 if (!data.WriteStringVector(bundleNameList)) {
43 ACCESSTOKEN_LOG_ERROR(LABEL, "bundleNameList write failed.");
44 return ERROR;
45 }
46 int32_t error = Remote()->SendRequest(
47 static_cast<uint32_t>(IAppMgr::Message::REGISTER_APPLICATION_STATE_OBSERVER), data, reply, option);
48 if (error != ERR_NONE) {
49 ACCESSTOKEN_LOG_ERROR(LABEL, "RegisterAppStatus failed, error: %{public}d", error);
50 return ERROR;
51 }
52 return reply.ReadInt32();
53 }
54
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)55 int32_t AppManagerPrivacyProxy::UnregisterApplicationStateObserver(
56 const sptr<IApplicationStateObserver>& observer)
57 {
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option;
61 if (!data.WriteInterfaceToken(GetDescriptor())) {
62 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
63 return ERROR;
64 }
65 if (!data.WriteRemoteObject(observer->AsObject())) {
66 ACCESSTOKEN_LOG_ERROR(LABEL, "observer write failed.");
67 return ERROR;
68 }
69 int32_t error = Remote()->SendRequest(
70 static_cast<uint32_t>(IAppMgr::Message::UNREGISTER_APPLICATION_STATE_OBSERVER), data, reply, option);
71 if (error != ERR_NONE) {
72 ACCESSTOKEN_LOG_ERROR(LABEL, "set microphoneMute failed, error: %d", error);
73 return error;
74 }
75 return reply.ReadInt32();
76 }
77
GetForegroundApplications(std::vector<AppStateData> & list)78 int32_t AppManagerPrivacyProxy::GetForegroundApplications(std::vector<AppStateData>& list)
79 {
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option;
83 if (!data.WriteInterfaceToken(GetDescriptor())) {
84 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
85 return ERROR;
86 }
87 int32_t error = Remote()->SendRequest(
88 static_cast<uint32_t>(IAppMgr::Message::GET_FOREGROUND_APPLICATIONS), data, reply, option);
89 if (error != ERR_NONE) {
90 ACCESSTOKEN_LOG_ERROR(LABEL, "GetForegroundApplications failed, error: %{public}d", error);
91 return error;
92 }
93 int32_t infoSize = reply.ReadInt32();
94 if (infoSize > CYCLE_LIMIT) {
95 ACCESSTOKEN_LOG_ERROR(LABEL, "infoSize is too large");
96 return ERROR;
97 }
98 for (int32_t i = 0; i < infoSize; i++) {
99 std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
100 if (info != nullptr) {
101 list.emplace_back(*info);
102 }
103 }
104 return reply.ReadInt32();
105 }
106 } // namespace AccessToken
107 } // namespace Security
108 } // namespace OHOS
109