1 /*
2  * Copyright (c) 2023 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 "shutdown_stub_delegator.h"
17 
18 #include "power_common.h"
19 #include "shutdown/shutdown_client_ipc_interface_code.h"
20 #include "xcollie/xcollie.h"
21 
22 namespace OHOS {
23 namespace PowerMgr {
24 namespace {
25 constexpr int32_t DFX_DELAY_S = 60;
26 
27 }
28 int32_t ShutdownStubDelegator::HandleRemoteRequest(
29     uint32_t code, MessageParcel& data, [[maybe_unused]] MessageParcel& reply, [[maybe_unused]] MessageOption& option)
30 {
31     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer("ShutdownStub", DFX_DELAY_S, nullptr, nullptr,
32         HiviewDFX::XCOLLIE_FLAG_LOG);
33     int32_t ret = ERR_OK;
34     switch (code) {
35         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_TAKEOVER_SHUTDOWN_CALLBACK):
36             ret = RegisterTakeOverShutdownCallback(data);
37             break;
38         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_TAKEOVER_SHUTDOWN_CALLBACK):
39             ret = UnRegisterTakeOverShutdownCallback(data);
40             break;
41         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_ASYNC_SHUTDOWN_CALLBACK):
42             ret = RegisterAsyncShutdownCallback(data);
43             break;
44         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_ASYNC_SHUTDOWN_CALLBACK):
45             ret = UnRegisterAsyncShutdownCallback(data);
46             break;
47         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_SYNC_SHUTDOWN_CALLBACK):
48             ret = RegisterSyncShutdownCallback(data);
49             break;
50         case static_cast<int32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_SYNC_SHUTDOWN_CALLBACK):
51             ret = UnRegisterSyncShutdownCallback(data);
52             break;
53         default:
54             ret = ERR_INVALID_OPERATION;
55             break;
56     }
57     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
58     return ret;
59 }
60 
RegisterTakeOverShutdownCallback(MessageParcel & data)61 int32_t ShutdownStubDelegator::RegisterTakeOverShutdownCallback(MessageParcel& data)
62 {
63     uint32_t priority;
64     sptr<IRemoteObject> obj = data.ReadRemoteObject();
65     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
66     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
67     auto callback = iface_cast<ITakeOverShutdownCallback>(obj);
68     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
69     stub_.RegisterShutdownCallback(callback, static_cast<ShutdownPriority>(priority));
70     return ERR_OK;
71 }
72 
UnRegisterTakeOverShutdownCallback(MessageParcel & data)73 int32_t ShutdownStubDelegator::UnRegisterTakeOverShutdownCallback(MessageParcel& data)
74 {
75     sptr<IRemoteObject> obj = data.ReadRemoteObject();
76     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
77     auto callback = iface_cast<ITakeOverShutdownCallback>(obj);
78     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
79     stub_.UnRegisterShutdownCallback(callback);
80     return ERR_OK;
81 }
82 
RegisterAsyncShutdownCallback(MessageParcel & data)83 int32_t ShutdownStubDelegator::RegisterAsyncShutdownCallback(MessageParcel& data)
84 {
85     uint32_t priority;
86     sptr<IRemoteObject> obj = data.ReadRemoteObject();
87     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
88     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
89     auto callback = iface_cast<IAsyncShutdownCallback>(obj);
90     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
91     stub_.RegisterShutdownCallback(callback, static_cast<ShutdownPriority>(priority));
92     return ERR_OK;
93 }
94 
UnRegisterAsyncShutdownCallback(MessageParcel & data)95 int32_t ShutdownStubDelegator::UnRegisterAsyncShutdownCallback(MessageParcel& data)
96 {
97     sptr<IRemoteObject> obj = data.ReadRemoteObject();
98     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
99     auto callback = iface_cast<IAsyncShutdownCallback>(obj);
100     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
101     stub_.UnRegisterShutdownCallback(callback);
102     return ERR_OK;
103 }
104 
RegisterSyncShutdownCallback(MessageParcel & data)105 int32_t ShutdownStubDelegator::RegisterSyncShutdownCallback(MessageParcel& data)
106 {
107     uint32_t priority;
108     sptr<IRemoteObject> obj = data.ReadRemoteObject();
109     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
110     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
111     auto callback = iface_cast<ISyncShutdownCallback>(obj);
112     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
113     stub_.RegisterShutdownCallback(callback, static_cast<ShutdownPriority>(priority));
114     return ERR_OK;
115 }
116 
UnRegisterSyncShutdownCallback(MessageParcel & data)117 int32_t ShutdownStubDelegator::UnRegisterSyncShutdownCallback(MessageParcel& data)
118 {
119     sptr<IRemoteObject> obj = data.ReadRemoteObject();
120     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
121     auto callback = iface_cast<ISyncShutdownCallback>(obj);
122     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
123     stub_.UnRegisterShutdownCallback(callback);
124     return ERR_OK;
125 }
126 } // namespace PowerMgr
127 } // namespace OHOS
128