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 "notification.h"
31 #include "notification_constant.h"
32 #include "notification_content.h"
33 #include "notification_helper.h"
34 #include "string_wrapper.h"
35 #include "tokenid_kit.h"
36 #include "utils/mod.rs.h"
37 #include "want_params.h"
38
39 namespace OHOS::Request {
40 using namespace OHOS::Security::AccessToken;
41 using namespace OHOS::Notification;
42 using namespace OHOS::EventFwk;
43 static constexpr uint8_t DOWNLOAD_ACTION = 0;
44
GetTopUid(int & uid)45 int GetTopUid(int &uid)
46 {
47 sptr<IRemoteObject> token;
48 auto ret = OHOS::AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token);
49 if (ret != 0) {
50 REQUEST_HILOGE("GetTopUid failed, ret: %{public}d", ret);
51 return ret;
52 }
53 auto info = OHOS::AppExecFwk::RunningProcessInfo();
54 AppExecFwk::AppMgrClient().GetRunningProcessInfoByToken(token, info);
55
56 uid = info.uid_;
57 return 0;
58 }
59
GetCallingBundle(rust::u64 tokenId)60 rust::string GetCallingBundle(rust::u64 tokenId)
61 {
62 auto tokenType = AccessTokenKit::GetTokenTypeFlag(static_cast<uint32_t>(tokenId));
63 if (tokenType != TOKEN_HAP) {
64 REQUEST_HILOGE("invalid token");
65 return rust::string("");
66 }
67 HapTokenInfo info;
68 int ret = AccessTokenKit::GetHapTokenInfo(tokenId, info);
69 if (ret != 0) {
70 REQUEST_HILOGE("failed to get hap info, ret: %{public}d", ret);
71 return rust::string("");
72 }
73 return rust::string(info.bundleName);
74 }
75
IsSystemAPI(uint64_t tokenId)76 bool IsSystemAPI(uint64_t tokenId)
77 {
78 return TokenIdKit::IsSystemAppByFullTokenID(tokenId);
79 }
80
CheckPermission(uint64_t tokenId,rust::str permission)81 bool CheckPermission(uint64_t tokenId, rust::str permission)
82 {
83 auto perm = std::string(permission);
84 TypeATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(static_cast<AccessTokenID>(tokenId));
85 if (tokenType == TOKEN_INVALID) {
86 REQUEST_HILOGE("invalid token id");
87 return false;
88 }
89 int result = AccessTokenKit::VerifyAccessToken(tokenId, perm);
90 if (result != PERMISSION_GRANTED) {
91 REQUEST_HILOGE("check permission %{public}s failed ret %{public}d", perm.c_str(), result);
92 return false;
93 }
94 return true;
95 }
96
RequestBackgroundNotify(RequestTaskMsg msg,rust::str filePath,rust::str fileName,uint32_t percent)97 int RequestBackgroundNotify(RequestTaskMsg msg, rust::str filePath, rust::str fileName, uint32_t percent)
98 {
99 REQUEST_HILOGD("Background Notification, percent is %{public}d", percent);
100 auto requestTemplate = std::make_shared<NotificationTemplate>();
101
102 requestTemplate->SetTemplateName("downloadTemplate");
103 OHOS::AAFwk::WantParams wantParams;
104 wantParams.SetParam("progressValue", OHOS::AAFwk::Integer::Box(percent));
105 wantParams.SetParam("fileName", OHOS::AAFwk::String::Box(std::string(fileName)));
106 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
107 if (msg.action == DOWNLOAD_ACTION) {
108 wantParams.SetParam("title", OHOS::AAFwk::String::Box("下载"));
109 normalContent->SetTitle("下载");
110 } else {
111 wantParams.SetParam("title", OHOS::AAFwk::String::Box("上传"));
112 normalContent->SetTitle("上传");
113 }
114 requestTemplate->SetTemplateData(std::make_shared<OHOS::AAFwk::WantParams>(wantParams));
115 normalContent->SetText(std::string(fileName));
116
117 auto content = std::make_shared<NotificationContent>(normalContent);
118 NotificationRequest req(msg.task_id);
119 req.SetCreatorUid(msg.uid);
120 req.SetContent(content);
121 req.SetTemplate(requestTemplate);
122 req.SetSlotType(NotificationConstant::OTHER);
123 OHOS::ErrCode errCode = NotificationHelper::PublishNotification(req);
124 if (errCode != OHOS::ERR_OK) {
125 REQUEST_HILOGE("notification errCode: %{public}d", errCode);
126 }
127 return errCode;
128 }
129
PublishStateChangeEvent(rust::str bundleName,uint32_t taskId,int32_t state,int32_t uid)130 bool PublishStateChangeEvent(rust::str bundleName, uint32_t taskId, int32_t state, int32_t uid)
131 {
132 REQUEST_HILOGD("PublishStateChangeEvents in.");
133 static constexpr const char *eventAction = "ohos.request.event.COMPLETE";
134
135 Want want;
136 want.SetAction(eventAction);
137 want.SetBundle(std::string(bundleName));
138 std::vector<int32_t> subscriberUids;
139 subscriberUids.push_back(uid);
140
141 std::string data = std::to_string(taskId);
142 CommonEventData commonData(want, state, data);
143 CommonEventPublishInfo publishInfo;
144 publishInfo.SetBundleName(std::string(bundleName));
145 publishInfo.SetSubscriberUid(subscriberUids);
146
147 bool res = CommonEventManager::PublishCommonEvent(commonData, publishInfo);
148 if (!res) {
149 REQUEST_HILOGE("PublishStateChangeEvents failed!");
150 }
151 return res;
152 }
153
154 } // namespace OHOS::Request