• 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_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 "rpc_errno.h"
24 #include "samgr_lite.h"
25 
26 namespace OHOS {
~AbilitySelfCallback()27 AbilitySelfCallback::~AbilitySelfCallback()
28 {
29     if (svcIdentity_ != nullptr) {
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(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)47 int32_t AbilitySelfCallback::Callback(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
48 {
49     if (data == nullptr) {
50         HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallback io is nullptr");
51         return PARAM_NULL_ERROR;
52     }
53     IAbilityStartCallback iAbilityStartCallback = GetInstance().GetCallback();
54     if (iAbilityStartCallback == nullptr) {
55         return PARAM_NULL_ERROR;
56     }
57 
58     int32_t ret = 0;
59     ReadInt32(data, &ret);
60     auto resultCode = static_cast<uint8_t>(ret);
61     if (code == START_ABILITY_CALLBACK) {
62         return InnerCallback(START_ABILITY_SUCCESS, resultCode, iAbilityStartCallback);
63     }
64     HILOG_ERROR(HILOG_MODULE_APP, "AbilitySelfCallback get error callback type");
65     return GET_CALLBACK_TYPE_ERROR;
66 }
67 
GenerateLocalServiceId()68 int32 AbilitySelfCallback::GenerateLocalServiceId()
69 {
70     svcIdentity_ = reinterpret_cast<SvcIdentity *>(AdapterMalloc(sizeof(SvcIdentity)));
71     if (svcIdentity_ == nullptr) {
72         return CALLBACK_GENERATE_LOCAL_SERVICEID_FAILED;
73     }
74 
75     objectStub_.func = Callback;
76     objectStub_.args = NULL;
77     objectStub_.isRemote = false;
78     svcIdentity_->handle = IPC_INVALID_HANDLE;
79     svcIdentity_->token = SERVICE_TYPE_ANONYMOUS;
80     svcIdentity_->cookie = reinterpret_cast<uintptr_t>(&objectStub_);
81 
82     return ERR_OK;
83 }
84 
RegisterAbilitySelfCallback(IAbilityStartCallback & iAbilityStartCallback)85 const SvcIdentity *AbilitySelfCallback::RegisterAbilitySelfCallback(IAbilityStartCallback &iAbilityStartCallback)
86 {
87     if (iAbilityStartCallback == nullptr) {
88         return nullptr;
89     }
90     if (svcIdentity_ == nullptr) {
91         int32 ret = GenerateLocalServiceId();
92         if (ret != ERR_OK) {
93             return nullptr;
94         }
95     }
96     iAbilityStartCallback_ = iAbilityStartCallback;
97     return svcIdentity_;
98 }
99 
GetCallback()100 const IAbilityStartCallback AbilitySelfCallback::GetCallback()
101 {
102     return iAbilityStartCallback_;
103 }
104 } // namespace