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