1 /*
2  * Copyright (c) 2023-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 FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_HANDLER
17 #define FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_HANDLER
18 
19 #include <mutex>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "aot/aot_args.h"
25 #include "bundle_mgr_service.h"
26 #include "event_report.h"
27 #include "inner_bundle_info.h"
28 #include "nocopyable.h"
29 #include "serial_queue.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 class AOTHandler final {
34 public:
35     static AOTHandler& GetInstance();
36     void HandleInstall(const std::unordered_map<std::string, InnerBundleInfo> &infos) const;
37     void HandleOTA();
38     void HandleIdle() const;
39     ErrCode HandleCompile(const std::string &bundleName, const std::string &compileMode, bool isAllBundle,
40         std::vector<std::string> &compileResults) const;
41     void HandleResetAOT(const std::string &bundleName, bool isAllBundle) const;
42     ErrCode HandleCopyAp(const std::string &bundleName, bool isAllBundle, std::vector<std::string> &results) const;
43 private:
44     AOTHandler();
45     ~AOTHandler() = default;
46     DISALLOW_COPY_AND_MOVE(AOTHandler);
47 
48     ErrCode MkApDestDirIfNotExist() const;
49     void CopyApWithBundle(const std::string &bundleName, const BundleInfo &bundleInfo,
50         const int32_t userId, std::vector<std::string> &results) const;
51     std::string GetSouceAp(const std::string &mergedAp, const std::string &rtAp) const;
52     bool IsSupportARM64() const;
53     std::string GetArkProfilePath(const std::string &bundleName, const std::string &moduleName) const;
54     std::optional<AOTArgs> BuildAOTArgs(const InnerBundleInfo &info, const std::string &moduleName,
55         const std::string &compileMode, bool isEnableBaselinePgo = false) const;
56     void HandleInstallWithSingleHap(const InnerBundleInfo &info, const std::string &compileMode) const;
57     bool NeedCompile(const InnerBundleInfo &info, const std::string &moduleName) const;
58     ErrCode HandleCompileWithSingleHap(const InnerBundleInfo &info, const std::string &moduleName,
59         const std::string &compileMode, bool isEnableBaselinePgo = false) const;
60     EventInfo HandleCompileWithBundle(const std::string &bundleName, const std::string &compileMode,
61         std::shared_ptr<BundleDataMgr> dataMgr) const;
62     ErrCode HandleCompileBundles(const std::vector<std::string> &bundleNames, const std::string &compileMode,
63         std::shared_ptr<BundleDataMgr> &dataMgr, std::vector<std::string> &compileResults) const;
64     ErrCode HandleCompileModules(const std::vector<std::string> &moduleNames, const std::string &compileMode,
65         InnerBundleInfo &info, std::string &compileResult) const;
66     void ClearArkCacheDir() const;
67     void ResetAOTFlags() const;
68     void HandleIdleWithSingleHap(
69         const InnerBundleInfo &info, const std::string &moduleName, const std::string &compileMode) const;
70     bool CheckDeviceState() const;
71     ErrCode AOTInternal(const std::optional<AOTArgs> &aotArgs, uint32_t versionCode) const;
72     AOTCompileStatus ConvertToAOTCompileStatus(const ErrCode ret) const;
73     void HandleOTACompile();
74     void BeforeOTACompile();
75     void OTACompile() const;
76     void OTACompileInternal() const;
77     bool GetOTACompileList(std::vector<std::string> &bundleNames) const;
78     bool GetUserBehaviourAppList(std::vector<std::string> &bundleNames, int32_t size) const;
79     bool IsOTACompileSwitchOn() const;
80     void ReportSysEvent(const std::map<std::string, EventInfo> &sysEventMap) const;
81 
82     void DeleteArkAp(const BundleInfo &bundleInfo, const int32_t userId) const;
83     void ClearArkAp(const std::string &oldAOTVersion, const std::string &curAOTVersion) const;
84     std::string GetCurAOTVersion() const;
85     bool GetOldAOTVersion(std::string &oldAOTVersion) const;
86     void SaveAOTVersion(const std::string &curAOTVersion) const;
87 private:
88     mutable std::mutex executeMutex_;
89     mutable std::mutex idleMutex_;
90     mutable std::mutex compileMutex_;
91     std::atomic<bool> OTACompileDeadline_ { false };
92     std::shared_ptr<SerialQueue> serialQueue_;
93 };
94 }  // namespace AppExecFwk
95 }  // namespace OHOS
96 #endif  // FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_HANDLER
97