1 /*
2  * Copyright (c) 2022-2023 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_ABILITY_RUNTIME_QUICK_FIX_MANAGER_APPLY_TASK_H
17 #define OHOS_ABILITY_RUNTIME_QUICK_FIX_MANAGER_APPLY_TASK_H
18 
19 #include "app_mgr_interface.h"
20 #include "event_handler.h"
21 #include "quick_fix_result_info.h"
22 #include "quick_fix/quick_fix_manager_interface.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 class QuickFixManagerService;
27 class QuickFixManagerApplyTask : public std::enable_shared_from_this<QuickFixManagerApplyTask> {
28 public:
QuickFixManagerApplyTask(sptr<AppExecFwk::IQuickFixManager> bundleQfMgr,sptr<AppExecFwk::IAppMgr> appMgr,std::shared_ptr<AppExecFwk::EventHandler> handler,wptr<QuickFixManagerService> service)29     QuickFixManagerApplyTask(sptr<AppExecFwk::IQuickFixManager> bundleQfMgr, sptr<AppExecFwk::IAppMgr> appMgr,
30         std::shared_ptr<AppExecFwk::EventHandler> handler, wptr<QuickFixManagerService> service)
31         : bundleQfMgr_(bundleQfMgr), appMgr_(appMgr), eventHandler_(handler), quickFixMgrService_(service)
32     {}
33 
34     virtual ~QuickFixManagerApplyTask();
35 
36     enum TaskType {
37         QUICK_FIX_APPLY,
38         QUICK_FIX_REVOKE,
39     };
40 
41     void Run(const std::vector<std::string> &quickFixFiles, bool isDebug = false);
42     void HandlePatchDeployed();
43     void HandlePatchSwitched();
44     void HandlePatchDeleted();
45 
46     bool SetQuickFixInfo(const std::shared_ptr<AppExecFwk::QuickFixResult> &result);
47     bool ExtractQuickFixDataFromJson(nlohmann::json& resultJson);
48     bool GetRunningState();
49 
50     void RemoveTimeoutTask();
51     void NotifyApplyStatus(int32_t resultCode);
52     void RemoveSelf();
53 
54     void PostSwitchQuickFixTask();
55     void PostDeleteQuickFixTask();
56 
57     void UnregAppStateObserver();
58 
59     void RunRevoke();
60     void InitRevokeTask(const std::string &bundleName, bool isSoContained);
61     std::string GetBundleName();
62     TaskType GetTaskType();
63     void HandleRevokePatchDeleted();
64     void HandleRevokePatchSwitched();
65     void PostRevokeQuickFixDeleteTask();
66     void PostRevokeQuickFixProcessDiedTask();
67 private:
68     void PostDeployQuickFixTask(const std::vector<std::string> &quickFixFiles, bool isDebug = false);
69     void PostTimeOutTask();
70     void PostNotifyLoadRepairPatchTask();
71     void PostNotifyUnloadRepairPatchTask();
72     void PostNotifyHotReloadPageTask();
73     void RegAppStateObserver();
74     void PostRevokeQuickFixTask();
75     void HandleRevokeQuickFixAppRunning();
76     void PostRevokeQuickFixNotifyUnloadPatchTask();
77     void HandleRevokeQuickFixAppStop();
78 
79     sptr<AppExecFwk::IQuickFixManager> bundleQfMgr_ = nullptr;
80     sptr<AppExecFwk::IAppMgr> appMgr_ = nullptr;
81     sptr<AppExecFwk::IApplicationStateObserver> appStateCallback_ = nullptr;
82     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
83     wptr<QuickFixManagerService> quickFixMgrService_ = nullptr;
84     std::string bundleName_;
85     int bundleVersionCode_ = 0;
86     int patchVersionCode_ = 0;
87     bool isRunning_ = false;
88     bool isSoContained_ = false;
89     AppExecFwk::QuickFixType type_ = AppExecFwk::QuickFixType::UNKNOWN;
90     std::vector<std::string> moduleNames_;
91     TaskType taskType_ = TaskType::QUICK_FIX_APPLY;
92 };
93 } // namespace AAFwk
94 } // namespace OHOS
95 #endif // OHOS_ABILITY_RUNTIME_QUICK_FIX_MANAGER_APPLY_TASK_H
96