1 /*
2 * Copyright (C) 2024 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 "request_utils.h"
17
18 #include <want.h>
19
20 #include "ability_manager_client.h"
21 #include "access_token.h"
22 #include "accesstoken_kit.h"
23 #include "app_mgr_client.h"
24 #include "common_event_data.h"
25 #include "common_event_manager.h"
26 #include "common_event_publish_info.h"
27 #include "cxx.h"
28 #include "int_wrapper.h"
29 #include "log.h"
30 #include "string_wrapper.h"
31 #include "tokenid_kit.h"
32 #include "utils/mod.rs.h"
33
34 namespace OHOS::Request {
35 using namespace OHOS::Security::AccessToken;
36 using namespace OHOS::EventFwk;
37
GetTopUid(int &uid)38 int GetTopUid(int &uid)
39 {
40 sptr<IRemoteObject> token;
41 auto ret = OHOS::AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token);
42 if (ret != 0) {
43 REQUEST_HILOGE("GetTopUid failed, ret: %{public}d", ret);
44 return ret;
45 }
46 auto info = OHOS::AppExecFwk::RunningProcessInfo();
47 AppExecFwk::AppMgrClient().GetRunningProcessInfoByToken(token, info);
48
49 uid = info.uid_;
50 return 0;
51 }
52
GetCallingBundle(rust::u64 tokenId)53 rust::string GetCallingBundle(rust::u64 tokenId)
54 {
55 auto tokenType = AccessTokenKit::GetTokenTypeFlag(static_cast<uint32_t>(tokenId));
56 if (tokenType != TOKEN_HAP) {
57 REQUEST_HILOGE("invalid token");
58 return rust::string("");
59 }
60 HapTokenInfo info;
61 int ret = AccessTokenKit::GetHapTokenInfo(tokenId, info);
62 if (ret != 0) {
63 REQUEST_HILOGE("failed to get hap info, ret: %{public}d", ret);
64 return rust::string("");
65 }
66 return rust::string(info.bundleName);
67 }
68
IsSystemAPI(uint64_t tokenId)69 bool IsSystemAPI(uint64_t tokenId)
70 {
71 return TokenIdKit::IsSystemAppByFullTokenID(tokenId);
72 }
73
CheckPermission(uint64_t tokenId, rust::str permission)74 bool CheckPermission(uint64_t tokenId, rust::str permission)
75 {
76 auto perm = std::string(permission);
77 TypeATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(static_cast<AccessTokenID>(tokenId));
78 if (tokenType == TOKEN_INVALID) {
79 REQUEST_HILOGE("invalid token id");
80 return false;
81 }
82 int result = AccessTokenKit::VerifyAccessToken(tokenId, perm);
83 if (result != PERMISSION_GRANTED) {
84 REQUEST_HILOGE("check permission %{public}s failed ret %{public}d", perm.c_str(), result);
85 return false;
86 }
87 return true;
88 }
89
PublishStateChangeEvent(rust::str bundleName, uint32_t taskId, int32_t state)90 bool PublishStateChangeEvent(rust::str bundleName, uint32_t taskId, int32_t state)
91 {
92 REQUEST_HILOGD("PublishStateChangeEvents in.");
93 static constexpr const char *eventAction = "ohos.request.event.COMPLETE";
94
95 Want want;
96 want.SetAction(eventAction);
97 want.SetBundle(std::string(bundleName));
98
99 std::string data = std::to_string(taskId);
100 CommonEventData commonData(want, state, data);
101 CommonEventPublishInfo publishInfo;
102 publishInfo.SetBundleName(std::string(bundleName));
103
104 bool res = CommonEventManager::PublishCommonEvent(commonData, publishInfo);
105 if (!res) {
106 REQUEST_HILOGE("PublishStateChangeEvents failed!");
107 }
108 return res;
109 }
110
111 } // namespace OHOS::Request