• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #ifndef OHOS_REQUEST_ACTION_WRAPPER_H
17 #define OHOS_REQUEST_ACTION_WRAPPER_H
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <memory>
22 #include <vector>
23 
24 #include "request_action.h"
25 #include "accesstoken_kit.h"
26 #include "cxx.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 namespace OHOS::Request {
30 
31 const static std::vector<std::string> Permissions = { "ohos.permission.INTERNET",
32     "ohos.permission.UPLOAD_SESSION_MANAGER", "ohos.permission.DOWNLOAD_SESSION_MANAGER" };
33 
DisableTaskNotification(rust::str taskId)34 inline void DisableTaskNotification(rust::str taskId)
35 {
36     std::vector<std::string> tids = { std::string(taskId) };
37     auto w = std::unordered_map<std::string, ExceptionErrorCode>();
38     RequestAction::GetInstance()->DisableTaskNotification(tids, w);
39     for (auto &elem : w) {
40         std::cout << "task" << elem.first << static_cast<int32_t>(elem.second) << std::endl;
41     }
42 }
43 
Permission(std::unique_ptr<const char * []> & perms)44 inline NativeTokenInfoParams Permission(std::unique_ptr<const char *[]> &perms)
45 {
46     return NativeTokenInfoParams{
47         .dcapsNum = 0,
48         .permsNum = Permissions.size(),
49         .aclsNum = 0,
50         .perms = perms.get(),
51         .processName = "disable_task_notification",
52         .aplStr = "system_core",
53     };
54 }
55 
SetMode(rust::str taskId,int32_t mode)56 inline void SetMode(rust::str taskId, int32_t mode)
57 {
58     std::string tid = std::string(taskId);
59     RequestAction::GetInstance()->SetMode(tid, static_cast<Mode>(mode));
60 }
61 
SetAccessTokenPermission()62 inline void SetAccessTokenPermission()
63 {
64     auto perms = std::make_unique<const char *[]>(Permissions.size());
65     for (size_t i = 0; i < Permissions.size(); i++) {
66         perms[i] = Permissions[i].c_str();
67     }
68     auto infoInstance = Permission(perms);
69     auto tokenId = GetAccessTokenId(&infoInstance);
70     SetSelfTokenID(tokenId);
71     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
72 }
73 
74 } // namespace OHOS::Request
75 #endif