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     } else {
67         auto bundleName = eventAction->GetValue("bundleName");
68         auto bundle = bundleName->GetString();
69         auto ability = abilityName->GetString();
70         if (ability.empty()) {
71             return -1;
72         }
73         if (bundle.empty()) {
74             bundle = defaultBundleName;
75         }
76         want.SetElementName(bundle, ability);
77     }
78 
79     return AppExecFwk::FormMgr::GetInstance().RouterEvent(formId, want, token_);
80 }
81 
RequestPublishFormEvent(const AAFwk::Want & want,const std::string & formBindingDataStr,int64_t & formId,std::string & errMsg)82 int32_t FormUtilsImpl::RequestPublishFormEvent(const AAFwk::Want& want,
83     const std::string& formBindingDataStr, int64_t& formId, std::string &errMsg)
84 {
85     std::unique_ptr<AppExecFwk::FormProviderData> formBindingData = std::make_unique<AppExecFwk::FormProviderData>();
86     bool withFormBindingData = false;
87     if (!formBindingDataStr.empty()) {
88         withFormBindingData = true;
89         formBindingData->SetDataString(const_cast<std::string&>(formBindingDataStr));
90         formBindingData->ParseImagesData();
91     }
92     std::vector<AppExecFwk::FormDataProxy> formDataProxies;
93     int32_t ret = AppExecFwk::FormMgr::GetInstance().RequestPublishFormWithSnapshot(const_cast<Want&>(want),
94         withFormBindingData, formBindingData, formId, formDataProxies);
95     if (ret != ERR_OK) {
96         errMsg = OHOS::AppExecFwk::FormMgr::GetInstance().GetErrorMessage(ret);
97     }
98 
99     return ret;
100 }
101 
BackgroundEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)102 int32_t FormUtilsImpl::BackgroundEvent(
103     const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
104 {
105     ContainerScope scope(containerId);
106     auto container = Container::Current();
107     auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
108     CHECK_NULL_RETURN(aceContainer, -1);
109     auto token = aceContainer->GetToken();
110     CHECK_NULL_RETURN(token, -1);
111     AAFwk::Want want;
112     auto eventAction = JsonUtil::ParseJsonString(action);
113     auto bundleName = eventAction->GetValue("bundleName");
114     auto abilityName = eventAction->GetValue("abilityName");
115     auto params = eventAction->GetValue("params");
116     auto bundle = bundleName->GetString();
117     auto ability = abilityName->GetString();
118     if (ability.empty()) {
119         return -1;
120     }
121     if (bundle.empty()) {
122         bundle = defaultBundleName;
123     }
124     want.SetElementName(bundle, ability);
125     if (params->IsValid()) {
126         auto child = params->GetChild();
127         while (child->IsValid()) {
128             auto key = child->GetKey();
129             if (child->IsNull()) {
130                 want.SetParam(key, std::string());
131             } else if (child->IsString()) {
132                 want.SetParam(key, child->GetString());
133             } else if (child->IsNumber()) {
134                 want.SetParam(key, child->GetInt());
135             } else {
136                 want.SetParam(key, std::string());
137             }
138             child = child->GetNext();
139         }
140     }
141     want.SetParam("params", params->ToString());
142     return AppExecFwk::FormMgr::GetInstance().BackgroundEvent(formId, want, token);
143 }
144 } // namespace OHOS::Ace
145