• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_execute_param.h"
17 
18 #include <charconv>
19 
20 #include "hilog_tag_wrapper.h"
21 #include "hitrace_meter.h"
22 #include "int_wrapper.h"
23 #include "string_wrapper.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 using WantParams = OHOS::AAFwk::WantParams;
ReadFromParcel(Parcel & parcel)28 bool InsightIntentExecuteParam::ReadFromParcel(Parcel &parcel)
29 {
30     bundleName_ = Str16ToStr8(parcel.ReadString16());
31     moduleName_ = Str16ToStr8(parcel.ReadString16());
32     abilityName_ = Str16ToStr8(parcel.ReadString16());
33     insightIntentName_ = Str16ToStr8(parcel.ReadString16());
34     std::shared_ptr<WantParams> wantParams(parcel.ReadParcelable<WantParams>());
35     if (wantParams == nullptr) {
36         return false;
37     }
38     insightIntentParam_ = wantParams;
39     executeMode_ = parcel.ReadInt32();
40     insightIntentId_ = parcel.ReadUint64();
41     displayId_ = parcel.ReadInt32();
42     parcel.ReadStringVector(&uris_);
43     flags_ = parcel.ReadInt32();
44     return true;
45 }
46 
Unmarshalling(Parcel & parcel)47 InsightIntentExecuteParam *InsightIntentExecuteParam::Unmarshalling(Parcel &parcel)
48 {
49     InsightIntentExecuteParam *param = new (std::nothrow) InsightIntentExecuteParam();
50     if (param == nullptr) {
51         return nullptr;
52     }
53 
54     if (!param->ReadFromParcel(parcel)) {
55         delete param;
56         param = nullptr;
57     }
58     return param;
59 }
60 
Marshalling(Parcel & parcel) const61 bool InsightIntentExecuteParam::Marshalling(Parcel &parcel) const
62 {
63     parcel.WriteString16(Str8ToStr16(bundleName_));
64     parcel.WriteString16(Str8ToStr16(moduleName_));
65     parcel.WriteString16(Str8ToStr16(abilityName_));
66     parcel.WriteString16(Str8ToStr16(insightIntentName_));
67     parcel.WriteParcelable(insightIntentParam_.get());
68     parcel.WriteInt32(executeMode_);
69     parcel.WriteUint64(insightIntentId_);
70     parcel.WriteInt32(displayId_);
71     parcel.WriteStringVector(uris_);
72     parcel.WriteInt32(flags_);
73     return true;
74 }
75 
IsInsightIntentExecute(const AAFwk::Want & want)76 bool InsightIntentExecuteParam::IsInsightIntentExecute(const AAFwk::Want &want)
77 {
78     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_NAME)) {
79         return true;
80     }
81     return false;
82 }
83 
GenerateFromWant(const AAFwk::Want & want,InsightIntentExecuteParam & executeParam)84 bool InsightIntentExecuteParam::GenerateFromWant(const AAFwk::Want &want,
85     InsightIntentExecuteParam &executeParam)
86 {
87     const WantParams &wantParams = want.GetParams();
88     if (!wantParams.HasParam(INSIGHT_INTENT_EXECUTE_PARAM_NAME)) {
89         TAG_LOGE(AAFwkTag::INTENT, "empty want");
90         return false;
91     }
92     uint64_t insightIntentId = 0;
93     std::string idStr = wantParams.GetStringParam(INSIGHT_INTENT_EXECUTE_PARAM_ID);
94     auto res = std::from_chars(idStr.c_str(), idStr.c_str() + idStr.size(), insightIntentId);
95     if (res.ec != std::errc()) {
96         TAG_LOGE(AAFwkTag::ABILITY, "invalid insight intent ID");
97         return false;
98     }
99 
100     AppExecFwk::ElementName elementName = want.GetElement();
101     executeParam.bundleName_ = elementName.GetBundleName();
102     executeParam.moduleName_ = elementName.GetModuleName();
103     executeParam.abilityName_ = elementName.GetAbilityName();
104     executeParam.insightIntentName_ = wantParams.GetStringParam(INSIGHT_INTENT_EXECUTE_PARAM_NAME);
105     executeParam.insightIntentId_ = insightIntentId;
106     executeParam.executeMode_ = wantParams.GetIntParam(INSIGHT_INTENT_EXECUTE_PARAM_MODE, 0);
107 
108     auto insightIntentParam = wantParams.GetWantParams(INSIGHT_INTENT_EXECUTE_PARAM_PARAM);
109     UpdateInsightIntentCallerInfo(wantParams, insightIntentParam);
110     executeParam.insightIntentParam_ = std::make_shared<WantParams>(insightIntentParam);
111     executeParam.uris_ = want.GetStringArrayParam(INSIGHT_INTENT_EXECUTE_PARAM_URI);
112     executeParam.flags_ = wantParams.GetIntParam(INSIGHT_INTENT_EXECUTE_PARAM_FLAGS, 0);
113 
114     return true;
115 }
116 
RemoveInsightIntent(AAFwk::Want & want)117 bool InsightIntentExecuteParam::RemoveInsightIntent(AAFwk::Want &want)
118 {
119     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
120     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_NAME)) {
121         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_NAME);
122     }
123     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_ID)) {
124         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_ID);
125     }
126     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_MODE)) {
127         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_MODE);
128     }
129     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_PARAM)) {
130         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_PARAM);
131     }
132     if (want.HasParameter(INSIGHT_INTENT_SRC_ENTRY)) {
133         want.RemoveParam(INSIGHT_INTENT_SRC_ENTRY);
134     }
135     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_URI)) {
136         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_URI);
137     }
138     if (want.HasParameter(INSIGHT_INTENT_EXECUTE_PARAM_FLAGS)) {
139         want.RemoveParam(INSIGHT_INTENT_EXECUTE_PARAM_FLAGS);
140     }
141     return true;
142 }
143 
UpdateInsightIntentCallerInfo(const WantParams & wantParams,WantParams & insightIntentParam)144 void InsightIntentExecuteParam::UpdateInsightIntentCallerInfo(const WantParams &wantParams,
145     WantParams &insightIntentParam)
146 {
147     insightIntentParam.Remove(AAFwk::Want::PARAM_RESV_CALLER_TOKEN);
148     insightIntentParam.SetParam(AAFwk::Want::PARAM_RESV_CALLER_TOKEN,
149         AAFwk::Integer::Box(wantParams.GetIntParam(AAFwk::Want::PARAM_RESV_CALLER_TOKEN, 0)));
150 
151     insightIntentParam.Remove(AAFwk::Want::PARAM_RESV_CALLER_UID);
152     insightIntentParam.SetParam(AAFwk::Want::PARAM_RESV_CALLER_UID,
153         AAFwk::Integer::Box(wantParams.GetIntParam(AAFwk::Want::PARAM_RESV_CALLER_UID, 0)));
154 
155     insightIntentParam.Remove(AAFwk::Want::PARAM_RESV_CALLER_PID);
156     insightIntentParam.SetParam(AAFwk::Want::PARAM_RESV_CALLER_PID,
157         AAFwk::Integer::Box(wantParams.GetIntParam(AAFwk::Want::PARAM_RESV_CALLER_PID, 0)));
158 
159     insightIntentParam.Remove(AAFwk::Want::PARAM_RESV_CALLER_BUNDLE_NAME);
160     insightIntentParam.SetParam(AAFwk::Want::PARAM_RESV_CALLER_BUNDLE_NAME,
161         AAFwk::String::Box(wantParams.GetStringParam(AAFwk::Want::PARAM_RESV_CALLER_BUNDLE_NAME)));
162 }
163 } // namespace AppExecFwk
164 } // namespace OHOS
165