1 /*
2  * Copyright (c) 2020 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 "util/abilityms_helper.h"
17 
18 #include <cstring>
19 
20 #include "adapter.h"
21 #include "bundle_inner_interface.h"
22 #include "bundle_manager.h"
23 #include "iproxy_client.h"
24 #include "samgr_lite.h"
25 
26 namespace OHOS {
27 const char * const LAUNCHER_BUNDLE_NAME = "com.huawei.launcher";
28 const char * const LAUNCHER_ABILITY_NAME = "MainAbility";
29 const char * const ACE_ABILITY_NAME = "AceAbility";
30 const int MAX_BUNDLE_NAME_SIZE = 128;
31 
IsLauncherAbility(const char * bundleName)32 bool AbilityMsHelper::IsLauncherAbility(const char *bundleName)
33 {
34     if (bundleName == nullptr) {
35         PRINTW("AbilityMsHelper", "bundleName is null");
36         return false;
37     }
38     return strcmp(LAUNCHER_BUNDLE_NAME, bundleName) == 0;
39 }
40 
IsAceAbility(const char * abilityName)41 bool AbilityMsHelper::IsAceAbility(const char *abilityName)
42 {
43     if (abilityName == nullptr) {
44         PRINTW("AbilityMsHelper", "abilityName is null");
45         return false;
46     }
47     return strcmp(ACE_ABILITY_NAME, abilityName) == 0;
48 }
49 
SetLauncherWant(Want & want)50 AbilityMsStatus AbilityMsHelper::SetLauncherWant(Want &want)
51 {
52     ElementName elementName = {};
53     SetElementAbilityName(&elementName, LAUNCHER_ABILITY_NAME);
54     SetElementBundleName(&elementName, LAUNCHER_BUNDLE_NAME);
55 
56     SetWantElement(&want, elementName);
57     ClearElement(&elementName);
58     return AbilityMsStatus::Ok();
59 }
60 
SetKeepAliveWant(const BundleInfo & bundleInfo,Want & want)61 AbilityMsStatus AbilityMsHelper::SetKeepAliveWant(const BundleInfo &bundleInfo, Want &want)
62 {
63     if (bundleInfo.numOfAbility <= 0) {
64         return AbilityMsStatus::HelpStatus("no ability exist");
65     }
66     ElementName elementName = {};
67     if (bundleInfo.abilityInfos[0].name == nullptr) {
68         return AbilityMsStatus::HelpStatus("abilityName is null");
69     }
70     if (bundleInfo.bundleName == nullptr) {
71         return AbilityMsStatus::HelpStatus("bundleName is null");
72     }
73     SetElementAbilityName(&elementName, bundleInfo.abilityInfos[0].name);
74     SetElementBundleName(&elementName, bundleInfo.bundleName);
75 
76     SetWantElement(&want, elementName);
77     ClearElement(&elementName);
78     return AbilityMsStatus::Ok();
79 }
80 
81 #ifdef OHOS_DEBUG
AbilityStateToString(State state)82 std::string AbilityMsHelper::AbilityStateToString(State state)
83 {
84     std::string result;
85     switch (state) {
86         case STATE_UNINITIALIZED:
87             result = "uninitialized";
88             break;
89         case STATE_INITIAL:
90             result = "initial";
91             break;
92         case STATE_INACTIVE:
93             result = "inactive";
94             break;
95         case STATE_ACTIVE:
96             result = "active";
97             break;
98         case STATE_BACKGROUND:
99             result = "background";
100             break;
101         default:
102             break;
103     }
104     return result;
105 }
106 #endif
107 
IsLegalBundleName(const char * bundleName)108 bool AbilityMsHelper::IsLegalBundleName(const char *bundleName)
109 {
110     if (bundleName == nullptr) {
111         return false;
112     }
113     int len = strlen(bundleName);
114     if (len <= 0 || len > MAX_BUNDLE_NAME_SIZE) {
115         return false;
116     }
117     return true;
118 }
119 
CheckVisiblePermission(pid_t callingUid,pid_t targetUid,bool isVisible)120 bool AbilityMsHelper::CheckVisiblePermission(pid_t callingUid, pid_t targetUid, bool isVisible)
121 {
122     if (callingUid == SYSTEM_UID || callingUid == HUKS_UID) {
123         return true;
124     }
125     if (isVisible || callingUid == targetUid) {
126         return true;
127     }
128     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(BMS_SERVICE, BMS_FEATURE);
129     if (iUnknown == nullptr) {
130         return false;
131     }
132     BmsServerProxy *bmsServerProxy = nullptr;
133     int result = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **) &bmsServerProxy);
134     if (result != 0 || bmsServerProxy == nullptr) {
135         return false;
136     }
137     char *bundleName = nullptr;
138     uint8_t ret = bmsServerProxy->GetBundleNameForUid(callingUid, &bundleName);
139     if (ret == 0 && IsLauncherAbility(bundleName)) {
140         AdapterFree(bundleName);
141         return true;
142     }
143     AdapterFree(bundleName);
144     return false;
145 }
146 }
147