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 "ui_service_host_stub.h"
17 #include "hilog_tag_wrapper.h"
18 
19 namespace OHOS {
20 namespace AAFwk {
21 
UIServiceHostStub()22 UIServiceHostStub::UIServiceHostStub()
23 {
24     TAG_LOGI(AAFwkTag::UISERVC_EXT, "called");
25     requestFuncMap_[SEND_DATA] = &UIServiceHostStub::OnSendData;
26 }
27 
~UIServiceHostStub()28 UIServiceHostStub::~UIServiceHostStub()
29 {
30     TAG_LOGI(AAFwkTag::UISERVC_EXT, "called");
31     requestFuncMap_.clear();
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int UIServiceHostStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
35     MessageOption& option)
36 {
37     std::u16string descriptor = UIServiceHostStub::GetDescriptor();
38     std::u16string remoteDescriptor = data.ReadInterfaceToken();
39     if (descriptor != remoteDescriptor) {
40         return ERR_INVALID_STATE;
41     }
42     auto itFunc = requestFuncMap_.find(code);
43     if (itFunc != requestFuncMap_.end()) {
44         auto requestFunc = itFunc->second;
45         if (requestFunc != nullptr) {
46             return (this->*requestFunc)(data, reply);
47         }
48     }
49     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50 }
51 
OnSendData(MessageParcel & data,MessageParcel & reply)52 int32_t UIServiceHostStub::OnSendData(MessageParcel& data, MessageParcel& reply)
53 {
54     std::unique_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
55     if (wantParams == nullptr) {
56         TAG_LOGE(AAFwkTag::UISERVC_EXT, "UIServiceHostStub::OnSendData, read WantParams failed");
57         return ERR_INVALID_VALUE;
58     }
59     int32_t result = SendData(*wantParams);
60     if (!reply.WriteInt32(result)) {
61         TAG_LOGE(AAFwkTag::UISERVC_EXT, "UIServiceHostStub::OnSendData, write result failed.");
62         return IPC_STUB_ERR;
63     }
64     return NO_ERROR;
65 }
66 }
67 }
68