• 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 "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     if (!data.WriteInt32(-1)) { // -1 is default connect id of ability manager
66         SLOGE("Failed to write connect id");
67         return ERR_MARSHALLING;
68     }
69 
70     sptr<IRemoteObject> remote = GetSystemAbility();
71     if (remote == nullptr) {
72         return ERR_SERVICE_NOT_EXIST;
73     }
74     if (remote->SendRequest(AAFwk::IAbilityManager::START_CALL_ABILITY, data, reply, option) != 0) {
75         SLOGE("Send request error");
76         return ERR_IPC_SEND_REQUEST;
77     }
78     return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE;
79 }
80 
GetSystemAbility()81 sptr<IRemoteObject> AbilityConnectHelper::GetSystemAbility()
82 {
83     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
84     if (systemManager == nullptr) {
85         SLOGE("Fail to get registry");
86         return nullptr;
87     }
88     sptr<IRemoteObject> remote = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
89     if (remote == nullptr) {
90         SLOGE("Fail to connect ability manager service");
91         return nullptr;
92     }
93     SLOGI("Connect ability manager service success");
94     return remote;
95 }
96 
AbilityConnectionStub()97 AbilityConnectionStub::AbilityConnectionStub()
98 {}
99 
~AbilityConnectionStub()100 AbilityConnectionStub::~AbilityConnectionStub()
101 {}
102 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)103 int AbilityConnectionStub::OnRemoteRequest(
104     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
105 {
106     auto descriptor = AbilityConnectionStub::GetDescriptor();
107     auto remoteDescriptor = data.ReadInterfaceToken();
108     if (descriptor != remoteDescriptor) {
109         SLOGE("Local descriptor is not equal to remote");
110         return AVSESSION_ERROR;
111     }
112 
113     auto element = data.ReadParcelable<AppExecFwk::ElementName>();
114     if (element == nullptr) {
115         SLOGE("callback stub receive element is nullptr");
116         return AVSESSION_ERROR;
117     }
118     if (code == AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE) {
119         auto remoteObject = data.ReadRemoteObject();
120         if (remoteObject == nullptr) {
121             SLOGE("callback stub receive remoteObject is nullptr");
122             delete element;
123             return AVSESSION_ERROR;
124         }
125         auto resultCode = data.ReadInt32();
126         OnAbilityConnectDone(*element, remoteObject, resultCode);
127         delete element;
128         return ERR_NONE;
129     }
130     if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) {
131         auto resultCode = data.ReadInt32();
132         OnAbilityDisconnectDone(*element, resultCode);
133         delete element;
134         return ERR_NONE;
135     }
136     delete element;
137     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
138 }
139 
~AbilityConnectCallback()140 AbilityConnectCallback::~AbilityConnectCallback()
141 {}
142 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)143 void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
144     const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode)
145 {
146     SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
147         resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
148 }
149 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)150 void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)
151 {
152     SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
153         resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
154 }
155 } // namespace OHOS::AVSession