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_self_callback.h"
17
18 #include "adapter.h"
19 #include "ability_callback_utils.h"
20 #include "ability_manager.h"
21 #include "iproxy_client.h"
22 #include "log.h"
23 #include "samgr_lite.h"
24
25 namespace OHOS {
~AbilitySelfCallback()26 AbilitySelfCallback::~AbilitySelfCallback()
27 {
28 if (svcIdentity_ != nullptr) {
29 (void) UnregisterIpcCallback(*svcIdentity_);
30 AdapterFree(svcIdentity_);
31 }
32 }
33
InnerCallback(const char * resultMessage,uint8_t resultCode,const IAbilityStartCallback & iAbilityStartCallback)34 int32_t InnerCallback(const char *resultMessage, uint8_t resultCode, const IAbilityStartCallback &iAbilityStartCallback)
35 {
36 if (resultMessage == nullptr || iAbilityStartCallback == nullptr) {
37 return PARAM_NULL_ERROR;
38 }
39 if (resultCode == ERR_OK) {
40 iAbilityStartCallback(resultCode, resultMessage);
41 } else {
42 iAbilityStartCallback(resultCode, ObtainErrorMessage(resultCode).c_str());
43 }
44 return ERR_OK;
45 }
46
Callback(const IpcContext * context,void * ipcMsg,IpcIo * io,void * arg)47 int32_t AbilitySelfCallback::Callback(const IpcContext *context, void *ipcMsg, IpcIo *io, void *arg)
48 {
49 if (ipcMsg == nullptr) {
50 HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallback ipcMsg is nullptr");
51 return PARAM_NULL_ERROR;
52 }
53
54 if (io == nullptr) {
55 HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallback io is nullptr");
56 FreeBuffer(NULL, ipcMsg);
57 return PARAM_NULL_ERROR;
58 }
59 IAbilityStartCallback iAbilityStartCallback = GetInstance().GetCallback();
60 if (iAbilityStartCallback == nullptr) {
61 FreeBuffer(NULL, ipcMsg);
62 return PARAM_NULL_ERROR;
63 }
64 uint32_t callbackType = 0;
65 int32_t ret = GetCode(ipcMsg, &callbackType);
66 if (ret != LITEIPC_OK) {
67 FreeBuffer(NULL, ipcMsg);
68 HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallbck get callback type failed");
69 return GET_CALLBACK_TYPE_ERROR;
70 }
71 auto resultCode = static_cast<uint8_t>(IpcIoPopInt32(io));
72 FreeBuffer(NULL, ipcMsg);
73 if (callbackType == START_ABILITY_CALLBACK) {
74 return InnerCallback(START_ABILITY_SUCCESS, resultCode, iAbilityStartCallback);
75 }
76 HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallback get error callback type");
77 return GET_CALLBACK_TYPE_ERROR;
78 }
79
GenerateLocalServiceId()80 int32 AbilitySelfCallback::GenerateLocalServiceId()
81 {
82 svcIdentity_ = reinterpret_cast<SvcIdentity *>(AdapterMalloc(sizeof(SvcIdentity)));
83 if (svcIdentity_ == nullptr) {
84 return CALLBACK_GENERATE_LOCAL_SERVICEID_FAILED;
85 }
86
87 int32_t ret = RegisterIpcCallback(Callback, 0, IPC_WAIT_FOREVER, svcIdentity_, NULL);
88 if (ret != LITEIPC_OK) {
89 AdapterFree(svcIdentity_);
90 svcIdentity_ = nullptr;
91 return CALLBACK_GENERATE_LOCAL_SERVICEID_FAILED;
92 }
93 return ERR_OK;
94 }
95
RegisterAbilitySelfCallback(IAbilityStartCallback & iAbilityStartCallback)96 const SvcIdentity *AbilitySelfCallback::RegisterAbilitySelfCallback(IAbilityStartCallback &iAbilityStartCallback)
97 {
98 if (iAbilityStartCallback == nullptr) {
99 return nullptr;
100 }
101 if (svcIdentity_ == nullptr) {
102 int32 ret = GenerateLocalServiceId();
103 if (ret != ERR_OK) {
104 return nullptr;
105 }
106 }
107 iAbilityStartCallback_ = iAbilityStartCallback;
108 return svcIdentity_;
109 }
110
GetCallback()111 const IAbilityStartCallback AbilitySelfCallback::GetCallback()
112 {
113 return iAbilityStartCallback_;
114 }
115 } // namespace