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 "ability_manager_interface.h"
19 #include "avsession_errors.h"
20 #include "avsession_log.h"
21 #include "iservice_registry.h"
22 #include "ipc_skeleton.h"
23 #include "message_parcel.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS::AVSession {
GetInstance()27 AbilityConnectHelper& AbilityConnectHelper::GetInstance()
28 {
29 static AbilityConnectHelper abilityConnectHelper;
30 return abilityConnectHelper;
31 }
32
StartAbilityByCall(const std::string & bundleName,const std::string & abilityName)33 int32_t AbilityConnectHelper::StartAbilityByCall(const std::string& bundleName, const std::string& abilityName)
34 {
35 SLOGI("bundleName=%{public}s abilityName=%{public}s", bundleName.c_str(), abilityName.c_str());
36 MessageParcel data;
37 MessageParcel reply;
38 MessageOption option;
39 if (!data.WriteInterfaceToken(ABILITY_MANAGER_INTERFACE_TOKEN)) {
40 SLOGE("write interface token failed");
41 return ERR_MARSHALLING;
42 }
43
44 AAFwk::Want want;
45 AppExecFwk::ElementName element("", bundleName, abilityName);
46 want.SetElement(element);
47 if (!data.WriteParcelable(&want)) {
48 SLOGE("want write failed");
49 return ERR_INVALID_PARAM;
50 }
51
52 sptr<AAFwk::IAbilityConnection> connect = new(std::nothrow) AbilityConnectCallback();
53 if (connect == nullptr) {
54 SLOGE("connect is nullptr");
55 return ERR_NO_MEMORY;
56 }
57 if (!data.WriteRemoteObject(connect->AsObject())) {
58 SLOGE("resolve write failed");
59 return ERR_MARSHALLING;
60 }
61 if (!data.WriteBool(false)) {
62 SLOGE("Failed to write flag");
63 return ERR_MARSHALLING;
64 }
65
66 sptr<IRemoteObject> remote = GetSystemAbility();
67 if (remote == nullptr) {
68 return ERR_SERVICE_NOT_EXIST;
69 }
70 if (remote->SendRequest(AAFwk::IAbilityManager::START_CALL_ABILITY, data, reply, option) != 0) {
71 SLOGE("Send request error");
72 return ERR_IPC_SEND_REQUEST;
73 }
74 return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE;
75 }
76
GetSystemAbility()77 sptr<IRemoteObject> AbilityConnectHelper::GetSystemAbility()
78 {
79 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
80 if (systemManager == nullptr) {
81 SLOGE("Fail to get registry");
82 return nullptr;
83 }
84 sptr<IRemoteObject> remote = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
85 if (remote == nullptr) {
86 SLOGE("Fail to connect ability manager service");
87 return nullptr;
88 }
89 SLOGI("Connect ability manager service success");
90 return remote;
91 }
92
AbilityConnectionStub()93 AbilityConnectionStub::AbilityConnectionStub()
94 {}
95
~AbilityConnectionStub()96 AbilityConnectionStub::~AbilityConnectionStub()
97 {}
98
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)99 int AbilityConnectionStub::OnRemoteRequest(
100 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
101 {
102 auto descriptor = AbilityConnectionStub::GetDescriptor();
103 auto remoteDescriptor = data.ReadInterfaceToken();
104 if (descriptor != remoteDescriptor) {
105 SLOGE("Local descriptor is not equal to remote");
106 return AVSESSION_ERROR;
107 }
108
109 auto element = data.ReadParcelable<AppExecFwk::ElementName>();
110 if (element == nullptr) {
111 SLOGE("callback stub receive element is nullptr");
112 return AVSESSION_ERROR;
113 }
114 if (code == AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE) {
115 auto remoteObject = data.ReadRemoteObject();
116 if (remoteObject == nullptr) {
117 SLOGE("callback stub receive remoteObject is nullptr");
118 delete element;
119 return AVSESSION_ERROR;
120 }
121 auto resultCode = data.ReadInt32();
122 OnAbilityConnectDone(*element, remoteObject, resultCode);
123 delete element;
124 return ERR_NONE;
125 }
126 if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) {
127 auto resultCode = data.ReadInt32();
128 OnAbilityDisconnectDone(*element, resultCode);
129 delete element;
130 return ERR_NONE;
131 }
132 delete element;
133 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
134 }
135
~AbilityConnectCallback()136 AbilityConnectCallback::~AbilityConnectCallback()
137 {}
138
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)139 void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
140 const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode)
141 {
142 SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
143 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
144 }
145
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)146 void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)
147 {
148 SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
149 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
150 }
151 } // namespace OHOS::AVSession