• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H
17 #define SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H
18 
19 #include <bundle_mgr_interface.h>
20 #include <string>
21 #include "admin_manager.h"
22 #include "common_event_subscriber.h"
23 #include "enterprise_device_mgr_stub.h"
24 #include "hilog/log.h"
25 #include "plugin_manager.h"
26 #include "policy_manager.h"
27 #include "system_ability.h"
28 #include "enterprise_admin_proxy.h"
29 
30 namespace OHOS {
31 namespace EDM {
32 class EnterpriseDeviceMgrAbility : public SystemAbility, public EnterpriseDeviceMgrStub {
33     DECLARE_SYSTEM_ABILITY(EnterpriseDeviceMgrAbility);
34 
35 public:
36     using CommonEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const EventFwk::CommonEventData &data);
37     EnterpriseDeviceMgrAbility();
38     DISALLOW_COPY_AND_MOVE(EnterpriseDeviceMgrAbility);
39     ~EnterpriseDeviceMgrAbility();
40     static sptr<EnterpriseDeviceMgrAbility> GetInstance();
41 
42     ErrCode EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type,
43         int32_t userId) override;
44     ErrCode DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId) override;
45     ErrCode DisableSuperAdmin(std::string &bundleName) override;
46     ErrCode HandleDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data) override;
47     ErrCode GetDevicePolicy(uint32_t code, AppExecFwk::ElementName *admin, MessageParcel &reply) override;
48     ErrCode GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList) override;
49     ErrCode GetEnterpriseInfo(AppExecFwk::ElementName &admin, MessageParcel &reply) override;
50     ErrCode SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo) override;
51     ErrCode SubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
52     ErrCode UnsubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
53     bool IsSuperAdmin(std::string &bundleName) override;
54     bool IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId) override;
55     std::unordered_map<std::string, CommonEventCallbackFunc> commonEventFuncMap_;
56 
57 protected:
58     void OnStart() override;
59     void OnStop() override;
60     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
61     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
62     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
63 
64 private:
65     bool IsHdc();
66     void AddCommonEventFuncMap();
67     ErrCode CheckCallingUid(std::string &bundleName);
68     ErrCode RemoveAdminItem(std::string adminName, std::string policyName, std::string policyValue);
69     ErrCode RemoveAdmin(const std::string &adminName, int32_t userId);
70     ErrCode GetAllPermissionsByAdmin(const std::string& bundleInfoName,
71         std::vector<std::string> &permissionList, int32_t userId);
72     int32_t GetCurrentUserId();
73     ErrCode UpdateDeviceAdmin(AppExecFwk::ElementName &admin);
74     ErrCode VerifyEnableAdminCondition(AppExecFwk::ElementName &admin, AdminType type, int32_t userId);
75     ErrCode HandleManagedEvent(const AppExecFwk::ElementName &admin,
76         const std::vector<uint32_t> &events, bool subscribe);
77     bool VerifyCallingPermission(const std::string &permissionName);
78     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
79     std::shared_ptr<EventFwk::CommonEventSubscriber> CreateEnterpriseDeviceEventSubscriber(
80         EnterpriseDeviceMgrAbility &listener);
81     void OnCommonEventUserRemoved(const EventFwk::CommonEventData &data);
82     void OnCommonEventPackageAdded(const EventFwk::CommonEventData &data);
83     void OnCommonEventPackageRemoved(const EventFwk::CommonEventData &data);
84     void OnCommonEventPackageAddedOrRemoved(const EventFwk::CommonEventData &data, ManagedEvent event);
85     bool CheckManagedEvent(uint32_t event);
86     static std::mutex mutexLock_;
87     static sptr<EnterpriseDeviceMgrAbility> instance_;
88     std::shared_ptr<PolicyManager> policyMgr_;
89     std::shared_ptr<AdminManager> adminMgr_;
90     std::shared_ptr<PluginManager> pluginMgr_;
91     bool registerToService_ = false;
92     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber = nullptr;
93 };
94 class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber {
95 public:
96     EnterpriseDeviceEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
97         EnterpriseDeviceMgrAbility &listener);
98     ~EnterpriseDeviceEventSubscriber() override = default;
99 
100     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
101 private:
102       EnterpriseDeviceMgrAbility &listener_;
103 };
104 } // namespace EDM
105 } // namespace OHOS
106 #endif // SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H
107