• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_connect_helper.h"
17 
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "iservice_registry.h"
21 #include "ipc_skeleton.h"
22 #include "message_parcel.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS::AVSession {
GetInstance()26 AbilityConnectHelper& AbilityConnectHelper::GetInstance()
27 {
28     static AbilityConnectHelper abilityConnectHelper;
29     return abilityConnectHelper;
30 }
31 
StartAbilityForegroundByCall(const std::string & bundleName,const std::string & abilityName)32 int32_t AbilityConnectHelper::StartAbilityForegroundByCall(const std::string& bundleName,
33     const std::string& abilityName)
34 {
35     SLOGI("StartAbilityForegroundByCall bundleName=%{public}s abilityName=%{public}s",
36         bundleName.c_str(), abilityName.c_str());
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option;
40     if (!data.WriteInterfaceToken(ABILITY_MANAGER_INTERFACE_TOKEN)) {
41         SLOGE("write interface token failed");
42         return ERR_MARSHALLING;
43     }
44     AAFwk::Want want;
45     AppExecFwk::ElementName element("", bundleName, abilityName);
46     want.SetElement(element);
47     want.SetParam("ohos.aafwk.param.callAbilityToForeground", true);
48 
49     if (!data.WriteParcelable(&want)) {
50         SLOGE("want write failed");
51         return ERR_INVALID_PARAM;
52     }
53     sptr<AAFwk::IAbilityConnection> connect = new(std::nothrow) AbilityConnectCallback();
54     if (connect == nullptr) {
55         SLOGE("connect is nullptr");
56         return ERR_NO_MEMORY;
57     }
58     if (!data.WriteRemoteObject(connect->AsObject())) {
59         SLOGE("resolve write failed");
60         return ERR_MARSHALLING;
61     }
62     if (!data.WriteBool(false)) {
63         SLOGE("Failed to write flag");
64         return ERR_MARSHALLING;
65     }
66     if (!data.WriteInt32(-1)) { // -1 is default connect id of ability manager
67         SLOGE("Failed to write connect id");
68         return ERR_MARSHALLING;
69     }
70 
71     sptr<IRemoteObject> remote = GetSystemAbility();
72     if (remote == nullptr) {
73         return ERR_SERVICE_NOT_EXIST;
74     }
75     if (remote->SendRequest(static_cast<uint32_t>(AVSESSION_START_CALL_ABILITY_CODE),
76         data, reply, option) != 0) {
77         SLOGE("Send request error");
78         return ERR_IPC_SEND_REQUEST;
79     }
80     return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE;
81 }
82 
StartAbilityByCall(const std::string & bundleName,const std::string & abilityName)83 int32_t AbilityConnectHelper::StartAbilityByCall(const std::string& bundleName, const std::string& abilityName)
84 {
85     SLOGI("bundleName=%{public}s abilityName=%{public}s", bundleName.c_str(), abilityName.c_str());
86     MessageParcel data;
87     MessageParcel reply;
88     MessageOption option;
89     if (!data.WriteInterfaceToken(ABILITY_MANAGER_INTERFACE_TOKEN)) {
90         SLOGE("write interface token failed");
91         return ERR_MARSHALLING;
92     }
93 
94     AAFwk::Want want;
95     AppExecFwk::ElementName element("", bundleName, abilityName);
96     want.SetElement(element);
97     if (!data.WriteParcelable(&want)) {
98         SLOGE("want write failed");
99         return ERR_INVALID_PARAM;
100     }
101 
102     sptr<AAFwk::IAbilityConnection> connect = new(std::nothrow) AbilityConnectCallback();
103     if (connect == nullptr) {
104         SLOGE("connect is nullptr");
105         return ERR_NO_MEMORY;
106     }
107     if (!data.WriteRemoteObject(connect->AsObject())) {
108         SLOGE("resolve write failed");
109         return ERR_MARSHALLING;
110     }
111     if (!data.WriteBool(false)) {
112         SLOGE("Failed to write flag");
113         return ERR_MARSHALLING;
114     }
115     if (!data.WriteInt32(-1)) { // -1 is default connect id of ability manager
116         SLOGE("Failed to write connect id");
117         return ERR_MARSHALLING;
118     }
119 
120     sptr<IRemoteObject> remote = GetSystemAbility();
121     if (remote == nullptr) {
122         return ERR_SERVICE_NOT_EXIST;
123     }
124     if (remote->SendRequest(static_cast<uint32_t>(AVSESSION_START_CALL_ABILITY_CODE),
125         data, reply, option) != 0) {
126         SLOGE("Send request error");
127         return ERR_IPC_SEND_REQUEST;
128     }
129     return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE;
130 }
131 
GetSystemAbility()132 sptr<IRemoteObject> AbilityConnectHelper::GetSystemAbility()
133 {
134     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135     if (systemManager == nullptr) {
136         SLOGE("Fail to get registry");
137         return nullptr;
138     }
139     sptr<IRemoteObject> remote = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
140     if (remote == nullptr) {
141         SLOGE("Fail to connect ability manager service");
142         return nullptr;
143     }
144     SLOGI("Connect ability manager service success");
145     return remote;
146 }
147 
AbilityConnectionStub()148 AbilityConnectionStub::AbilityConnectionStub()
149 {}
150 
~AbilityConnectionStub()151 AbilityConnectionStub::~AbilityConnectionStub()
152 {}
153 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)154 int AbilityConnectionStub::OnRemoteRequest(
155     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
156 {
157     CHECK_AND_RETURN_RET_LOG(IPCSkeleton::IsLocalCalling(), AVSESSION_ERROR, "forbid rpc remote request");
158     auto descriptor = AbilityConnectionStub::GetDescriptor();
159     auto remoteDescriptor = data.ReadInterfaceToken();
160     if (descriptor != remoteDescriptor) {
161         SLOGE("Local descriptor is not equal to remote");
162         return AVSESSION_ERROR;
163     }
164 
165     auto element = data.ReadParcelable<AppExecFwk::ElementName>();
166     if (element == nullptr) {
167         SLOGE("callback stub receive element is nullptr");
168         return AVSESSION_ERROR;
169     }
170     if (code == AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE) {
171         auto remoteObject = data.ReadRemoteObject();
172         if (remoteObject == nullptr) {
173             SLOGE("callback stub receive remoteObject is nullptr");
174             delete element;
175             element = nullptr;
176             return AVSESSION_ERROR;
177         }
178         auto resultCode = data.ReadInt32();
179         OnAbilityConnectDone(*element, remoteObject, resultCode);
180         delete element;
181         element = nullptr;
182         return ERR_NONE;
183     }
184     if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) {
185         auto resultCode = data.ReadInt32();
186         OnAbilityDisconnectDone(*element, resultCode);
187         delete element;
188         element = nullptr;
189         return ERR_NONE;
190     }
191     delete element;
192     element = nullptr;
193     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
194 }
195 
~AbilityConnectCallback()196 AbilityConnectCallback::~AbilityConnectCallback()
197 {}
198 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)199 void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
200     const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode)
201 {
202     SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
203         resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
204 }
205 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)206 void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)
207 {
208     SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
209         resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
210 }
211 } // namespace OHOS::AVSession