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(token_, -1);
36 AAFwk::Want want;
37 auto eventAction = JsonUtil::ParseJsonString(action);
38 auto uri = eventAction->GetValue("uri");
39 auto params = eventAction->GetValue("params");
40 if (params->IsValid()) {
41 auto child = params->GetChild();
42 while (child->IsValid()) {
43 auto key = child->GetKey();
44 if (child->IsNull()) {
45 want.SetParam(key, std::string());
46 } else if (child->IsString()) {
47 want.SetParam(key, child->GetString());
48 } else if (child->IsNumber()) {
49 want.SetParam(key, child->GetInt());
50 } else {
51 want.SetParam(key, std::string());
52 }
53 child = child->GetNext();
54 }
55 }
56 want.SetParam("params", params->ToString());
57 auto abilityName = eventAction->GetValue("abilityName");
58 if (uri->IsValid() && !abilityName->IsValid()) {
59 auto uriStr = uri->GetString();
60 want.SetUri(uriStr);
61 } else {
62 auto bundleName = eventAction->GetValue("bundleName");
63 auto bundle = bundleName->GetString();
64 auto ability = abilityName->GetString();
65 if (ability.empty()) {
66 return -1;
67 }
68 if (bundle.empty()) {
69 bundle = defaultBundleName;
70 }
71 want.SetElementName(bundle, ability);
72 }
73
74 return AppExecFwk::FormMgr::GetInstance().RouterEvent(formId, want, token_);
75 }
76
BackgroundEvent(const int64_t formId,const std::string & action,const int32_t containerId,const std::string & defaultBundleName)77 int32_t FormUtilsImpl::BackgroundEvent(
78 const int64_t formId, const std::string& action, const int32_t containerId, const std::string& defaultBundleName)
79 {
80 ContainerScope scope(containerId);
81 auto container = Container::Current();
82 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
83 CHECK_NULL_RETURN(aceContainer, -1);
84 auto token = aceContainer->GetToken();
85 CHECK_NULL_RETURN(token, -1);
86 AAFwk::Want want;
87 auto eventAction = JsonUtil::ParseJsonString(action);
88 auto bundleName = eventAction->GetValue("bundleName");
89 auto abilityName = eventAction->GetValue("abilityName");
90 auto params = eventAction->GetValue("params");
91 auto bundle = bundleName->GetString();
92 auto ability = abilityName->GetString();
93 if (ability.empty()) {
94 return -1;
95 }
96 if (bundle.empty()) {
97 bundle = defaultBundleName;
98 }
99 want.SetElementName(bundle, ability);
100 if (params->IsValid()) {
101 auto child = params->GetChild();
102 while (child->IsValid()) {
103 auto key = child->GetKey();
104 if (child->IsNull()) {
105 want.SetParam(key, std::string());
106 } else if (child->IsString()) {
107 want.SetParam(key, child->GetString());
108 } else if (child->IsNumber()) {
109 want.SetParam(key, child->GetInt());
110 } else {
111 want.SetParam(key, std::string());
112 }
113 child = child->GetNext();
114 }
115 }
116 want.SetParam("params", params->ToString());
117 return AppExecFwk::FormMgr::GetInstance().BackgroundEvent(formId, want, token);
118 }
119 } // namespace OHOS::Ace
120