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 "bundle_status_callback_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 {
BundleStatusCallbackHost()26 BundleStatusCallbackHost::BundleStatusCallbackHost()
27 {
28     APP_LOGI("create bundle status callback host instance");
29 }
30 
~BundleStatusCallbackHost()31 BundleStatusCallbackHost::~BundleStatusCallbackHost()
32 {
33     APP_LOGI("destroy bundle status callback host instance");
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int BundleStatusCallbackHost::OnRemoteRequest(
37     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39     BundleMemoryGuard memoryGuard;
40     APP_LOGD("bundle status callback host onReceived message, the message code is %{public}u", code);
41     std::u16string descripter = BundleStatusCallbackHost::GetDescriptor();
42     std::u16string remoteDescripter = data.ReadInterfaceToken();
43     if (descripter != remoteDescripter) {
44         APP_LOGE("fail to write reply message in status callback host due to the reply is nullptr");
45         return OBJECT_NULL;
46     }
47 
48     switch (code) {
49         case static_cast<uint32_t>(BundleStatusCallbackInterfaceCode::ON_BUNDLE_STATE_CHANGED): {
50             uint8_t installType = data.ReadUint8();
51             int32_t resultCode = data.ReadInt32();
52             std::string resultMsg = Str16ToStr8(data.ReadString16());
53             std::string bundleName = Str16ToStr8(data.ReadString16());
54             OnBundleStateChanged(installType, resultCode, resultMsg, bundleName);
55             break;
56         }
57         default:
58             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59     }
60     return NO_ERROR;
61 }
62 }  // namespace AppExecFwk
63 }  // namespace OHOS
64