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 "interceptor/crowd_test_interceptor.h"
17 
18 #include "ability_util.h"
19 #include "start_ability_utils.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 constexpr const char* ACTION_MARKET_CROWDTEST = "ohos.want.action.marketCrowdTest";
25 }
DoProcess(AbilityInterceptorParam param)26 ErrCode CrowdTestInterceptor::DoProcess(AbilityInterceptorParam param)
27 {
28     if (StartAbilityUtils::skipCrowTest) {
29         StartAbilityUtils::skipCrowTest = false;
30         return ERR_OK;
31     }
32     if (CheckCrowdtest(param.want, param.userId)) {
33         TAG_LOGE(AAFwkTag::ABILITYMGR, "Crowdtest expired.");
34 #ifdef SUPPORT_GRAPHICS
35         if (param.isWithUI) {
36             int ret = IN_PROCESS_CALL(AbilityUtil::StartAppgallery(param.want.GetBundle(), param.requestCode,
37                 param.userId, ACTION_MARKET_CROWDTEST));
38             if (ret != ERR_OK) {
39                 TAG_LOGE(AAFwkTag::ABILITYMGR, "Crowdtest implicit start appgallery failed.");
40                 return ret;
41             }
42         }
43 #endif
44         return ERR_CROWDTEST_EXPIRED;
45     }
46     return ERR_OK;
47 }
48 
CheckCrowdtest(const Want & want,int32_t userId)49 bool CrowdTestInterceptor::CheckCrowdtest(const Want &want, int32_t userId)
50 {
51     // get crowdtest status and time
52     AppExecFwk::ApplicationInfo appInfo;
53     if (!StartAbilityUtils::GetApplicationInfo(want.GetBundle(), userId, appInfo)) {
54         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get application info.");
55         return false;
56     }
57 
58     auto appCrowdtestDeadline = appInfo.crowdtestDeadline;
59     int64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::
60         system_clock::now().time_since_epoch()).count();
61     if (appCrowdtestDeadline > 0 && appCrowdtestDeadline < now) {
62         TAG_LOGI(AAFwkTag::ABILITYMGR, "The application is expired, expired time is %{public}s",
63             std::to_string(appCrowdtestDeadline).c_str());
64         return true;
65     }
66     return false;
67 }
68 } // namespace AAFwk
69 } // namespace OHOS