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 "continuous_task_change_callback.h"
17 #include "access_token.h"
18 #include "accesstoken_log.h"
19 #include "access_token_error.h"
20 
21 
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "BackgroundTaskSubscriberStub"
28 };
29 }
30 
BackgroundTaskSubscriberStub()31 BackgroundTaskSubscriberStub::BackgroundTaskSubscriberStub()
32 {
33     ACCESSTOKEN_LOG_INFO(LABEL, "BackgroundTaskSubscriberStub Instance create.");
34 }
35 
~BackgroundTaskSubscriberStub()36 BackgroundTaskSubscriberStub::~BackgroundTaskSubscriberStub()
37 {
38     ACCESSTOKEN_LOG_INFO(LABEL, "BackgroundTaskSubscriberStub Instance destroy.");
39 }
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t BackgroundTaskSubscriberStub::OnRemoteRequest(
42     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44     if (data.ReadInterfaceToken() != GetDescriptor()) {
45         ACCESSTOKEN_LOG_INFO(LABEL, "BackgroundTaskSubscriberStub: ReadInterfaceToken failed.");
46         return ERROR_IPC_REQUEST_FAIL;
47     }
48     switch (static_cast<IBackgroundTaskSubscriber::Message>(code)) {
49         case IBackgroundTaskSubscriber::Message::ON_CONTINUOUS_TASK_START: {
50             HandleOnContinuousTaskStart(data, reply);
51             return NO_ERROR;
52         }
53         case IBackgroundTaskSubscriber::Message::ON_CONTINUOUS_TASK_STOP: {
54             HandleOnContinuousTaskStop(data, reply);
55             return NO_ERROR;
56         }
57         default: {
58             ACCESSTOKEN_LOG_DEBUG(LABEL, "Default case code: %{public}d.", code);
59             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60         }
61     }
62     return NO_ERROR;
63 }
64 
HandleOnContinuousTaskStart(MessageParcel & data,MessageParcel & reply)65 void BackgroundTaskSubscriberStub::HandleOnContinuousTaskStart(MessageParcel &data, MessageParcel &reply)
66 {
67     std::shared_ptr<ContinuousTaskCallbackInfo> continuousTaskCallbackInfo(
68         data.ReadParcelable<ContinuousTaskCallbackInfo>());
69     if (continuousTaskCallbackInfo == nullptr) {
70         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed.");
71         return;
72     }
73     OnContinuousTaskStart(continuousTaskCallbackInfo);
74 }
75 
HandleOnContinuousTaskStop(MessageParcel & data,MessageParcel & reply)76 void BackgroundTaskSubscriberStub::HandleOnContinuousTaskStop(MessageParcel &data, MessageParcel &reply)
77 {
78     std::shared_ptr<ContinuousTaskCallbackInfo> continuousTaskCallbackInfo(
79         data.ReadParcelable<ContinuousTaskCallbackInfo>());
80     if (continuousTaskCallbackInfo == nullptr) {
81         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed.");
82         return;
83     }
84     OnContinuousTaskStop(continuousTaskCallbackInfo);
85 }
86 } // namespace AccessToken
87 } // namespace Security
88 } // namespace OHOS
89