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 "app_log_tag_wrapper.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "string_ex.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::u16string BMS_PROXY_INTERFACE_TOKEN = u"ohos.appexecfwk.BundleMgr";
30 }
31
GetBmsProxy()32 sptr<IRemoteObject> BundleMgrProxyNative::GetBmsProxy()
33 {
34 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35 if (samgrProxy == nullptr) {
36 APP_LOGE("fail to get samgr");
37 return nullptr;
38 }
39 return samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
40 }
41
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)42 bool BundleMgrProxyNative::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
43 {
44 LOG_I(BMS_TAG_QUERY, "begin to get bundle info for self");
45 MessageParcel data;
46 if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
47 LOG_E(BMS_TAG_QUERY, "fail to GetBundleInfoForSelf due to write InterfaceToken fail");
48 return false;
49 }
50 if (!data.WriteInt32(flags)) {
51 LOG_E(BMS_TAG_QUERY, "fail to GetBundleInfoForSelf due to write flag fail");
52 return false;
53 }
54 if (!GetParcelableInfo<BundleInfo>(GET_BUNDLE_INFO_FOR_SELF_NATIVE, data, bundleInfo)) {
55 LOG_E(BMS_TAG_QUERY, "fail to GetBundleInfoForSelf from server");
56 return false;
57 }
58 return true;
59 }
60
GetCompatibleDeviceTypeNative(std::string & deviceType)61 bool BundleMgrProxyNative::GetCompatibleDeviceTypeNative(std::string &deviceType)
62 {
63 LOG_I(BMS_TAG_QUERY, "begin to get compatible device type");
64 MessageParcel data;
65 if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
66 LOG_E(BMS_TAG_QUERY, "Write interfaceToken failed");
67 return false;
68 }
69
70 MessageParcel reply;
71 if (!SendTransactCmd(GET_COMPATIBLED_DEVICE_TYPE_NATIVE, data, reply)) {
72 return false;
73 }
74 int32_t res = reply.ReadInt32();
75 if (res != NO_ERROR) {
76 APP_LOGE("reply result failed");
77 return false;
78 }
79 deviceType = reply.ReadString();
80 APP_LOGD("get compatible device type success");
81 return true;
82 }
83
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply)84 bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
85 {
86 MessageOption option(MessageOption::TF_SYNC);
87
88 sptr<IRemoteObject> remote = GetBmsProxy();
89 if (remote == nullptr) {
90 APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
91 return false;
92 }
93 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
94 if (result != NO_ERROR) {
95 APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
96 return false;
97 }
98 return true;
99 }
100
101 template<typename T>
GetParcelableInfo(uint32_t code,MessageParcel & data,T & parcelableInfo)102 bool BundleMgrProxyNative::GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo)
103 {
104 MessageParcel reply;
105 if (!SendTransactCmd(code, data, reply)) {
106 return false;
107 }
108
109 int32_t res = reply.ReadInt32();
110 if (res != NO_ERROR) {
111 APP_LOGE("reply result failed");
112 return false;
113 }
114 std::unique_ptr<T> info(reply.ReadParcelable<T>());
115 if (info == nullptr) {
116 APP_LOGE("readParcelableInfo failed");
117 return false;
118 }
119 parcelableInfo = *info;
120 APP_LOGD("get parcelable info success");
121 return true;
122 }
123 } // namespace AppExecFwk
124 } // namespace OHOS