1 /*
2  * Copyright (c) 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 #include "enterprise_device_management_adapter_impl.h"
17 
18 #include "nweb_log.h"
19 
20 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
21 #include "browser_proxy.h"
22 #endif
23 
24 namespace OHOS::NWeb {
25 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
26 using namespace OHOS::EDM;
27 #endif
28 
29 namespace {
30 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
31 const char* const BROWSER_POLICY_CHANGED_EVENT = "com.ohos.edm.browserpolicychanged";
32 #endif
33 }
34 
35 // static
GetInstance()36 EnterpriseDeviceManagementAdapterImpl& EnterpriseDeviceManagementAdapterImpl::GetInstance()
37 {
38     static EnterpriseDeviceManagementAdapterImpl instance;
39     return instance;
40 }
41 
42 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
NWebEdmEventSubscriber(EventFwk::CommonEventSubscribeInfo & in,std::shared_ptr<EdmPolicyChangedEventCallbackAdapter> eventCallback)43 NWebEdmEventSubscriber::NWebEdmEventSubscriber(
44     EventFwk::CommonEventSubscribeInfo& in,
45     std::shared_ptr<EdmPolicyChangedEventCallbackAdapter> eventCallback)
46     : EventFwk::CommonEventSubscriber(in), eventCallback_(eventCallback) {}
47 
OnReceiveEvent(const EventFwk::CommonEventData & data)48 void NWebEdmEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
49 {
50     const std::string action = data.GetWant().GetAction();
51     WVLOG_I("Receive edm policy action: %{public}s", action.c_str());
52     if (action != BROWSER_POLICY_CHANGED_EVENT) {
53         return;
54     }
55 
56     if (eventCallback_) {
57         eventCallback_->Changed();
58     }
59 }
60 #endif
61 
RegistPolicyChangeEventCallback(std::shared_ptr<EdmPolicyChangedEventCallbackAdapter> eventCallback)62 void EnterpriseDeviceManagementAdapterImpl::RegistPolicyChangeEventCallback(
63     std::shared_ptr<EdmPolicyChangedEventCallbackAdapter> eventCallback)
64 {
65 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
66     WVLOG_I("Regist edm policy change event callback");
67     eventCallback_ = std::move(eventCallback);
68 #endif
69 }
70 
StartObservePolicyChange()71 bool EnterpriseDeviceManagementAdapterImpl::StartObservePolicyChange()
72 {
73 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
74     WVLOG_I("Start observing edm policy change event");
75 
76     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
77     skill.AddEvent(BROWSER_POLICY_CHANGED_EVENT);
78     EventFwk::CommonEventSubscribeInfo info(skill);
79     int32_t systemEdmUid = 3057;
80     info.SetPublisherUid(systemEdmUid);
81     this->commonEventSubscriber_ = std::make_shared<NWebEdmEventSubscriber>(info, this->eventCallback_);
82     bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber_);
83     if (ret == false) {
84         WVLOG_E("Start observing edm policy failed");
85     }
86     return ret;
87 #else
88     return false;
89 #endif
90 }
91 
StopObservePolicyChange()92 bool EnterpriseDeviceManagementAdapterImpl::StopObservePolicyChange()
93 {
94 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
95     WVLOG_I("Stop observing edm policy change event");
96 
97     if (this->commonEventSubscriber_ != nullptr) {
98         bool ret = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber_);
99         if (ret == false) {
100             WVLOG_E("Stop observing edm policy change failed");
101         }
102         return ret;
103     }
104     return true;
105 #else
106     return true;
107 #endif
108 }
109 
GetPolicies(std::string & policies)110 int32_t EnterpriseDeviceManagementAdapterImpl::GetPolicies(std::string& policies)
111 {
112 #if defined(NWEB_ENTERPRISE_DEVICE_MANAGER_ENABLE)
113     auto proxy = BrowserProxy::GetBrowserProxy();
114     if (!proxy) {
115         WVLOG_E("EnterpriseDeviceManagementAdapterImpl BrowserProxy is null");
116         return -1;
117     }
118     return proxy->GetPolicies(policies);
119 #else
120     WVLOG_I("Enterprise device management not supported.");
121     return -1;
122 #endif
123 }
124 
125 } // namespace OHOS::NWeb
126