• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 JSAPPIMPL_H
17 #define JSAPPIMPL_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <thread>
22 
23 #include "JsApp.h"
24 #include "ace_ability.h"
25 #include "ace_run_args.h"
26 #include "device_config.h"
27 #include "device_type.h"
28 
29 
30 namespace OHOS {
31 namespace Previewer {
32     struct PreviewerWindowModel;
33 }
34 namespace Rosen {
35     class GlfwRenderContext;
36 }
37 #if defined(__APPLE__) || defined(_WIN32)
38 namespace AbilityRuntime {
39     class Simulator;
40     struct Options;
41 }
42 namespace AppExecFwk {
43     class Configuration;
44 }
45 #endif
46 }
47 
48 class JsAppImpl : public JsApp {
49 public:
50     JsAppImpl() noexcept;
51     ~JsAppImpl();
52     JsAppImpl& operator=(const JsAppImpl&) = delete;
53     JsAppImpl(const JsAppImpl&) = delete;
54 
55     static JsAppImpl& GetInstance();
56     void Start() override;
57     void Restart() override;
58     void Interrupt() override;
59     std::string GetJSONTree() override;
60     std::string GetDefaultJSONTree() override;
61     void OrientationChanged(std::string commandOrientation) override;
62     void ResolutionChanged(int32_t changedOriginWidth, int32_t changedOriginHeight,
63                            int32_t changedWidth, int32_t changedHeight, int32_t screenDensity) override;
64     void SetResolutionParams(int32_t changedOriginWidth, int32_t changedOriginHeight,
65                            int32_t changedWidth, int32_t changedHeight, int32_t screenDensity);
66     void SetArgsColorMode(const std::string& value) override;
67     void SetArgsAceVersion(const std::string& value) override;
68     void SetDeviceOrentation(const std::string& value);
69     std::string GetOrientation() const override;
70     std::string GetColorMode() const override;
71     void ColorModeChanged(const std::string commandColorMode) override;
72     void ReloadRuntimePage(const std::string currentPage) override;
73     void SetScreenDensity(const std::string value) override;
74     void SetConfigChanges(const std::string value) override;
75     bool MemoryRefresh(const std::string memoryRefreshArgs) const override;
76     void LoadDocument(const std::string, const std::string, const Json::Value) override;
77 
78     void DispatchBackPressedEvent() const;
79     void DispatchKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent) const;
80     void DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) const;
81     void DispatchAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) const;
82     void DispatchInputMethodEvent(const unsigned int code_point) const;
83 
84 protected:
85     void SetJsAppArgs(OHOS::Ace::Platform::AceRunArgs& args);
86     void RunJsApp();
87     void RunNormalAbility();
88     void RunDebugAbility();
89     double watchScreenDensity = 320;  // Watch Screen Density
90     double phoneScreenDensity = 480;  // Phone Screen Density
91     double tvScreenDensity = 320;     // TV Screen Density
92     double tabletScreenDensity = 400; // Tablet Screen Density
93     double carScreenDensity = 320;    // Car Screen Density
94     double twoInOneScreenDensity = 240;    // Car Screen Density
95 
96 private:
97     void SetAssetPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
98     void SetProjectModel(OHOS::Ace::Platform::AceRunArgs& args) const;
99     void SetPageProfile(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
100     void SetDeviceWidth(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const;
101     void SetDeviceHeight(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const;
102     void SetWindowTitle(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
103     void SetUrl(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
104     void SetConfigChanges(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
105     void SetColorMode(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
106     void SetOrientation(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
107     void SetAceVersionArgs(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
108     void SetLanguage(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
109     void SetRegion(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
110     void SetScript(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
111     void SetSystemResourcesPath(OHOS::Ace::Platform::AceRunArgs& args) const;
112     void SetAppResourcesPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
113     void SetFormsEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool formsEnabled) const;
114     void SetContainerSdkPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
115     void SetOnRender(OHOS::Ace::Platform::AceRunArgs& args) const;
116     void SetOnRouterChange(OHOS::Ace::Platform::AceRunArgs& args) const;
117     void SetOnError(OHOS::Ace::Platform::AceRunArgs& args) const;
118     void SetComponentModeEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool isComponentMode) const;
119     void AssignValueForWidthAndHeight(const int32_t origWidth, const int32_t origHeight,
120                                       const int32_t compWidth, const int32_t compHeight);
121     void AdaptDeviceType(OHOS::Ace::Platform::AceRunArgs& args, const std::string,
122                          const int32_t, double screenDendity = 0) const;
123     void ParseSystemParams(OHOS::Ace::Platform::AceRunArgs& args, Json::Value paramObj);
124     void SetSystemParams(OHOS::Ace::Platform::SystemParams& args, Json::Value paramObj);
125     void SetDeviceScreenDensity(const int32_t screenDensity, const std::string type);
126     std::string GetDeviceTypeName(const OHOS::Ace::DeviceType) const;
127     void InitGlfwEnv();
128     const double BASE_SCREEN_DENSITY = 160; // Device Baseline Screen Density
129     std::unique_ptr<OHOS::Ace::Platform::AceAbility> ability;
130     std::atomic<bool> isStop;
131     int32_t width = 0;
132     int32_t height = 0;
133     int32_t orignalWidth = 0;
134     int32_t orignalHeight = 0;
135     OHOS::Ace::Platform::AceRunArgs aceRunArgs;
136     std::shared_ptr<OHOS::Rosen::GlfwRenderContext> glfwRenderContext;
137 #if defined(__APPLE__) || defined(_WIN32)
138     std::unique_ptr<OHOS::AbilityRuntime::Simulator> simulator;
139     int64_t debugAbilityId = -1;
140     void SetSimulatorParams(OHOS::AbilityRuntime::Options& options);
141     void SetSimulatorCommonParams(OHOS::AbilityRuntime::Options& options);
142     void SetSimulatorConfigParams(OHOS::AbilityRuntime::Options& options);
143     std::shared_ptr<OHOS::AppExecFwk::Configuration> UpdateConfiguration(OHOS::Ace::Platform::AceRunArgs& args);
144 
145     std::shared_ptr<OHOS::Previewer::PreviewerWindowModel> windowModel;
146     void SetWindowParams() const;
147     template<typename T>
SetDevice(const OHOS::Ace::DeviceType & deviceType)148     T SetDevice(const OHOS::Ace::DeviceType& deviceType) const
149     {
150         static_assert(std::is_enum_v<T>, "T must be an enum type");
151         switch (deviceType) {
152             case OHOS::Ace::DeviceType::WATCH:
153                 return T::WATCH;
154             case OHOS::Ace::DeviceType::TV:
155                 return T::TV;
156             case OHOS::Ace::DeviceType::CAR:
157                 return T::CAR;
158             case OHOS::Ace::DeviceType::TABLET:
159                 return T::TABLET;
160             case OHOS::Ace::DeviceType::PHONE:
161                 return T::PHONE;
162             default:
163                 return T::UNKNOWN;
164         }
165     }
166     template<typename T>
SetColorMode(const OHOS::Ace::ColorMode & colorMode)167     T SetColorMode(const OHOS::Ace::ColorMode& colorMode) const
168     {
169         static_assert(std::is_enum_v<T>, "T must be an enum type");
170         if (colorMode == OHOS::Ace::ColorMode::LIGHT) {
171             return T::LIGHT;
172         } else if (colorMode == OHOS::Ace::ColorMode::DARK) {
173             return T::DARK;
174         } else {
175             return T::COLOR_MODE_UNDEFINED;
176         }
177     }
178     template<typename T>
SetOrientation(const OHOS::Ace::DeviceOrientation & orientation)179     T SetOrientation(const OHOS::Ace::DeviceOrientation& orientation) const
180     {
181         static_assert(std::is_enum_v<T>, "T must be an enum type");
182         if (orientation == OHOS::Ace::DeviceOrientation::PORTRAIT) {
183             return T::PORTRAIT;
184         } else if (orientation == OHOS::Ace::DeviceOrientation::LANDSCAPE) {
185             return T::LANDSCAPE;
186         } else {
187             return T::ORIENTATION_UNDEFINED;
188         }
189     }
190 #endif
191 };
192 
193 #endif // JSAPPIMPL_H
194