1 /*
2 * Copyright (c) 2022 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 "base/utils/utils.h"
23 #include "core/common/container_scope.h"
24 #include "core/common/form_manager.h"
25 #include "frameworks/base/json/json_util.h"
26
27 namespace OHOS::Ace {
RouterEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)28 int32_t FormUtilsImpl::RouterEvent(
29 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
30 {
31 ContainerScope scope(containerId);
32 auto container = Container::Current();
33 auto ace_container = AceType::DynamicCast<Platform::AceContainer>(container);
34 auto token_ = ace_container->GetToken();
35 CHECK_NULL_RETURN_NOLOG(token_, -1);
36 AAFwk::Want want;
37 auto eventAction = JsonUtil::ParseJsonString(action);
38 auto bundleName = eventAction->GetValue("bundleName");
39 auto abilityName = eventAction->GetValue("abilityName");
40 auto params = eventAction->GetValue("params");
41 auto bundle = bundleName->GetString();
42 auto ability = abilityName->GetString();
43 if (ability.empty()) {
44 return -1;
45 }
46 if (bundle.empty()) {
47 bundle = defaultBundleName;
48 }
49 want.SetElementName(bundle, ability);
50 if (params->IsValid()) {
51 auto child = params->GetChild();
52 while (child->IsValid()) {
53 auto key = child->GetKey();
54 if (child->IsNull()) {
55 want.SetParam(key, std::string());
56 } else if (child->IsString()) {
57 want.SetParam(key, child->GetString());
58 } else if (child->IsNumber()) {
59 want.SetParam(key, child->GetInt());
60 } else {
61 want.SetParam(key, std::string());
62 }
63 child = child->GetNext();
64 }
65 }
66 want.SetParam("params", params->ToString());
67 return AppExecFwk::FormMgr::GetInstance().RouterEvent(formId, want, token_);
68 }
69
BackgroundEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)70 int32_t FormUtilsImpl::BackgroundEvent(
71 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
72 {
73 ContainerScope scope(containerId);
74 auto container = Container::Current();
75 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
76 CHECK_NULL_RETURN_NOLOG(aceContainer, -1);
77 auto token = aceContainer->GetToken();
78 CHECK_NULL_RETURN_NOLOG(token, -1);
79 AAFwk::Want want;
80 auto eventAction = JsonUtil::ParseJsonString(action);
81 auto bundleName = eventAction->GetValue("bundleName");
82 auto abilityName = eventAction->GetValue("abilityName");
83 auto params = eventAction->GetValue("params");
84 auto bundle = bundleName->GetString();
85 auto ability = abilityName->GetString();
86 if (ability.empty()) {
87 return -1;
88 }
89 if (bundle.empty()) {
90 bundle = defaultBundleName;
91 }
92 want.SetElementName(bundle, ability);
93 if (params->IsValid()) {
94 auto child = params->GetChild();
95 while (child->IsValid()) {
96 auto key = child->GetKey();
97 if (child->IsNull()) {
98 want.SetParam(key, std::string());
99 } else if (child->IsString()) {
100 want.SetParam(key, child->GetString());
101 } else if (child->IsNumber()) {
102 want.SetParam(key, child->GetInt());
103 } else {
104 want.SetParam(key, std::string());
105 }
106 child = child->GetNext();
107 }
108 }
109 want.SetParam("params", params->ToString());
110 return AppExecFwk::FormMgr::GetInstance().BackgroundEvent(formId, want, token);
111 }
112 } // namespace OHOS::Ace