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 #include "adapter/ohos/entrance/ace_form_ability.h"
17
18 #include "res_config.h"
19 #include "resource_manager.h"
20
21 #include "adapter/ohos/entrance/pa_container.h"
22 #include "adapter/ohos/entrance/pa_engine/pa_backend.h"
23 #include "adapter/ohos/entrance/platform_event_callback.h"
24 #include "adapter/ohos/entrance/utils.h"
25 #include "base/log/log.h"
26 #include "base/utils/utils.h"
27 #include "core/common/backend.h"
28
29 namespace OHOS::Ace {
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 using FormPlatformFinish = std::function<void()>;
33 class FormPlatformEventCallback final : public Platform::PlatformEventCallback {
34 public:
FormPlatformEventCallback(FormPlatformFinish onFinish)35 explicit FormPlatformEventCallback(FormPlatformFinish onFinish) : onFinish_(onFinish) {}
36
37 ~FormPlatformEventCallback() override = default;
38
OnFinish() const39 void OnFinish() const override
40 {
41 LOGI("FormPlatformEventCallback OnFinish");
42 CHECK_NULL_VOID_NOLOG(onFinish_);
43 onFinish_();
44 }
45
OnStatusBarBgColorChanged(uint32_t color)46 void OnStatusBarBgColorChanged(uint32_t color) override
47 {
48 LOGI("FormPlatformEventCallback OnStatusBarBgColorChanged");
49 }
50
51 private:
52 FormPlatformFinish onFinish_;
53 };
54
55 int32_t AceFormAbility::instanceId_ = 300000;
56 const std::string AceFormAbility::START_PARAMS_KEY = "__startParams";
57 const std::string AceFormAbility::URI = "url";
58
REGISTER_AA(AceFormAbility)59 REGISTER_AA(AceFormAbility)
60
61 AceFormAbility::AceFormAbility()
62 {
63 instanceId_++;
64 }
65
LoadFormEnv(const OHOS::AAFwk::Want & want)66 void AceFormAbility::LoadFormEnv(const OHOS::AAFwk::Want& want)
67 {
68 // get url
69 std::string parsedUrl;
70 if (want.HasParameter(URI)) {
71 parsedUrl = want.GetStringParam(URI);
72 } else {
73 parsedUrl = "form.js";
74 }
75
76 // get asset
77 auto packagePathStr = GetBundleCodePath();
78 auto moduleInfo = GetHapModuleInfo();
79 CHECK_NULL_VOID_NOLOG(moduleInfo);
80 packagePathStr += "/" + moduleInfo->package + "/";
81
82 // init form ability
83 bool isHap = moduleInfo ? !moduleInfo->hapPath.empty() : false;
84 std::string& packagePath = isHap ? moduleInfo->hapPath : packagePathStr;
85 BackendType backendType = BackendType::FORM;
86 bool isArkApp = GetIsArkFromConfig(packagePath, isHap);
87
88 Platform::PaContainer::CreateContainer(instanceId_, backendType, isArkApp, this,
89 std::make_unique<FormPlatformEventCallback>([this]() { TerminateAbility(); }));
90
91 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
92 if (info != nullptr && !info->srcPath.empty()) {
93 LOGI("AceFormAbility srcPath:%{public}s url:%{public}s", info->srcPath.c_str(), parsedUrl.c_str());
94 auto assetBasePathStr = { "assets/js/" + info->srcPath + "/" };
95 Platform::PaContainer::AddAssetPath(instanceId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
96 } else {
97 LOGI("AceFormAbility parsedUrl:%{public}s", parsedUrl.c_str());
98 auto assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
99 Platform::PaContainer::AddAssetPath(instanceId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
100 }
101 std::shared_ptr<ApplicationInfo> appInfo = GetApplicationInfo();
102 if (appInfo) {
103 /* Note: DO NOT modify the sequence of adding libPath */
104 std::string nativeLibraryPath = appInfo->nativeLibraryPath;
105 std::string quickFixLibraryPath = appInfo->appQuickFix.deployedAppqfInfo.nativeLibraryPath;
106 std::vector<std::string> libPaths;
107 if (!quickFixLibraryPath.empty()) {
108 std::string libPath = GenerateFullPath(GetBundleCodePath(), quickFixLibraryPath);
109 libPaths.push_back(libPath);
110 LOGI("napi quick fix lib path = %{private}s", libPath.c_str());
111 }
112 if (!nativeLibraryPath.empty()) {
113 std::string libPath = GenerateFullPath(GetBundleCodePath(), nativeLibraryPath);
114 libPaths.push_back(libPath);
115 LOGI("napi lib path = %{private}s", libPath.c_str());
116 }
117 if (!libPaths.empty()) {
118 Platform::PaContainer::AddLibPath(instanceId_, libPaths);
119 }
120 }
121
122 // run form ability
123 Platform::PaContainer::RunPa(instanceId_, parsedUrl, want);
124 }
125
OnCreate(const OHOS::AAFwk::Want & want)126 OHOS::AppExecFwk::FormProviderInfo AceFormAbility::OnCreate(const OHOS::AAFwk::Want& want)
127 {
128 std::string formId = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_IDENTITY_KEY);
129 LOGI("AceFormAbility::OnCreate formId = %{public}s.", formId.c_str());
130 Platform::PaContainer::OnCreate(instanceId_, want);
131 OHOS::AppExecFwk::FormProviderInfo formProviderInfo;
132 formProviderInfo.SetFormData(Platform::PaContainer::GetFormData(instanceId_));
133 std::string formData = formProviderInfo.GetFormData().GetDataString();
134 LOGI("AceFormAbility::OnCreate return ok, formData: %{public}s", formData.c_str());
135 return formProviderInfo;
136 }
137
OnDelete(const int64_t formId)138 void AceFormAbility::OnDelete(const int64_t formId)
139 {
140 LOGI("AceFormAbility::OnDelete called: %{public}s", std::to_string(formId).c_str());
141 Platform::PaContainer::OnDelete(instanceId_, formId);
142 }
143
OnTriggerEvent(const int64_t formId,const std::string & message)144 void AceFormAbility::OnTriggerEvent(const int64_t formId, const std::string& message)
145 {
146 LOGI("AceFormAbility::OnTriggerEvent called: %{public}s", std::to_string(formId).c_str());
147 Platform::PaContainer::OnTriggerEvent(instanceId_, formId, message);
148 }
149
OnAcquireFormState(const OHOS::AAFwk::Want & want)150 AppExecFwk::FormState AceFormAbility::OnAcquireFormState(const OHOS::AAFwk::Want &want)
151 {
152 LOGI("AceFormAbility::OnAcquireState called");
153 int32_t formState = Platform::PaContainer::OnAcquireFormState(instanceId_, want);
154 if (formState <= (int32_t) AppExecFwk::FormState::UNKNOWN || formState > (int32_t) AppExecFwk::FormState::READY) {
155 return AppExecFwk::FormState::UNKNOWN;
156 } else {
157 return (AppExecFwk::FormState) formState;
158 }
159 }
160
OnUpdate(const int64_t formId)161 void AceFormAbility::OnUpdate(const int64_t formId)
162 {
163 LOGI("AceFormAbility::OnUpdate called: %{public}s", std::to_string(formId).c_str());
164 Platform::PaContainer::OnUpdate(instanceId_, formId);
165 }
166
OnCastTemptoNormal(const int64_t formId)167 void AceFormAbility::OnCastTemptoNormal(const int64_t formId)
168 {
169 LOGI("AceFormAbility::OnCastTemptoNormal called: %{public}s", std::to_string(formId).c_str());
170 Platform::PaContainer::OnCastTemptoNormal(instanceId_, formId);
171 }
172
OnVisibilityChanged(const std::map<int64_t,int32_t> & formEventsMap)173 void AceFormAbility::OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap)
174 {
175 LOGI("AceFormAbility::OnVisibilityChanged called");
176 Platform::PaContainer::OnVisibilityChanged(instanceId_, formEventsMap);
177 }
178
OnShare(int64_t formId,OHOS::AAFwk::WantParams & wantParams)179 bool AceFormAbility::OnShare(int64_t formId, OHOS::AAFwk::WantParams &wantParams)
180 {
181 LOGD("AceFormAbility::OnShare called");
182 return Platform::PaContainer::OnShare(instanceId_, formId, wantParams);
183 }
184
OnStart(const OHOS::AAFwk::Want & want)185 void AceFormAbility::OnStart(const OHOS::AAFwk::Want& want)
186 {
187 LOGI("AceFormAbility::OnStart start");
188 Ability::OnStart(want);
189 LoadFormEnv(want);
190 }
191
OnStop()192 void AceFormAbility::OnStop()
193 {
194 LOGI("AceFormAbility::OnStop start ");
195 Ability::OnStop();
196 }
197
OnConnect(const Want & want)198 sptr<IRemoteObject> AceFormAbility::OnConnect(const Want& want)
199 {
200 LOGI("AceFormAbility::OnConnect start");
201 Ability::OnConnect(want);
202 return GetFormRemoteObject();
203 }
204
OnDisconnect(const Want & want)205 void AceFormAbility::OnDisconnect(const Want& want)
206 {
207 LOGI("AceFormAbility::OnDisconnect start");
208 Ability::OnDisconnect(want);
209 }
210 } // namespace OHOS::Ace
211