• 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 "app_manager.h"
17 
18 #include "ability_manager_client.h"
19 #include "element_name.h"
20 #include "wukong_define.h"
21 #include "wukong_util.h"
22 
23 namespace OHOS {
24 namespace WuKong {
25 namespace {
26 const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
27 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
28 
29 const std::string STRING_START_ABILITY_OK = "start ability successfully.";
30 const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
31 }  // namespace
32 
AllowAbilityStart(const AAFwk::Want & want,const std::string & bundleName)33 bool AppManager::BlockAbilityController::AllowAbilityStart(const AAFwk::Want &want, const std::string &bundleName)
34 {
35     TRACK_LOG_STD();
36     std::vector<std::string> blocklist;
37     std::vector<std::string> tempAllowList;
38     bool orderFlag;
39     auto util = WuKongUtil::GetInstance();
40 
41     tempAllowList = util->GetTempAllowList();
42     // if bundleName in the tempAllow list to allow ability start.
43     auto it = find(tempAllowList.begin(), tempAllowList.end(), bundleName);
44     orderFlag = util->GetOrderFlag();
45     if (orderFlag && tempAllowList.size() != 0) {
46         if (it != tempAllowList.end()) {
47             DEBUG_LOG("bundle start allow");
48             return true;
49         } else {
50             return false;
51         }
52     }
53 
54     util->GetBlockList(blocklist);
55     DEBUG_LOG_STR("BundleName: %s", bundleName.c_str());
56 
57     // if bundleName in the block list to unallow ability start.
58     it = find(blocklist.begin(), blocklist.end(), bundleName);
59     if (it == blocklist.end()) {
60         DEBUG_LOG("bundle start prohibition");
61         return true;
62     }
63 
64     TRACK_LOG_END();
65     return false;
66 }
67 
68 // turn to background
AllowAbilityBackground(const std::string & bundleName)69 bool AppManager::BlockAbilityController::AllowAbilityBackground(const std::string &bundleName)
70 {
71     if (WuKongUtil::GetInstance()->GetOrderFlag()) {
72         return false;
73     } else {
74         return true;
75     }
76 }
77 
StartAbilityByBundleInfo(std::string abilityName,std::string bundleName)78 ErrCode AppManager::StartAbilityByBundleInfo(std::string abilityName, std::string bundleName)
79 {
80     TRACK_LOG_STD();
81     AAFwk::Want want;
82     int result;
83     std::string output;
84     if (abilityName.size() == 0 || bundleName.size() == 0) {
85         if (abilityName.size() == 0) {
86             output.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
87         }
88         if (bundleName.size() == 0) {
89             output.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
90         }
91         result = OHOS::ERR_INVALID_VALUE;
92     } else {
93         AppExecFwk::ElementName element("", bundleName, abilityName);
94         want.SetElement(element);
95         result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
96         if (result == OHOS::ERR_OK) {
97             output = STRING_START_ABILITY_OK;
98         } else {
99             output = STRING_START_ABILITY_NG;
100         }
101     }
102     DEBUG_LOG_STR("%s", output.c_str());
103     TRACK_LOG_STR("result %s", std::to_string(result).c_str());
104     return result;
105 }
106 
SetAbilityController()107 void AppManager::SetAbilityController()
108 {
109     if (abilityController_ == nullptr) {
110         abilityController_ = new (std::nothrow) BlockAbilityController();
111     }
112     OHOS::AAFwk::AbilityManagerClient::GetInstance()->SetAbilityController(abilityController_, true);
113 }
114 }  // namespace WuKong
115 }  // namespace OHOS
116