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 #include "native_message_handler_callback.h"
16 #include "string_ex.h"
17
18 namespace OHOS {
19 namespace MiscServices {
OnTerminated()20 int32_t NativeMessageHandlerCallback::OnTerminated()
21 {
22 if (messageHandler_ == nullptr) {
23 IMSA_HILOGE("messageHandler_ is nullptr");
24 return ErrorCode::ERROR_CLIENT_NULL_POINTER;
25 }
26
27 if (messageHandler_->onTerminatedFunc == nullptr) {
28 IMSA_HILOGE("onTerminatedFunc is nullptr");
29 return ErrorCode::ERROR_CLIENT_NULL_POINTER;
30 }
31
32 auto ret = messageHandler_->onTerminatedFunc(messageHandler_);
33 if (ret != ErrorCode::NO_ERROR) {
34 IMSA_HILOGE("onTerminatedFunc execute failed! ret: %{public}d", ret);
35 return ErrorCode::ERROR_MESSAGE_HANDLER;
36 }
37 return ErrorCode::NO_ERROR;
38 }
39
OnMessage(const ArrayBuffer & arrayBuffer)40 int32_t NativeMessageHandlerCallback::OnMessage(const ArrayBuffer &arrayBuffer)
41 {
42 if (messageHandler_ == nullptr) {
43 IMSA_HILOGE("messageHandler_ is nullptr");
44 return ErrorCode::ERROR_CLIENT_NULL_POINTER;
45 }
46
47 if (messageHandler_->onMessageFunc == nullptr) {
48 IMSA_HILOGE("onMessageFunc is nullptr");
49 return ErrorCode::ERROR_CLIENT_NULL_POINTER;
50 }
51 std::u16string msgIdStr16 = Str8ToStr16(arrayBuffer.msgId);
52 auto ret = messageHandler_->onMessageFunc(messageHandler_, msgIdStr16.c_str(), msgIdStr16.length(),
53 arrayBuffer.msgParam.data(), arrayBuffer.msgParam.size());
54 if (ret != ErrorCode::NO_ERROR) {
55 IMSA_HILOGE("onMessageFunc execute failed! ret: %{public}d", ret);
56 return ErrorCode::ERROR_MESSAGE_HANDLER;
57 }
58 return ErrorCode::NO_ERROR;
59 }
60 } // namespace MiscServices
61 } // namespace OHOS