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 "ipc_remote_object_internal.h"
17 #include "ipc_inner_object.h"
18 #include "message_parcel.h"
19 #include "log_tags.h"
20 #include "ipc_debug.h"
21 #include "ipc_error_code.h"
22
23 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_CAPI, "IPCRemoteObject" };
24
IPCDeathRecipient(OH_OnDeathRecipientCallback deathRecipientCallback,OH_OnDeathRecipientDestroyCallback destroyCallback,void * userData)25 IPCDeathRecipient::IPCDeathRecipient(OH_OnDeathRecipientCallback deathRecipientCallback,
26 OH_OnDeathRecipientDestroyCallback destroyCallback, void *userData)
27 : deathRecipientCallback_(deathRecipientCallback), destroyCallback_(destroyCallback), userData_(userData)
28 {
29 }
30
~IPCDeathRecipient()31 IPCDeathRecipient::~IPCDeathRecipient()
32 {
33 if (destroyCallback_ != nullptr) {
34 destroyCallback_(userData_);
35 }
36 deathRecipientCallback_ = nullptr;
37 destroyCallback_ = nullptr;
38 userData_ = nullptr;
39 }
40
OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject> &)41 void IPCDeathRecipient::OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject>&)
42 {
43 if (deathRecipientCallback_ != nullptr) {
44 deathRecipientCallback_(userData_);
45 }
46 }
47
OHIPCRemoteServiceStub(std::u16string & desc,OH_OnRemoteRequestCallback requestCallback,OH_OnRemoteDestroyCallback destroyCallback,void * userData)48 OHIPCRemoteServiceStub::OHIPCRemoteServiceStub(std::u16string &desc, OH_OnRemoteRequestCallback requestCallback,
49 OH_OnRemoteDestroyCallback destroyCallback, void *userData)
50 : IPCObjectStub(desc), requestCallback_(requestCallback), destroyCallback_(destroyCallback), userData_(userData)
51 {
52 }
53
~OHIPCRemoteServiceStub()54 OHIPCRemoteServiceStub::~OHIPCRemoteServiceStub()
55 {
56 if (destroyCallback_ != nullptr) {
57 (void)destroyCallback_(userData_);
58 }
59 destroyCallback_ = nullptr;
60 userData_ = nullptr;
61 requestCallback_ = nullptr;
62 }
63
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption &)64 int OHIPCRemoteServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data,
65 OHOS::MessageParcel &reply, OHOS::MessageOption &)
66 {
67 if (requestCallback_ == nullptr) {
68 ZLOGE(LOG_LABEL, "Callback is null for code: %{public}u", code);
69 return OH_IPC_INNER_ERROR;
70 }
71 OHIPCParcel parcelData{ &data };
72 OHIPCParcel parcelReply{ &reply };
73 int err = requestCallback_(code, &parcelData, &parcelReply, userData_);
74 if (err != OH_IPC_SUCCESS
75 && !IsIpcErrorCode(err)
76 && !IsUserDefinedError(err)) {
77 ZLOGE(LOG_LABEL, "user define error code:%{public}d out of range[%{public}d, %{public}d]",
78 err, OH_IPC_USER_ERROR_CODE_MIN, OH_IPC_USER_ERROR_CODE_MAX);
79 err = OH_IPC_INVALID_USER_ERROR_CODE;
80 }
81 return err;
82 }
83