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_SERVICE_H
17 #define OHOS_ABILITY_RUNTIME_QUICK_FIX_MANAGER_SERVICE_H
18 
19 #include <mutex>
20 
21 #include "event_runner.h"
22 #include "event_handler.h"
23 #include "quick_fix/quick_fix_manager_interface.h"
24 #include "quick_fix_manager_apply_task.h"
25 #include "quick_fix_manager_stub.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 class QuickFixManagerService : public QuickFixManagerStub,
30                                public std::enable_shared_from_this<QuickFixManagerService> {
31 public:
32     QuickFixManagerService() = default;
33     virtual ~QuickFixManagerService() = default;
34 
35     /**
36      * @brief Get the instance of quick fix manager service.
37      *
38      * @return Returns the instance.
39      */
40     static sptr<QuickFixManagerService> GetInstance();
41 
42     /**
43      * @brief Init quick fix manager service, such as event runner and event handler.
44      *
45      * @return Returns true when init succeed, false when init failed.
46      */
47     bool Init();
48 
49     /**
50      * @brief Apply quick fix.
51      *
52      * @param quickFixFiles Quick fix files need to apply, this value should include file path and file name.
53      * @param isDebug this value is for the quick fix debug mode selection.
54      * @return Returns 0 on success, error code on failure.
55      */
56     int32_t ApplyQuickFix(const std::vector<std::string> &quickFixFiles, bool isDebug = false) override;
57 
58     /**
59      * @brief Get applyed quick fix info.
60      *
61      * @param bundleName Bundle name of quick fix info.
62      * @param quickFixInfo Quick fix info, including bundleName, bundleVersion and so on.
63      * @return Returns 0 on success, error code on failure.
64      */
65     int32_t GetApplyedQuickFixInfo(const std::string &bundleName, ApplicationQuickFixInfo &quickFixInfo) override;
66 
67     /**
68      * @brief Revoke quick fix by bundle name.
69      *
70      * @param bundleName quick fix files need to revoke.
71      * @return returns QUICK_FIX_OK on success, error code on failure.
72      */
73     int32_t RevokeQuickFix(const std::string &bundleName) override;
74 
75     /**
76      * @brief Remove quick fix apply task.
77      *
78      */
79     void RemoveApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask);
80 
81 private:
82     bool CheckTaskRunningState(const std::string &bundleName);
83     void AddApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask);
84     int32_t GetQuickFixInfo(const std::string &bundleName, bool &patchExists, bool &isSoContained);
85 
86     static std::mutex mutex_;
87     static sptr<QuickFixManagerService> instance_;
88     std::mutex eventMutex_;
89     std::mutex taskMutex_;
90     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
91     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
92     std::vector<std::shared_ptr<QuickFixManagerApplyTask>> applyTasks_;
93 
94     DISALLOW_COPY_AND_MOVE(QuickFixManagerService);
95 };
96 } // namespace AAFwk
97 } // namespace OHOS
98 #endif // OHOS_ABILITY_RUNTIME_QUICK_FIX_MANAGER_SERVICE_H
99