1 /*
2 * Copyright (c) 2021 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 "core/common/backend.h"
27
28 namespace OHOS::Ace {
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 using FormPlatformFinish = std::function<void()>;
32 class FormPlatformEventCallback final : public Platform::PlatformEventCallback {
33 public:
FormPlatformEventCallback(FormPlatformFinish onFinish)34 explicit FormPlatformEventCallback(FormPlatformFinish onFinish) : onFinish_(onFinish) {}
35
36 ~FormPlatformEventCallback() = default;
37
OnFinish() const38 virtual void OnFinish() const
39 {
40 LOGI("FormPlatformEventCallback OnFinish");
41 if (onFinish_) {
42 onFinish_();
43 }
44 }
45
OnStatusBarBgColorChanged(uint32_t color)46 virtual void OnStatusBarBgColorChanged(uint32_t color)
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 if (moduleInfo != nullptr) {
80 packagePathStr += "/" + moduleInfo->package + "/";
81 }
82
83 // init form ability
84 BackendType backendType = BackendType::FORM;
85 bool isArkApp = GetIsArkFromConfig(packagePathStr);
86 Platform::PaContainer::CreateContainer(instanceId_, backendType, isArkApp, this,
87 std::make_unique<FormPlatformEventCallback>([this]() { TerminateAbility(); }));
88
89 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
90 if (info != nullptr && !info->srcPath.empty()) {
91 LOGI("AceFormAbility srcPath:%{public}s url:%{public}s", info->srcPath.c_str(), parsedUrl.c_str());
92 auto assetBasePathStr = { "assets/js/" + info->srcPath + "/" };
93 Platform::PaContainer::AddAssetPath(instanceId_, packagePathStr, assetBasePathStr);
94 } else {
95 LOGI("AceFormAbility parsedUrl:%{public}s", parsedUrl.c_str());
96 auto assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
97 Platform::PaContainer::AddAssetPath(instanceId_, packagePathStr, assetBasePathStr);
98 }
99 std::shared_ptr<ApplicationInfo> appInfo = GetApplicationInfo();
100 if (appInfo) {
101 std::string nativeLibraryPath = appInfo->nativeLibraryPath;
102 if (!nativeLibraryPath.empty()) {
103 if (nativeLibraryPath.back() == '/') {
104 nativeLibraryPath.pop_back();
105 }
106 std::string libPath = GetBundleCodePath();
107 if (libPath.back() == '/') {
108 libPath += nativeLibraryPath;
109 } else {
110 libPath += "/" + nativeLibraryPath;
111 }
112 LOGI("napi lib path = %{private}s", libPath.c_str());
113 Platform::PaContainer::AddLibPath(instanceId_, libPath);
114 }
115 }
116
117 // run form ability
118 Platform::PaContainer::RunPa(instanceId_, parsedUrl, want);
119 }
120
OnCreate(const OHOS::AAFwk::Want & want)121 OHOS::AppExecFwk::FormProviderInfo AceFormAbility::OnCreate(const OHOS::AAFwk::Want& want)
122 {
123 std::string formId = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_IDENTITY_KEY);
124 LOGI("AceFormAbility::OnCreate formId = %{public}s.", formId.c_str());
125 Platform::PaContainer::OnCreate(instanceId_, want);
126 OHOS::AppExecFwk::FormProviderInfo formProviderInfo;
127 formProviderInfo.SetFormData(Platform::PaContainer::GetFormData(instanceId_));
128 std::string formData = formProviderInfo.GetFormData().GetDataString();
129 LOGI("AceFormAbility::OnCreate return ok, formData: %{public}s", formData.c_str());
130 return formProviderInfo;
131 }
132
OnDelete(const int64_t formId)133 void AceFormAbility::OnDelete(const int64_t formId)
134 {
135 LOGI("AceFormAbility::OnDelete called: %{public}s", std::to_string(formId).c_str());
136 Platform::PaContainer::OnDelete(instanceId_, formId);
137 }
138
OnTriggerEvent(const int64_t formId,const std::string & message)139 void AceFormAbility::OnTriggerEvent(const int64_t formId, const std::string& message)
140 {
141 LOGI("AceFormAbility::OnTriggerEvent called: %{public}s", std::to_string(formId).c_str());
142 Platform::PaContainer::OnTriggerEvent(instanceId_, formId, message);
143 }
144
OnAcquireFormState(const OHOS::AAFwk::Want & want)145 AppExecFwk::FormState AceFormAbility::OnAcquireFormState(const OHOS::AAFwk::Want &want)
146 {
147 LOGI("AceFormAbility::OnAcquireState called");
148 int32_t formState = Platform::PaContainer::OnAcquireFormState(instanceId_, want);
149 if (formState <= (int32_t) AppExecFwk::FormState::UNKNOWN || formState > (int32_t) AppExecFwk::FormState::READY) {
150 return AppExecFwk::FormState::UNKNOWN;
151 } else {
152 return (AppExecFwk::FormState) formState;
153 }
154 }
155
OnUpdate(const int64_t formId)156 void AceFormAbility::OnUpdate(const int64_t formId)
157 {
158 LOGI("AceFormAbility::OnUpdate called: %{public}s", std::to_string(formId).c_str());
159 Platform::PaContainer::OnUpdate(instanceId_, formId);
160 }
161
OnCastTemptoNormal(const int64_t formId)162 void AceFormAbility::OnCastTemptoNormal(const int64_t formId)
163 {
164 LOGI("AceFormAbility::OnCastTemptoNormal called: %{public}s", std::to_string(formId).c_str());
165 Platform::PaContainer::OnCastTemptoNormal(instanceId_, formId);
166 }
167
OnVisibilityChanged(const std::map<int64_t,int32_t> & formEventsMap)168 void AceFormAbility::OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap)
169 {
170 LOGI("AceFormAbility::OnVisibilityChanged called");
171 Platform::PaContainer::OnVisibilityChanged(instanceId_, formEventsMap);
172 }
173
OnStart(const OHOS::AAFwk::Want & want)174 void AceFormAbility::OnStart(const OHOS::AAFwk::Want& want)
175 {
176 LOGI("AceFormAbility::OnStart start");
177 Ability::OnStart(want);
178 LoadFormEnv(want);
179 }
180
OnStop()181 void AceFormAbility::OnStop()
182 {
183 LOGI("AceFormAbility::OnStop start ");
184 Ability::OnStop();
185 }
186
OnConnect(const Want & want)187 sptr<IRemoteObject> AceFormAbility::OnConnect(const Want& want)
188 {
189 LOGI("AceFormAbility::OnConnect start");
190 Ability::OnConnect(want);
191 return GetFormRemoteObject();
192 }
193
OnDisconnect(const Want & want)194 void AceFormAbility::OnDisconnect(const Want& want)
195 {
196 LOGI("AceFormAbility::OnDisconnect start");
197 Ability::OnDisconnect(want);
198 }
199
200 } // namespace OHOS::Ace
201