1 /*
2  * Copyright (c) 2023-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 "insight_intent_utils.h"
17 
18 #include "ability_manager_errors.h"
19 #include "bundle_mgr_helper.h"
20 #include "hilog_tag_wrapper.h"
21 #include "in_process_call_wrapper.h"
22 #include "insight_intent_profile.h"
23 #include "os_account_manager_wrapper.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
CheckAbilityName(const InsightIntentInfo & info,const std::string & abilityName,const AppExecFwk::ExecuteMode & executeMode)28 bool CheckAbilityName(const InsightIntentInfo &info, const std::string &abilityName,
29     const AppExecFwk::ExecuteMode &executeMode)
30 {
31     bool matched = false;
32     switch (executeMode) {
33         case AppExecFwk::ExecuteMode::UI_ABILITY_FOREGROUND:
34         case AppExecFwk::ExecuteMode::UI_ABILITY_BACKGROUND:
35             matched = info.uiAbilityIntentInfo.abilityName == abilityName;
36             break;
37         case AppExecFwk::ExecuteMode::UI_EXTENSION_ABILITY:
38             matched = info.uiExtensionIntentInfo.abilityName == abilityName;
39             break;
40         case AppExecFwk::ExecuteMode::SERVICE_EXTENSION_ABILITY:
41             matched = info.serviceExtensionIntentInfo.abilityName == abilityName;
42             break;
43         default:
44             break;
45     }
46     if (!matched) {
47         TAG_LOGW(AAFwkTag::INTENT, "ability name mismatch");
48     }
49     return matched;
50 }
51 } // namespace
52 
GetSrcEntry(const AppExecFwk::ElementName & elementName,const std::string & intentName,const AppExecFwk::ExecuteMode & executeMode,std::string & srcEntry)53 uint32_t InsightIntentUtils::GetSrcEntry(const AppExecFwk::ElementName &elementName, const std::string &intentName,
54     const AppExecFwk::ExecuteMode &executeMode, std::string &srcEntry)
55 {
56     TAG_LOGD(AAFwkTag::INTENT, "get srcEntry, elementName: %{public}s, intentName: %{public}s, mode: %{public}d",
57         elementName.GetURI().c_str(), intentName.c_str(), executeMode);
58     auto bundleName = elementName.GetBundleName();
59     auto moduleName = elementName.GetModuleName();
60     auto abilityName = elementName.GetAbilityName();
61     if (bundleName.empty() || moduleName.empty() || abilityName.empty() || intentName.empty()) {
62         TAG_LOGE(AAFwkTag::INTENT, "input param empty");
63         return ERR_INVALID_VALUE;
64     }
65 
66     auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
67     if (bundleMgrHelper == nullptr) {
68         return ERR_NULL_OBJECT;
69     }
70 
71     // Get json profile firstly
72     std::string profile;
73     auto ret = IN_PROCESS_CALL(bundleMgrHelper->GetJsonProfile(AppExecFwk::INTENT_PROFILE, bundleName, moduleName,
74         profile, AppExecFwk::OsAccountManagerWrapper::GetCurrentActiveAccountId()));
75     if (ret != ERR_OK) {
76         TAG_LOGE(AAFwkTag::INTENT, "Get json profile failed code: %{public}d", ret);
77         return AAFwk::ERR_INSIGHT_INTENT_GET_PROFILE_FAILED;
78     }
79 
80     // Transform json string
81     std::vector<InsightIntentInfo> infos;
82     if (!InsightIntentProfile::TransformTo(profile, infos)) {
83         TAG_LOGE(AAFwkTag::INTENT, "Transform profile failed");
84         return ERR_INVALID_VALUE;
85     }
86 
87     // Get srcEntry when intentName matched
88     for (const auto &info: infos) {
89         if (info.intentName == intentName && CheckAbilityName(info, abilityName, executeMode)) {
90             srcEntry = info.srcEntry;
91             TAG_LOGD(AAFwkTag::INTENT, "srcEntry: %{public}s", srcEntry.c_str());
92             return ERR_OK;
93         }
94     }
95 
96     TAG_LOGE(AAFwkTag::INTENT, "get srcEntry failed");
97     return AAFwk::ERR_INSIGHT_INTENT_START_INVALID_COMPONENT;
98 }
99 } // namespace AbilityRuntime
100 } // namespace OHOS
101