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