1 /*
2 * Copyright (c) 2025-2025 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 "ohos.bundle.appDomainVerify.proj.hpp"
17 #include "ohos.bundle.appDomainVerify.impl.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20 #include <map>
21 #include "app_domain_verify_hilog.h"
22 #include "app_domain_verify_mgr_client.h"
23 #include "js_common_defined.h"
24 #include "api_event_reporter.h"
25 using namespace taihe;
26 using namespace OHOS::AppDomainVerify;
27 namespace {
CheckInput(const std::string & input)28 bool CheckInput(const std::string& input)
29 {
30 if (input.empty() || input.size() > MAX_STR_INPUT_SIZE) {
31 return false;
32 }
33 return true;
34 }
BuildError(ErrorCode errorCode)35 array<string> BuildError(ErrorCode errorCode)
36 {
37 taihe::set_business_error(errorCode, ErrCodeMap[errorCode]);
38 return taihe::array<string>(nullptr, 0);
39 }
BuildStringArray(const std::vector<std::string> & data)40 array<string> BuildStringArray(const std::vector<std::string>& data)
41 {
42 array<string> resArr(data.size(), "");
43 for (int i = 0; i < data.size(); i++) {
44 resArr[i] = data[i];
45 }
46 return resArr;
47 }
queryAssociatedDomains(string_view bundleName)48 array<string> queryAssociatedDomains(string_view bundleName)
49 {
50 Dfx::ApiEventReporter reporter("QueryAssociatedDomains");
51 if (!CheckInput(bundleName.c_str())) {
52 reporter.WriteEndEvent(Dfx::API_FAIL, ErrorCode::E_PARAM_ERROR);
53 return BuildError(ErrorCode::E_PARAM_ERROR);
54 }
55 std::vector<std::string> domains;
56
57 auto ret = AppDomainVerifyMgrClient::GetInstance()->QueryAssociatedDomains(bundleName.c_str(), domains);
58 if (ret != 0) {
59 if (ErrCodeMap.count(static_cast<ErrorCode>(ret)) != 0) {
60 reporter.WriteEndEvent(Dfx::API_FAIL, static_cast<ErrorCode>(ret));
61 return BuildError(static_cast<ErrorCode>(ret));
62 } else {
63 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "unknown error:%{public}d.", ret);
64 reporter.WriteEndEvent(Dfx::API_FAIL, ret);
65 return BuildStringArray(domains);
66 }
67 }
68 reporter.WriteEndEvent(Dfx::API_SUCCESS, ret);
69
70 return BuildStringArray(domains);
71 }
72
queryAssociatedBundleNames(string_view domain)73 array<string> queryAssociatedBundleNames(string_view domain)
74 {
75 Dfx::ApiEventReporter reporter("QueryAssociatedBundleNames");
76 if (!CheckInput(domain.c_str())) {
77 reporter.WriteEndEvent(Dfx::API_FAIL, ErrorCode::E_PARAM_ERROR);
78 return BuildError(ErrorCode::E_PARAM_ERROR);
79 }
80 std::vector<std::string> bundleNames;
81
82 auto ret = AppDomainVerifyMgrClient::GetInstance()->QueryAssociatedBundleNames(domain.c_str(), bundleNames);
83 if (ret != 0) {
84 if (ErrCodeMap.count(static_cast<ErrorCode>(ret)) != 0) {
85 reporter.WriteEndEvent(Dfx::API_FAIL, static_cast<ErrorCode>(ret));
86 return BuildError(static_cast<ErrorCode>(ret));
87 } else {
88 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "unknown error:%{public}d.", ret);
89 reporter.WriteEndEvent(Dfx::API_FAIL, ret);
90 return BuildStringArray(bundleNames);
91 }
92 }
93 reporter.WriteEndEvent(Dfx::API_SUCCESS, ret);
94
95 return BuildStringArray(bundleNames);
96 }
97 } // namespace
98
99 TH_EXPORT_CPP_API_queryAssociatedDomains(queryAssociatedDomains);
100 TH_EXPORT_CPP_API_queryAssociatedBundleNames(queryAssociatedBundleNames);
101