• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)41 bool BundleMgrProxyNative::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
42 {
43     APP_LOGI("begin to get bundle info for self");
44     MessageParcel data;
45     if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
46         APP_LOGE("fail to GetBundleInfoForSelf due to write InterfaceToken fail");
47         return false;
48     }
49     if (!data.WriteInt32(flags)) {
50         APP_LOGE("fail to GetBundleInfoForSelf due to write flag fail");
51         return false;
52     }
53     if (!GetParcelableInfo<BundleInfo>(GET_BUNDLE_INFO_FOR_SELF_NATIVE, data, bundleInfo)) {
54         APP_LOGE("fail to GetBundleInfoForSelf from server");
55         return false;
56     }
57     return true;
58 }
59 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply)60 bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
61 {
62     MessageOption option(MessageOption::TF_SYNC);
63 
64     sptr<IRemoteObject> remote = GetBmsProxy();
65     if (remote == nullptr) {
66         APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
67         return false;
68     }
69     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
70     if (result != NO_ERROR) {
71         APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
72         return false;
73     }
74     return true;
75 }
76 
77 template<typename T>
GetParcelableInfo(uint32_t code,MessageParcel & data,T & parcelableInfo)78 bool BundleMgrProxyNative::GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo)
79 {
80     MessageParcel reply;
81     if (!SendTransactCmd(code, data, reply)) {
82         return false;
83     }
84 
85     int32_t res = reply.ReadInt32();
86     if (res != NO_ERROR) {
87         APP_LOGE("reply result failed");
88         return false;
89     }
90     std::unique_ptr<T> info(reply.ReadParcelable<T>());
91     if (info == nullptr) {
92         APP_LOGE("readParcelableInfo failed");
93         return false;
94     }
95     parcelableInfo = *info;
96     APP_LOGD("get parcelable info success");
97     return true;
98 }
99 }  // namespace AppExecFwk
100 }  // namespace OHOS