• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
21 #include <string>
22 
23 #include "admin_manager.h"
24 #include "app_mgr_interface.h"
25 #include "common_event_subscriber.h"
26 #include "enterprise_admin_proxy.h"
27 #include "enterprise_device_mgr_stub.h"
28 #include "hilog/log.h"
29 #include "plugin_manager.h"
30 #include "policy_manager.h"
31 #include "system_ability.h"
32 
33 namespace OHOS {
34 namespace EDM {
35 class EnterpriseDeviceMgrAbility : public SystemAbility, public EnterpriseDeviceMgrStub {
36     DECLARE_SYSTEM_ABILITY(EnterpriseDeviceMgrAbility);
37 
38 public:
39     using CommonEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const EventFwk::CommonEventData &data);
40     using AddSystemAbilityFunc =
41         void (EnterpriseDeviceMgrAbility::*)(int32_t systemAbilityId, const std::string &deviceId);
42     EnterpriseDeviceMgrAbility();
43     DISALLOW_COPY_AND_MOVE(EnterpriseDeviceMgrAbility);
44     ~EnterpriseDeviceMgrAbility() override;
45     static sptr<EnterpriseDeviceMgrAbility> GetInstance();
46 
47     ErrCode EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type, int32_t userId) override;
48     ErrCode DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId) override;
49     ErrCode DisableSuperAdmin(const std::string &bundleName) override;
50     ErrCode HandleDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data, MessageParcel &reply,
51         int32_t userId) override;
52     ErrCode GetDevicePolicy(uint32_t code, MessageParcel &data, MessageParcel &reply, int32_t userId) override;
53     ErrCode GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList) override;
54     ErrCode GetEnterpriseInfo(AppExecFwk::ElementName &admin, MessageParcel &reply) override;
55     ErrCode SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo) override;
56     ErrCode SubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
57     ErrCode UnsubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
58     ErrCode AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName) override;
59     bool IsSuperAdmin(const std::string &bundleName) override;
60     bool IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId) override;
61     void ConnectAbilityOnSystemEvent(const std::string &bundleName, ManagedEvent event);
62     std::unordered_map<std::string, CommonEventCallbackFunc> commonEventFuncMap_;
63     std::unordered_map<int32_t, AddSystemAbilityFunc> addSystemAbilityFuncMap_;
64 
65 protected:
66     void OnStart() override;
67     void OnStop() override;
68     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
69     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
70     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
71 
72 private:
73     void AddCommonEventFuncMap();
74     void AddOnAddSystemAbilityFuncMap();
75     bool SubscribeAppState();
76     bool UnsubscribeAppState();
77     ErrCode CheckCallingUid(const std::string &bundleName);
78     ErrCode RemoveAdminItem(const std::string &adminName, const std::string &policyName, const std::string &policyValue,
79         int32_t userId);
80     ErrCode RemoveAdmin(const std::string &adminName, int32_t userId);
81     ErrCode RemovePolicyAndAdmin(const std::string &bundleName);
82     ErrCode GetAllPermissionsByAdmin(const std::string &bundleInfoName, std::vector<std::string> &permissionList,
83         int32_t userId);
84     int32_t GetCurrentUserId();
85     ErrCode HandleApplicationEvent(const std::vector<uint32_t> &events, bool subscribe);
86     ErrCode UpdateDeviceAdmin(AppExecFwk::ElementName &admin);
87     ErrCode VerifyEnableAdminCondition(AppExecFwk::ElementName &admin, AdminType type, int32_t userId);
88     ErrCode VerifyManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events);
89     ErrCode UpdateDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data, MessageParcel &reply,
90         int32_t userId);
91     ErrCode CheckGetPolicyPermission(MessageParcel &data, MessageParcel &reply, const std::string &getPermission,
92         std::string &adminName);
93     bool VerifyCallingPermission(const std::string &permissionName);
94     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
95     sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr();
96     std::shared_ptr<EventFwk::CommonEventSubscriber> CreateEnterpriseDeviceEventSubscriber(
97         EnterpriseDeviceMgrAbility &listener);
98     void OnCommonEventUserRemoved(const EventFwk::CommonEventData &data);
99     void OnCommonEventPackageAdded(const EventFwk::CommonEventData &data);
100     void OnCommonEventPackageRemoved(const EventFwk::CommonEventData &data);
101     bool ShouldUnsubscribeAppState(const std::string &adminName, int32_t userId);
102     bool CheckManagedEvent(uint32_t event);
103     void OnAppManagerServiceStart(int32_t systemAbilityId, const std::string &deviceId);
104     void OnCommonEventServiceStart(int32_t systemAbilityId, const std::string &deviceId);
105     std::shared_ptr<PolicyManager> GetAndSwitchPolicyManagerByUserId(int32_t userId);
106     void InitAllPolices();
107 
108     static std::mutex mutexLock_;
109     static sptr<EnterpriseDeviceMgrAbility> instance_;
110     std::shared_ptr<PolicyManager> policyMgr_;
111     std::map<std::int32_t, std::shared_ptr<PolicyManager>> policyMgrMap_;
112     std::shared_ptr<AdminManager> adminMgr_;
113     std::shared_ptr<PluginManager> pluginMgr_;
114     bool registerToService_ = false;
115     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber = nullptr;
116     sptr<AppExecFwk::IApplicationStateObserver> appStateObserver_;
117 };
118 class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber {
119 public:
120     EnterpriseDeviceEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
121         EnterpriseDeviceMgrAbility &listener);
122     ~EnterpriseDeviceEventSubscriber() override = default;
123 
124     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
125 
126 private:
127     EnterpriseDeviceMgrAbility &listener_;
128 };
129 } // namespace EDM
130 } // namespace OHOS
131 #endif // SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H
132