1 /*
2 * Copyright (c) 2022-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 #include "adapter/ohos/entrance/form_utils_impl.h"
17
18 #include "form_mgr.h"
19 #include "want.h"
20
21 #include "adapter/ohos/entrance/ace_container.h"
22 #include "adapter/ohos/osal/want_wrap_ohos.h"
23 #include "base/utils/utils.h"
24 #include "core/common/container_scope.h"
25 #include "core/common/form_manager.h"
26 #include "frameworks/base/json/json_util.h"
27
28
29 namespace OHOS::Ace {
30 namespace {
31 constexpr int32_t ERR_OK = 0;
32 }
RouterEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)33 int32_t FormUtilsImpl::RouterEvent(
34 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
35 {
36 ContainerScope scope(containerId);
37 auto container = Container::Current();
38 auto ace_container = AceType::DynamicCast<Platform::AceContainer>(container);
39 auto token_ = ace_container->GetToken();
40 CHECK_NULL_RETURN(token_, -1);
41 AAFwk::Want want;
42 auto eventAction = JsonUtil::ParseJsonString(action);
43 auto uri = eventAction->GetValue("uri");
44 auto params = eventAction->GetValue("params");
45 if (params->IsValid()) {
46 auto child = params->GetChild();
47 while (child->IsValid()) {
48 auto key = child->GetKey();
49 if (child->IsNull()) {
50 want.SetParam(key, std::string());
51 } else if (child->IsString()) {
52 want.SetParam(key, child->GetString());
53 } else if (child->IsNumber()) {
54 want.SetParam(key, child->GetInt());
55 } else {
56 want.SetParam(key, std::string());
57 }
58 child = child->GetNext();
59 }
60 }
61 want.SetParam("params", params->ToString());
62 auto abilityName = eventAction->GetValue("abilityName");
63 if (uri->IsValid() && !abilityName->IsValid()) {
64 auto uriStr = uri->GetString();
65 want.SetUri(uriStr);
66 auto bundleName = eventAction->GetValue("bundleName");
67 auto bundle = bundleName->GetString();
68 if (!bundle.empty()) {
69 want.SetElementName(bundle, std::string());
70 }
71 } else {
72 auto bundleName = eventAction->GetValue("bundleName");
73 auto bundle = bundleName->GetString();
74 auto ability = abilityName->GetString();
75 if (ability.empty()) {
76 return -1;
77 }
78 if (bundle.empty()) {
79 bundle = defaultBundleName;
80 }
81 want.SetElementName(bundle, ability);
82 }
83
84 return AppExecFwk::FormMgr::GetInstance().RouterEvent(formId, want, token_);
85 }
86
RequestPublishFormEvent(const AAFwk::Want & want,const std::string & formBindingDataStr,int64_t & formId,std::string & errMsg)87 int32_t FormUtilsImpl::RequestPublishFormEvent(const AAFwk::Want& want,
88 const std::string& formBindingDataStr, int64_t& formId, std::string &errMsg)
89 {
90 std::unique_ptr<AppExecFwk::FormProviderData> formBindingData = std::make_unique<AppExecFwk::FormProviderData>();
91 bool withFormBindingData = false;
92 if (!formBindingDataStr.empty()) {
93 withFormBindingData = true;
94 formBindingData->SetDataString(const_cast<std::string&>(formBindingDataStr));
95 formBindingData->ParseImagesData();
96 }
97 std::vector<AppExecFwk::FormDataProxy> formDataProxies;
98 int32_t ret = AppExecFwk::FormMgr::GetInstance().RequestPublishFormWithSnapshot(const_cast<Want&>(want),
99 withFormBindingData, formBindingData, formId, formDataProxies);
100 if (ret != ERR_OK) {
101 errMsg = OHOS::AppExecFwk::FormMgr::GetInstance().GetErrorMessage(ret);
102 }
103
104 return ret;
105 }
106
BackgroundEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)107 int32_t FormUtilsImpl::BackgroundEvent(
108 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
109 {
110 ContainerScope scope(containerId);
111 auto container = Container::Current();
112 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
113 CHECK_NULL_RETURN(aceContainer, -1);
114 auto token = aceContainer->GetToken();
115 CHECK_NULL_RETURN(token, -1);
116 AAFwk::Want want;
117 auto eventAction = JsonUtil::ParseJsonString(action);
118 auto bundleName = eventAction->GetValue("bundleName");
119 auto abilityName = eventAction->GetValue("abilityName");
120 auto params = eventAction->GetValue("params");
121 auto bundle = bundleName->GetString();
122 auto ability = abilityName->GetString();
123 if (ability.empty()) {
124 return -1;
125 }
126 if (bundle.empty()) {
127 bundle = defaultBundleName;
128 }
129 want.SetElementName(bundle, ability);
130 if (params->IsValid()) {
131 auto child = params->GetChild();
132 while (child->IsValid()) {
133 auto key = child->GetKey();
134 if (child->IsNull()) {
135 want.SetParam(key, std::string());
136 } else if (child->IsString()) {
137 want.SetParam(key, child->GetString());
138 } else if (child->IsNumber()) {
139 want.SetParam(key, child->GetInt());
140 } else {
141 want.SetParam(key, std::string());
142 }
143 child = child->GetNext();
144 }
145 }
146 want.SetParam("params", params->ToString());
147 return AppExecFwk::FormMgr::GetInstance().BackgroundEvent(formId, want, token);
148 }
149 } // namespace OHOS::Ace
150