From d85e15d9ef4f9243ef1a72a34ff1a0ce4d9847ec Mon Sep 17 00:00:00 2001 From: caiminggang Date: Thu, 17 Nov 2022 11:23:27 +0800 Subject: [PATCH] =?UTF-8?q?edm=E6=8E=A5=E5=8F=A3=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: caiminggang --- bundle.json | 5 +- .../src/js_enterprise_admin_extension.cpp | 2 +- interfaces/inner_api/BUILD.gn | 15 +- .../inner_api/{ => common}/include/ent_info.h | 0 .../include/enterprise_device_mgr_proxy.h | 12 +- .../include/ienterprise_device_mgr.h | 0 .../{ => common}/include/policy_info.h | 1 + .../inner_api/{ => common}/src/ent_info.cpp | 0 .../src/enterprise_device_mgr_proxy.cpp | 33 +- .../include/datetime_manager_proxy.h} | 17 +- .../src/datetime_manager_proxy.cpp} | 18 +- interfaces/kits/{ => admin_manager}/BUILD.gn | 11 +- .../include/admin_manager_addon.h} | 73 +-- .../src/admin_manager_addon.cpp} | 494 ++---------------- .../kits/common/include/napi_edm_common.h | 74 +++ .../kits/common/include/napi_edm_error.h | 40 ++ .../kits/common/src/napi_edm_common.cpp | 252 +++++++++ interfaces/kits/common/src/napi_edm_error.cpp | 87 +++ interfaces/kits/datetime_manager/BUILD.gn | 48 ++ .../include/datetime_manager_addon.h | 49 ++ .../src/datetime_manager_addon.cpp | 98 ++++ .../kits/enterprise_admin_extension/BUILD.gn | 2 +- ...erprise_admin_extension_ability_module.cpp | 10 +- .../BUILD.gn | 2 +- ...erprise_admin_extension_context_module.cpp | 10 +- services/edm/include/iplugin.h | 2 + services/edm/include/iplugin_template.h | 30 +- .../edm/src/enterprise_device_mgr_ability.cpp | 28 +- .../edm/src/enterprise_device_mgr_stub.cpp | 9 +- services/edm_plugin/BUILD.gn | 2 +- services/edm_plugin/plugin.gni | 2 +- test/unittest/BUILD.gn | 2 +- .../enterprise_device_mgr_proxy_test.cpp | 24 +- test/unittest/include/iplugin_mock.h | 5 + test/unittest/include/plugin_manager_test.h | 5 + ...st.cpp => datetime_manager_proxy_test.cpp} | 128 ++--- .../src/enterprise_device_mgr_proxy_test.cpp | 2 +- 37 files changed, 898 insertions(+), 694 deletions(-) rename interfaces/inner_api/{ => common}/include/ent_info.h (100%) rename interfaces/inner_api/{ => common}/include/enterprise_device_mgr_proxy.h (82%) rename interfaces/inner_api/{ => common}/include/ienterprise_device_mgr.h (100%) rename interfaces/inner_api/{ => common}/include/policy_info.h (97%) rename interfaces/inner_api/{ => common}/src/ent_info.cpp (100%) rename interfaces/inner_api/{ => common}/src/enterprise_device_mgr_proxy.cpp (94%) rename interfaces/inner_api/{include/device_settings_manager.h => datetime_manager/include/datetime_manager_proxy.h} (66%) rename interfaces/inner_api/{src/device_settings_manager.cpp => datetime_manager/src/datetime_manager_proxy.cpp} (71%) rename interfaces/kits/{ => admin_manager}/BUILD.gn (78%) rename interfaces/kits/{include/enterprise_device_manager_addon.h => admin_manager/include/admin_manager_addon.h} (58%) rename interfaces/kits/{src/enterprise_device_manager_addon.cpp => admin_manager/src/admin_manager_addon.cpp} (57%) create mode 100644 interfaces/kits/common/include/napi_edm_common.h create mode 100644 interfaces/kits/common/include/napi_edm_error.h create mode 100644 interfaces/kits/common/src/napi_edm_common.cpp create mode 100644 interfaces/kits/common/src/napi_edm_error.cpp create mode 100644 interfaces/kits/datetime_manager/BUILD.gn create mode 100644 interfaces/kits/datetime_manager/include/datetime_manager_addon.h create mode 100644 interfaces/kits/datetime_manager/src/datetime_manager_addon.cpp rename test/unittest/src/{device_settings_manager_test.cpp => datetime_manager_proxy_test.cpp} (67%) diff --git a/bundle.json b/bundle.json index 02636897a..8313fcfae 100644 --- a/bundle.json +++ b/bundle.json @@ -79,7 +79,8 @@ "//base/customization/enterprise_device_management/tools/edm:tools_edm" ], "fwk_group":[ - "//base/customization/enterprise_device_management/interfaces/kits:enterprisedevicemanager", + "//base/customization/enterprise_device_management/interfaces/kits/admin_manager:adminmanager", + "//base/customization/enterprise_device_management/interfaces/kits/datetime_manager:datetimemanager", "//base/customization/enterprise_device_management/framework/extension:enterprise_admin_extension", "//base/customization/enterprise_device_management/interfaces/kits/enterprise_admin_extension:enterpriseadminextensionability_napi", "//base/customization/enterprise_device_management/interfaces/kits/enterprise_admin_extension_context:enterpriseadminextensioncontext_napi", @@ -88,7 +89,7 @@ ], "service_group":[ "//base/customization/enterprise_device_management/services/edm:edmservice", - "//base/customization/enterprise_device_management/services/edm_plugin:device_settings_plugin", + "//base/customization/enterprise_device_management/services/edm_plugin:datetime_manager_plugin", "//base/customization/enterprise_device_management/sa_profile:edm_sa_profile", "//base/customization/enterprise_device_management/etc/init:edm.cfg" ] diff --git a/framework/extension/src/js_enterprise_admin_extension.cpp b/framework/extension/src/js_enterprise_admin_extension.cpp index b289ea136..2f57825e9 100644 --- a/framework/extension/src/js_enterprise_admin_extension.cpp +++ b/framework/extension/src/js_enterprise_admin_extension.cpp @@ -77,7 +77,7 @@ void JsEnterpriseAdminExtension::Init(const std::shared_ptrGet(); HILOG_INFO("JsEnterpriseAdminExtension::Init Bind."); diff --git a/interfaces/inner_api/BUILD.gn b/interfaces/inner_api/BUILD.gn index 115d45a77..ab5504a6e 100644 --- a/interfaces/inner_api/BUILD.gn +++ b/interfaces/inner_api/BUILD.gn @@ -14,12 +14,12 @@ import("//base/customization/enterprise_device_management/edm.gni") import("//build/ohos.gni") -SRC_PATH = "$EDM_INNERKITS_PATH/src" -INCLUDE_PATH = "$EDM_INNERKITS_PATH/include" +SRC_PATH = "$EDM_INNERKITS_PATH/" config("edmservice_kits_config") { include_dirs = [ - "include", + "common/include", + "datetime_manager/include", "$SUBSYSTEM_DIR/common/native/include", ] } @@ -28,12 +28,9 @@ ohos_shared_library("edmservice_kits") { public_configs = [ ":edmservice_kits_config" ] sources = [ - "$INCLUDE_PATH/device_settings_manager.h", - "$INCLUDE_PATH/ent_info.h", - "$INCLUDE_PATH/enterprise_device_mgr_proxy.h", - "$SRC_PATH/device_settings_manager.cpp", - "$SRC_PATH/ent_info.cpp", - "$SRC_PATH/enterprise_device_mgr_proxy.cpp", + "$SRC_PATH/common/src/ent_info.cpp", + "$SRC_PATH/common/src/enterprise_device_mgr_proxy.cpp", + "$SRC_PATH/datetime_manager/src/datetime_manager_proxy.cpp", "$SUBSYSTEM_DIR/common/native/src/edm_sys_manager.cpp", ] diff --git a/interfaces/inner_api/include/ent_info.h b/interfaces/inner_api/common/include/ent_info.h similarity index 100% rename from interfaces/inner_api/include/ent_info.h rename to interfaces/inner_api/common/include/ent_info.h diff --git a/interfaces/inner_api/include/enterprise_device_mgr_proxy.h b/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h similarity index 82% rename from interfaces/inner_api/include/enterprise_device_mgr_proxy.h rename to interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h index 45894ab96..761762e21 100644 --- a/interfaces/inner_api/include/enterprise_device_mgr_proxy.h +++ b/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h @@ -47,18 +47,18 @@ public: void GetEnabledSuperAdmin(std::string &enabledAdmin); bool IsSuperAdminExist(); void GetEnabledAdmins(std::vector &enabledAdminList); - bool IsPolicyDisable(int policyCode, bool &isDisabled); - bool GetPolicyValue(int policyCode, std::string &policyData); - bool GetPolicyArray(int policyCode, std::vector &policyData); - bool GetPolicyConfig(int policyCode, std::map &policyData); - + bool IsPolicyDisable(AppExecFwk::ElementName *admin, int policyCode, bool &isDisabled); + bool GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData); + bool GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode, std::vector &policyData); + bool GetPolicyConfig(AppExecFwk::ElementName *admin, int policyCode, std::map &policyData); + bool GetPolicy(AppExecFwk::ElementName *admin, int policyCode, MessageParcel &reply); private: static std::shared_ptr instance_; static std::mutex mutexLock_; void GetEnabledAdmins(std::uint32_t type, std::vector &enabledAdminList); sptr GetRemoteObject(); - bool GetPolicy(int policyCode, MessageParcel &reply); }; } // namespace EDM } // namespace OHOS diff --git a/interfaces/inner_api/include/ienterprise_device_mgr.h b/interfaces/inner_api/common/include/ienterprise_device_mgr.h similarity index 100% rename from interfaces/inner_api/include/ienterprise_device_mgr.h rename to interfaces/inner_api/common/include/ienterprise_device_mgr.h diff --git a/interfaces/inner_api/include/policy_info.h b/interfaces/inner_api/common/include/policy_info.h similarity index 97% rename from interfaces/inner_api/include/policy_info.h rename to interfaces/inner_api/common/include/policy_info.h index 565e6cd8a..eb26ae849 100644 --- a/interfaces/inner_api/include/policy_info.h +++ b/interfaces/inner_api/common/include/policy_info.h @@ -23,6 +23,7 @@ namespace OHOS { namespace EDM { enum { SET_DATETIME = 1001, + GET_DEVICE_SERIAL = 1002 }; #define POLICY_CODE_TO_NAME(ENUM_CODE, POLICY_NAME) do { \ diff --git a/interfaces/inner_api/src/ent_info.cpp b/interfaces/inner_api/common/src/ent_info.cpp similarity index 100% rename from interfaces/inner_api/src/ent_info.cpp rename to interfaces/inner_api/common/src/ent_info.cpp diff --git a/interfaces/inner_api/src/enterprise_device_mgr_proxy.cpp b/interfaces/inner_api/common/src/enterprise_device_mgr_proxy.cpp similarity index 94% rename from interfaces/inner_api/src/enterprise_device_mgr_proxy.cpp rename to interfaces/inner_api/common/src/enterprise_device_mgr_proxy.cpp index ac26a4a5a..d532ed3e3 100644 --- a/interfaces/inner_api/src/enterprise_device_mgr_proxy.cpp +++ b/interfaces/inner_api/common/src/enterprise_device_mgr_proxy.cpp @@ -290,10 +290,10 @@ bool EnterpriseDeviceMgrProxy::IsAdminEnabled(AppExecFwk::ElementName &admin, in return ret; } -bool EnterpriseDeviceMgrProxy::IsPolicyDisable(int policyCode, bool &isDisabled) +bool EnterpriseDeviceMgrProxy::IsPolicyDisable(AppExecFwk::ElementName *admin, int policyCode, bool &isDisabled) { MessageParcel reply; - if (!GetPolicy(policyCode, reply)) { + if (!GetPolicy(admin, policyCode, reply)) { isDisabled = false; return false; } else { @@ -322,20 +322,21 @@ int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, Message return ret; } -bool EnterpriseDeviceMgrProxy::GetPolicyValue(int policyCode, std::string &policyData) +bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData) { MessageParcel reply; - if (!GetPolicy(policyCode, reply)) { + if (!GetPolicy(admin, policyCode, reply)) { return false; } policyData = Str16ToStr8(reply.ReadString16()); return true; } -bool EnterpriseDeviceMgrProxy::GetPolicyArray(int policyCode, std::vector &policyData) +bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode, + std::vector &policyData) { MessageParcel reply; - if (!GetPolicy(policyCode, reply)) { + if (!GetPolicy(admin, policyCode, reply)) { return false; } std::vector readVector16; @@ -351,10 +352,11 @@ bool EnterpriseDeviceMgrProxy::GetPolicyArray(int policyCode, std::vector &policyData) +bool EnterpriseDeviceMgrProxy::GetPolicyConfig(AppExecFwk::ElementName *admin, int policyCode, + std::map &policyData) { MessageParcel reply; - if (!GetPolicy(policyCode, reply)) { + if (!GetPolicy(admin, policyCode, reply)) { return false; } std::vector keys16; @@ -386,7 +388,7 @@ bool EnterpriseDeviceMgrProxy::GetPolicyConfig(int policyCode, std::mapSendRequest(funcCode, data, reply, option); if (FAILED(res)) { EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail."); return false; } - std::int32_t requestRes = ERR_INVALID_VALUE; - bool blRes = reply.ReadInt32(requestRes) && (requestRes == ERR_OK); - if (!blRes) { - EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", requestRes); - } - return blRes; + return true; } void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector &enabledAdminList) diff --git a/interfaces/inner_api/include/device_settings_manager.h b/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h similarity index 66% rename from interfaces/inner_api/include/device_settings_manager.h rename to interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h index cb4409b74..c085c972b 100644 --- a/interfaces/inner_api/include/device_settings_manager.h +++ b/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h @@ -13,25 +13,24 @@ * limitations under the License. */ -#ifndef INTERFACES_INNER_API_INCLUDE_DEVICE_SETTINGS_MANAGER_H -#define INTERFACES_INNER_API_INCLUDE_DEVICE_SETTINGS_MANAGER_H +#ifndef INTERFACES_INNER_API_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_PROXY_H +#define INTERFACES_INNER_API_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_PROXY_H #include "enterprise_device_mgr_proxy.h" namespace OHOS { namespace EDM { -class DeviceSettingsManager { +class DatetimeManagerProxy { public: - DeviceSettingsManager(); - ~DeviceSettingsManager(); - static std::shared_ptr GetDeviceSettingsManager(); + DatetimeManagerProxy(); + ~DatetimeManagerProxy(); + static std::shared_ptr GetDatetimeManagerProxy(); int32_t SetDateTime(AppExecFwk::ElementName &admin, int64_t time); private: static std::shared_ptr proxy_; - static std::shared_ptr instance_; + static std::shared_ptr instance_; static std::mutex mutexLock_; }; } // namespace EDM } // namespace OHOS - -#endif // INTERFACES_INNER_API_INCLUDE_DEVICE_SETTINGS_MANAGER_H +#endif // INTERFACES_INNER_API_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_PROXY_H \ No newline at end of file diff --git a/interfaces/inner_api/src/device_settings_manager.cpp b/interfaces/inner_api/datetime_manager/src/datetime_manager_proxy.cpp similarity index 71% rename from interfaces/inner_api/src/device_settings_manager.cpp rename to interfaces/inner_api/datetime_manager/src/datetime_manager_proxy.cpp index 3989b5c48..c03d62e4d 100644 --- a/interfaces/inner_api/src/device_settings_manager.cpp +++ b/interfaces/inner_api/datetime_manager/src/datetime_manager_proxy.cpp @@ -13,36 +13,36 @@ * limitations under the License. */ -#include "device_settings_manager.h" +#include "datetime_manager_proxy.h" #include "edm_log.h" #include "func_code.h" #include "policy_info.h" namespace OHOS { namespace EDM { -std::shared_ptr DeviceSettingsManager::instance_ = nullptr; -std::mutex DeviceSettingsManager::mutexLock_; +std::shared_ptr DatetimeManagerProxy::instance_ = nullptr; +std::mutex DatetimeManagerProxy::mutexLock_; const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr"; -DeviceSettingsManager::DeviceSettingsManager() {} +DatetimeManagerProxy::DatetimeManagerProxy() {} -DeviceSettingsManager::~DeviceSettingsManager() {} +DatetimeManagerProxy::~DatetimeManagerProxy() {} -std::shared_ptr DeviceSettingsManager::GetDeviceSettingsManager() +std::shared_ptr DatetimeManagerProxy::GetDatetimeManagerProxy() { if (instance_ == nullptr) { std::lock_guard lock(mutexLock_); if (instance_ == nullptr) { - std::shared_ptr temp = std::make_shared(); + std::shared_ptr temp = std::make_shared(); instance_ = temp; } } return instance_; } -int32_t DeviceSettingsManager::SetDateTime(AppExecFwk::ElementName &admin, int64_t time) +int32_t DatetimeManagerProxy::SetDateTime(AppExecFwk::ElementName &admin, int64_t time) { - EDMLOGD("DeviceSettingsManager::SetDateTime"); + EDMLOGD("DatetimeManagerProxy::SetDateTime"); auto proxy = EnterpriseDeviceMgrProxy::GetInstance(); if (proxy == nullptr) { EDMLOGE("can not get EnterpriseDeviceMgrProxy"); diff --git a/interfaces/kits/BUILD.gn b/interfaces/kits/admin_manager/BUILD.gn similarity index 78% rename from interfaces/kits/BUILD.gn rename to interfaces/kits/admin_manager/BUILD.gn index 318223581..a841a0693 100644 --- a/interfaces/kits/BUILD.gn +++ b/interfaces/kits/admin_manager/BUILD.gn @@ -14,17 +14,20 @@ import("//base/customization/enterprise_device_management/edm.gni") import("//build/ohos.gni") -ohos_shared_library("enterprisedevicemanager") { +ohos_shared_library("adminmanager") { include_dirs = [ "//third_party/node/src", "$SUBSYSTEM_DIR/common/native/include", - "$SUBSYSTEM_DIR/interfaces/kits/include", + "$SUBSYSTEM_DIR/interfaces/kits/admin_manager/include", + "$SUBSYSTEM_DIR/interfaces/kits/common/include", "$SUBSYSTEM_DIR/interfaces/inner_api/include", ] sources = [ "$SUBSYSTEM_DIR/common/native/src/hisysevent_adapter.cpp", - "src/enterprise_device_manager_addon.cpp", + "$SUBSYSTEM_DIR/interfaces/kits/common/src/napi_edm_common.cpp", + "$SUBSYSTEM_DIR/interfaces/kits/common/src/napi_edm_error.cpp", + "src/admin_manager_addon.cpp", ] configs = [ "//base/customization/enterprise_device_management/common/config:coverage_flags" ] @@ -40,7 +43,7 @@ ohos_shared_library("enterprisedevicemanager") { "os_account:os_account_innerkits", ] - relative_install_dir = "module" + relative_install_dir = "module/enterprise" subsystem_name = "customization" part_name = "enterprise_device_management" } diff --git a/interfaces/kits/include/enterprise_device_manager_addon.h b/interfaces/kits/admin_manager/include/admin_manager_addon.h similarity index 58% rename from interfaces/kits/include/enterprise_device_manager_addon.h rename to interfaces/kits/admin_manager/include/admin_manager_addon.h index 011875aab..06506fda5 100644 --- a/interfaces/kits/include/enterprise_device_manager_addon.h +++ b/interfaces/kits/admin_manager/include/admin_manager_addon.h @@ -13,17 +13,19 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_INCLUDE_ENTERPRISE_DEVICE_MANAGER_ADDON_H -#define INTERFACES_KITS_INCLUDE_ENTERPRISE_DEVICE_MANAGER_ADDON_H +#ifndef INTERFACES_KITS_ADMIN_MANAGER_INCLUDE_ADMIN_MANAGER_ADDON_H +#define INTERFACES_KITS_ADMIN_MANAGER_INCLUDE_ADMIN_MANAGER_ADDON_H #include "admin_type.h" -#include "device_settings_manager.h" +#include "datetime_manager_proxy.h" #include "edm_errors.h" #include "element_name.h" #include "ent_info.h" #include "enterprise_device_mgr_proxy.h" #include "ienterprise_device_mgr.h" #include "managed_event.h" +#include "napi_edm_error.h" +#include "napi_edm_common.h" #include "napi/native_common.h" #include "napi/native_node_api.h" #include "napi/native_api.h" @@ -31,18 +33,6 @@ namespace OHOS { namespace EDM { -struct AsyncCallbackInfo { - napi_env env; - napi_async_work asyncWork; - napi_deferred deferred; - napi_ref callback = 0; - ErrCode ret; - bool boolRet = true; - int32_t err = 0; - uint32_t errCode = 0; - std::string errMessage; -}; - struct AsyncEnableAdminCallbackInfo : AsyncCallbackInfo { OHOS::AppExecFwk::ElementName elementName; EntInfo entInfo; @@ -64,11 +54,6 @@ struct AsyncGetEnterpriseInfoCallbackInfo : AsyncCallbackInfo { EntInfo entInfo; }; -struct AsyncSetDateTimeCallbackInfo : AsyncCallbackInfo { - OHOS::AppExecFwk::ElementName elementName; - int64_t time; -}; - struct AsyncSetEnterpriseInfoCallbackInfo : AsyncCallbackInfo { OHOS::AppExecFwk::ElementName elementName; EntInfo entInfo; @@ -83,25 +68,16 @@ struct AsyncIsAdminEnabledCallbackInfo : AsyncCallbackInfo { int32_t userId = 0; }; -struct AsyncGetDeviceSettingsManagerCallbackInfo : AsyncCallbackInfo { - napi_env env; - napi_async_work asyncWork; - napi_deferred deferred; - napi_ref callback = 0; -}; - struct AsyncSubscribeManagedEventCallbackInfo : AsyncCallbackInfo { OHOS::AppExecFwk::ElementName elementName; std::vector managedEvent; bool subscribe = true; }; -class EnterpriseDeviceManagerAddon { +class AdminManager { public: - EnterpriseDeviceManagerAddon(); - ~EnterpriseDeviceManagerAddon() = default; - static thread_local napi_ref g_classDeviceSettingsManager; - + AdminManager(); + ~AdminManager() = default; static napi_value Init(napi_env env, napi_value exports); static napi_value EnableAdmin(napi_env env, napi_callback_info info); static napi_value DisableAdmin(napi_env env, napi_callback_info info); @@ -110,12 +86,7 @@ public: static napi_value SetEnterpriseInfo(napi_env env, napi_callback_info info); static napi_value IsSuperAdmin(napi_env env, napi_callback_info info); static napi_value isAdminEnabled(napi_env env, napi_callback_info info); - static napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, std::string workName, - napi_async_execute_callback execute, napi_async_complete_callback complete); static napi_value CreateAdminTypeObject(napi_env env); - static bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr); - static napi_value GetDeviceSettingsManager(napi_env env, napi_callback_info info); - static napi_value SetDateTime(napi_env env, napi_callback_info info); static napi_value SubscribeManagedEvent(napi_env env, napi_callback_info info); static napi_value UnsubscribeManagedEvent(napi_env env, napi_callback_info info); @@ -126,49 +97,23 @@ public: static void NativeSetEnterpriseInfo(napi_env env, void *data); static void NativeIsSuperAdmin(napi_env env, void *data); static void NativeIsAdminEnabled(napi_env env, void *data); - static void NativeSetDateTime(napi_env env, void *data); static void NativeSubscribeManagedEvent(napi_env env, void *data); - - static void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data); - static void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data); static void NativeGetEnterpriseInfoComplete(napi_env env, napi_status status, void *data); static void ConvertEnterpriseInfo(napi_env env, napi_value objEntInfo, EntInfo &entInfo); static bool ParseEnterpriseInfo(napi_env env, EntInfo &enterpriseInfo, napi_value args); - - static bool ParseString(napi_env env, std::string ¶m, napi_value args); static bool ParseManagedEvent(napi_env env, std::vector &managedEvent, napi_value args); - static bool ParseElementName(napi_env env, OHOS::AppExecFwk::ElementName &elementName, napi_value args); - static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); static void CreateAdminTypeObject(napi_env env, napi_value value); - static bool ParseInt(napi_env env, int32_t ¶m, napi_value args); - static bool ParseLong(napi_env env, int64_t ¶m, napi_value args); static void CreateManagedEventObject(napi_env env, napi_value value); - - static napi_value DeviceSettingsManagerConstructor(napi_env env, napi_callback_info info); private: - static napi_value CreateError(napi_env env, ErrCode errorCode); - static napi_value CreateError(napi_env env, int32_t errorCode, const std::string &errMessage); - static std::pair GetMessageFromReturncode(ErrCode returnCode); static bool checkEnableAdminParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId); static bool checkAdminWithUserIdParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId); static napi_value HandleManagedEvent(napi_env env, napi_callback_info info, bool subscribe); - #define ASSERT_AND_THROW_PARAM_ERROR(env, assertion, message) \ - do { \ - if (!(assertion)) { \ - napi_throw((env), CreateError((env), (EdmReturnErrCode::PARAM_ERROR), (message))); \ - napi_value ret = nullptr; \ - return ret; \ - } \ - } while (0) - - static std::map errMessageMap; static std::shared_ptr proxy_; - static std::shared_ptr deviceSettingsManager_; }; } // namespace EDM } // namespace OHOS -#endif // INTERFACES_KITS_INCLUDE_ENTERPRISE_DEVICE_MANAGER_ADDON_H +#endif // INTERFACES_KITS_ADMIN_MANAGER_INCLUDE_ADMIN_MANAGER_ADDON_H diff --git a/interfaces/kits/src/enterprise_device_manager_addon.cpp b/interfaces/kits/admin_manager/src/admin_manager_addon.cpp similarity index 57% rename from interfaces/kits/src/enterprise_device_manager_addon.cpp rename to interfaces/kits/admin_manager/src/admin_manager_addon.cpp index 810fa2242..b6d820099 100644 --- a/interfaces/kits/src/enterprise_device_manager_addon.cpp +++ b/interfaces/kits/admin_manager/src/admin_manager_addon.cpp @@ -12,79 +12,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "enterprise_device_manager_addon.h" +#include "admin_manager_addon.h" #include "edm_log.h" +#include "hisysevent_adapter.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "os_account_manager.h" #include "system_ability_definition.h" -#include "hisysevent_adapter.h" using namespace OHOS::EDM; -namespace { -constexpr int32_t ARR_INDEX_ZERO = 0; -constexpr int32_t ARR_INDEX_ONE = 1; -constexpr int32_t ARR_INDEX_TWO = 2; -constexpr int32_t ARR_INDEX_THREE = 3; -constexpr int32_t ARR_INDEX_FOUR = 4; - -constexpr size_t ARGS_SIZE_ZERO = 0; -constexpr size_t ARGS_SIZE_ONE = 1; -constexpr size_t ARGS_SIZE_TWO = 2; -constexpr size_t ARGS_SIZE_THREE = 3; -constexpr size_t ARGS_SIZE_FOUR = 4; -constexpr size_t ARGS_SIZE_FIVE = 5; - -constexpr size_t CALLBACK_SIZE = 1; - -constexpr int32_t NAPI_RETURN_ZERO = 0; -constexpr int32_t NAPI_RETURN_ONE = 1; -} +std::shared_ptr AdminManager::proxy_ = nullptr; -std::map EnterpriseDeviceManagerAddon::errMessageMap = { - { - EdmReturnErrCode::PERMISSION_DENIED, "the application does not have permission to call this function." - }, - { - EdmReturnErrCode::SYSTEM_ABNORMALLY, "the system ability work abnormally." - }, - { - EdmReturnErrCode::ENABLE_ADMIN_FAILED, "failed to enable the administrator application of the device." - }, - { - EdmReturnErrCode::COMPONENT_INVALID, "the administrator ability component is invalid." - }, - { - EdmReturnErrCode::ADMIN_INACTIVE, "the application is not a administrator of the device." - }, - { - EdmReturnErrCode::DISABLE_ADMIN_FAILED, "failed to disable the administrator application of the device." - }, - { - EdmReturnErrCode::UID_INVALID, "the specified user ID is invalid." - }, - { - EdmReturnErrCode::INTERFACE_UNSUPPORTED, "the specified interface is not supported." - }, - { - EdmReturnErrCode::PARAM_ERROR, "invalid input parameter." - }, - { - EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED, - "the administrator application does not have permission to manage the device." - }, - { - EdmReturnErrCode::MANAGED_EVENTS_INVALID, "the specified managed event is is invalid." - } -}; - -std::shared_ptr EnterpriseDeviceManagerAddon::proxy_ = nullptr; -std::shared_ptr EnterpriseDeviceManagerAddon::deviceSettingsManager_ = nullptr; -thread_local napi_ref EnterpriseDeviceManagerAddon::g_classDeviceSettingsManager; - -napi_value EnterpriseDeviceManagerAddon::EnableAdmin(napi_env env, napi_callback_info info) +napi_value AdminManager::EnableAdmin(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_EnableAdmin called"); ReportEdmEvent(ReportType::EDM_FUNC_EVENT, "enableAdmin", ""); @@ -130,7 +71,7 @@ napi_value EnterpriseDeviceManagerAddon::EnableAdmin(napi_env env, napi_callback return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeEnableAdmin(napi_env env, void *data) +void AdminManager::NativeEnableAdmin(napi_env env, void *data) { if (data == nullptr) { EDMLOGE("data is nullptr"); @@ -147,17 +88,7 @@ void EnterpriseDeviceManagerAddon::NativeEnableAdmin(napi_env env, void *data) static_cast(asyncCallbackInfo->adminType), asyncCallbackInfo->userId); } -std::pair EnterpriseDeviceManagerAddon::GetMessageFromReturncode(ErrCode returnCode) -{ - auto iter = errMessageMap.find(returnCode); - if (iter != errMessageMap.end()) { - return std::make_pair(returnCode, iter->second); - } else { - return std::make_pair(EdmReturnErrCode::PARAM_ERROR, "some thing wrong happend"); - } -} - -bool EnterpriseDeviceManagerAddon::checkEnableAdminParamType(napi_env env, size_t argc, +bool AdminManager::checkEnableAdminParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId) { EDMLOGI("argc = %{public}zu", argc); @@ -198,79 +129,7 @@ bool EnterpriseDeviceManagerAddon::checkEnableAdminParamType(napi_env env, size_ MatchValueType(env, argv[ARR_INDEX_FOUR], napi_function); } -void EnterpriseDeviceManagerAddon::NativeBoolCallbackComplete(napi_env env, napi_status status, void *data) -{ - if (data == nullptr) { - EDMLOGE("data is nullptr"); - return; - } - AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); - if (asyncCallbackInfo->deferred != nullptr) { - EDMLOGD("asyncCallbackInfo->deferred != nullptr"); - if (asyncCallbackInfo->ret == ERR_OK) { - EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet); - napi_value result = nullptr; - napi_get_boolean(env, asyncCallbackInfo->boolRet, &result); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); - } else { - napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret)); - } - } else { - napi_value callbackValue[ARGS_SIZE_TWO] = { 0 }; - if (asyncCallbackInfo->ret == ERR_OK) { - napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]); - EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet); - napi_get_boolean(env, asyncCallbackInfo->boolRet, &callbackValue[ARR_INDEX_ONE]); - } else { - EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ", - GetMessageFromReturncode(asyncCallbackInfo->ret).first, - GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str()); - callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret); - napi_get_null(env, &callbackValue[ARR_INDEX_ONE]); - } - napi_value callback = nullptr; - napi_value result = nullptr; - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result); - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; -} - -void EnterpriseDeviceManagerAddon::NativeVoidCallbackComplete(napi_env env, napi_status status, void *data) -{ - if (data == nullptr) { - EDMLOGE("data is nullptr"); - return; - } - AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); - napi_value error = nullptr; - if (asyncCallbackInfo->callback == nullptr) { - EDMLOGD("asyncCallbackInfo->deferred != nullptr"); - if (asyncCallbackInfo->ret == ERR_OK) { - napi_get_null(env, &error); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, error); - } else { - napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret)); - } - } else { - if (asyncCallbackInfo->ret == ERR_OK) { - napi_get_null(env, &error); - } else { - error = CreateError(env, asyncCallbackInfo->ret); - } - napi_value callback = nullptr; - napi_value result = nullptr; - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &error, &result); - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; -} - -bool EnterpriseDeviceManagerAddon::checkAdminWithUserIdParamType(napi_env env, size_t argc, +bool AdminManager::checkAdminWithUserIdParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId) { EDMLOGI("argc = %{public}zu", argc); @@ -303,7 +162,7 @@ bool EnterpriseDeviceManagerAddon::checkAdminWithUserIdParamType(napi_env env, s MatchValueType(env, argv[ARR_INDEX_TWO], napi_function); } -napi_value EnterpriseDeviceManagerAddon::DisableAdmin(napi_env env, napi_callback_info info) +napi_value AdminManager::DisableAdmin(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_DisableAdmin called"); ReportEdmEvent(ReportType::EDM_FUNC_EVENT, "disableAdmin", ""); @@ -344,7 +203,7 @@ napi_value EnterpriseDeviceManagerAddon::DisableAdmin(napi_env env, napi_callbac return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeDisableAdmin(napi_env env, void *data) +void AdminManager::NativeDisableAdmin(napi_env env, void *data) { EDMLOGI("NAPI_NativeDisableAdmin called"); if (data == nullptr) { @@ -361,7 +220,7 @@ void EnterpriseDeviceManagerAddon::NativeDisableAdmin(napi_env env, void *data) asyncCallbackInfo->ret = proxy_->DisableAdmin(asyncCallbackInfo->elementName, asyncCallbackInfo->userId); } -napi_value EnterpriseDeviceManagerAddon::DisableSuperAdmin(napi_env env, napi_callback_info info) +napi_value AdminManager::DisableSuperAdmin(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_DisableSuperAdmin called"); size_t argc = ARGS_SIZE_TWO; @@ -392,7 +251,7 @@ napi_value EnterpriseDeviceManagerAddon::DisableSuperAdmin(napi_env env, napi_ca return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeDisableSuperAdmin(napi_env env, void *data) +void AdminManager::NativeDisableSuperAdmin(napi_env env, void *data) { EDMLOGI("NAPI_NativeDisableSuperAdmin called"); if (data == nullptr) { @@ -409,7 +268,7 @@ void EnterpriseDeviceManagerAddon::NativeDisableSuperAdmin(napi_env env, void *d asyncCallbackInfo->ret = proxy_->DisableSuperAdmin(asyncCallbackInfo->bundleName); } -napi_value EnterpriseDeviceManagerAddon::GetEnterpriseInfo(napi_env env, napi_callback_info info) +napi_value AdminManager::GetEnterpriseInfo(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_GetEnterpriseInfo called"); size_t argc = ARGS_SIZE_TWO; @@ -445,7 +304,7 @@ napi_value EnterpriseDeviceManagerAddon::GetEnterpriseInfo(napi_env env, napi_ca return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeGetEnterpriseInfo(napi_env env, void *data) +void AdminManager::NativeGetEnterpriseInfo(napi_env env, void *data) { EDMLOGI("NAPI_NativeDisableSuperAdmin called"); if (data == nullptr) { @@ -461,7 +320,7 @@ void EnterpriseDeviceManagerAddon::NativeGetEnterpriseInfo(napi_env env, void *d asyncCallbackInfo->ret = proxy_->GetEnterpriseInfo(asyncCallbackInfo->elementName, asyncCallbackInfo->entInfo); } -void EnterpriseDeviceManagerAddon::NativeGetEnterpriseInfoComplete(napi_env env, napi_status status, void *data) +void AdminManager::NativeGetEnterpriseInfoComplete(napi_env env, napi_status status, void *data) { if (data == nullptr) { EDMLOGE("data is nullptr"); @@ -500,7 +359,7 @@ void EnterpriseDeviceManagerAddon::NativeGetEnterpriseInfoComplete(napi_env env, asyncCallbackInfo = nullptr; } -napi_value EnterpriseDeviceManagerAddon::SetEnterpriseInfo(napi_env env, napi_callback_info info) +napi_value AdminManager::SetEnterpriseInfo(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_SetEnterpriseInfo called"); size_t argc = ARGS_SIZE_THREE; @@ -538,63 +397,7 @@ napi_value EnterpriseDeviceManagerAddon::SetEnterpriseInfo(napi_env env, napi_ca return asyncWorkReturn; } -napi_value EnterpriseDeviceManagerAddon::SetDateTime(napi_env env, napi_callback_info info) -{ - EDMLOGI("NAPI_SetDateTime called"); - size_t argc = ARGS_SIZE_THREE; - napi_value argv[ARGS_SIZE_THREE] = {nullptr}; - napi_value thisArg = nullptr; - void *data = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); - ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error"); - bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object) && - MatchValueType(env, argv[ARR_INDEX_ONE], napi_number); - if (argc > ARGS_SIZE_TWO) { - matchFlag = matchFlag && MatchValueType(env, argv[ARR_INDEX_TWO], napi_function); - } - ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error"); - auto asyncCallbackInfo = new (std::nothrow) AsyncSetDateTimeCallbackInfo(); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - bool ret = ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]); - ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error"); - EDMLOGD("SetDateTime: asyncCallbackInfo->elementName.bundlename %{public}s, " - "asyncCallbackInfo->abilityname:%{public}s", - asyncCallbackInfo->elementName.GetBundleName().c_str(), - asyncCallbackInfo->elementName.GetAbilityName().c_str()); - ret = ParseLong(env, asyncCallbackInfo->time, argv[ARR_INDEX_ONE]); - ASSERT_AND_THROW_PARAM_ERROR(env, ret, "time param error"); - if (argc > ARGS_SIZE_TWO) { - EDMLOGD("NAPI_SetDateTime argc == ARGS_SIZE_THREE"); - napi_create_reference(env, argv[ARR_INDEX_TWO], NAPI_RETURN_ONE, &asyncCallbackInfo->callback); - } - - napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "SetDateTime", - NativeSetDateTime, NativeVoidCallbackComplete); - callbackPtr.release(); - return asyncWorkReturn; -} - -void EnterpriseDeviceManagerAddon::NativeSetDateTime(napi_env env, void *data) -{ - EDMLOGI("NAPI_NativeSetDateTime called"); - if (data == nullptr) { - EDMLOGE("data is nullptr"); - return; - } - AsyncSetDateTimeCallbackInfo *asyncCallbackInfo = static_cast(data); - auto deviceSettingsManager_ = DeviceSettingsManager::GetDeviceSettingsManager(); - if (deviceSettingsManager_ == nullptr) { - EDMLOGE("can not get DeviceSettingsManager"); - return; - } - asyncCallbackInfo->ret = deviceSettingsManager_->SetDateTime(asyncCallbackInfo->elementName, - asyncCallbackInfo->time); -} - -void EnterpriseDeviceManagerAddon::NativeSetEnterpriseInfo(napi_env env, void *data) +void AdminManager::NativeSetEnterpriseInfo(napi_env env, void *data) { EDMLOGI("NAPI_NativeSetEnterpriseInfo called"); if (data == nullptr) { @@ -610,7 +413,7 @@ void EnterpriseDeviceManagerAddon::NativeSetEnterpriseInfo(napi_env env, void *d asyncCallbackInfo->ret = proxy_->SetEnterpriseInfo(asyncCallbackInfo->elementName, asyncCallbackInfo->entInfo); } -napi_value EnterpriseDeviceManagerAddon::IsSuperAdmin(napi_env env, napi_callback_info info) +napi_value AdminManager::IsSuperAdmin(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_IsSuperAdmin called"); size_t argc = ARGS_SIZE_TWO; @@ -640,7 +443,7 @@ napi_value EnterpriseDeviceManagerAddon::IsSuperAdmin(napi_env env, napi_callbac return asyncWorkReturn; } -napi_value EnterpriseDeviceManagerAddon::isAdminEnabled(napi_env env, napi_callback_info info) +napi_value AdminManager::isAdminEnabled(napi_env env, napi_callback_info info) { EDMLOGI("isAdminEnabled called"); size_t argc = ARGS_SIZE_THREE; @@ -681,7 +484,7 @@ napi_value EnterpriseDeviceManagerAddon::isAdminEnabled(napi_env env, napi_callb return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeIsSuperAdmin(napi_env env, void *data) +void AdminManager::NativeIsSuperAdmin(napi_env env, void *data) { EDMLOGI("NAPI_NativeIsSuperAdmin called"); if (data == nullptr) { @@ -698,7 +501,7 @@ void EnterpriseDeviceManagerAddon::NativeIsSuperAdmin(napi_env env, void *data) asyncCallbackInfo->boolRet = proxy_->IsSuperAdmin(asyncCallbackInfo->bundleName); } -void EnterpriseDeviceManagerAddon::NativeIsAdminEnabled(napi_env env, void *data) +void AdminManager::NativeIsAdminEnabled(napi_env env, void *data) { EDMLOGI("NAPI_NativeIsAdminEnabled called"); if (data == nullptr) { @@ -715,19 +518,19 @@ void EnterpriseDeviceManagerAddon::NativeIsAdminEnabled(napi_env env, void *data asyncCallbackInfo->boolRet = proxy_->IsAdminEnabled(asyncCallbackInfo->elementName, asyncCallbackInfo->userId); } -napi_value EnterpriseDeviceManagerAddon::SubscribeManagedEvent(napi_env env, napi_callback_info info) +napi_value AdminManager::SubscribeManagedEvent(napi_env env, napi_callback_info info) { EDMLOGI("SubscribeManagedEvent called"); return HandleManagedEvent(env, info, true); } -napi_value EnterpriseDeviceManagerAddon::UnsubscribeManagedEvent(napi_env env, napi_callback_info info) +napi_value AdminManager::UnsubscribeManagedEvent(napi_env env, napi_callback_info info) { EDMLOGI("UnsubscribeManagedEvent called"); return HandleManagedEvent(env, info, false); } -napi_value EnterpriseDeviceManagerAddon::HandleManagedEvent(napi_env env, napi_callback_info info, bool subscribe) +napi_value AdminManager::HandleManagedEvent(napi_env env, napi_callback_info info, bool subscribe) { size_t argc = ARGS_SIZE_THREE; napi_value argv[ARGS_SIZE_THREE] = {nullptr}; @@ -760,7 +563,7 @@ napi_value EnterpriseDeviceManagerAddon::HandleManagedEvent(napi_env env, napi_c return asyncWorkReturn; } -void EnterpriseDeviceManagerAddon::NativeSubscribeManagedEvent(napi_env env, void *data) +void AdminManager::NativeSubscribeManagedEvent(napi_env env, void *data) { EDMLOGI("NAPI_NativeSubscribeManagedEvent called"); if (data == nullptr) { @@ -778,73 +581,7 @@ void EnterpriseDeviceManagerAddon::NativeSubscribeManagedEvent(napi_env env, voi asyncCallbakInfo->managedEvent, asyncCallbakInfo->subscribe); } -bool EnterpriseDeviceManagerAddon::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType) -{ - napi_valuetype valueType = napi_undefined; - napi_typeof(env, value, &valueType); - return valueType == targetType; -} - -napi_value EnterpriseDeviceManagerAddon::HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, std::string workName, - napi_async_execute_callback execute, napi_async_complete_callback complete) -{ - napi_value result = nullptr; - if (context->callback == nullptr) { - napi_create_promise(env, &context->deferred, &result); - } else { - napi_get_undefined(env, &result); - } - napi_value resource = nullptr; - napi_get_undefined(env, &resource); - napi_value resourceName = nullptr; - napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work(env, resource, resourceName, execute, complete, - static_cast(context), &context->asyncWork); - napi_queue_async_work(env, context->asyncWork); - return result; -} - -napi_value EnterpriseDeviceManagerAddon::CreateError(napi_env env, ErrCode errorCode) -{ - return CreateError(env, GetMessageFromReturncode(errorCode).first, - GetMessageFromReturncode(errorCode).second); -} - -napi_value EnterpriseDeviceManagerAddon::CreateError(napi_env env, int32_t errorCode, const std::string &errMessage) -{ - napi_value result = nullptr; - napi_value message = nullptr; - napi_value errorCodeStr = nullptr; - napi_create_string_utf8(env, static_cast(std::to_string(errorCode).data()), - std::to_string(errorCode).size(), &errorCodeStr); - napi_create_string_utf8(env, errMessage.c_str(), errMessage.size(), &message); - napi_create_error(env, errorCodeStr, message, &result); - return result; -} - -bool EnterpriseDeviceManagerAddon::ParseInt(napi_env env, int32_t ¶m, napi_value args) -{ - napi_valuetype valueType = napi_undefined; - if (napi_typeof(env, args, &valueType)!= napi_ok || - valueType != napi_number || napi_get_value_int32(env, args, ¶m) != napi_ok) { - EDMLOGE("Wrong argument type. int32 expected."); - return false; - } - return true; -} - -bool EnterpriseDeviceManagerAddon::ParseLong(napi_env env, int64_t ¶m, napi_value args) -{ - napi_valuetype valueType = napi_undefined; - if (napi_typeof(env, args, &valueType)!= napi_ok || - valueType != napi_number || napi_get_value_int64(env, args, ¶m) != napi_ok) { - EDMLOGE("Wrong argument type. int64 expected."); - return false; - } - return true; -} - -bool EnterpriseDeviceManagerAddon::ParseEnterpriseInfo(napi_env env, EntInfo &enterpriseInfo, napi_value args) +bool AdminManager::ParseEnterpriseInfo(napi_env env, EntInfo &enterpriseInfo, napi_value args) { napi_valuetype valueType; if (napi_typeof(env, args, &valueType) != napi_ok || valueType != napi_object) { @@ -875,7 +612,7 @@ bool EnterpriseDeviceManagerAddon::ParseEnterpriseInfo(napi_env env, EntInfo &en return true; } -void EnterpriseDeviceManagerAddon::ConvertEnterpriseInfo(napi_env env, napi_value objEntInfo, EntInfo &entInfo) +void AdminManager::ConvertEnterpriseInfo(napi_env env, napi_value objEntInfo, EntInfo &entInfo) { std::string enterpriseName = entInfo.enterpriseName; std::string description = entInfo.description; @@ -889,38 +626,7 @@ void EnterpriseDeviceManagerAddon::ConvertEnterpriseInfo(napi_env env, napi_valu NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objEntInfo, "description", nDescription)); } -bool EnterpriseDeviceManagerAddon::ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args) -{ - napi_valuetype valueType; - NAPI_CALL_BASE(env, napi_typeof(env, args, &valueType), false); - if (valueType != napi_object) { - EDMLOGE("Parameter element valueType error"); - return false; - } - std::string bundleName; - std::string abilityName; - napi_value prop = nullptr; - if (napi_get_named_property(env, args, "bundleName", &prop) != napi_ok || - !GetStringFromNAPI(env, prop, bundleName)) { - EDMLOGE("Parameter element bundleName error"); - return false; - } - EDMLOGD("ParseElementName bundleName %{public}s ", bundleName.c_str()); - - prop = nullptr; - if (napi_get_named_property(env, args, "abilityName", &prop) != napi_ok || - !GetStringFromNAPI(env, prop, abilityName)) { - EDMLOGE("Parameter abilityname error"); - return false; - } - EDMLOGD("ParseElementName abilityname %{public}s", abilityName.c_str()); - - elementName.SetBundleName(bundleName); - elementName.SetAbilityName(abilityName); - return true; -} - -bool EnterpriseDeviceManagerAddon::ParseManagedEvent(napi_env env, +bool AdminManager::ParseManagedEvent(napi_env env, std::vector &managedEvent, napi_value args) { uint32_t len; @@ -941,26 +647,7 @@ bool EnterpriseDeviceManagerAddon::ParseManagedEvent(napi_env env, return true; } -bool EnterpriseDeviceManagerAddon::GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr) -{ - std::string result; - size_t size = 0; - - if (napi_get_value_string_utf8(env, value, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) { - EDMLOGE("can not get string size"); - return false; - } - result.reserve(size + NAPI_RETURN_ONE); - result.resize(size); - if (napi_get_value_string_utf8(env, value, result.data(), (size + NAPI_RETURN_ONE), &size) != napi_ok) { - EDMLOGE("can not get string value"); - return false; - } - resultStr = result; - return true; -} - -void EnterpriseDeviceManagerAddon::CreateAdminTypeObject(napi_env env, napi_value value) +void AdminManager::CreateAdminTypeObject(napi_env env, napi_value value) { napi_value nNomal; NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, AdminType::NORMAL, &nNomal)); @@ -970,7 +657,7 @@ void EnterpriseDeviceManagerAddon::CreateAdminTypeObject(napi_env env, napi_valu NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "ADMIN_TYPE_SUPER", nSuper)); } -void EnterpriseDeviceManagerAddon::CreateManagedEventObject(napi_env env, napi_value value) +void AdminManager::CreateManagedEventObject(napi_env env, napi_value value) { napi_value nBundleAdded; NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, @@ -982,115 +669,7 @@ void EnterpriseDeviceManagerAddon::CreateManagedEventObject(napi_env env, napi_v NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "MANAGED_EVENT_BUNDLE_REMOVED", nBundleRemoved)); } -bool EnterpriseDeviceManagerAddon::ParseString(napi_env env, std::string ¶m, napi_value args) -{ - napi_valuetype valuetype; - if (napi_typeof(env, args, &valuetype) != napi_ok || valuetype != napi_string || - !GetStringFromNAPI(env, args, param)) { - EDMLOGE("can not get string value"); - return false; - } - EDMLOGD("ParseString param = %{public}s.", param.c_str()); - return true; -} - -napi_value EnterpriseDeviceManagerAddon::DeviceSettingsManagerConstructor(napi_env env, napi_callback_info info) -{ - napi_value jsthis = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr)); - return jsthis; -} - -napi_value EnterpriseDeviceManagerAddon::GetDeviceSettingsManager(napi_env env, napi_callback_info info) -{ - EDMLOGI("GetDeviceSettingsManager called"); - size_t argc = ARGS_SIZE_ONE; - napi_value argv[ARGS_SIZE_ONE] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); - EDMLOGD("GetDeviceSettingsManager argc = [%{public}zu]", argc); - if (argc > ARGS_SIZE_ZERO) { - ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_function), - "parameter type error"); - } - AsyncGetDeviceSettingsManagerCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetDeviceSettingsManagerCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr - }; - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - if (argc > (ARGS_SIZE_ONE - CALLBACK_SIZE)) { - EDMLOGD("GetDeviceSettingsManager asyncCallback."); - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[ARR_INDEX_ZERO], &valuetype)); - ASSERT_AND_THROW_PARAM_ERROR(env, valuetype == napi_function, "Wrong argument type. Function expected."); - napi_create_reference(env, argv[ARR_INDEX_ZERO], NAPI_RETURN_ONE, &asyncCallbackInfo->callback); - - napi_value resourceName; - napi_create_string_latin1(env, "GetDeviceSettingsManager", NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, nullptr, resourceName, [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - AsyncGetDeviceSettingsManagerCallbackInfo *asyncCallbackInfo = - static_cast(data); - napi_value result[ARGS_SIZE_TWO] = { 0 }; - napi_value callback = 0; - napi_value undefined = 0; - napi_value callResult = 0; - napi_value m_classDeviceSettingsManager = nullptr; - napi_get_reference_value(env, g_classDeviceSettingsManager, &m_classDeviceSettingsManager); - napi_get_undefined(env, &undefined); - napi_new_instance(env, m_classDeviceSettingsManager, 0, nullptr, &result[ARR_INDEX_ONE]); - napi_get_null(env, &result[ARR_INDEX_ZERO]); - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_call_function(env, undefined, callback, ARGS_SIZE_TWO, &result[ARR_INDEX_ZERO], &callResult); - if (asyncCallbackInfo->callback != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - static_cast(asyncCallbackInfo), &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - napi_value result; - NAPI_CALL(env, napi_create_int32(env, NAPI_RETURN_ONE, &result)); - callbackPtr.release(); - return result; - } else { - napi_deferred deferred; - napi_value promise; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; - - napi_value resourceName; - napi_create_string_latin1(env, "GetDeviceSettingsManager", NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, nullptr, resourceName, [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - EDMLOGI("begin load DeviceSettingsManager"); - AsyncGetDeviceSettingsManagerCallbackInfo *asyncCallbackInfo = - static_cast(data); - napi_value result; - napi_value m_classDeviceSettingsManager = nullptr; - napi_get_reference_value(env, g_classDeviceSettingsManager, &m_classDeviceSettingsManager); - napi_new_instance(env, m_classDeviceSettingsManager, 0, nullptr, &result); - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - static_cast(asyncCallbackInfo), &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - callbackPtr.release(); - return promise; - } -} - -napi_value EnterpriseDeviceManagerAddon::Init(napi_env env, napi_value exports) +napi_value AdminManager::Init(napi_env env, napi_value exports) { napi_value nAdminType = nullptr; NAPI_CALL(env, napi_create_object(env, &nAdminType)); @@ -1108,7 +687,6 @@ napi_value EnterpriseDeviceManagerAddon::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getEnterpriseInfo", GetEnterpriseInfo), DECLARE_NAPI_FUNCTION("setEnterpriseInfo", SetEnterpriseInfo), DECLARE_NAPI_FUNCTION("isSuperAdmin", IsSuperAdmin), - DECLARE_NAPI_FUNCTION("getDeviceSettingsManager", GetDeviceSettingsManager), DECLARE_NAPI_FUNCTION("subscribeManagedEvent", SubscribeManagedEvent), DECLARE_NAPI_FUNCTION("unsubscribeManagedEvent", UnsubscribeManagedEvent), @@ -1116,14 +694,6 @@ napi_value EnterpriseDeviceManagerAddon::Init(napi_env env, napi_value exports) DECLARE_NAPI_PROPERTY("ManagedEvent", nManagedEvent), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); - - napi_value m_classDeviceSettingsManager; - napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("setDateTime", SetDateTime), - }; - NAPI_CALL(env, napi_define_class(env, "DeviceSettingsManager", NAPI_AUTO_LENGTH, DeviceSettingsManagerConstructor, - nullptr, sizeof(properties) / sizeof(*properties), properties, &m_classDeviceSettingsManager)); - napi_create_reference(env, m_classDeviceSettingsManager, 1, &g_classDeviceSettingsManager); return exports; } @@ -1131,8 +701,8 @@ static napi_module g_edmServiceModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, - .nm_register_func = EnterpriseDeviceManagerAddon::Init, - .nm_modname = "enterpriseDeviceManager", + .nm_register_func = AdminManager::Init, + .nm_modname = "enterprise.adminManager", .nm_priv = ((void *)0), .reserved = { 0 }, }; diff --git a/interfaces/kits/common/include/napi_edm_common.h b/interfaces/kits/common/include/napi_edm_common.h new file mode 100644 index 000000000..9c649af58 --- /dev/null +++ b/interfaces/kits/common/include/napi_edm_common.h @@ -0,0 +1,74 @@ +/* + * 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 INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H +#define INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H + +#include + +#include "napi/native_api.h" +#include "want.h" + +namespace OHOS { +namespace EDM { +struct AsyncCallbackInfo { + napi_env env; + napi_async_work asyncWork; + napi_deferred deferred; + napi_ref callback = 0; + ErrCode ret; + bool boolRet = true; + std::string stringRet; + int32_t err = 0; + uint32_t errCode = 0; + std::string errMessage; + + virtual ~AsyncCallbackInfo() {}; +}; + +constexpr int32_t ARR_INDEX_ZERO = 0; +constexpr int32_t ARR_INDEX_ONE = 1; +constexpr int32_t ARR_INDEX_TWO = 2; +constexpr int32_t ARR_INDEX_THREE = 3; +constexpr int32_t ARR_INDEX_FOUR = 4; + +constexpr size_t ARGS_SIZE_ZERO = 0; +constexpr size_t ARGS_SIZE_ONE = 1; +constexpr size_t ARGS_SIZE_TWO = 2; +constexpr size_t ARGS_SIZE_THREE = 3; +constexpr size_t ARGS_SIZE_FOUR = 4; +constexpr size_t ARGS_SIZE_FIVE = 5; + +constexpr size_t CALLBACK_SIZE = 1; + +constexpr int32_t NAPI_RETURN_ZERO = 0; +constexpr int32_t NAPI_RETURN_ONE = 1; + +bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); +bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args); +bool ParseLong(napi_env env, int64_t ¶m, napi_value args); +bool ParseInt(napi_env env, int32_t ¶m, napi_value args); +bool ParseString(napi_env env, std::string ¶m, napi_value args); +bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr); +void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data); +napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, std::string workName, + napi_async_execute_callback execute, napi_async_complete_callback complete); +void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data); +void NativeStringCallbackComplete(napi_env env, napi_status status, void *data); +} // namespace EDM +} // namespace OHOS + +#endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H + \ No newline at end of file diff --git a/interfaces/kits/common/include/napi_edm_error.h b/interfaces/kits/common/include/napi_edm_error.h new file mode 100644 index 000000000..d07197d8e --- /dev/null +++ b/interfaces/kits/common/include/napi_edm_error.h @@ -0,0 +1,40 @@ +/* + * 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 INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ERROR_ADDON_H +#define INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ERROR_ADDON_H + +#include +#include "edm_errors.h" +#include "napi/native_api.h" +#include "napi/native_common.h" + +namespace OHOS { +namespace EDM { + napi_value CreateError(napi_env env, ErrCode errorCode); + napi_value CreateError(napi_env env, int32_t errorCode, const std::string &errMessage); + std::pair GetMessageFromReturncode(ErrCode returnCode); + #define ASSERT_AND_THROW_PARAM_ERROR(env, assertion, message) \ + do { \ + if (!(assertion)) { \ + napi_throw((env), CreateError((env), (EdmReturnErrCode::PARAM_ERROR), (message))); \ + napi_value ret = nullptr; \ + return ret; \ + } \ + } while (0) +} // namespace EDM +} // namespace OHOS + +#endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ERROR_ADDON_H diff --git a/interfaces/kits/common/src/napi_edm_common.cpp b/interfaces/kits/common/src/napi_edm_common.cpp new file mode 100644 index 000000000..6c8012102 --- /dev/null +++ b/interfaces/kits/common/src/napi_edm_common.cpp @@ -0,0 +1,252 @@ +/* + * 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 "napi_edm_common.h" + +#include "edm_errors.h" +#include "edm_log.h" +#include "js_native_api.h" +#include "js_native_api_types.h" +#include "napi_edm_error.h" +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace EDM { +bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType) +{ + napi_valuetype valueType = napi_undefined; + napi_typeof(env, value, &valueType); + return valueType == targetType; +} + +bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args) +{ + napi_valuetype valueType; + NAPI_CALL_BASE(env, napi_typeof(env, args, &valueType), false); + if (valueType != napi_object) { + EDMLOGE("Parameter element valueType error"); + return false; + } + std::string bundleName; + std::string abilityName; + napi_value prop = nullptr; + if (napi_get_named_property(env, args, "bundleName", &prop) != napi_ok || + !GetStringFromNAPI(env, prop, bundleName)) { + EDMLOGE("Parameter element bundleName error"); + return false; + } + EDMLOGD("ParseElementName bundleName %{public}s ", bundleName.c_str()); + + prop = nullptr; + if (napi_get_named_property(env, args, "abilityName", &prop) != napi_ok || + !GetStringFromNAPI(env, prop, abilityName)) { + EDMLOGE("Parameter abilityname error"); + return false; + } + EDMLOGD("ParseElementName abilityname %{public}s", abilityName.c_str()); + + elementName.SetBundleName(bundleName); + elementName.SetAbilityName(abilityName); + return true; +} + +bool ParseLong(napi_env env, int64_t ¶m, napi_value args) +{ + napi_valuetype valueType = napi_undefined; + if (napi_typeof(env, args, &valueType)!= napi_ok || + valueType != napi_number || napi_get_value_int64(env, args, ¶m) != napi_ok) { + EDMLOGE("Wrong argument type. int64 expected."); + return false; + } + return true; +} + +bool ParseInt(napi_env env, int32_t ¶m, napi_value args) +{ + napi_valuetype valueType = napi_undefined; + if (napi_typeof(env, args, &valueType)!= napi_ok || + valueType != napi_number || napi_get_value_int32(env, args, ¶m) != napi_ok) { + EDMLOGE("Wrong argument type. int32 expected."); + return false; + } + return true; +} + +bool ParseString(napi_env env, std::string ¶m, napi_value args) +{ + napi_valuetype valuetype; + if (napi_typeof(env, args, &valuetype) != napi_ok || valuetype != napi_string || + !GetStringFromNAPI(env, args, param)) { + EDMLOGE("can not get string value"); + return false; + } + EDMLOGD("ParseString param = %{public}s.", param.c_str()); + return true; +} + +bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr) +{ + std::string result; + size_t size = 0; + + if (napi_get_value_string_utf8(env, value, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) { + EDMLOGE("can not get string size"); + return false; + } + result.reserve(size + NAPI_RETURN_ONE); + result.resize(size); + if (napi_get_value_string_utf8(env, value, result.data(), (size + NAPI_RETURN_ONE), &size) != napi_ok) { + EDMLOGE("can not get string value"); + return false; + } + resultStr = result; + return true; +} + +void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data) +{ + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + napi_value error = nullptr; + if (asyncCallbackInfo->callback == nullptr) { + EDMLOGD("asyncCallbackInfo->deferred != nullptr"); + if (asyncCallbackInfo->ret == ERR_OK) { + napi_get_null(env, &error); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, error); + } else { + napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret)); + } + } else { + if (asyncCallbackInfo->ret == ERR_OK) { + napi_get_null(env, &error); + } else { + error = CreateError(env, asyncCallbackInfo->ret); + } + napi_value callback = nullptr; + napi_value result = nullptr; + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); + napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &error, &result); + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, std::string workName, + napi_async_execute_callback execute, napi_async_complete_callback complete) +{ + napi_value result = nullptr; + if (context->callback == nullptr) { + napi_create_promise(env, &context->deferred, &result); + } else { + napi_get_undefined(env, &result); + } + napi_value resource = nullptr; + napi_get_undefined(env, &resource); + napi_value resourceName = nullptr; + napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, resource, resourceName, execute, complete, + static_cast(context), &context->asyncWork); + napi_queue_async_work(env, context->asyncWork); + return result; +} + +void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data) +{ + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + if (asyncCallbackInfo->deferred != nullptr) { + EDMLOGD("asyncCallbackInfo->deferred != nullptr"); + if (asyncCallbackInfo->ret == ERR_OK) { + EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet); + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackInfo->boolRet, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret)); + } + } else { + napi_value callbackValue[ARGS_SIZE_TWO] = { 0 }; + if (asyncCallbackInfo->ret == ERR_OK) { + napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]); + EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet); + napi_get_boolean(env, asyncCallbackInfo->boolRet, &callbackValue[ARR_INDEX_ONE]); + } else { + EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ", + GetMessageFromReturncode(asyncCallbackInfo->ret).first, + GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str()); + callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret); + napi_get_null(env, &callbackValue[ARR_INDEX_ONE]); + } + napi_value callback = nullptr; + napi_value result = nullptr; + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); + napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result); + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +void NativeStringCallbackComplete(napi_env env, napi_status status, void *data) +{ + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); + if (asyncCallbackInfo->deferred != nullptr) { + EDMLOGD("asyncCallbackInfo->deferred != nullptr"); + if (asyncCallbackInfo->ret == ERR_OK) { + EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str()); + napi_value result = nullptr; + napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret)); + } + } else { + napi_value callbackValue[ARGS_SIZE_TWO] = { 0 }; + if (asyncCallbackInfo->ret == ERR_OK) { + napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]); + EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str()); + napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH, + &callbackValue[ARR_INDEX_ONE]); + } else { + EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ", + GetMessageFromReturncode(asyncCallbackInfo->ret).first, + GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str()); + callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret); + napi_get_null(env, &callbackValue[ARR_INDEX_ONE]); + } + napi_value callback = nullptr; + napi_value result = nullptr; + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); + napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result); + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/common/src/napi_edm_error.cpp b/interfaces/kits/common/src/napi_edm_error.cpp new file mode 100644 index 000000000..81e2a9b8e --- /dev/null +++ b/interfaces/kits/common/src/napi_edm_error.cpp @@ -0,0 +1,87 @@ +/* + * 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 "napi_edm_error.h" + +#include + +namespace OHOS { +namespace EDM { +static const std::unordered_map errMessageMap = { + { + EdmReturnErrCode::PERMISSION_DENIED, "the application does not have permission to call this function." + }, + { + EdmReturnErrCode::SYSTEM_ABNORMALLY, "the system ability work abnormally." + }, + { + EdmReturnErrCode::ENABLE_ADMIN_FAILED, "failed to enable the administrator application of the device." + }, + { + EdmReturnErrCode::COMPONENT_INVALID, "the administrator ability component is invalid." + }, + { + EdmReturnErrCode::ADMIN_INACTIVE, "the application is not a administrator of the device." + }, + { + EdmReturnErrCode::DISABLE_ADMIN_FAILED, "failed to disable the administrator application of the device." + }, + { + EdmReturnErrCode::UID_INVALID, "the specified user ID is invalid." + }, + { + EdmReturnErrCode::INTERFACE_UNSUPPORTED, "the specified interface is not supported." + }, + { + EdmReturnErrCode::PARAM_ERROR, "invalid input parameter." + }, + { + EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED, + "the administrator application does not have permission to manage the device." + }, + { + EdmReturnErrCode::MANAGED_EVENTS_INVALID, "the specified managed event is is invalid." + } +}; + +napi_value CreateError(napi_env env, ErrCode errorCode) +{ + return CreateError(env, GetMessageFromReturncode(errorCode).first, + GetMessageFromReturncode(errorCode).second); +} + +napi_value CreateError(napi_env env, int32_t errorCode, const std::string &errMessage) +{ + napi_value result = nullptr; + napi_value message = nullptr; + napi_value errorCodeStr = nullptr; + napi_create_string_utf8(env, static_cast(std::to_string(errorCode).data()), + std::to_string(errorCode).size(), &errorCodeStr); + napi_create_string_utf8(env, errMessage.c_str(), errMessage.size(), &message); + napi_create_error(env, errorCodeStr, message, &result); + return result; +} + +std::pair GetMessageFromReturncode(ErrCode returnCode) +{ + auto iter = errMessageMap.find(returnCode); + if (iter != errMessageMap.end()) { + return std::make_pair(returnCode, iter->second); + } else { + return std::make_pair(EdmReturnErrCode::PARAM_ERROR, "some thing wrong happend"); + } +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/datetime_manager/BUILD.gn b/interfaces/kits/datetime_manager/BUILD.gn new file mode 100644 index 000000000..3c21af5a5 --- /dev/null +++ b/interfaces/kits/datetime_manager/BUILD.gn @@ -0,0 +1,48 @@ +# 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. + +import("//base/customization/enterprise_device_management/edm.gni") +import("//build/ohos.gni") + +ohos_shared_library("datetimemanager") { + include_dirs = [ + "//third_party/node/src", + "$SUBSYSTEM_DIR/common/native/include", + "$SUBSYSTEM_DIR/interfaces/kits/datetime_manager/include", + "$SUBSYSTEM_DIR/interfaces/kits/common/include", + "$SUBSYSTEM_DIR/interfaces/inner_api/include", + ] + + sources = [ + "$SUBSYSTEM_DIR/interfaces/kits/common/src/napi_edm_common.cpp", + "$SUBSYSTEM_DIR/interfaces/kits/common/src/napi_edm_error.cpp", + "src/datetime_manager_addon.cpp", + ] + + configs = [ "//base/customization/enterprise_device_management/common/config:coverage_flags" ] + + deps = [ "$EDM_INNERKITS_PATH:edmservice_kits" ] + + external_deps = [ + "ability_base:want", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "os_account:os_account_innerkits", + ] + + relative_install_dir = "module/enterprise" + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/interfaces/kits/datetime_manager/include/datetime_manager_addon.h b/interfaces/kits/datetime_manager/include/datetime_manager_addon.h new file mode 100644 index 000000000..e5e323ae0 --- /dev/null +++ b/interfaces/kits/datetime_manager/include/datetime_manager_addon.h @@ -0,0 +1,49 @@ +/* + * 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 INTERFACES_KITS_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_ADDON_H +#define INTERFACES_KITS_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_ADDON_H + +#include "datetime_manager_proxy.h" +#include "edm_errors.h" +#include "napi_edm_error.h" +#include "napi_edm_common.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "napi/native_api.h" +#include "want.h" + +namespace OHOS { +namespace EDM { +struct AsyncSetDateTimeCallbackInfo : AsyncCallbackInfo { + OHOS::AppExecFwk::ElementName elementName; + int64_t time; +}; + +class DatetimeManagerAddon { +public: + DatetimeManagerAddon(); + ~DatetimeManagerAddon() = default; + + static napi_value Init(napi_env env, napi_value exports); +private: + static napi_value SetDateTime(napi_env env, napi_callback_info info); + static void NativeSetDateTime(napi_env env, void *data); + static std::shared_ptr dateTimeManagerProxy_; +}; +} // namespace EDM +} // namespace OHOS + +#endif // INTERFACES_KITS_DATETIME_MANAGER_INCLUDE_DATETIME_MANAGER_ADDON_H diff --git a/interfaces/kits/datetime_manager/src/datetime_manager_addon.cpp b/interfaces/kits/datetime_manager/src/datetime_manager_addon.cpp new file mode 100644 index 000000000..ff23da9a6 --- /dev/null +++ b/interfaces/kits/datetime_manager/src/datetime_manager_addon.cpp @@ -0,0 +1,98 @@ +/* + * 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 "datetime_manager_addon.h" +#include "edm_log.h" + +using namespace OHOS::EDM; + +napi_value DatetimeManagerAddon::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor property[] = { + DECLARE_NAPI_FUNCTION("setDateTime", SetDateTime), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); + return exports; +} + +napi_value DatetimeManagerAddon::SetDateTime(napi_env env, napi_callback_info info) +{ + EDMLOGI("NAPI_SetDateTime called"); + size_t argc = ARGS_SIZE_THREE; + napi_value argv[ARGS_SIZE_THREE] = {nullptr}; + napi_value thisArg = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); + ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error"); + bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object) && + MatchValueType(env, argv[ARR_INDEX_ONE], napi_number); + if (argc > ARGS_SIZE_TWO) { + matchFlag = matchFlag && MatchValueType(env, argv[ARR_INDEX_TWO], napi_function); + } + ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error"); + auto asyncCallbackInfo = new (std::nothrow) AsyncSetDateTimeCallbackInfo(); + if (asyncCallbackInfo == nullptr) { + return nullptr; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + bool ret = ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]); + ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error"); + EDMLOGD("SetDateTime: asyncCallbackInfo->elementName.bundlename %{public}s, " + "asyncCallbackInfo->abilityname:%{public}s", + asyncCallbackInfo->elementName.GetBundleName().c_str(), + asyncCallbackInfo->elementName.GetAbilityName().c_str()); + ret = ParseLong(env, asyncCallbackInfo->time, argv[ARR_INDEX_ONE]); + ASSERT_AND_THROW_PARAM_ERROR(env, ret, "time param error"); + if (argc > ARGS_SIZE_TWO) { + EDMLOGD("NAPI_SetDateTime argc == ARGS_SIZE_THREE"); + napi_create_reference(env, argv[ARR_INDEX_TWO], NAPI_RETURN_ONE, &asyncCallbackInfo->callback); + } + + napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "SetDateTime", + NativeSetDateTime, NativeVoidCallbackComplete); + callbackPtr.release(); + return asyncWorkReturn; +} + +void DatetimeManagerAddon::NativeSetDateTime(napi_env env, void *data) +{ + EDMLOGI("NAPI_NativeSetDateTime called"); + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncSetDateTimeCallbackInfo *asyncCallbackInfo = static_cast(data); + auto dateTimeManagerProxy_ = DatetimeManagerProxy::GetDatetimeManagerProxy(); + if (dateTimeManagerProxy_ == nullptr) { + EDMLOGE("can not get DatetimeManagerProxy"); + return; + } + asyncCallbackInfo->ret = dateTimeManagerProxy_->SetDateTime(asyncCallbackInfo->elementName, + asyncCallbackInfo->time); +} + +static napi_module g_dateTimeManagerModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = DatetimeManagerAddon::Init, + .nm_modname = "enterprise.dateTimeManager", + .nm_priv = ((void *)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void DateTimeManagerRegister() +{ + napi_module_register(&g_dateTimeManagerModule); +} \ No newline at end of file diff --git a/interfaces/kits/enterprise_admin_extension/BUILD.gn b/interfaces/kits/enterprise_admin_extension/BUILD.gn index 45808a94f..0948a6a42 100644 --- a/interfaces/kits/enterprise_admin_extension/BUILD.gn +++ b/interfaces/kits/enterprise_admin_extension/BUILD.gn @@ -48,7 +48,7 @@ ohos_shared_library("enterpriseadminextensionability_napi") { external_deps = [ "napi:ace_napi" ] - relative_install_dir = "module" + relative_install_dir = "module/enterprise" part_name = "enterprise_device_management" subsystem_name = "customization" } diff --git a/interfaces/kits/enterprise_admin_extension/enterprise_admin_extension_ability_module.cpp b/interfaces/kits/enterprise_admin_extension/enterprise_admin_extension_ability_module.cpp index 641a27631..40021662e 100644 --- a/interfaces/kits/enterprise_admin_extension/enterprise_admin_extension_ability_module.cpp +++ b/interfaces/kits/enterprise_admin_extension/enterprise_admin_extension_ability_module.cpp @@ -20,18 +20,18 @@ extern const char _binary_enterprise_admin_extension_ability_js_end[]; extern const char _binary_enterprise_admin_extension_ability_abc_start[]; extern const char _binary_enterprise_admin_extension_ability_abc_end[]; -extern "C" __attribute__((constructor)) void NAPI_EnterpriseAdminExtensionAbility_AutoRegister() +extern "C" __attribute__((constructor)) void NAPI_enterprise_EnterpriseAdminExtensionAbility_AutoRegister() { auto moduleManager = NativeModuleManager::GetInstance(); NativeModule newModuleInfo = { - .name = "EnterpriseAdminExtensionAbility", - .fileName = "libenterpriseadminextensionability_napi.so/enterprise_admin_extension_ability.js", + .name = "enterprise.EnterpriseAdminExtensionAbility", + .fileName = "enterprise/libenterpriseadminextensionability_napi.so/enterprise_admin_extension_ability.js", }; moduleManager->Register(&newModuleInfo); } -extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExtensionAbility_GetJSCode( +extern "C" __attribute__((visibility("default"))) void NAPI_enterprise_EnterpriseAdminExtensionAbility_GetJSCode( const char **buf, int *bufLen) { if (buf != nullptr) { @@ -45,7 +45,7 @@ extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExten } // EnterpriseAdminExtention JS register -extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExtensionAbility_GetABCCode( +extern "C" __attribute__((visibility("default"))) void NAPI_enterprise_EnterpriseAdminExtensionAbility_GetABCCode( const char **buf, int *buflen) { if (buf != nullptr) { diff --git a/interfaces/kits/enterprise_admin_extension_context/BUILD.gn b/interfaces/kits/enterprise_admin_extension_context/BUILD.gn index 0efa465f1..a5cd09bc3 100644 --- a/interfaces/kits/enterprise_admin_extension_context/BUILD.gn +++ b/interfaces/kits/enterprise_admin_extension_context/BUILD.gn @@ -48,7 +48,7 @@ ohos_shared_library("enterpriseadminextensioncontext_napi") { external_deps = [ "napi:ace_napi" ] - relative_install_dir = "module" + relative_install_dir = "module/enterprise" part_name = "enterprise_device_management" subsystem_name = "customization" } diff --git a/interfaces/kits/enterprise_admin_extension_context/enterprise_admin_extension_context_module.cpp b/interfaces/kits/enterprise_admin_extension_context/enterprise_admin_extension_context_module.cpp index cb850c2ea..8aa83de49 100644 --- a/interfaces/kits/enterprise_admin_extension_context/enterprise_admin_extension_context_module.cpp +++ b/interfaces/kits/enterprise_admin_extension_context/enterprise_admin_extension_context_module.cpp @@ -20,18 +20,18 @@ extern const char _binary_enterprise_admin_extension_context_js_end[]; extern const char _binary_enterprise_admin_extension_context_abc_start[]; extern const char _binary_enterprise_admin_extension_context_abc_end[]; -extern "C" __attribute__((constructor)) void NAPI_EnterpriseAdminExtensionContext_AutoRegister() +extern "C" __attribute__((constructor)) void NAPI_enterprise_EnterpriseAdminExtensionContext_AutoRegister() { auto moduleManager = NativeModuleManager::GetInstance(); NativeModule newModuleInfo = { - .name = "EnterpriseAdminExtensionContext", - .fileName = "libenterpriseadminextensioncontext_napi.so/enterprise_admin_extension_context.js", + .name = "enterprise.EnterpriseAdminExtensionContext", + .fileName = "enterprise/libenterpriseadminextensioncontext_napi.so/enterprise_admin_extension_context.js", }; moduleManager->Register(&newModuleInfo); } -extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExtensionContext_GetJSCode( +extern "C" __attribute__((visibility("default"))) void NAPI_enterprise_EnterpriseAdminExtensionContext_GetJSCode( const char **buf, int *bufLen) { if (buf != nullptr) { @@ -45,7 +45,7 @@ extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExten } // EnterpriseAdminExtensionContext JS register -extern "C" __attribute__((visibility("default"))) void NAPI_EnterpriseAdminExtensionContext_GetABCCode( +extern "C" __attribute__((visibility("default"))) void NAPI_enterprise_EnterpriseAdminExtensionContext_GetABCCode( const char **buf, int *buflen) { if (buf != nullptr) { diff --git a/services/edm/include/iplugin.h b/services/edm/include/iplugin.h index c3e4a334a..0180dd203 100644 --- a/services/edm/include/iplugin.h +++ b/services/edm/include/iplugin.h @@ -51,6 +51,8 @@ public: virtual ErrCode OnAdminRemove(const std::string &adminName, const std::string &policyData) = 0; virtual void OnAdminRemoveDone(const std::string &adminName, const std::string ¤tJsonData) = 0; virtual ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply); + virtual ErrCode OnGetPolicy(std::string &policyData, MessageParcel &reply) = 0; + std::uint32_t GetCode(); std::string GetPolicyName(); bool NeedSavePolicy(); diff --git a/services/edm/include/iplugin_template.h b/services/edm/include/iplugin_template.h index be804b33c..dc86f4ec3 100644 --- a/services/edm/include/iplugin_template.h +++ b/services/edm/include/iplugin_template.h @@ -34,19 +34,21 @@ namespace EDM { template class IPluginTemplate : public IPlugin { public: - virtual ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, std::string &policyData, + ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, std::string &policyData, bool &isChanged) override; - virtual ErrCode MergePolicyData(const std::string &adminName, std::string &policyData) override; + ErrCode MergePolicyData(const std::string &adminName, std::string &policyData) override; - virtual void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName, + void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName, bool isGlobalChanged) override; - virtual ErrCode OnAdminRemove(const std::string &adminName, const std::string ¤tJsonData) override; + ErrCode OnAdminRemove(const std::string &adminName, const std::string ¤tJsonData) override; - virtual void OnAdminRemoveDone(const std::string &adminName, const std::string &removedJsonData) override; + void OnAdminRemoveDone(const std::string &adminName, const std::string &removedJsonData) override; - virtual ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply) override; + ErrCode WritePolicyToParcel(const std::string &policyData, MessageParcel &reply) override; + + ErrCode OnGetPolicy(std::string &policyData, MessageParcel &reply) override; /* * Sets the handle of the policy processing object. @@ -372,6 +374,13 @@ ErrCode IPluginTemplate::OnHandlePolicy(std::uint32_t funcCode, MessageP return res; } +template +ErrCode IPluginTemplate::OnGetPolicy(std::string &policyData, MessageParcel &reply) +{ + EDMLOGI("IPluginTemplate::OnGetPolicy"); + return instance_->OnGetPolicy(policyData, reply); +} + template void IPluginTemplate::SetOnHandlePolicyListener(Supplier &&listener, FuncOperateType type) { @@ -760,6 +769,8 @@ public: */ static std::shared_ptr GetPlugin(); + virtual ErrCode OnGetPolicy(std::string &policyData, MessageParcel &reply); + static void DestroyPlugin(); private: @@ -788,6 +799,13 @@ std::shared_ptr PluginSingleton::GetPlugin() return pluginInstance_; } +template +ErrCode PluginSingleton::OnGetPolicy(std::string &policyData, MessageParcel &reply) +{ + EDMLOGI("PluginSingleton::OnGetPolicy"); + return ERR_OK; +} + template void PluginSingleton::DestroyPlugin() { diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index 8e32f2040..b55706f37 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -643,20 +643,30 @@ ErrCode EnterpriseDeviceMgrAbility::GetDevicePolicy(uint32_t code, AppExecFwk::E std::shared_ptr plugin = pluginMgr_->GetPluginByFuncCode(code); if (plugin == nullptr) { EDMLOGW("GetDevicePolicy: get plugin failed"); - reply.WriteInt32(ERR_EDM_GET_PLUGIN_MGR_FAILED); - return ERR_EDM_GET_PLUGIN_MGR_FAILED; + return EdmReturnErrCode::INTERFACE_UNSUPPORTED; + } + if (admin != nullptr) { + std::shared_ptr deviceAdmin = adminMgr_->GetAdminByPkgName(admin->GetBundleName(), GetCurrentUserId()); + if (deviceAdmin == nullptr) { + EDMLOGW("HandleDevicePolicy: get admin failed"); + return EdmReturnErrCode::ADMIN_INACTIVE; + } + if (!deviceAdmin->CheckPermission(plugin->GetPermission())) { + EDMLOGW("GetDevicePolicy: admin check permission failed %{public}s", plugin->GetPermission().c_str()); + return EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED; + } + } + if (!VerifyCallingPermission(plugin->GetPermission())) { + EDMLOGW("GetDevicePolicy: VerifyCallingPermission failed"); + return EdmReturnErrCode::PERMISSION_DENIED; } std::string policyName = plugin->GetPolicyName(); std::string policyValue; std::string adminName = (admin == nullptr) ? "" : admin->GetBundleName(); - if (policyMgr_->GetPolicy(adminName, policyName, policyValue) != ERR_OK) { - EDMLOGW("GetDevicePolicy: get policy failed"); - reply.WriteInt32(ERR_EDM_POLICY_NOT_FIND); - } else { - reply.WriteInt32(ERR_OK); - plugin->WritePolicyToParcel(policyValue, reply); + if (plugin->NeedSavePolicy()) { + policyMgr_->GetPolicy(adminName, policyName, policyValue); } - + plugin->OnGetPolicy(policyValue, reply); return ERR_OK; } diff --git a/services/edm/src/enterprise_device_mgr_stub.cpp b/services/edm/src/enterprise_device_mgr_stub.cpp index 742dc6179..ff1e300a1 100644 --- a/services/edm/src/enterprise_device_mgr_stub.cpp +++ b/services/edm/src/enterprise_device_mgr_stub.cpp @@ -138,16 +138,13 @@ ErrCode EnterpriseDeviceMgrStub::HandleDevicePolicyInner(uint32_t code, MessageP ErrCode EnterpriseDeviceMgrStub::GetDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply) { AppExecFwk::ElementName *admin = nullptr; - if (data.ReadInt32() != ERR_OK) { - admin = AppExecFwk::ElementName::Unmarshalling(data); + if (data.ReadInt32() == 0) { + admin = data.ReadParcelable(); } else { admin = nullptr; } - if (admin != nullptr) { - EDMLOGD("GetDevicePolicyInner bundleName:: %{public}s : abilityName : %{public}s code : %{public}x", - admin->GetBundleName().c_str(), admin->GetAbilityName().c_str(), code); - } ErrCode retCode = GetDevicePolicy(code, admin, reply); + reply.WriteInt32(retCode); delete admin; return retCode; } diff --git a/services/edm_plugin/BUILD.gn b/services/edm_plugin/BUILD.gn index df196a0a4..6a051b45c 100644 --- a/services/edm_plugin/BUILD.gn +++ b/services/edm_plugin/BUILD.gn @@ -18,7 +18,7 @@ import("plugin.gni") PLUGIN_ROOT = "$SUBSYSTEM_DIR/services/edm_plugin" PLUGIN_SRC_PATH = "$PLUGIN_ROOT/src" -edm_plugin_shared_library("device_settings_plugin") { +edm_plugin_shared_library("datetime_manager_plugin") { sources = [ "$PLUGIN_SRC_PATH/set_datetime_plugin.cpp" ] configs = [ "//base/customization/enterprise_device_management/common/config:coverage_flags" ] diff --git a/services/edm_plugin/plugin.gni b/services/edm_plugin/plugin.gni index 732dffe7a..6eed010ef 100644 --- a/services/edm_plugin/plugin.gni +++ b/services/edm_plugin/plugin.gni @@ -19,7 +19,7 @@ template("edm_plugin_shared_library") { include_dirs += [ "//base/customization/enterprise_device_management/services/edm/include", - "//base/customization/enterprise_device_management/interfaces/inner_api/include", + "//base/customization/enterprise_device_management/interfaces/inner_api/common/include", "//base/customization/enterprise_device_management/services/edm/include/utils", "//base/customization/enterprise_device_management/services/edm_plugin/include", "//third_party/jsoncpp/include", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 659ae9c69..65d479ffc 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -39,7 +39,7 @@ ohos_unittest("EdmServicesUnitTest") { sources = [ "./src/admin_manager_test.cpp", "./src/cmd_utils.cpp", - "./src/device_settings_manager_test.cpp", + "./src/datetime_manager_proxy_test.cpp", "./src/edm_permission_test.cpp", "./src/edm_sys_manager_test.cpp", "./src/ent_info_test.cpp", diff --git a/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp b/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp index 3f8854730..30f398c01 100644 --- a/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp +++ b/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp @@ -231,7 +231,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestIsPolicyDisableSuc, TestSize.Level1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequest)); int funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, SET_DATETIME); bool isDisabled = false; - bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(funcCode, isDisabled); + bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(nullptr, funcCode, isDisabled); EXPECT_TRUE(ret); EXPECT_TRUE(!isDisabled); } @@ -248,7 +248,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestIsPolicyDisableFail, TestSize.Level1) EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestFail)); - bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(funcCode, isDisabled); + bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(nullptr, funcCode, isDisabled); EXPECT_TRUE(!ret); EXPECT_TRUE(!isDisabled); } @@ -261,7 +261,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestIsPolicyDisableFail, TestSize.Level1) HWTEST_F(EnterpriseDeviceMgrProxyTest, TestIsPolicyDisableFuncCodeFail, TestSize.Level1) { bool isDisabled = false; - bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(FUNC_CODE_ERR, isDisabled); + bool ret = enterpriseDeviceMgrProxyTest->IsPolicyDisable(nullptr, FUNC_CODE_ERR, isDisabled); EXPECT_TRUE(!ret); EXPECT_TRUE(!isDisabled); } @@ -278,7 +278,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyValueSuc, TestSize.Level1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequest)); int funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, SET_DATETIME); std::string policyData; - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyValue(funcCode, policyData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyValue(nullptr, funcCode, policyData); EXPECT_TRUE(ret); } @@ -294,7 +294,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyValueReplyFail, TestSize.Lev EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestReplyFail)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyValue(funcCode, policyData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyValue(nullptr, funcCode, policyData); EXPECT_TRUE(!ret); } @@ -360,7 +360,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyArrayEnableAdmin, TestSize.L .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestEnableAdmin)); std::vector policyArrayData; - enterpriseDeviceMgrProxyTest->GetPolicyArray(funcCode, policyArrayData); + enterpriseDeviceMgrProxyTest->GetPolicyArray(nullptr, funcCode, policyArrayData); EXPECT_TRUE(policyArrayData.size() == 1); } @@ -376,7 +376,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyArrayFail, TestSize.Level1) EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestFail)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyArray(funcCode, policyArrayData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyArray(nullptr, funcCode, policyArrayData); EXPECT_TRUE(!ret); } @@ -392,7 +392,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyArraySuc, TestSize.Level1) EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequest)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyArray(funcCode, policyArrayData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyArray(nullptr, funcCode, policyArrayData); EXPECT_TRUE(ret); } @@ -408,7 +408,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyConfigSuc, TestSize.Level1) EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequest)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(funcCode, policyMapData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(nullptr, funcCode, policyMapData); EXPECT_TRUE(ret); } @@ -424,7 +424,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyConfigReplyFail, TestSize.Le EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestReplyFail)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(funcCode, policyMapData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(nullptr, funcCode, policyMapData); EXPECT_TRUE(!ret); } @@ -440,7 +440,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyConfigEnableAdminNotEqual, T EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestEnableAdmin)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(funcCode, policyMapData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(nullptr, funcCode, policyMapData); EXPECT_TRUE(!ret); } @@ -456,7 +456,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestGetPolicyConfigEnableAdminSizeEqual, EXPECT_CALL(*object_, SendRequest(_, _, _, _)) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestMapEnableAdminTwoSuc)); - bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(funcCode, policyMapData); + bool ret = enterpriseDeviceMgrProxyTest->GetPolicyConfig(nullptr, funcCode, policyMapData); EXPECT_TRUE(ret); } diff --git a/test/unittest/include/iplugin_mock.h b/test/unittest/include/iplugin_mock.h index 4521baf46..be4587740 100644 --- a/test/unittest/include/iplugin_mock.h +++ b/test/unittest/include/iplugin_mock.h @@ -48,6 +48,11 @@ public: { policyName_ = policyName; } + + ErrCode OnGetPolicy(std::string &policyData, MessageParcel &reply) + { + return 0; + } }; } // namespace EDM } // namespace OHOS diff --git a/test/unittest/include/plugin_manager_test.h b/test/unittest/include/plugin_manager_test.h index fa4fe4e79..3cc54d652 100644 --- a/test/unittest/include/plugin_manager_test.h +++ b/test/unittest/include/plugin_manager_test.h @@ -59,6 +59,11 @@ public: return IPlugin::WritePolicyToParcel(policyData, reply); } + ErrCode OnGetPolicy(std::string &policyData, MessageParcel &reply) + { + return ERR_OK; + } + ~TestPlugin() override = default; }; diff --git a/test/unittest/src/device_settings_manager_test.cpp b/test/unittest/src/datetime_manager_proxy_test.cpp similarity index 67% rename from test/unittest/src/device_settings_manager_test.cpp rename to test/unittest/src/datetime_manager_proxy_test.cpp index 2d4422969..69aa5b636 100644 --- a/test/unittest/src/device_settings_manager_test.cpp +++ b/test/unittest/src/datetime_manager_proxy_test.cpp @@ -1,64 +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 -#include -#include -#include "cmd_utils.h" -#include "device_settings_manager.h" - -using namespace testing::ext; - -namespace OHOS { -namespace EDM { -namespace TEST { -class DeviceSettingsManagerTest : public testing::Test { -protected: - void SetUp() override; - - void TearDown() override; - - std::shared_ptr deviceSettingsManager; -}; - -void DeviceSettingsManagerTest::SetUp() -{ - deviceSettingsManager = DeviceSettingsManager::GetDeviceSettingsManager(); -} - -void DeviceSettingsManagerTest::TearDown() -{ - if (deviceSettingsManager) { - deviceSettingsManager.reset(); - } -} - -/** - * @tc.name: TestSetDateTime - * @tc.desc: Test SetDateTime func. - * @tc.type: FUNC - */ -HWTEST_F(DeviceSettingsManagerTest, TestSetDateTime, TestSize.Level1) -{ - AppExecFwk::ElementName admin; - admin.SetBundleName("com.edm.test.demo"); - admin.SetAbilityName("com.edm.test.demo.Ability"); - int64_t time = 1674365400000; - int32_t ret = deviceSettingsManager->SetDateTime(admin, time); - EXPECT_TRUE(ret != ERR_OK); -} -} // namespace TEST -} // namespace EDM -} // namespace OHOS +/* + * 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 +#include +#include +#include "cmd_utils.h" +#include "datetime_manager_proxy.h" + +using namespace testing::ext; + +namespace OHOS { +namespace EDM { +namespace TEST { +class DatetimeManagerProxyTest : public testing::Test { +protected: + void SetUp() override; + + void TearDown() override; + + std::shared_ptr dateTimeManagerProxy; +}; + +void DatetimeManagerProxyTest::SetUp() +{ + dateTimeManagerProxy = DatetimeManagerProxy::GetDatetimeManagerProxy(); +} + +void DatetimeManagerProxyTest::TearDown() +{ + if (dateTimeManagerProxy) { + dateTimeManagerProxy.reset(); + } +} + +/** + * @tc.name: TestSetDateTime + * @tc.desc: Test SetDateTime func. + * @tc.type: FUNC + */ +HWTEST_F(DatetimeManagerProxyTest, TestSetDateTime, TestSize.Level1) +{ + AppExecFwk::ElementName admin; + admin.SetBundleName("com.edm.test.demo"); + admin.SetAbilityName("com.edm.test.demo.Ability"); + int64_t time = 1674365400000; + int32_t ret = dateTimeManagerProxy->SetDateTime(admin, time); + EXPECT_TRUE(ret != ERR_OK); +} +} // namespace TEST +} // namespace EDM +} // namespace OHOS diff --git a/test/unittest/src/enterprise_device_mgr_proxy_test.cpp b/test/unittest/src/enterprise_device_mgr_proxy_test.cpp index 2f02b31a8..11e4bb3b3 100644 --- a/test/unittest/src/enterprise_device_mgr_proxy_test.cpp +++ b/test/unittest/src/enterprise_device_mgr_proxy_test.cpp @@ -84,7 +84,7 @@ HWTEST_F(EnterpriseDeviceMgrProxyTest, TestDisableAdmin, TestSize.Level1) int funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, SET_DATETIME); bool isDisabled; - enterpriseDeviceMgrProxyTest->IsPolicyDisable(funcCode, isDisabled); + enterpriseDeviceMgrProxyTest->IsPolicyDisable(nullptr, funcCode, isDisabled); EXPECT_FALSE(isDisabled); -- Gitee