• 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 "ability_interceptor.h"
17 
18 #include <chrono>
19 #include <string>
20 
21 #include "ability_manager_errors.h"
22 #include "app_running_control_rule_result.h"
23 #include "bundlemgr/bundle_mgr_interface.h"
24 #include "bundle_constants.h"
25 #include "hilog_wrapper.h"
26 #include "in_process_call_wrapper.h"
27 #include "ipc_skeleton.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace AAFwk {
32 const std::string ACTION_MARKET_CROWDTEST = "ohos.want.action.marketCrowdTest";
33 const std::string ACTION_MARKET_DISPOSED = "ohos.want.action.marketDisposed";
34 const std::string PERMISSION_MANAGE_DISPOSED_APP_STATUS = "ohos.permission.MANAGE_DISPOSED_APP_STATUS";
35 
~AbilityInterceptor()36 AbilityInterceptor::~AbilityInterceptor()
37 {}
38 
CrowdTestInterceptor()39 CrowdTestInterceptor::CrowdTestInterceptor()
40 {}
41 
~CrowdTestInterceptor()42 CrowdTestInterceptor::~CrowdTestInterceptor()
43 {}
44 
DoProcess(const Want & want,int requestCode,int32_t userId,bool isForeground)45 ErrCode CrowdTestInterceptor::DoProcess(const Want &want, int requestCode, int32_t userId, bool isForeground)
46 {
47     if (CheckCrowdtest(want, userId)) {
48         HILOG_ERROR("Crowdtest expired.");
49 #ifdef SUPPORT_GRAPHICS
50         if (isForeground) {
51             int ret = IN_PROCESS_CALL(AbilityUtil::StartAppgallery(requestCode, userId, ACTION_MARKET_CROWDTEST));
52             if (ret != ERR_OK) {
53                 HILOG_ERROR("Crowdtest implicit start appgallery failed.");
54                 return ret;
55             }
56         }
57 #endif
58         return ERR_CROWDTEST_EXPIRED;
59     }
60     return ERR_OK;
61 }
62 
CheckCrowdtest(const Want & want,int32_t userId)63 bool CrowdTestInterceptor::CheckCrowdtest(const Want &want, int32_t userId)
64 {
65     // get bms
66     auto bms = AbilityUtil::GetBundleManager();
67     if (!bms) {
68         HILOG_ERROR("GetBundleManager failed");
69         return false;
70     }
71 
72     // get crowdtest status and time
73     std::string bundleName = want.GetBundle();
74     AppExecFwk::ApplicationInfo callerAppInfo;
75     bool result = IN_PROCESS_CALL(
76         bms->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
77             userId, callerAppInfo)
78     );
79     if (!result) {
80         HILOG_ERROR("GetApplicaionInfo from bms failed.");
81         return false;
82     }
83 
84     auto appDistributionType = callerAppInfo.appDistributionType;
85     auto appCrowdtestDeadline = callerAppInfo.crowdtestDeadline;
86     int64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::
87         system_clock::now().time_since_epoch()).count();
88     if (appDistributionType == AppExecFwk::Constants::APP_DISTRIBUTION_TYPE_CROWDTESTING &&
89         appCrowdtestDeadline < now) {
90         HILOG_INFO("The application is expired, expired time is %{public}s",
91             std::to_string(appCrowdtestDeadline).c_str());
92         return true;
93     }
94     return false;
95 }
96 
ControlInterceptor()97 ControlInterceptor::ControlInterceptor()
98 {}
99 
~ControlInterceptor()100 ControlInterceptor::~ControlInterceptor()
101 {}
102 
DoProcess(const Want & want,int requestCode,int32_t userId,bool isForeground)103 ErrCode ControlInterceptor::DoProcess(const Want &want, int requestCode, int32_t userId, bool isForeground)
104 {
105     AppExecFwk::AppRunningControlRuleResult controlRule;
106     if (CheckControl(want, userId, controlRule)) {
107         HILOG_INFO("The target application is intercpted. %{public}s", controlRule.controlMessage.c_str());
108 #ifdef SUPPORT_GRAPHICS
109         if (isForeground && controlRule.controlWant != nullptr) {
110             int ret = IN_PROCESS_CALL(AbilityManagerClient::GetInstance()->StartAbility(*controlRule.controlWant,
111                 userId, requestCode));
112             if (ret != ERR_OK) {
113                 HILOG_ERROR("Control implicit start appgallery failed.");
114                 return ret;
115             }
116         }
117 #endif
118         return ERR_DISPOSED_STATUS;
119     }
120     return ERR_OK;
121 }
122 
CheckControl(const Want & want,int32_t userId,AppExecFwk::AppRunningControlRuleResult & controlRule)123 bool ControlInterceptor::CheckControl(const Want &want, int32_t userId,
124     AppExecFwk::AppRunningControlRuleResult &controlRule)
125 {
126     // get bms
127     auto bms = AbilityUtil::GetBundleManager();
128     if (!bms) {
129         HILOG_ERROR("GetBundleManager failed");
130         return false;
131     }
132 
133     // get disposed status
134     std::string bundleName = want.GetBundle();
135     auto appControlMgr = bms->GetAppControlProxy();
136     if (appControlMgr == nullptr) {
137         HILOG_ERROR("Get appControlMgr failed");
138         return false;
139     }
140 
141     auto ret = IN_PROCESS_CALL(appControlMgr->GetAppRunningControlRule(bundleName, userId, controlRule));
142     if (ret != ERR_OK) {
143         HILOG_INFO("Get No AppRunningControlRule");
144         return false;
145     }
146     return true;
147 }
148 } // namespace AAFwk
149 } // namespace OHOS
150