1 /*
2 * Copyright (c) 2022-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 "default_app_host.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "app_log_wrapper.h"
20 #include "appexecfwk_errors.h"
21 #include "bundle_framework_core_ipc_interface_code.h"
22 #include "bundle_memory_guard.h"
23 #include "hitrace_meter.h"
24 #include "ipc_types.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
DefaultAppHost()28 DefaultAppHost::DefaultAppHost()
29 {
30 LOG_D(BMS_TAG_DEFAULT, "create DefaultAppHost");
31 }
32
~DefaultAppHost()33 DefaultAppHost::~DefaultAppHost()
34 {
35 LOG_D(BMS_TAG_DEFAULT, "destroy DefaultAppHost");
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int DefaultAppHost::OnRemoteRequest(
39 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
40 {
41 BundleMemoryGuard memoryGuard;
42 LOG_I(BMS_TAG_DEFAULT, "DefaultAppHost OnRemoteRequest, message code : %{public}u", code);
43 std::u16string descriptor = DefaultAppHost::GetDescriptor();
44 std::u16string remoteDescriptor = data.ReadInterfaceToken();
45 if (descriptor != remoteDescriptor) {
46 LOG_E(BMS_TAG_DEFAULT, "descriptor invalid");
47 return OBJECT_NULL;
48 }
49
50 switch (code) {
51 case static_cast<uint32_t>(DefaultAppInterfaceCode::IS_DEFAULT_APPLICATION):
52 return HandleIsDefaultApplication(data, reply);
53 case static_cast<uint32_t>(DefaultAppInterfaceCode::GET_DEFAULT_APPLICATION):
54 return HandleGetDefaultApplication(data, reply);
55 case static_cast<uint32_t>(DefaultAppInterfaceCode::SET_DEFAULT_APPLICATION):
56 return HandleSetDefaultApplication(data, reply);
57 case static_cast<uint32_t>(DefaultAppInterfaceCode::RESET_DEFAULT_APPLICATION):
58 return HandleResetDefaultApplication(data, reply);
59 default:
60 LOG_W(BMS_TAG_DEFAULT, "DefaultAppHost receive unknown code, code = %{public}d", code);
61 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 }
64
HandleIsDefaultApplication(Parcel & data,Parcel & reply)65 ErrCode DefaultAppHost::HandleIsDefaultApplication(Parcel& data, Parcel& reply)
66 {
67 LOG_I(BMS_TAG_DEFAULT, "begin to HandleIsDefaultApplication");
68 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
69 std::string type = data.ReadString();
70 bool isDefaultApp = false;
71 ErrCode ret = IsDefaultApplication(type, isDefaultApp);
72 if (!reply.WriteInt32(ret)) {
73 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
74 return ERR_APPEXECFWK_PARCEL_ERROR;
75 }
76 if (ret == ERR_OK) {
77 if (!reply.WriteBool(isDefaultApp)) {
78 LOG_E(BMS_TAG_DEFAULT, "write isDefaultApp failed");
79 return ERR_APPEXECFWK_PARCEL_ERROR;
80 }
81 }
82 return ERR_OK;
83 }
84
HandleGetDefaultApplication(Parcel & data,Parcel & reply)85 ErrCode DefaultAppHost::HandleGetDefaultApplication(Parcel& data, Parcel& reply)
86 {
87 LOG_I(BMS_TAG_DEFAULT, "begin to HandleGetDefaultApplication");
88 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
89 int32_t userId = data.ReadInt32();
90 std::string type = data.ReadString();
91 BundleInfo bundleInfo;
92 ErrCode ret = GetDefaultApplication(userId, type, bundleInfo);
93 if (!reply.WriteInt32(ret)) {
94 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
95 return ERR_APPEXECFWK_PARCEL_ERROR;
96 }
97 if (ret == ERR_OK) {
98 if (!reply.WriteParcelable(&bundleInfo)) {
99 LOG_E(BMS_TAG_DEFAULT, "write bundleInfo failed");
100 return ERR_APPEXECFWK_PARCEL_ERROR;
101 }
102 }
103 return ERR_OK;
104 }
105
HandleSetDefaultApplication(Parcel & data,Parcel & reply)106 ErrCode DefaultAppHost::HandleSetDefaultApplication(Parcel& data, Parcel& reply)
107 {
108 LOG_I(BMS_TAG_DEFAULT, "begin to HandleSetDefaultApplication");
109 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
110 int32_t userId = data.ReadInt32();
111 std::string type = data.ReadString();
112 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
113 if (want == nullptr) {
114 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<Want> failed");
115 return ERR_APPEXECFWK_PARCEL_ERROR;
116 }
117 ErrCode ret = SetDefaultApplication(userId, type, *want);
118 if (!reply.WriteInt32(ret)) {
119 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
120 return ERR_APPEXECFWK_PARCEL_ERROR;
121 }
122 return ERR_OK;
123 }
124
HandleResetDefaultApplication(Parcel & data,Parcel & reply)125 ErrCode DefaultAppHost::HandleResetDefaultApplication(Parcel& data, Parcel& reply)
126 {
127 LOG_I(BMS_TAG_DEFAULT, "begin to HandleResetDefaultApplication");
128 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
129 int32_t userId = data.ReadInt32();
130 std::string type = data.ReadString();
131 ErrCode ret = ResetDefaultApplication(userId, type);
132 if (!reply.WriteInt32(ret)) {
133 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
134 return ERR_APPEXECFWK_PARCEL_ERROR;
135 }
136 return ERR_OK;
137 }
138 }
139 }
140