From 386664c8230fb7d6384fb29241be9ae84c52d931 Mon Sep 17 00:00:00 2001 From: zhouyan Date: Fri, 22 Apr 2022 10:38:48 +0800 Subject: [PATCH] =?UTF-8?q?tokensync=E7=94=B1=E5=B8=B8=E9=A9=BB=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=8C=89=E9=9C=80=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouyan --- interfaces/innerkits/tokensync/BUILD.gn | 2 + .../src/token_sync_load_callback.cpp | 64 +++++++++++++++++++ .../tokensync/src/token_sync_load_callback.h | 37 +++++++++++ .../src/token_sync_manager_client.cpp | 32 +++++++--- .../tokensync/src/token_sync_manager_client.h | 2 + services/tokensyncmanager/sa_profile/3504.xml | 2 +- services/tokensyncmanager/token_sync.cfg | 10 +-- 7 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp create mode 100644 interfaces/innerkits/tokensync/src/token_sync_load_callback.h diff --git a/interfaces/innerkits/tokensync/BUILD.gn b/interfaces/innerkits/tokensync/BUILD.gn index c4634b55f..9767ad4ac 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("libtokensync_sdk") { "//base/security/access_token/interfaces/innerkits/tokensync/include", "//base/security/access_token/interfaces/innerkits/accesstoken/include", "//base/security/access_token/frameworks/accesstoken/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", #"//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk/main/cpp/include/permission", ] @@ -44,6 +45,7 @@ ohos_shared_library("libtokensync_sdk") { #"main/cpp/src/permission/permission_manager_client.cpp", #"main/cpp/src/permission/permission_manager_proxy.cpp", "src/token_sync_kit.cpp", + "src/token_sync_load_callback.cpp", "src/token_sync_manager_client.cpp", "src/token_sync_manager_proxy.cpp", ] diff --git a/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp b/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp new file mode 100644 index 000000000..a9af77e15 --- /dev/null +++ b/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "token_sync_load_callback.h" + +#include "accesstoken_log.h" +#include "i_token_sync_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncLoadCallBack"}; +} + +TokenSyncLoadCallback::TokenSyncLoadCallback() +{ + ACCESSTOKEN_LOG_INFO(LABEL, "enter"); +} + +void TokenSyncLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + if (systemAbilityId != ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE) { + ACCESSTOKEN_LOG_ERROR(LABEL, "start aystemabilityId is not TokenSync!"); + return; + } + + if (remoteObject == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remoteObject is null."); + return; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "OnLoadSystemAbilitySuccess start systemAbilityId: %d success!", + systemAbilityId); + + localSa_ = remoteObject; +} + +void TokenSyncLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + if (systemAbilityId != ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "start aystemabilityId is not TokenSync!"); + return; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "OnLoadSystemAbilityFail systemAbilityId: %d failed.", systemAbilityId); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/tokensync/src/token_sync_load_callback.h b/interfaces/innerkits/tokensync/src/token_sync_load_callback.h new file mode 100644 index 000000000..3c3e26046 --- /dev/null +++ b/interfaces/innerkits/tokensync/src/token_sync_load_callback.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TOKEN_SYNC_LOAD_CALLBACK_H +#define TOKEN_SYNC_LOAD_CALLBACK_H + +#include "system_ability_load_callback_stub.h" +#include "i_token_sync_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class TokenSyncLoadCallback : public SystemAbilityLoadCallbackStub { +public: + explicit TokenSyncLoadCallback(); + + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); + sptr localSa_; +private: +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // TOKEN_SYNC_LOAD_CALLBACK_H diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp index 3cd5a9d56..d4142ff8d 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp @@ -14,6 +14,9 @@ */ #include "token_sync_manager_client.h" +#include "token_sync_load_callback.h" + +#include #include "accesstoken_log.h" #include "hap_token_info_for_sync_parcel.h" @@ -75,19 +78,32 @@ sptr TokenSyncManagerClient::GetProxy() const { auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sam == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "GetSystemAbilityManager is null"); + ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); return nullptr; } - auto tokensyncSa = sam->GetSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); - if (tokensyncSa == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "GetSystemAbility %{public}d is null", - ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); - return nullptr; + + sptr loadCallback = new TokenSyncLoadCallback(); + sptr tsm = loadCallback->localSa_; + while (tsm == nullptr) { + int32_t count = 0; + if (count >= TokenSyncManagerClient::GET_SYSTEM_ABILITY_REPEAT_TIMES) { + break; + } + int32_t result = sam->LoadSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE, loadCallback); + if (result != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", + ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); + return nullptr; + } + + count++; + sleep(TokenSyncManagerClient::GET_SYSTEM_ABILITY_WAIT_TIME_MS); + tsm = loadCallback->localSa_; } - auto proxy = iface_cast(tokensyncSa); + auto proxy = iface_cast(tsm); if (proxy == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "iface_cast get null"); + ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null"); return nullptr; } return proxy; diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h index f416cba31..5be21fee5 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h @@ -28,6 +28,8 @@ namespace Security { namespace AccessToken { class TokenSyncManagerClient final { public: + static const int GET_SYSTEM_ABILITY_REPEAT_TIMES = 10; + static const int GET_SYSTEM_ABILITY_WAIT_TIME_MS = 500; static TokenSyncManagerClient& GetInstance(); virtual ~TokenSyncManagerClient(); diff --git a/services/tokensyncmanager/sa_profile/3504.xml b/services/tokensyncmanager/sa_profile/3504.xml index 4293f1452..b0ef046f1 100644 --- a/services/tokensyncmanager/sa_profile/3504.xml +++ b/services/tokensyncmanager/sa_profile/3504.xml @@ -17,7 +17,7 @@ 3504 libtoken_sync_manager_service.z.so - true + false true 1 diff --git a/services/tokensyncmanager/token_sync.cfg b/services/tokensyncmanager/token_sync.cfg index e8e2f4782..d2e19e30e 100644 --- a/services/tokensyncmanager/token_sync.cfg +++ b/services/tokensyncmanager/token_sync.cfg @@ -1,15 +1,9 @@ { - "jobs" : [{ - "name" : "late-fs", - "cmds" : [ - "start token_sync_service" - ] - } - ], "services" : [{ "name" : "token_sync_service", "path" : ["/system/bin/sa_main", "/system/profile/token_sync_service.xml"], - "importance" : -20, + "ondemand" : true, + "start-mode" : "condition", "uid" : "system", "gid" : ["system"] } -- Gitee