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 #ifndef APP_DOMAIN_VERIFY_VERIFY_TASK_H
17 #define APP_DOMAIN_VERIFY_VERIFY_TASK_H
18 #include <functional>
19 #include <unordered_map>
20 #include <unordered_set>
21 #include "bundle_verify_status_info.h"
22 #include "i_verify_task.h"
23 #include "http_client_response.h"
24 #include "app_verify_base_info.h"
25 #include "inner_verify_status.h"
26 #include "dfx/app_domain_verify_hisysevent.h"
27 #include "skill_uri.h"
28 #include "app_domain_verify_mgr_client.h"
29 #include "i_http_task.h"
30 
31 namespace OHOS {
32 namespace AppDomainVerify {
33 using StatusHandleFunc  = std::function<bool(std::string, int)>;
34 class VerifyTask : public IVerifyTask {
35 public:
36     void OnPostVerify(const std::string &uri, const OHOS::NetStack::HttpClient::HttpClientResponse &response) override;
37     void OnSaveVerifyResult() override;
38     bool OnPreRequest(OHOS::NetStack::HttpClient::HttpClientRequest &request, const std::string &uri) override;
39     OHOS::AppDomainVerify::TaskType GetType() override;
40     const HostVerifyStatusMap &GetUriVerifyMap() override;
41 
42     VerifyTask(OHOS::AppDomainVerify::TaskType type, const AppVerifyBaseInfo &appVerifyBaseInfo,
43         const VerifyResultInfo& verifyResultInfo);
44     void InitUriUnVerifySetMap(const VerifyResultInfo& verifyResultInfo);
45     void Execute();
46 protected:
47     int64_t CalcRetryDuration(int verifyCnt);
48     bool HandleFailureClientError(std::string verifyTime, int verifyCnt);
49     bool HandleStateSuccess(std::string verifyTime, int verifyCnt);
50     bool HandleForbiddenForever(std::string verifyTime, int verifyCnt);
51     bool IsNeedRetry(const std::tuple<InnerVerifyStatus, std::string, int>& info);
52     OHOS::AppDomainVerify::TaskType& GetTaskType();
53     AppVerifyBaseInfo& GetAppVerifyBaseInfo();
54 private:
55     void UpdateVerifyResultInfo(const std::string& uri, InnerVerifyStatus status);
56     virtual bool SaveDomainVerifyStatus(const std::string& bundleName, const VerifyResultInfo& verifyResultInfo);
57     OHOS::AppDomainVerify::TaskType type_;
58     AppVerifyBaseInfo appVerifyBaseInfo_;
59     std::unordered_set<std::string> unVerifiedSet_;
60     VerifyResultInfo verifyResultInfo_;
61     std::unordered_map<InnerVerifyStatus, StatusHandleFunc> staHandlerMap;
62 };
63 }
64 }
65 #endif  // APP_DOMAIN_VERIFY_VERIFY_TASK_H
66