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 int32_t externalErrorCode = ret;
94 if (ret != ERR_OK) {
95 OHOS::AppExecFwk::FormMgr::GetInstance().GetExternalError(ret, externalErrorCode, errMsg);
96 }
97
98 return externalErrorCode;
99 }
100
BackgroundEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)101 int32_t FormUtilsImpl::BackgroundEvent(
102 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
103 {
104 ContainerScope scope(containerId);
105 auto container = Container::Current();
106 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
107 CHECK_NULL_RETURN(aceContainer, -1);
108 auto token = aceContainer->GetToken();
109 CHECK_NULL_RETURN(token, -1);
110 AAFwk::Want want;
111 auto eventAction = JsonUtil::ParseJsonString(action);
112 auto bundleName = eventAction->GetValue("bundleName");
113 auto abilityName = eventAction->GetValue("abilityName");
114 auto params = eventAction->GetValue("params");
115 auto bundle = bundleName->GetString();
116 auto ability = abilityName->GetString();
117 if (ability.empty()) {
118 return -1;
119 }
120 if (bundle.empty()) {
121 bundle = defaultBundleName;
122 }
123 want.SetElementName(bundle, ability);
124 if (params->IsValid()) {
125 auto child = params->GetChild();
126 while (child->IsValid()) {
127 auto key = child->GetKey();
128 if (child->IsNull()) {
129 want.SetParam(key, std::string());
130 } else if (child->IsString()) {
131 want.SetParam(key, child->GetString());
132 } else if (child->IsNumber()) {
133 want.SetParam(key, child->GetInt());
134 } else {
135 want.SetParam(key, std::string());
136 }
137 child = child->GetNext();
138 }
139 }
140 want.SetParam("params", params->ToString());
141 return AppExecFwk::FormMgr::GetInstance().BackgroundEvent(formId, want, token);
142 }
143 } // namespace OHOS::Ace
144