• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ability_inner_feature.h"
17 
18 #include "ability_connect_trans_param.h"
19 #include "ability_errors.h"
20 #include "ability_info.h"
21 #include "ability_message_id.h"
22 #include "ability_mgr_handler.h"
23 #include "ability_service_interface.h"
24 #include "adapter.h"
25 #include "ohos_init.h"
26 #include "iproxy_client.h"
27 #include "samgr_lite.h"
28 #include "securec.h"
29 #include "util/abilityms_helper.h"
30 #include "utils.h"
31 #include "want_utils.h"
32 
33 namespace OHOS {
34 AbilityInnerFeatureImpl g_amsInnerImpl = {
35     SERVER_IPROXY_IMPL_BEGIN,
36     .Invoke = AbilityInnerFeature::Invoke,
37     .StartKeepAliveApps = AbilityInnerFeature::StartKeepAliveApps,
38     .TerminateApp = AbilityInnerFeature::TerminateApp,
39     IPROXY_END
40 };
41 
42 InvokeFunc AbilityInnerFeature::invokeFuncList[COMMAND_END - INNER_BEGIN] {
43     AbilityInnerFeature::TerminateAppInvoke,
44     AbilityInnerFeature::DumpAbilityInvoke,
45 };
46 
Init()47 static void Init()
48 {
49     SamgrLite *samgrLite = SAMGR_GetInstance();
50     CHECK_NULLPTR_RETURN(samgrLite, "AbilityInnerFeature", "get samgr error");
51     BOOL result = samgrLite->RegisterFeature(AMS_SERVICE, AbilityInnerFeature::GetInstance());
52     if (result == FALSE) {
53         PRINTE("AbilityInnerFeature", "ams register feature failure");
54         return;
55     }
56     g_amsInnerImpl.ams = AbilityInnerFeature::GetInstance();
57     auto publicApi = GET_IUNKNOWN(g_amsInnerImpl);
58     CHECK_NULLPTR_RETURN(publicApi, "AbilityMgrFeatureLite", "publicApi is nullptr");
59     BOOL apiResult = samgrLite->RegisterFeatureApi(AMS_SERVICE, AMS_INNER_FEATURE, publicApi);
60     PRINTI("AbilityInnerFeature", "ams inner feature init %{public}s", apiResult ? "success" : "failure");
61 }
62 SYSEX_FEATURE_INIT(Init);
63 
AbilityInnerFeature()64 AbilityInnerFeature::AbilityInnerFeature() : Feature(), identity_()
65 {
66     this->Feature::GetName = AbilityInnerFeature::GetFeatureName;
67     this->Feature::OnInitialize = AbilityInnerFeature::OnFeatureInitialize;
68     this->Feature::OnStop = AbilityInnerFeature::OnFeatureStop;
69     this->Feature::OnMessage = AbilityInnerFeature::OnFeatureMessage;
70 }
71 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)72 int32 AbilityInnerFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
73 {
74     PRINTI("AbilityInnerFeature", "ams invoke called");
75     if (req == nullptr) {
76         return EC_INVALID;
77     }
78     if (funcId >= INNER_BEGIN && funcId < COMMAND_END) {
79         return invokeFuncList[funcId - INNER_BEGIN](origin, req);
80     }
81     return COMMAND_ERROR;
82 }
83 
GetFeatureName(Feature * feature)84 const char *AbilityInnerFeature::GetFeatureName(Feature *feature)
85 {
86     (void) feature;
87     return AMS_INNER_FEATURE;
88 }
89 
OnFeatureInitialize(Feature * feature,Service * parent,Identity identity)90 void AbilityInnerFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity)
91 {
92     CHECK_NULLPTR_RETURN(feature, "AbilityInnerFeature", "initialize fail");
93     (static_cast<AbilityInnerFeature *>(feature))->identity_ = identity;
94 }
95 
OnFeatureStop(Feature * feature,Identity identity)96 void AbilityInnerFeature::OnFeatureStop(Feature *feature, Identity identity)
97 {
98     (void) feature;
99     (void) identity;
100 }
101 
OnFeatureMessage(Feature * feature,Request * request)102 BOOL AbilityInnerFeature::OnFeatureMessage(Feature *feature, Request *request)
103 {
104     if (feature == nullptr || request == nullptr) {
105         return FALSE;
106     }
107 
108     AbilityMgrHandler::GetInstance().ServiceMsgProcess(*request);
109     return TRUE;
110 }
111 
TerminateAppInvoke(const void * origin,IpcIo * req)112 int32 AbilityInnerFeature::TerminateAppInvoke(const void *origin, IpcIo *req)
113 {
114     size_t len = 0;
115     char *bundleName = reinterpret_cast<char *>(IpcIoPopString(req, &len));
116     if (bundleName == nullptr || len == 0) {
117         PRINTE("AbilityInnerFeature", "bundleName is null");
118         return EC_INVALID;
119     }
120     return TerminateApp(bundleName);
121 }
122 
StartKeepAliveApps()123 int32 AbilityInnerFeature::StartKeepAliveApps()
124 {
125     Request request = {
126         .msgId = AMS_SERVICE_INITED,
127     };
128     return SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
129 }
130 
TerminateApp(const char * bundleName)131 int32 AbilityInnerFeature::TerminateApp(const char *bundleName)
132 {
133     if (!AbilityMsHelper::IsLegalBundleName(bundleName)) {
134         return EC_INVALID;
135     }
136 
137     char *name = Utils::Strdup(bundleName);
138     if (name == nullptr) {
139         return EC_NOMEMORY;
140     }
141     Request request = {
142         .msgId = AMS_TERMINATE_APP,
143         .len = 0,
144         .data = reinterpret_cast<void *>(name),
145     };
146     int32 propRet = SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
147     if (propRet != EC_SUCCESS) {
148         PRINTE("AbilityInnerFeature", "send request failure");
149         AdapterFree(name);
150         return EC_COMMU;
151     }
152     return EC_SUCCESS;
153 }
154 
DumpAbilityInvoke(const void * origin,IpcIo * req)155 int32 AbilityInnerFeature::DumpAbilityInvoke(const void *origin, IpcIo *req)
156 {
157     Want want = { nullptr, nullptr, nullptr, 0 };
158     if (!DeserializeWant(&want, req) || (want.sid == nullptr)) {
159         ClearWant(&want);
160         return EC_FAILURE;
161     }
162 #ifdef __LINUX__
163     BinderAcquire(want.sid->ipcContext, want.sid->handle);
164 #endif
165     auto client = new AbilityDumpClient(want);
166     ClearWant(&want);
167     Request request = {
168         .msgId = AMS_DUMP_ABILITY,
169         .len = 0,
170         .data = reinterpret_cast<void *>(client),
171     };
172     int32 propRet = SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
173     if (propRet != EC_SUCCESS) {
174         PRINTE("AbilityInnerFeature", "send request failure");
175         delete client;
176         return EC_COMMU;
177     }
178     return EC_SUCCESS;
179 }
180 } // namespace OHOS
181