1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_JS_ABILITY_STAGE_H
17 #define OHOS_ABILITY_RUNTIME_JS_ABILITY_STAGE_H
18 
19 #include "ability_delegator_infos.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 #include "ability_stage.h"
25 #include "configuration.h"
26 #include "js_startup_task.h"
27 #include "resource_manager.h"
28 #include "nlohmann/json.hpp"
29 #include "native_engine/native_value.h"
30 
31 class NativeReference;
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 class JsRuntime;
36 class JsAbilityStage : public AbilityStage {
37 public:
38     static std::shared_ptr<AbilityStage> Create(
39         const std::unique_ptr<Runtime>& runtime, const AppExecFwk::HapModuleInfo& hapModuleInfo);
40 
41     JsAbilityStage(JsRuntime& jsRuntime, std::unique_ptr<NativeReference>&& jsAbilityStageObj);
42     ~JsAbilityStage() override;
43 
44     void Init(const std::shared_ptr<Context> &context,
45         const std::weak_ptr<AppExecFwk::OHOSApplication> application) override;
46 
47     void OnCreate(const AAFwk::Want &want) const override;
48 
49     void OnDestroy() const override;
50 
51     std::string OnAcceptWant(const AAFwk::Want &want) override;
52 
53     std::string OnNewProcessRequest(const AAFwk::Want &want) override;
54 
55     void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
56 
57     void OnMemoryLevel(int32_t level) override;
58 
59     int32_t RunAutoStartupTask(const std::function<void()> &callback, bool &isAsyncCallback,
60         const std::shared_ptr<Context> &stageContext) override;
61 
62 private:
63     napi_value CallObjectMethod(const char* name, napi_value const * argv = nullptr, size_t argc = 0);
64 
65     std::shared_ptr<AppExecFwk::DelegatorAbilityStageProperty> CreateStageProperty() const;
66 
67     std::string GetHapModuleProp(const std::string &propName) const;
68 
69     static bool UseCommonChunk(const AppExecFwk::HapModuleInfo& hapModuleInfo);
70 
71     int32_t RegisterStartupTaskFromProfile(std::vector<JsStartupTask> &jsStartupTasks);
72 
73     bool GetProfileInfoFromResourceManager(std::vector<std::string> &profileInfo);
74 
75     bool AnalyzeProfileInfoAndRegisterStartupTask(const std::vector<std::string> &profileInfo);
76 
77     void SetOptionalParameters(const nlohmann::json &module, JsStartupTask &jsStartupTask);
78 
79     std::unique_ptr<NativeReference> LoadJsSrcEntry(const std::string &srcEntry);
80 
81     bool LoadJsStartupConfig(const std::string &srcEntry);
82 
83     bool GetResFromResMgr(
84         const std::string &resName,
85         const std::shared_ptr<Global::Resource::ResourceManager> &resMgr,
86         bool isCompressed, std::vector<std::string> &profileInfo);
87 
88     bool IsFileExisted(const std::string &filePath);
89 
90     bool TransformFileToJsonString(const std::string &resPath, std::string &profile);
91 
92     void SetJsAbilityStage(const std::shared_ptr<Context> &context);
93 
94     JsRuntime& jsRuntime_;
95     std::shared_ptr<NativeReference> jsAbilityStageObj_;
96     std::shared_ptr<NativeReference> shellContextRef_;
97 };
98 }  // namespace AbilityRuntime
99 }  // namespace OHOS
100 #endif  // OHOS_ABILITY_RUNTIME_JS_ABILITY_STAGE_H
101