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 
26 
27 namespace OHOS::Ace {
28 namespace {
29     constexpr int32_t ERR_OK = 0;
30 }
RouterEvent( const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)31 int32_t FormUtilsImpl::RouterEvent(
32     const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
33 {
34     ContainerScope scope(containerId);
35     auto container = Container::Current();
36     auto ace_container = AceType::DynamicCast<Platform::AceContainer>(container);
37     auto token_ = ace_container->GetToken();
38     CHECK_NULL_RETURN(token_, -1);
39     AAFwk::Want want;
40     auto eventAction = JsonUtil::ParseJsonString(action);
41     auto uri = eventAction->GetValue("uri");
42     auto params = eventAction->GetValue("params");
43     if (params->IsValid()) {
44         auto child = params->GetChild();
45         while (child->IsValid()) {
46             auto key = child->GetKey();
47             if (child->IsNull()) {
48                 want.SetParam(key, std::string());
49             } else if (child->IsString()) {
50                 want.SetParam(key, child->GetString());
51             } else if (child->IsNumber()) {
52                 want.SetParam(key, child->GetInt());
53             } else {
54                 want.SetParam(key, std::string());
55             }
56             child = child->GetNext();
57         }
58     }
59     want.SetParam("params", params->ToString());
60     auto abilityName = eventAction->GetValue("abilityName");
61     if (uri->IsValid() && !abilityName->IsValid()) {
62         auto uriStr = uri->GetString();
63         want.SetUri(uriStr);
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