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 "bundle_mgr_proxy_native.h"
17
18 #include "app_log_wrapper.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const std::u16string BMS_PROXY_INTERFACE_TOKEN = u"ohos.appexecfwk.BundleMgr";
29 }
30
GetBmsProxy()31 sptr<IRemoteObject> BundleMgrProxyNative::GetBmsProxy()
32 {
33 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34 if (samgrProxy == nullptr) {
35 APP_LOGE("fail to get samgr.");
36 return nullptr;
37 }
38 return samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
39 }
40
GetBundleNameForUid(const int uid,std::string & bundleName)41 bool BundleMgrProxyNative::GetBundleNameForUid(const int uid, std::string &bundleName)
42 {
43 APP_LOGI("begin to GetBundleNameForUid of %{private}d", uid);
44 MessageParcel data;
45 if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
46 APP_LOGE("fail to GetBundleNameForUid due to write InterfaceToken fail");
47 return false;
48 }
49 if (!data.WriteInt32(uid)) {
50 APP_LOGE("fail to GetBundleNameForUid due to write uid fail");
51 return false;
52 }
53
54 MessageParcel reply;
55 if (!SendTransactCmd(GET_BUNDLE_NAME_FOR_UID, data, reply)) {
56 APP_LOGE("fail to GetBundleNameForUid from server");
57 return false;
58 }
59 if (!reply.ReadBool()) {
60 APP_LOGE("reply result false");
61 return false;
62 }
63 bundleName = reply.ReadString();
64 return true;
65 }
66
GetApplicationInfo(const std::string & appName,ApplicationFlag flag,int32_t userId,ApplicationInfo & appInfo)67 bool BundleMgrProxyNative::GetApplicationInfo(
68 const std::string &appName, ApplicationFlag flag, int32_t userId, ApplicationInfo &appInfo)
69 {
70 APP_LOGI("begin to GetApplicationInfo : %{private}s", appName.c_str());
71 MessageParcel data;
72 if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
73 APP_LOGE("fail to GetBundleNameForUid due to write InterfaceToken fail");
74 return false;
75 }
76
77 if (!data.WriteString(appName)) {
78 APP_LOGE("fail to GetApplicationInfo due to write appName fail");
79 return false;
80 }
81 if (!data.WriteInt32(static_cast<int>(flag))) {
82 APP_LOGE("fail to GetApplicationInfo due to write flag fail");
83 return false;
84 }
85 if (!data.WriteInt32(userId)) {
86 APP_LOGE("fail to GetApplicationInfo due to write userId fail");
87 return false;
88 }
89
90 if (!GetParcelableInfo<ApplicationInfo>(GET_APPLICATION_INFO, data, appInfo)) {
91 APP_LOGE("fail to GetApplicationInfo from server");
92 return false;
93 }
94 return true;
95 }
96
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply)97 bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
98 {
99 MessageOption option(MessageOption::TF_SYNC);
100
101 sptr<IRemoteObject> remote = GetBmsProxy();
102 if (remote == nullptr) {
103 APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
104 return false;
105 }
106 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
107 if (result != NO_ERROR) {
108 APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
109 return false;
110 }
111 return true;
112 }
113
114 template<typename T>
GetParcelableInfo(uint32_t code,MessageParcel & data,T & parcelableInfo)115 bool BundleMgrProxyNative::GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo)
116 {
117 MessageParcel reply;
118 if (!SendTransactCmd(code, data, reply)) {
119 return false;
120 }
121
122 if (!reply.ReadBool()) {
123 APP_LOGE("reply result false");
124 return false;
125 }
126
127 std::unique_ptr<T> info(reply.ReadParcelable<T>());
128 if (info == nullptr) {
129 APP_LOGE("readParcelableInfo failed");
130 return false;
131 }
132 parcelableInfo = *info;
133 APP_LOGD("get parcelable info success");
134 return true;
135 }
136 } // namespace AppExecFwk
137 } // namespace OHOS