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