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 "background_task_manager_access_proxy.h"
17 #include "accesstoken_log.h"
18 #include "errors.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "BackgroundTaskManagerAccessProxy"};
25 static constexpr int32_t ERROR = -1;
26 static constexpr int32_t MAX_CALLBACK_NUM = 10 * 1024;
27 }
28
SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)29 int32_t BackgroundTaskManagerAccessProxy::SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
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(subscriber->AsObject())) {
39 ACCESSTOKEN_LOG_ERROR(LABEL, "Write callerToken failed.");
40 return ERROR;
41 }
42 sptr<IRemoteObject> remote = Remote();
43 if (remote == nullptr) {
44 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
45 return ERROR;
46 }
47 int32_t error = remote->SendRequest(
48 static_cast<uint32_t>(IBackgroundTaskMgr::Message::SUBSCRIBE_BACKGROUND_TASK), data, reply, option);
49 if (error != ERR_NONE) {
50 ACCESSTOKEN_LOG_ERROR(LABEL, "Regist background task observer failed, error: %{public}d", error);
51 return ERROR;
52 }
53 int32_t result;
54 if (!reply.ReadInt32(result)) {
55 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
56 return ERROR;
57 }
58 return result;
59 }
60
UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)61 int32_t BackgroundTaskManagerAccessProxy::UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
62 {
63 MessageParcel data;
64 MessageParcel reply;
65 MessageOption option;
66 if (!data.WriteInterfaceToken(GetDescriptor())) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed.");
68 return ERROR;
69 }
70 if (!data.WriteRemoteObject(subscriber->AsObject())) {
71 ACCESSTOKEN_LOG_ERROR(LABEL, "Write callerToken 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>(IBackgroundTaskMgr::Message::UNSUBSCRIBE_BACKGROUND_TASK), data, reply, option);
81 if (error != ERR_NONE) {
82 ACCESSTOKEN_LOG_ERROR(LABEL, "Unregist background task observer failed, error: %d", error);
83 return error;
84 }
85 int32_t result;
86 if (!reply.ReadInt32(result)) {
87 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
88 return ERROR;
89 }
90 return result;
91 }
92
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)93 int32_t BackgroundTaskManagerAccessProxy::GetContinuousTaskApps(
94 std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option;
99 if (!data.WriteInterfaceToken(GetDescriptor())) {
100 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
101 return ERROR;
102 }
103 sptr<IRemoteObject> remote = Remote();
104 if (remote == nullptr) {
105 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
106 return ERROR;
107 }
108 int32_t error = remote->SendRequest(
109 static_cast<uint32_t>(IBackgroundTaskMgr::Message::GET_CONTINUOUS_TASK_APPS), data, reply, option);
110 if (error != ERR_NONE) {
111 ACCESSTOKEN_LOG_ERROR(LABEL, "Get continuous task apps failed, error: %{public}d", error);
112 return ERROR;
113 }
114 int32_t result;
115 if (!reply.ReadInt32(result)) {
116 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
117 return ERROR;
118 }
119 if (result != ERR_OK) {
120 ACCESSTOKEN_LOG_ERROR(LABEL, "GetContinuousTaskApps failed.");
121 return result;
122 }
123 int32_t infoSize = reply.ReadInt32();
124 if ((infoSize < 0) || (infoSize > MAX_CALLBACK_NUM)) {
125 ACCESSTOKEN_LOG_ERROR(LABEL, "InfoSize:%{public}d invalid.", infoSize);
126 return ERROR;
127 }
128 for (int32_t i = 0; i < infoSize; i++) {
129 auto info = ContinuousTaskCallbackInfo::Unmarshalling(reply);
130 if (info == nullptr) {
131 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to Read Parcelable infos.");
132 return ERROR;
133 }
134 list.emplace_back(info);
135 }
136 return result;
137 }
138 } // namespace AccessToken
139 } // namespace Security
140 } // namespace OHOS
141