• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "app_startup_task_matcher.h"
17 
18 #include <string>
19 
20 #include "insight_intent_execute_param.h"
21 #include "hilog_tag_wrapper.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
ModuleStartStartupTaskMatcher(const std::string & moduleName)25 ModuleStartStartupTaskMatcher::ModuleStartStartupTaskMatcher(const std::string &moduleName) : moduleName_(moduleName)
26 {}
27 
Match(const AppStartupTask & task) const28 bool ModuleStartStartupTaskMatcher::Match(const AppStartupTask &task) const
29 {
30     return task.GetModuleName() == moduleName_ || task.GetModuleType() == AppExecFwk::ModuleType::SHARED;
31 }
32 
ExcludeFromAutoStartStartupTaskMatcher()33 ExcludeFromAutoStartStartupTaskMatcher::ExcludeFromAutoStartStartupTaskMatcher() {}
34 
Match(const AppStartupTask & task) const35 bool ExcludeFromAutoStartStartupTaskMatcher::Match(const AppStartupTask &task) const
36 {
37     return !task.GetIsExcludeFromAutoStart();
38 }
39 
UriStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)40 UriStartupTaskMatcher::UriStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)
41 {
42     if (!want) {
43         TAG_LOGW(AAFwkTag::STARTUP, "want is null");
44         return;
45     }
46     uri_ = std::make_shared<Uri>(want->GetUri());
47 }
48 
UriStartupTaskMatcher(std::shared_ptr<Uri> uri)49 UriStartupTaskMatcher::UriStartupTaskMatcher(std::shared_ptr<Uri> uri) : uri_(uri) {}
50 
Match(const AppStartupTask & task) const51 bool UriStartupTaskMatcher::Match(const AppStartupTask &task) const
52 {
53     if (!uri_) {
54         TAG_LOGW(AAFwkTag::STARTUP, "uri_ is null");
55         return false;
56     }
57 
58     TAG_LOGD(AAFwkTag::STARTUP, "task:%{public}s, uri:%{public}s", task.GetName().c_str(), uri_->ToString().c_str());
59     if (uri_->ToString().empty()) {
60         return false;
61     }
62 
63     const std::string scheme = uri_->GetScheme();
64     const std::string host = uri_->GetHost();
65     const std::string path = uri_->GetPath();
66     std::string uriToMatch = scheme + "://" + host + path;
67 
68     const auto &matchRules = task.GetUriMatchRules();
69     return std::any_of(matchRules.begin(), matchRules.end(), [&uriToMatch](const std::string &rule) {
70         return uriToMatch == rule;
71     });
72 }
73 
InsightIntentStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)74 InsightIntentStartupTaskMatcher::InsightIntentStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)
75 {
76     if (!want) {
77         TAG_LOGW(AAFwkTag::STARTUP, "want is null");
78         return;
79     }
80 
81     const AppExecFwk::WantParams &wantParams = want->GetParams();
82     if (!wantParams.HasParam(AppExecFwk::INSIGHT_INTENT_EXECUTE_PARAM_NAME)) {
83         return;
84     }
85     insightIntentName_ = wantParams.GetStringParam(AppExecFwk::INSIGHT_INTENT_EXECUTE_PARAM_NAME);
86 }
87 
InsightIntentStartupTaskMatcher(const std::string & insightIntentName)88 InsightIntentStartupTaskMatcher::InsightIntentStartupTaskMatcher(const std::string &insightIntentName)
89     : insightIntentName_(insightIntentName) {}
90 
Match(const AppStartupTask & task) const91 bool InsightIntentStartupTaskMatcher::Match(const AppStartupTask &task) const
92 {
93     TAG_LOGD(AAFwkTag::STARTUP, "task:%{public}s, insightIntentName:%{public}s", task.GetName().c_str(),
94         insightIntentName_.c_str());
95     if (insightIntentName_.empty()) {
96         return false;
97     }
98 
99     const auto &matchRules = task.GetInsightIntentMatchRules();
100     return std::any_of(matchRules.begin(), matchRules.end(), [&name = insightIntentName_](const std::string &rule) {
101         return name == rule;
102     });
103 }
104 
ActionStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)105 ActionStartupTaskMatcher::ActionStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)
106 {
107     if (!want) {
108         TAG_LOGW(AAFwkTag::STARTUP, "want is null");
109         return;
110     }
111     action_ = want->GetAction();
112 }
113 
ActionStartupTaskMatcher(const std::string & action)114 ActionStartupTaskMatcher::ActionStartupTaskMatcher(const std::string &action) : action_(action) {}
115 
Match(const AppStartupTask & task) const116 bool ActionStartupTaskMatcher::Match(const AppStartupTask &task) const
117 {
118     TAG_LOGD(AAFwkTag::STARTUP, "task:%{public}s, action:%{public}s", task.GetName().c_str(), action_.c_str());
119     if (action_.empty()) {
120         return false;
121     }
122 
123     const auto &matchRules = task.GetActionMatchRules();
124     return std::any_of(matchRules.begin(), matchRules.end(), [&action = action_](const std::string &rule) {
125         return action == rule;
126     });
127 }
128 
CustomizationStartupTaskMatcher(const std::string & customization)129 CustomizationStartupTaskMatcher::CustomizationStartupTaskMatcher(const std::string &customization)
130     : customization_(customization) {}
131 
Match(const AppStartupTask & task) const132 bool CustomizationStartupTaskMatcher::Match(const AppStartupTask &task) const
133 {
134     TAG_LOGD(AAFwkTag::STARTUP, "task:%{public}s, customization_:%{public}s", task.GetName().c_str(),
135         customization_.c_str());
136     if (customization_.empty()) {
137         return false;
138     }
139 
140     const auto &matchRules = task.GetCustomizationMatchRules();
141     return std::any_of(matchRules.begin(), matchRules.end(), [&custom = customization_](const std::string &rule) {
142         return custom == rule;
143     });
144 }
145 
146 
MatchRulesStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)147 MatchRulesStartupTaskMatcher::MatchRulesStartupTaskMatcher(std::shared_ptr<AAFwk::Want> want)
148 {
149     auto uriMatcher = std::make_shared<UriStartupTaskMatcher>(want);
150     auto actionMatcher = std::make_shared<ActionStartupTaskMatcher>(want);
151     auto insightIntentMatcher = std::make_shared<InsightIntentStartupTaskMatcher>(want);
152     matchers_.emplace_back(uriMatcher);
153     matchers_.emplace_back(actionMatcher);
154     matchers_.emplace_back(insightIntentMatcher);
155 }
156 
MatchRulesStartupTaskMatcher(const std::string & uri,const std::string & action,const std::string & insightIntentName)157 MatchRulesStartupTaskMatcher::MatchRulesStartupTaskMatcher(const std::string &uri, const std::string &action,
158     const std::string &insightIntentName)
159 {
160     auto uriMatcher = std::make_shared<UriStartupTaskMatcher>(std::make_shared<Uri>(uri));
161     auto actionMatcher = std::make_shared<ActionStartupTaskMatcher>(action);
162     auto insightIntentMatcher = std::make_shared<InsightIntentStartupTaskMatcher>(insightIntentName);
163     matchers_.emplace_back(uriMatcher);
164     matchers_.emplace_back(actionMatcher);
165     matchers_.emplace_back(insightIntentMatcher);
166 }
167 
Match(const AppStartupTask & task) const168 bool MatchRulesStartupTaskMatcher::Match(const AppStartupTask &task) const
169 {
170     if (moduleMatcher_ && !moduleMatcher_->Match(task)) {
171         return false;
172     }
173     for (const auto &matcher : matchers_) {
174         if (!matcher) {
175             TAG_LOGW(AAFwkTag::STARTUP, "matcher is null");
176             continue;
177         }
178         if (matcher->Match(task)) {
179             return true;
180         }
181     }
182     if (customizationMatcher_) {
183         return customizationMatcher_->Match(task);
184     }
185     return false;
186 }
187 
SetModuleMatcher(std::shared_ptr<ModuleStartStartupTaskMatcher> matcher)188 void MatchRulesStartupTaskMatcher::SetModuleMatcher(std::shared_ptr<ModuleStartStartupTaskMatcher> matcher)
189 {
190     moduleMatcher_ = matcher;
191 }
192 
SetCustomizationMatcher(std::shared_ptr<CustomizationStartupTaskMatcher> matcher)193 void MatchRulesStartupTaskMatcher::SetCustomizationMatcher(std::shared_ptr<CustomizationStartupTaskMatcher> matcher)
194 {
195     customizationMatcher_ = matcher;
196 }
197 
DefaultStartupTaskMatcher(const std::string & moduleName)198 DefaultStartupTaskMatcher::DefaultStartupTaskMatcher(const std::string &moduleName) : moduleMatcher_(moduleName)
199 {}
200 
Match(const AppStartupTask & task) const201 bool DefaultStartupTaskMatcher::Match(const AppStartupTask &task) const
202 {
203     if (!moduleMatcher_.Match(task)) {
204         return false;
205     }
206     return excludeMatcher_.Match(task);
207 }
208 } // namespace AbilityRuntime
209 } // namespace OHOS
210