1 /*
2  * Copyright (c) 2021-2022 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_ACE_FRAMEWORKS_CORE_COMMON_ACE_APPLICATION_INFO_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ACE_APPLICATION_INFO_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <mutex>
22 #include <set>
23 #include <shared_mutex>
24 #include <string>
25 #include <vector>
26 
27 #include "base/json/json_util.h"
28 #include "base/utils/macros.h"
29 #include "base/utils/noncopyable.h"
30 #include "base/utils/string_utils.h"
31 #include "interfaces/inner_api/ace/ace_forward_compatibility.h"
32 
33 namespace OHOS::Ace {
34 
35 enum class PlatformVersion {
36     VERSION_FIVE = 5,
37     VERSION_SIX,
38     VERSION_SEVEN,
39     VERSION_EIGHT,
40     VERSION_NINE,
41     VERSION_TEN,
42     VERSION_ELEVEN,
43     VERSION_TWELVE,
44     VERSION_THIRTEEN,
45     VERSION_FOURTEEN
46 };
47 struct AceBundleInfo {
48     uint32_t versionCode = 0;
49     std::string versionName;
50 };
51 
52 class ACE_FORCE_EXPORT AceApplicationInfo : public NonCopyable {
53 public:
54     ACE_EXPORT static AceApplicationInfo& GetInstance();
55 
56     virtual void SetLocale(const std::string& language, const std::string& countryOrRegion, const std::string& script,
57         const std::string& keywordsAndValues) = 0;
58     virtual void ChangeLocale(const std::string& language, const std::string& countryOrRegion) = 0;
59     virtual void SetDebug(bool isDebugVersion, bool needDebugBreakpoint) = 0;
60 
SetUserId(int userId)61     void SetUserId(int userId)
62     {
63         userId_ = userId;
64     }
65 
GetUserId()66     int GetUserId() const
67     {
68         return userId_;
69     }
70 
71     void SetPackageName(const std::string& packageName);
72     const std::string& GetPackageName() const;
73 
74     void SetUid(int32_t uid);
75     int32_t GetUid() const;
76 
77     void SetProcessName(const std::string& processName);
78     const std::string& GetProcessName() const;
79 
80     void SetAbilityName(const std::string& abilityName_);
81     const std::string& GetAbilityName() const;
82 
SetDataFileDirPath(const std::string & dataDirFilePath)83     void SetDataFileDirPath(const std::string& dataDirFilePath)
84     {
85         dataDirFilePath_ = dataDirFilePath;
86     }
GetDataFileDirPath()87     const std::string& GetDataFileDirPath() const
88     {
89         return dataDirFilePath_;
90     }
91 
SetApiTargetVersion(int32_t apiVersion)92     void SetApiTargetVersion(int32_t apiVersion)
93     {
94         apiVersion_ = apiVersion;
95     }
GetApiTargetVersion()96     int32_t GetApiTargetVersion() const
97     {
98         return apiVersion_;
99     }
100 
GreatOrEqualTargetAPIVersion(PlatformVersion version)101     bool GreatOrEqualTargetAPIVersion(PlatformVersion version)
102     {
103         return (apiVersion_ % 1000) >= static_cast<int32_t>(version);
104     }
105 
SetAppVersionName(const std::string & versionName)106     void SetAppVersionName(const std::string& versionName)
107     {
108         versionName_ = versionName;
109     }
110 
GetAppVersionName()111     const std::string& GetAppVersionName() const
112     {
113         return versionName_;
114     }
115 
SetAppVersionCode(uint32_t versionCode)116     void SetAppVersionCode(uint32_t versionCode)
117     {
118         versionCode_ = versionCode;
119     }
120 
GetAppVersionCode()121     uint32_t GetAppVersionCode() const
122     {
123         return versionCode_;
124     }
125 
126     virtual bool GetBundleInfo(const std::string& packageName, AceBundleInfo& bundleInfo) = 0;
127     virtual double GetLifeTime() const = 0;
128 
129     virtual std::string GetJsEngineParam(const std::string& key) const = 0;
130 
GetCountryOrRegion()131     const std::string& GetCountryOrRegion() const
132     {
133         return countryOrRegion_;
134     }
135 
GetLanguage()136     const std::string& GetLanguage() const
137     {
138         return language_;
139     }
140 
GetScript()141     const std::string& GetScript() const
142     {
143         return script_;
144     }
145 
GetLocaleTag()146     const std::string& GetLocaleTag() const
147     {
148         return localeTag_;
149     }
150 
151     std::string GetUnicodeSetting() const;
152 
IsRightToLeft()153     bool IsRightToLeft() const
154     {
155         return isRightToLeft_;
156     }
157 
IsDebugVersion()158     bool IsDebugVersion() const
159     {
160         return isDebugVersion_;
161     }
162 
IsNeedDebugBreakPoint()163     bool IsNeedDebugBreakPoint() const
164     {
165         return needDebugBreakpoint_;
166     }
167 
SetNeedDebugBreakPoint(const bool needDebugBreakpoint)168     void SetNeedDebugBreakPoint(const bool needDebugBreakpoint)
169     {
170         needDebugBreakpoint_ = needDebugBreakpoint;
171     }
172 
SetCardType()173     void SetCardType()
174     {
175         isCardType_ = true;
176     }
177 
GetIsCardType()178     bool GetIsCardType() const
179     {
180         return isCardType_;
181     }
SetBarrierfreeDuration(int32_t duration)182     void SetBarrierfreeDuration(int32_t duration)
183     {
184         barrierfreeDuration_ = duration;
185     }
GetBarrierfreeDuration()186     int32_t GetBarrierfreeDuration() const
187     {
188         return barrierfreeDuration_;
189     }
SetAccessibilityEnabled(bool isEnabled)190     void SetAccessibilityEnabled(bool isEnabled)
191     {
192         isAccessibilityEnabled_ = isEnabled;
193     }
IsAccessibilityEnabled()194     bool IsAccessibilityEnabled() const
195     {
196         return isAccessibilityEnabled_;
197     }
SetPid(int32_t pid)198     void SetPid(int32_t pid)
199     {
200         pid_ = pid;
201     }
GetPid()202     int32_t GetPid() const
203     {
204         return pid_;
205     }
206 
SetMissionId(int32_t missionId)207     void SetMissionId(int32_t missionId)
208     {
209         missionId_ = missionId;
210     }
GetMissionId()211     int32_t GetMissionId() const
212     {
213         return missionId_;
214     }
215 
IsUseNewPipeline()216     bool IsUseNewPipeline()
217     {
218         return useNewPipeline_.value_or(AceForwardCompatibility::IsUseNG());
219     }
220 
SetIsUseNewPipeline(bool useNewPipeline)221     void SetIsUseNewPipeline(bool useNewPipeline)
222     {
223         useNewPipeline_.emplace(useNewPipeline);
224     }
225 
SetTouchEventsPassThroughMode(bool isTouchEventsPassThrough)226     void SetTouchEventsPassThroughMode(bool isTouchEventsPassThrough)
227     {
228         std::unique_lock<std::shared_mutex> lock(eventsPassThroughMutex_);
229         isTouchEventsPassThrough_ = isTouchEventsPassThrough;
230     }
231 
IsTouchEventsPassThrough()232     bool IsTouchEventsPassThrough() const
233     {
234         std::shared_lock<std::shared_mutex> lock(eventsPassThroughMutex_);
235         return isTouchEventsPassThrough_;
236     }
237 
238 protected:
239     std::string countryOrRegion_;
240     std::string language_;
241     std::string script_;
242     std::string localeTag_;
243     std::string keywordsAndValues_;
244 
245     std::string packageName_;
246     std::string processName_;
247     std::string dataDirFilePath_;
248     std::string abilityName_;
249     int32_t pid_ = 0;
250     int32_t uid_ = 0;
251     int32_t barrierfreeDuration_ = 0;
252 
253     bool isRightToLeft_ = false;
254     bool isDebugVersion_ = false;
255     bool needDebugBreakpoint_ = false;
256     std::optional<bool> useNewPipeline_;
257 
258     bool isCardType_ = false;
259 
260     int userId_ = 0;
261     bool isAccessibilityEnabled_ = false;
262 
263     int32_t apiVersion_ = 0;
264     std::string versionName_;
265     uint32_t versionCode_ = 0;
266     int32_t missionId_ = -1;
267     mutable std::shared_mutex eventsPassThroughMutex_;
268     bool isTouchEventsPassThrough_ = false;
269 };
270 
271 } // namespace OHOS::Ace
272 
273 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ACE_APPLICATION_INFO_H
274