1 /*
2  * Copyright (c) 2021-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 "status_receiver_host.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_framework_core_ipc_interface_code.h"
20 #include "bundle_memory_guard.h"
21 #include "ipc_types.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
StatusReceiverHost()26 StatusReceiverHost::StatusReceiverHost()
27 {
28     APP_LOGD("create status receiver host instance");
29 }
30 
~StatusReceiverHost()31 StatusReceiverHost::~StatusReceiverHost()
32 {
33     APP_LOGD("destroy status receiver host instance");
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int StatusReceiverHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38     BundleMemoryGuard memoryGuard;
39     APP_LOGD("status receiver host onReceived message, the message code is %{public}u", code);
40     std::u16string descripter = StatusReceiverHost::GetDescriptor();
41     std::u16string remoteDescripter = data.ReadInterfaceToken();
42     if (descripter != remoteDescripter) {
43         APP_LOGE("fail to write reply message in status receiver host due to the reply is nullptr");
44         return OBJECT_NULL;
45     }
46 
47     switch (code) {
48         case static_cast<uint32_t>(StatusReceiverInterfaceCode::ON_FINISHED): {
49             int32_t resultCode = data.ReadInt32();
50             std::string resultMsg = Str16ToStr8(data.ReadString16());
51             OnFinished(resultCode, resultMsg);
52             break;
53         }
54         case static_cast<uint32_t>(StatusReceiverInterfaceCode::ON_STATUS_NOTIFY): {
55             int32_t progress = data.ReadInt32();
56             OnStatusNotify(progress);
57             break;
58         }
59         default:
60             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61     }
62     return NO_ERROR;
63 }
64 }  // namespace AppExecFwk
65 }  // namespace OHOS