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
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply)61 bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
62 {
63 MessageOption option(MessageOption::TF_SYNC);
64
65 sptr<IRemoteObject> remote = GetBmsProxy();
66 if (remote == nullptr) {
67 APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
68 return false;
69 }
70 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
71 if (result != NO_ERROR) {
72 APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
73 return false;
74 }
75 return true;
76 }
77
78 template<typename T>
GetParcelableInfo(uint32_t code,MessageParcel & data,T & parcelableInfo)79 bool BundleMgrProxyNative::GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo)
80 {
81 MessageParcel reply;
82 if (!SendTransactCmd(code, data, reply)) {
83 return false;
84 }
85
86 int32_t res = reply.ReadInt32();
87 if (res != NO_ERROR) {
88 APP_LOGE("reply result failed");
89 return false;
90 }
91 std::unique_ptr<T> info(reply.ReadParcelable<T>());
92 if (info == nullptr) {
93 APP_LOGE("readParcelableInfo failed");
94 return false;
95 }
96 parcelableInfo = *info;
97 APP_LOGD("get parcelable info success");
98 return true;
99 }
100 } // namespace AppExecFwk
101 } // namespace OHOS