diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 649aa5b80843140889691fdc46b7a6c90f8ab670..f66f0f00ba310347db067141a58f8031d8708c76 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -342,7 +342,7 @@ bool AbilityManagerService::Init() HILOG_ERROR("RegisterBundleEventCallback failed!"); } } else { - HILOG_ERROR("Get BundleManager or abilieyBundleEventCallback failed!"); + HILOG_ERROR("Get BundleManager or abilityBundleEventCallback failed!"); } }; handler_->PostTask(registerBundleEventCallbackTask, "RegisterBundleEventCallback"); @@ -4850,7 +4850,7 @@ void AbilityManagerService::UpdateMissionSnapShot(const sptr& tok } auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); if (!isSaCall) { - if(!CheckCallingTokenId(BUNDLE_NAME_LAUNCHER, GetUserId())) { + if (!CheckCallingTokenId(BUNDLE_NAME_LAUNCHER, GetUserId())) { HILOG_ERROR("Not launcher called, not allowed."); return; } diff --git a/services/uripermmgr/BUILD.gn b/services/uripermmgr/BUILD.gn index 195d46ac35586544d1065f2655baf6c118bf3f14..c0cfe6d456c4f18b699efe989b4d1f3284c7f587 100644 --- a/services/uripermmgr/BUILD.gn +++ b/services/uripermmgr/BUILD.gn @@ -25,6 +25,7 @@ config("upms_config") { } libupms_sources = [ + "src/uri_bundle_event_callback.cpp", "src/uri_permission_manager_service.cpp", "src/uri_permission_manager_stub_impl.cpp", ] @@ -42,6 +43,7 @@ ohos_shared_library("libupms") { deps = [ "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr", + "${ability_runtime_services_path}/common:event_report", "${ability_runtime_services_path}/common:perm_verification", ] @@ -53,6 +55,10 @@ ohos_shared_library("libupms") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "common_event_service:cesfwk_core", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", @@ -87,6 +93,8 @@ ohos_static_library("libupms_static") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "common_event_service:cesfwk_core", + "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/services/uripermmgr/include/uri_bundle_event_callback.h b/services/uripermmgr/include/uri_bundle_event_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..87d5bc3ca48e38c77868dcadc69689b494f7d6f6 --- /dev/null +++ b/services/uripermmgr/include/uri_bundle_event_callback.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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 "bundle_event_callback_host.h" +#include "common_event_support.h" +#include "hilog_wrapper.h" +#include "uri_permission_manager_stub_impl.h" + +namespace OHOS { +namespace AAFwk { +/** + * @brief This class is a callback class that will be registered to BundleManager. + * This class will be called by BundleManager when install, uninstall, updates of haps happens, + * and executes corresponding functionalities of Uri Permission manager. + */ +class UriBundleEventCallback : public AppExecFwk::BundleEventCallbackHost { +public: + UriBundleEventCallback() = default; + explicit UriBundleEventCallback(sptr impl); + ~UriBundleEventCallback() = default; + /** + * @brief The main callback function that will be called by BundleManager + * when install, uninstall, updates of haps happens to notify UriPermissionManger. + * @param eventData the data passed from BundleManager that includes bundleName, change type of haps + * etc. More can be found from BundleCommonEventMgr::NotifyBundleStatus() + */ + void OnReceiveEvent(const EventFwk::CommonEventData eventData) override; +private: + sptr upms_; +}; +} // namespace OHOS +} // namespace AAFwk \ No newline at end of file diff --git a/services/uripermmgr/include/uri_permission_manager_stub_impl.h b/services/uripermmgr/include/uri_permission_manager_stub_impl.h index f03ce023cf748b93be6d1f682fe73bc9409cee35..25f369594fec1d412d81171707e515e7ffcf2cf7 100644 --- a/services/uripermmgr/include/uri_permission_manager_stub_impl.h +++ b/services/uripermmgr/include/uri_permission_manager_stub_impl.h @@ -46,11 +46,12 @@ public: const std::string targetBundleName, int autoremove) override; void RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId) override; + void RevokeAllUriPermissions(int tokenId); int RevokeUriPermissionManually(const Uri &uri, const std::string bundleName) override; + sptr ConnectBundleManager(); private: sptr ConnectAppMgr(); - sptr ConnectBundleManager(); sptr ConnectStorageManager(); int GetCurrentAccountId(); void ClearAppMgrProxy(); diff --git a/services/uripermmgr/src/uri_bundle_event_callback.cpp b/services/uripermmgr/src/uri_bundle_event_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a47abf5cb919f100e17116d7ebeb31125b2c03b4 --- /dev/null +++ b/services/uripermmgr/src/uri_bundle_event_callback.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 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 "uri_bundle_event_callback.h" +namespace OHOS { +namespace AAFwk { +const std::string KEY_TOKEN = "accessTokenId"; +UriBundleEventCallback::UriBundleEventCallback(sptr impl) +{ + upms_ = impl; +} +void UriBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData) +{ + const Want& want = eventData.GetWant(); + // action contains the change type of haps. + std::string action = want.GetAction(); + std::string bundleName = want.GetElement().GetBundleName(); + int token = want.GetIntParam(KEY_TOKEN, 0); + // verify data + if (action.empty() || bundleName.empty()) { + HILOG_ERROR("OnReceiveEvent failed, empty action/bundleName"); + return; + } + HILOG_DEBUG("OnReceiveEvent, action:%{public}s.", action.c_str()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { + HILOG_DEBUG("revoke URI permission when uninstall."); + if (!upms_) { + HILOG_ERROR("Uri permission manager is nullptr"); + return; + } + upms_->RevokeAllUriPermissions(token); + } +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/uripermmgr/src/uri_permission_manager_service.cpp b/services/uripermmgr/src/uri_permission_manager_service.cpp index d0bee404edea7825859f1e34b7cd92c948c62c1e..18242e1298bc52dca7763139526f524aaa0bf328 100644 --- a/services/uripermmgr/src/uri_permission_manager_service.cpp +++ b/services/uripermmgr/src/uri_permission_manager_service.cpp @@ -20,6 +20,7 @@ #include "ipc_skeleton.h" #include "iservice_registry.h" #include "system_ability_definition.h" +#include "uri_bundle_event_callback.h" namespace OHOS { namespace AAFwk { @@ -75,6 +76,19 @@ bool UriPermissionManagerService::Init() if (impl_ == nullptr) { impl_ = new UriPermissionManagerStubImpl(); } + // Register UriBundleEventCallback to receive hap updates + HILOG_INFO("Register UriBundleEventCallback to receive hap updates."); + auto bms = impl_->ConnectBundleManager(); + sptr uriBundleEventCallback = + new (std::nothrow) UriBundleEventCallback(impl_); + if (bms && uriBundleEventCallback) { + bool re = bms->RegisterBundleEventCallback(uriBundleEventCallback); + if (!re) { + HILOG_ERROR("RegisterBundleEventCallback failed!"); + } + } else { + HILOG_ERROR("Get BundleManager or uriBundleEventCallback failed!"); + } ready_ = true; return true; } diff --git a/services/uripermmgr/src/uri_permission_manager_stub_impl.cpp b/services/uripermmgr/src/uri_permission_manager_stub_impl.cpp index bddcd8149d25a7c6803bf0a13ead3e35fc8f7433..9bfdc9c1c1a5b6f7ae34553901fa7a6f05e02834 100644 --- a/services/uripermmgr/src/uri_permission_manager_stub_impl.cpp +++ b/services/uripermmgr/src/uri_permission_manager_stub_impl.cpp @@ -174,6 +174,44 @@ void UriPermissionManagerStubImpl::RevokeUriPermission(const TokenId tokenId) } } +void UriPermissionManagerStubImpl::RevokeAllUriPermissions(int tokenId) +{ + HILOG_DEBUG("Start to remove all uri permission for uninstalled app."); + std::map> uriLists; + { + std::lock_guard guard(mutex_); + for (auto iter = uriMap_.begin(); iter != uriMap_.end();) { + auto& list = iter->second; + for (auto it = list.begin(); it != list.end();) { + if (it->targetTokenId == tokenId || it->fromTokenId == tokenId) { + HILOG_INFO("Erase an info form list."); + uriLists[it->targetTokenId].emplace_back(iter->first); + list.erase(it++); + } else { + it++; + } + } + if (list.size() == 0) { + uriMap_.erase(iter++); + } else { + iter++; + } + } + } + + auto storageMgrProxy = ConnectStorageManager(); + if (storageMgrProxy == nullptr) { + HILOG_ERROR("ConnectStorageManager failed"); + return; + } + + if (!uriLists.empty()) { + for (auto iter = uriLists.begin(); iter != uriLists.end(); iter++) { + storageMgrProxy->DeleteShareFile(iter->first, iter->second); + } + } +} + int UriPermissionManagerStubImpl::RevokeUriPermissionManually(const Uri &uri, const std::string bundleName) { HILOG_DEBUG("Start to remove uri permission manually.");