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