diff --git a/common/native/include/managed_event.h b/common/native/include/managed_event.h index b54e21f7eaf209ce303db8c914e9de9a1d173f55..8ce97dd4990335cb938afbc1b50b20bf24625208 100644 --- a/common/native/include/managed_event.h +++ b/common/native/include/managed_event.h @@ -22,7 +22,9 @@ namespace OHOS { namespace EDM { enum class ManagedEvent : uint32_t { BUNDLE_ADDED = 0, - BUNDLE_REMOVED, + BUNDLE_REMOVED = 1, + APP_START = 2, + APP_STOP = 3, }; } // namespace EDM } // namespace OHOS diff --git a/framework/extension/include/enterprise_admin_stub.h b/framework/extension/include/enterprise_admin_stub.h index 0e10aad82cd27d34411032c0031992ac8849db82..2a3b70eed74b73def0dfba8686f900bb9c01313d 100644 --- a/framework/extension/include/enterprise_admin_stub.h +++ b/framework/extension/include/enterprise_admin_stub.h @@ -50,6 +50,10 @@ private: void OnBundleRemovedInner(MessageParcel& data, MessageParcel& reply); + void OnAppStartInner(MessageParcel& data, MessageParcel& reply); + + void OnAppStopInner(MessageParcel& data, MessageParcel& reply); + using EnterpriseAdminFunc = void (EnterpriseAdminStub::*)(MessageParcel& data, MessageParcel& reply); std::map memberFuncMap_; diff --git a/framework/extension/include/enterprise_admin_stub_impl.h b/framework/extension/include/enterprise_admin_stub_impl.h index 92257f16bc33fa604254bccf6f8cdaa3e6567093..0051209ede32148e7bb4c3c590bae52939afb879 100644 --- a/framework/extension/include/enterprise_admin_stub_impl.h +++ b/framework/extension/include/enterprise_admin_stub_impl.h @@ -43,6 +43,10 @@ public: void OnBundleAdded(const std::string &bundleName) override; void OnBundleRemoved(const std::string &bundleName) override; + + void OnAppStart(const std::string &bundleName) override; + + void OnAppStop(const std::string &bundleName) override; private: std::weak_ptr extension_; }; diff --git a/framework/extension/include/ienterprise_admin.h b/framework/extension/include/ienterprise_admin.h index 6411aecb460e21fd3a43e6499f56c2e250ca5c41..2603694104629b4a075b2fa9595ffe39e5f663a6 100644 --- a/framework/extension/include/ienterprise_admin.h +++ b/framework/extension/include/ienterprise_admin.h @@ -41,11 +41,25 @@ public: */ virtual void OnBundleRemoved(const std::string &bundleName) = 0; + /** + * Called when an application package start on the device. + * @param bundleName Indicates the name of the bundle whose state has been started. + */ + virtual void OnAppStart(const std::string &bundleName) = 0; + + /** + * Called when an application package stop on the device. + * @param bundleName Indicates the name of the bundle whose state has been stopped. + */ + virtual void OnAppStop(const std::string &bundleName) = 0; + enum { COMMAND_ON_ADMIN_ENABLED = 1, COMMAND_ON_ADMIN_DISABLED = 2, COMMAND_ON_BUNDLE_ADDED = 3, - COMMAND_ON_BUNDLE_REMOVED = 4 + COMMAND_ON_BUNDLE_REMOVED = 4, + COMMAND_ON_APP_START = 5, + COMMAND_ON_APP_STOP = 6 }; }; } // namespace EDM diff --git a/framework/extension/include/js_enterprise_admin_extension.h b/framework/extension/include/js_enterprise_admin_extension.h index 965fb5aa9ef4f3b878bc6f3c279a7f5641e0d4a7..97fccc15fb964f65cec43ce5ddbd83dbec0af756 100644 --- a/framework/extension/include/js_enterprise_admin_extension.h +++ b/framework/extension/include/js_enterprise_admin_extension.h @@ -60,6 +60,10 @@ public: void OnBundleAdded(const std::string &bundleName); void OnBundleRemoved(const std::string &bundleName); + + void OnAppStart(const std::string &bundleName); + + void OnAppStop(const std::string &bundleName); private: NativeValue* CallObjectMethod(const char* name, NativeValue** argv, size_t argc); diff --git a/framework/extension/src/enterprise_admin_stub.cpp b/framework/extension/src/enterprise_admin_stub.cpp index 57dc650a556046e0fb599ea87c74c5c1741f3ea1..6f0b2dd78c6087e0bdc096984a88d1158330c457 100644 --- a/framework/extension/src/enterprise_admin_stub.cpp +++ b/framework/extension/src/enterprise_admin_stub.cpp @@ -35,6 +35,8 @@ void EnterpriseAdminStub::AddCallFuncMap() memberFuncMap_[COMMAND_ON_ADMIN_DISABLED] = &EnterpriseAdminStub::OnAdminDisabledInner; memberFuncMap_[COMMAND_ON_BUNDLE_ADDED] = &EnterpriseAdminStub::OnBundleAddedInner; memberFuncMap_[COMMAND_ON_BUNDLE_REMOVED] = &EnterpriseAdminStub::OnBundleRemovedInner; + memberFuncMap_[COMMAND_ON_APP_START] = &EnterpriseAdminStub::OnAppStartInner; + memberFuncMap_[COMMAND_ON_APP_STOP] = &EnterpriseAdminStub::OnAppStopInner; } void EnterpriseAdminStub::OnAdminEnabledInner(MessageParcel& data, MessageParcel& reply) @@ -63,6 +65,20 @@ void EnterpriseAdminStub::OnBundleRemovedInner(MessageParcel& data, MessageParce OnBundleRemoved(bundleName); } +void EnterpriseAdminStub::OnAppStartInner(MessageParcel& data, MessageParcel& reply) +{ + EDMLOGI("EnterpriseAdminStub::OnAppStart"); + std::string bundleName = data.ReadString(); + OnAppStart(bundleName); +} + +void EnterpriseAdminStub::OnAppStopInner(MessageParcel& data, MessageParcel& reply) +{ + EDMLOGI("EnterpriseAdminStub::OnAppStop"); + std::string bundleName = data.ReadString(); + OnAppStop(bundleName); +} + int32_t EnterpriseAdminStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption &option) { diff --git a/framework/extension/src/enterprise_admin_stub_impl.cpp b/framework/extension/src/enterprise_admin_stub_impl.cpp index 57219dbe588da23c703348fcec20e7673332408e..8d4f51c8ce4bd9f0c740f36c54c05c27b45a0261 100644 --- a/framework/extension/src/enterprise_admin_stub_impl.cpp +++ b/framework/extension/src/enterprise_admin_stub_impl.cpp @@ -57,5 +57,25 @@ void EnterpriseAdminStubImpl::OnBundleRemoved(const std::string &bundleName) EDMLOGD("EnterpriseAdminStubImpl %{public}s end successfully.", __func__); } } + +void EnterpriseAdminStubImpl::OnAppStart(const std::string &bundleName) +{ + EDMLOGI("EnterpriseAdminStubImpl %{public}s begin.", __func__); + auto extension = extension_.lock(); + if (extension != nullptr) { + extension->OnAppStart(bundleName); + EDMLOGD("EnterpriseAdminStubImpl %{public}s end successfully.", __func__); + } +} + +void EnterpriseAdminStubImpl::OnAppStop(const std::string &bundleName) +{ + EDMLOGI("EnterpriseAdminStubImpl %{public}s begin.", __func__); + auto extension = extension_.lock(); + if (extension != nullptr) { + extension->OnAppStop(bundleName); + EDMLOGD("EnterpriseAdminStubImpl %{public}s end successfully.", __func__); + } +} } // namespace EDM } // namespace OHOS \ No newline at end of file diff --git a/framework/extension/src/js_enterprise_admin_extension.cpp b/framework/extension/src/js_enterprise_admin_extension.cpp index 2f57825e95f9d7d87c1ac68abaa6419e7b4dbb07..0e3b70d64ef14e395a7e5e28163ea0b28d74811e 100644 --- a/framework/extension/src/js_enterprise_admin_extension.cpp +++ b/framework/extension/src/js_enterprise_admin_extension.cpp @@ -175,6 +175,28 @@ void JsEnterpriseAdminExtension::OnBundleRemoved(const std::string &bundleName) handler_->PostTask(task); } +void JsEnterpriseAdminExtension::OnAppStart(const std::string &bundleName) +{ + HILOG_INFO("JsEnterpriseAdminExtension::OnAppStart"); + auto task = [bundleName, this]() { + auto& engine = jsRuntime_.GetNativeEngine(); + NativeValue* argv[] = { AbilityRuntime::CreateJsValue(engine, bundleName) }; + CallObjectMethod("onAppStart", argv, JS_NAPI_ARGC_ONE); + }; + handler_->PostTask(task); +} + +void JsEnterpriseAdminExtension::OnAppStop(const std::string &bundleName) +{ + HILOG_INFO("JsEnterpriseAdminExtension::OnAppStop"); + auto task = [bundleName, this]() { + auto& engine = jsRuntime_.GetNativeEngine(); + NativeValue* argv[] = { AbilityRuntime::CreateJsValue(engine, bundleName) }; + CallObjectMethod("onAppStop", argv, JS_NAPI_ARGC_ONE); + }; + handler_->PostTask(task); +} + NativeValue* JsEnterpriseAdminExtension::CallObjectMethod(const char* name, NativeValue** argv, size_t argc) { HILOG_INFO("JsEnterpriseAdminExtension::CallObjectMethod(%{public}s), begin", name); diff --git a/interfaces/kits/admin_manager/src/admin_manager_addon.cpp b/interfaces/kits/admin_manager/src/admin_manager_addon.cpp index b6d82009908a0ac416ed400749acf55067b51faf..1718c8da250b01907369064b57753cca64301739 100644 --- a/interfaces/kits/admin_manager/src/admin_manager_addon.cpp +++ b/interfaces/kits/admin_manager/src/admin_manager_addon.cpp @@ -667,6 +667,14 @@ void AdminManager::CreateManagedEventObject(napi_env env, napi_value value) NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast(ManagedEvent::BUNDLE_REMOVED), &nBundleRemoved)); NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "MANAGED_EVENT_BUNDLE_REMOVED", nBundleRemoved)); + napi_value nAppStart; + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, + static_cast(ManagedEvent::APP_START), &nAppStart)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "MANAGED_EVENT_APP_START", nAppStart)); + napi_value nAppStop; + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, + static_cast(ManagedEvent::APP_STOP), &nAppStop)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "MANAGED_EVENT_APP_STOP", nAppStop)); } napi_value AdminManager::Init(napi_env env, napi_value exports) diff --git a/services/edm/BUILD.gn b/services/edm/BUILD.gn index 84d3e5bdc77a209b64236b6c8ff879cbe766e8be..bcf6d9a63866aa65213fb8cc6b90b6ceb220b49b 100644 --- a/services/edm/BUILD.gn +++ b/services/edm/BUILD.gn @@ -31,11 +31,13 @@ ohos_shared_library("edmservice") { "$EDM_SRC_PATH/ability_manager_death_recipient.cpp", "$EDM_SRC_PATH/admin.cpp", "$EDM_SRC_PATH/admin_manager.cpp", + "$EDM_SRC_PATH/app_state_observer.cpp", "$EDM_SRC_PATH/connection/enterprise_admin_connection.cpp", "$EDM_SRC_PATH/connection/enterprise_admin_proxy.cpp", "$EDM_SRC_PATH/connection/enterprise_bundle_connection.cpp", "$EDM_SRC_PATH/connection/enterprise_conn_manager.cpp", "$EDM_SRC_PATH/edm_permission.cpp", + "$EDM_SRC_PATH/enterprise_device_event_subscriber.cpp", "$EDM_SRC_PATH/enterprise_device_mgr_ability.cpp", "$EDM_SRC_PATH/enterprise_device_mgr_stub.cpp", "$EDM_SRC_PATH/iplugin.cpp", @@ -68,6 +70,7 @@ ohos_shared_library("edmservice") { external_deps = [ "ability_base:want", "ability_runtime:ability_manager", + "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", diff --git a/services/edm/include/app_state_observer.h b/services/edm/include/app_state_observer.h new file mode 100644 index 0000000000000000000000000000000000000000..a2693e8ebeebfc53f94557424971abaf465924ee --- /dev/null +++ b/services/edm/include/app_state_observer.h @@ -0,0 +1,39 @@ +/* + * 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 SERVICES_EDM_INCLUDE_EDM_APP_STATE_OBSERVER_H +#define SERVICES_EDM_INCLUDE_EDM_APP_STATE_OBSERVER_H + +#include "application_state_observer_stub.h" +#include "app_mgr_interface.h" +#include "edm_log.h" +#include "enterprise_device_mgr_ability.h" + +namespace OHOS { +namespace EDM { +class EnterpriseDeviceMgrAbility; +class ApplicationStateObserver : public AppExecFwk::ApplicationStateObserverStub { +public: + ApplicationStateObserver(EnterpriseDeviceMgrAbility &listener) : listener_(listener) {} + ~ApplicationStateObserver() {} + + void OnProcessCreated(const AppExecFwk::ProcessData &processData) override; + void OnProcessDied(const AppExecFwk::ProcessData &processData) override; +private: + EnterpriseDeviceMgrAbility &listener_; +}; +} // namespace EDM +} // namespace OHOS +#endif // SERVICES_EDM_INCLUDE_EDM_APP_STATE_OBSERVER_H \ No newline at end of file diff --git a/services/edm/include/connection/enterprise_admin_proxy.h b/services/edm/include/connection/enterprise_admin_proxy.h index 0a4dfa16a365b71fe8f2dd762944d0d07e12a518..e47dbf45ffa7f474379d366e34681a45aeec6836 100644 --- a/services/edm/include/connection/enterprise_admin_proxy.h +++ b/services/edm/include/connection/enterprise_admin_proxy.h @@ -36,6 +36,10 @@ public: void OnBundleAdded(const std::string &bundleName) override; void OnBundleRemoved(const std::string &bundleName) override; + + void OnAppStart(const std::string &bundleName) override; + + void OnAppStop(const std::string &bundleName) override; private: void SendRequest(uint32_t code, MessageParcel &data); }; diff --git a/services/edm/include/enterprise_device_event_subscriber.h b/services/edm/include/enterprise_device_event_subscriber.h new file mode 100644 index 0000000000000000000000000000000000000000..88f5a320eef0bce7189d078a4be7805bd5304d70 --- /dev/null +++ b/services/edm/include/enterprise_device_event_subscriber.h @@ -0,0 +1,38 @@ +/* + * 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 SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_EVENT_SUBSCRIBER_H +#define SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_EVENT_SUBSCRIBER_H + +#include "common_event_subscriber.h" +#include "enterprise_device_mgr_ability.h" + +namespace OHOS { +namespace EDM { +class EnterpriseDeviceMgrAbility; +class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber { +public: + EnterpriseDeviceEventSubscriber( + const EventFwk::CommonEventSubscribeInfo &subscribeInfo, + EnterpriseDeviceMgrAbility &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {} + ~EnterpriseDeviceEventSubscriber() override = default; + + void OnReceiveEvent(const EventFwk::CommonEventData &data) override; +private: + EnterpriseDeviceMgrAbility &listener_; +}; +} // namespace EDM +} // namespace OHOS +#endif // SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_EVENT_SUBSCRIBER_H \ No newline at end of file diff --git a/services/edm/include/enterprise_device_mgr_ability.h b/services/edm/include/enterprise_device_mgr_ability.h index 57d50caf9c51055ba59823833ea0145d19aa914f..b18ad50b0cc6e16b108cda92d09f9b3b841138cd 100644 --- a/services/edm/include/enterprise_device_mgr_ability.h +++ b/services/edm/include/enterprise_device_mgr_ability.h @@ -19,7 +19,8 @@ #include #include #include "admin_manager.h" -#include "common_event_subscriber.h" +#include "app_state_observer.h" +#include "enterprise_device_event_subscriber.h" #include "enterprise_device_mgr_stub.h" #include "hilog/log.h" #include "plugin_manager.h" @@ -34,6 +35,7 @@ class EnterpriseDeviceMgrAbility : public SystemAbility, public EnterpriseDevice public: using CommonEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const EventFwk::CommonEventData &data); + using AppEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const std::string); EnterpriseDeviceMgrAbility(); DISALLOW_COPY_AND_MOVE(EnterpriseDeviceMgrAbility); ~EnterpriseDeviceMgrAbility(); @@ -53,6 +55,7 @@ public: bool IsSuperAdmin(std::string &bundleName) override; bool IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId) override; std::unordered_map commonEventFuncMap_; + std::unordered_map appEventFuncMap_; protected: void OnStart() override; @@ -64,6 +67,9 @@ protected: private: bool IsHdc(); void AddCommonEventFuncMap(); + void AddAppEventFuncMap(); + bool SubscribeAppState(); + void UnsubscribeAppState(); ErrCode CheckCallingUid(std::string &bundleName); ErrCode RemoveAdminItem(std::string adminName, std::string policyName, std::string policyValue); ErrCode RemoveAdmin(const std::string &adminName, int32_t userId); @@ -75,13 +81,17 @@ private: ErrCode HandleManagedEvent(const AppExecFwk::ElementName &admin, const std::vector &events, bool subscribe); bool VerifyCallingPermission(const std::string &permissionName); - sptr GetBundleMgr(); + sptr GetBundleMgr(); + sptr GetAppMgr(); std::shared_ptr CreateEnterpriseDeviceEventSubscriber( EnterpriseDeviceMgrAbility &listener); void OnCommonEventUserRemoved(const EventFwk::CommonEventData &data); void OnCommonEventPackageAdded(const EventFwk::CommonEventData &data); void OnCommonEventPackageRemoved(const EventFwk::CommonEventData &data); void OnCommonEventPackageAddedOrRemoved(const EventFwk::CommonEventData &data, ManagedEvent event); + void OnAppStart(const std::string bundleName); + void OnAppStop(const std::string bundleName); + void OnAppStartOrStop(const std::string bundleName, ManagedEvent event); bool CheckManagedEvent(uint32_t event); static std::mutex mutexLock_; static sptr instance_; @@ -90,16 +100,7 @@ private: std::shared_ptr pluginMgr_; bool registerToService_ = false; std::shared_ptr commonEventSubscriber = nullptr; -}; -class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber { -public: - EnterpriseDeviceEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, - EnterpriseDeviceMgrAbility &listener); - ~EnterpriseDeviceEventSubscriber() override = default; - - void OnReceiveEvent(const EventFwk::CommonEventData &data) override; -private: - EnterpriseDeviceMgrAbility &listener_; + sptr appStateObserver_; }; } // namespace EDM } // namespace OHOS diff --git a/services/edm/src/app_state_observer.cpp b/services/edm/src/app_state_observer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9986083ef4606fe561f76b608b3e3000fcf5c63a --- /dev/null +++ b/services/edm/src/app_state_observer.cpp @@ -0,0 +1,53 @@ +/* + * 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 "app_state_observer.h" + +namespace OHOS { +namespace EDM { +const std::string ON_APP_START_EVENT = "OnApplicationStart"; +const std::string ON_APP_STOP_EVENT = "OnApplicationStop"; + +void ApplicationStateObserver::OnProcessCreated(const AppExecFwk::ProcessData &processData) +{ + EDMLOGI("OnProcessCreated : %{public}s", processData.bundleName.c_str()); + std::string bundleName = processData.bundleName; + auto func = listener_.appEventFuncMap_.find(ON_APP_START_EVENT); + if (func != listener_.appEventFuncMap_.end()) { + auto commonEventFunc = func->second; + if (commonEventFunc != nullptr) { + return (listener_.*commonEventFunc)(bundleName); + } + } else { + EDMLOGW("OnProcessCreated event is invalid"); + } +} + +void ApplicationStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData) +{ + EDMLOGI("OnProcessDied : %{public}s", processData.bundleName.c_str()); + std::string bundleName = processData.bundleName; + auto func = listener_.appEventFuncMap_.find(ON_APP_STOP_EVENT); + if (func != listener_.appEventFuncMap_.end()) { + auto commonEventFunc = func->second; + if (commonEventFunc != nullptr) { + return (listener_.*commonEventFunc)(bundleName); + } + } else { + EDMLOGW("OnProcessDied event is invalid"); + } +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/connection/enterprise_admin_proxy.cpp b/services/edm/src/connection/enterprise_admin_proxy.cpp index bb15750faa5975c394db6c9039ed2c5e7814bf71..554484469a97a1df33472860c9df55769bc80da3 100644 --- a/services/edm/src/connection/enterprise_admin_proxy.cpp +++ b/services/edm/src/connection/enterprise_admin_proxy.cpp @@ -64,6 +64,30 @@ void EnterpriseAdminProxy::OnBundleRemoved(const std::string &bundleName) SendRequest(COMMAND_ON_BUNDLE_REMOVED, data); } +void EnterpriseAdminProxy::OnAppStart(const std::string &bundleName) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + EDMLOGE("EnterpriseAdminProxy::%{public}s write descriptor failed!", __func__); + return; + } + data.WriteString(bundleName); + EDMLOGI("EnterpriseAdminProxy proxy OnAppStart"); + SendRequest(COMMAND_ON_APP_START, data); +} + +void EnterpriseAdminProxy::OnAppStop(const std::string &bundleName) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + EDMLOGE("EnterpriseAdminProxy::%{public}s write descriptor failed!", __func__); + return; + } + data.WriteString(bundleName); + EDMLOGI("EnterpriseAdminProxy proxy OnAppStop"); + SendRequest(COMMAND_ON_APP_STOP, data); +} + void EnterpriseAdminProxy::SendRequest(uint32_t code, MessageParcel &data) { MessageParcel reply; diff --git a/services/edm/src/connection/enterprise_bundle_connection.cpp b/services/edm/src/connection/enterprise_bundle_connection.cpp index 0f0cbff67c673dc454d57cf8e0430804f1feb7f2..e968868f1e2ec7d35a2decae1228e5a1221cd526 100644 --- a/services/edm/src/connection/enterprise_bundle_connection.cpp +++ b/services/edm/src/connection/enterprise_bundle_connection.cpp @@ -38,6 +38,12 @@ void EnterpriseBundleConnection::OnAbilityConnectDone( case static_cast(ManagedEvent::BUNDLE_REMOVED): proxy_->OnBundleRemoved(bundleName_); break; + case static_cast(ManagedEvent::APP_START): + proxy_->OnAppStart(bundleName_); + break; + case static_cast(ManagedEvent::APP_STOP): + proxy_->OnAppStop(bundleName_); + break; default: return; } diff --git a/services/edm/src/enterprise_device_event_subscriber.cpp b/services/edm/src/enterprise_device_event_subscriber.cpp new file mode 100644 index 0000000000000000000000000000000000000000..077364f42ab8a15348f18235e96dc84a36f793a1 --- /dev/null +++ b/services/edm/src/enterprise_device_event_subscriber.cpp @@ -0,0 +1,35 @@ +/* + * 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 "enterprise_device_event_subscriber.h" + +namespace OHOS { +namespace EDM { +void EnterpriseDeviceEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) +{ + const std::string action = data.GetWant().GetAction(); + EDMLOGI("EDM OnReceiveEvent get action: %{public}s", action.c_str()); + auto func = listener_.commonEventFuncMap_.find(action); + if (func != listener_.commonEventFuncMap_.end()) { + auto commonEventFunc = func->second; + if (commonEventFunc != nullptr) { + return (listener_.*commonEventFunc)(data); + } + } else { + EDMLOGW("OnReceiveEvent action is invalid"); + } +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index b968e7c9bf5f48538414d49544889f868d2fc46d..f94f6e3500ec3da1e5ce6dac17e5482f17a50095 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -46,6 +46,11 @@ std::mutex EnterpriseDeviceMgrAbility::mutexLock_; sptr EnterpriseDeviceMgrAbility::instance_; constexpr int32_t DEFAULT_USER_ID = 100; +const std::string PERMISSION_MANAGE_ENTERPRISE_DEVICE_ADMIN = "ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN"; +const std::string PERMISSION_SET_ENTERPRISE_INFO = "ohos.permission.SET_ENTERPRISE_INFO"; +const std::string PERMISSION_ENTERPRISE_SUBSCRIBE_MANAGED_EVENT = "ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT"; +const std::string ON_APP_START_EVENT = "OnApplicationStart"; +const std::string ON_APP_STOP_EVENT = "OnApplicationStop"; void EnterpriseDeviceMgrAbility::AddCommonEventFuncMap() { @@ -57,26 +62,10 @@ void EnterpriseDeviceMgrAbility::AddCommonEventFuncMap() &EnterpriseDeviceMgrAbility::OnCommonEventPackageRemoved; } -const std::string PERMISSION_MANAGE_ENTERPRISE_DEVICE_ADMIN = "ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN"; -const std::string PERMISSION_SET_ENTERPRISE_INFO = "ohos.permission.SET_ENTERPRISE_INFO"; -const std::string PERMISSION_ENTERPRISE_SUBSCRIBE_MANAGED_EVENT = "ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT"; -EnterpriseDeviceEventSubscriber::EnterpriseDeviceEventSubscriber( - const EventFwk::CommonEventSubscribeInfo &subscribeInfo, - EnterpriseDeviceMgrAbility &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {} - -void EnterpriseDeviceEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) -{ - const std::string action = data.GetWant().GetAction(); - EDMLOGI("EDM OnReceiveEvent get action: %{public}s", action.c_str()); - auto func = listener_.commonEventFuncMap_.find(action); - if (func != listener_.commonEventFuncMap_.end()) { - auto commonEventFunc = func->second; - if (commonEventFunc != nullptr) { - return (listener_.*commonEventFunc)(data); - } - } else { - EDMLOGW("OnReceiveEvent action is invalid"); - } +void EnterpriseDeviceMgrAbility::AddAppEventFuncMap() +{ + appEventFuncMap_[ON_APP_START_EVENT] = &EnterpriseDeviceMgrAbility::OnAppStart; + appEventFuncMap_[ON_APP_STOP_EVENT] = &EnterpriseDeviceMgrAbility::OnAppStop; } std::shared_ptr EnterpriseDeviceMgrAbility::CreateEnterpriseDeviceEventSubscriber( @@ -111,6 +100,38 @@ void EnterpriseDeviceMgrAbility::OnCommonEventUserRemoved(const EventFwk::Common } } +void EnterpriseDeviceMgrAbility::OnAppStart(const std::string bundleName) +{ + EDMLOGI("OnApplicationStart"); + OnAppStartOrStop(bundleName, ManagedEvent::APP_START); +} + +void EnterpriseDeviceMgrAbility::OnAppStop(const std::string bundleName) +{ + EDMLOGI("OnApplicationStop"); + OnAppStartOrStop(bundleName, ManagedEvent::APP_STOP); +} + +void EnterpriseDeviceMgrAbility::OnAppStartOrStop(const std::string bundleName, ManagedEvent event) +{ + std::unordered_map>> subAdmins; + adminMgr_->GetAdminBySubscribeEvent(event, subAdmins); + if (subAdmins.empty()) { + EDMLOGW("Get subscriber by common event failed."); + return; + } + AAFwk::Want want; + for (const auto &subAdmin : subAdmins) { + for (auto &it : subAdmin.second) { + want.SetElementName(it->adminInfo_.packageName_, it->adminInfo_.className_); + std::shared_ptr manager = DelayedSingleton::GetInstance(); + sptr connection = manager->CreateBundleConnection(want, + static_cast(event), subAdmin.first, bundleName); + manager->ConnectAbility(connection); + } + } +} + void EnterpriseDeviceMgrAbility::OnCommonEventPackageAdded(const EventFwk::CommonEventData &data) { EDMLOGI("OnCommonEventPackageAdded"); @@ -288,6 +309,12 @@ ErrCode EnterpriseDeviceMgrAbility::GetAllPermissionsByAdmin(const std::string & return ERR_OK; } +sptr EnterpriseDeviceMgrAbility::GetAppMgr() +{ + auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::APP_MGR_SERVICE_ID); + return iface_cast(remoteObject); +} + sptr EnterpriseDeviceMgrAbility::GetBundleMgr() { auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); @@ -295,6 +322,47 @@ sptr EnterpriseDeviceMgrAbility::GetBundleMgr() return proxy; } +bool EnterpriseDeviceMgrAbility::SubscribeAppState() +{ + if (appStateObserver_) { + EDMLOGD("appStateObserver_ is not null"); + return true; + } + sptr appMgr = GetAppMgr(); + if (!appMgr) { + EDMLOGE("appMgr is null"); + return false; + } + appStateObserver_ = new (std::nothrow) ApplicationStateObserver(*this); + if (!appStateObserver_) { + EDMLOGE("appStateObserver_ is null"); + return false; + } + int32_t err = appMgr->RegisterApplicationStateObserver(appStateObserver_); + if (err != 0) { + EDMLOGE("RegisterApplicationStateObserver fail!"); + appStateObserver_ = nullptr; + return false; + } + return true; +} + +void EnterpriseDeviceMgrAbility::UnsubscribeAppState() +{ + if (!appStateObserver_) { + EDMLOGD("appStateObserver_ is null"); + return; + } + sptr appMgr = GetAppMgr(); + if (appMgr) { + int32_t err = appMgr->UnregisterApplicationStateObserver(appStateObserver_); + if (err != ERR_OK) { + EDMLOGE("UnregisterApplicationStateObserver fail!"); + } + } + appStateObserver_.clear(); +} + bool EnterpriseDeviceMgrAbility::VerifyCallingPermission(const std::string &permissionName) { EDMLOGD("VerifyCallingPermission permission %{public}s", permissionName.c_str()); @@ -777,8 +845,14 @@ ErrCode EnterpriseDeviceMgrAbility::HandleManagedEvent(const AppExecFwk::Element } if (subscribe) { adminMgr_->SaveSubscribeEvents(events, adminItem, userId); + AddAppEventFuncMap(); + bool ret = SubscribeAppState(); + if (!ret) { + EDMLOGE("add SubscriberAppState failed!"); + } } else { adminMgr_->RemoveSubscribeEvents(events, adminItem, userId); + UnsubscribeAppState(); } return ERR_OK; } @@ -786,8 +860,10 @@ ErrCode EnterpriseDeviceMgrAbility::HandleManagedEvent(const AppExecFwk::Element bool EnterpriseDeviceMgrAbility::CheckManagedEvent(uint32_t event) { switch (event) { - case static_cast(ManagedEvent::BUNDLE_ADDED): - case static_cast(ManagedEvent::BUNDLE_REMOVED): + case static_cast(ManagedEvent::BUNDLE_ADDED) : + case static_cast(ManagedEvent::BUNDLE_REMOVED) : + case static_cast(ManagedEvent::APP_START) : + case static_cast(ManagedEvent::APP_STOP) : break; default: return false; diff --git a/test/unittest/include/enterprise_admin_stub_mock.h b/test/unittest/include/enterprise_admin_stub_mock.h index 166232a644ed92aec34fc96f3a05954924b153dc..5e52f435e0430e4b8ccf99724c8e05d834e138d3 100644 --- a/test/unittest/include/enterprise_admin_stub_mock.h +++ b/test/unittest/include/enterprise_admin_stub_mock.h @@ -46,6 +46,10 @@ public: void OnBundleRemoved(const std::string &bundleName) override {} + void OnAppStart(const std::string &bundleName) override {} + + void OnAppStop(const std::string &bundleName) override {} + uint32_t code_ = 0; }; } // namespace EDM