1 /*
2 * Copyright (c) 2022 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_proxy.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "app_log_wrapper.h"
20 #include "appexecfwk_errors.h"
21 #include "hitrace_meter.h"
22 #include "ipc_types.h"
23 #include "parcel.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
DefaultAppProxy(const sptr<IRemoteObject> & object)27 DefaultAppProxy::DefaultAppProxy(const sptr<IRemoteObject>& object)
28 : IRemoteProxy<IDefaultApp>(object)
29 {
30 LOG_D(BMS_TAG_DEFAULT, "create DefaultAppProxy");
31 }
32
~DefaultAppProxy()33 DefaultAppProxy::~DefaultAppProxy()
34 {
35 LOG_D(BMS_TAG_DEFAULT, "destroy DefaultAppProxy");
36 }
37
IsDefaultApplication(const std::string & type,bool & isDefaultApp)38 ErrCode DefaultAppProxy::IsDefaultApplication(const std::string& type, bool& isDefaultApp)
39 {
40 LOG_D(BMS_TAG_DEFAULT, "begin to call IsDefaultApplication");
41 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
42
43 MessageParcel data;
44 if (!data.WriteInterfaceToken(GetDescriptor())) {
45 LOG_E(BMS_TAG_DEFAULT, "WriteInterfaceToken failed");
46 return ERR_APPEXECFWK_PARCEL_ERROR;
47 }
48 if (!data.WriteString(type)) {
49 LOG_E(BMS_TAG_DEFAULT, "write type failed");
50 return ERR_APPEXECFWK_PARCEL_ERROR;
51 }
52
53 MessageParcel reply;
54 if (!SendRequest(DefaultAppInterfaceCode::IS_DEFAULT_APPLICATION, data, reply)) {
55 LOG_E(BMS_TAG_DEFAULT, "SendRequest failed");
56 return ERR_APPEXECFWK_PARCEL_ERROR;
57 }
58 ErrCode ret = reply.ReadInt32();
59 if (ret == ERR_OK) {
60 isDefaultApp = reply.ReadBool();
61 }
62 return ret;
63 }
64
GetDefaultApplication(int32_t userId,const std::string & type,BundleInfo & bundleInfo)65 ErrCode DefaultAppProxy::GetDefaultApplication(int32_t userId, const std::string& type, BundleInfo& bundleInfo)
66 {
67 LOG_D(BMS_TAG_DEFAULT, "begin to GetDefaultApplication");
68 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
69
70 if (type.empty()) {
71 LOG_E(BMS_TAG_DEFAULT, "type is empty");
72 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
73 }
74
75 MessageParcel data;
76 if (!data.WriteInterfaceToken(GetDescriptor())) {
77 LOG_E(BMS_TAG_DEFAULT, "WriteInterfaceToken failed");
78 return ERR_APPEXECFWK_PARCEL_ERROR;
79 }
80 if (!data.WriteInt32(userId)) {
81 LOG_E(BMS_TAG_DEFAULT, "write userId failed");
82 return ERR_APPEXECFWK_PARCEL_ERROR;
83 }
84 if (!data.WriteString(type)) {
85 LOG_E(BMS_TAG_DEFAULT, "write type failed");
86 return ERR_APPEXECFWK_PARCEL_ERROR;
87 }
88 return GetParcelableInfo<BundleInfo>(DefaultAppInterfaceCode::GET_DEFAULT_APPLICATION, data, bundleInfo);
89 }
90
SetDefaultApplication(int32_t userId,const std::string & type,const Want & want)91 ErrCode DefaultAppProxy::SetDefaultApplication(int32_t userId, const std::string& type, const Want& want)
92 {
93 LOG_D(BMS_TAG_DEFAULT, "begin to SetDefaultApplication");
94 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
95
96 MessageParcel data;
97 if (!data.WriteInterfaceToken(GetDescriptor())) {
98 LOG_E(BMS_TAG_DEFAULT, "WriteInterfaceToken failed");
99 return ERR_APPEXECFWK_PARCEL_ERROR;
100 }
101 if (!data.WriteInt32(userId)) {
102 LOG_E(BMS_TAG_DEFAULT, "write userId failed");
103 return ERR_APPEXECFWK_PARCEL_ERROR;
104 }
105 if (!data.WriteString(type)) {
106 LOG_E(BMS_TAG_DEFAULT, "write type failed");
107 return ERR_APPEXECFWK_PARCEL_ERROR;
108 }
109 if (!data.WriteParcelable(&want)) {
110 LOG_E(BMS_TAG_DEFAULT, "write want failed");
111 return ERR_APPEXECFWK_PARCEL_ERROR;
112 }
113
114 MessageParcel reply;
115 if (!SendRequest(DefaultAppInterfaceCode::SET_DEFAULT_APPLICATION, data, reply)) {
116 LOG_E(BMS_TAG_DEFAULT, "SendRequest failed");
117 return ERR_APPEXECFWK_PARCEL_ERROR;
118 }
119
120 return reply.ReadInt32();
121 }
122
ResetDefaultApplication(int32_t userId,const std::string & type)123 ErrCode DefaultAppProxy::ResetDefaultApplication(int32_t userId, const std::string& type)
124 {
125 LOG_D(BMS_TAG_DEFAULT, "begin to ResetDefaultApplication");
126 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
127
128 if (type.empty()) {
129 LOG_E(BMS_TAG_DEFAULT, "type is empty");
130 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
131 }
132
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(GetDescriptor())) {
135 LOG_E(BMS_TAG_DEFAULT, "WriteInterfaceToken failed");
136 return ERR_APPEXECFWK_PARCEL_ERROR;
137 }
138 if (!data.WriteInt32(userId)) {
139 LOG_E(BMS_TAG_DEFAULT, "write userId failed");
140 return ERR_APPEXECFWK_PARCEL_ERROR;
141 }
142 if (!data.WriteString(type)) {
143 LOG_E(BMS_TAG_DEFAULT, "write type failed");
144 return ERR_APPEXECFWK_PARCEL_ERROR;
145 }
146
147 MessageParcel reply;
148 if (!SendRequest(DefaultAppInterfaceCode::RESET_DEFAULT_APPLICATION, data, reply)) {
149 LOG_E(BMS_TAG_DEFAULT, "SendRequest failed");
150 return ERR_APPEXECFWK_PARCEL_ERROR;
151 }
152
153 return reply.ReadInt32();
154 }
155
156 template<typename T>
GetParcelableInfo(DefaultAppInterfaceCode code,MessageParcel & data,T & parcelableInfo)157 ErrCode DefaultAppProxy::GetParcelableInfo(DefaultAppInterfaceCode code, MessageParcel& data, T& parcelableInfo)
158 {
159 MessageParcel reply;
160 if (!SendRequest(code, data, reply)) {
161 LOG_E(BMS_TAG_DEFAULT, "SendRequest failed");
162 return ERR_APPEXECFWK_PARCEL_ERROR;
163 }
164 ErrCode ret = reply.ReadInt32();
165 if (ret != ERR_OK) {
166 LOG_E(BMS_TAG_DEFAULT, "host reply errCode : %{public}d", ret);
167 return ret;
168 }
169
170 std::unique_ptr<T> info(reply.ReadParcelable<T>());
171 if (info == nullptr) {
172 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable failed");
173 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
174 }
175 parcelableInfo = *info;
176 LOG_D(BMS_TAG_DEFAULT, "GetParcelableInfo success");
177 return ERR_OK;
178 }
179
SendRequest(DefaultAppInterfaceCode code,MessageParcel & data,MessageParcel & reply)180 bool DefaultAppProxy::SendRequest(DefaultAppInterfaceCode code, MessageParcel& data, MessageParcel& reply)
181 {
182 MessageOption option(MessageOption::TF_SYNC);
183 sptr<IRemoteObject> remote = Remote();
184 if (remote == nullptr) {
185 LOG_E(BMS_TAG_DEFAULT, "failed to send request %{public}hhu due to remote object null", code);
186 return false;
187 }
188 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
189 if (result != NO_ERROR) {
190 LOG_E(BMS_TAG_DEFAULT, "receive error code %{public}d in transact %{public}hhu", result, code);
191 return false;
192 }
193 return true;
194 }
195 }
196 }
197