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 FRAMEWORK_EXTENSION_INCLUDE_JS_ENTERPRISE_ADMIN_EXTENSION_H
17 #define FRAMEWORK_EXTENSION_INCLUDE_JS_ENTERPRISE_ADMIN_EXTENSION_H
18 
19 #include <string>
20 
21 #include "enterprise_admin_extension.h"
22 #include "js_runtime.h"
23 #include "policy_struct.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 /**
28  * @brief js-level enterprise admin extension.
29  */
30 class JsEnterpriseAdminExtension : public EnterpriseAdminExtension {
31 public:
32     JsEnterpriseAdminExtension(AbilityRuntime::JsRuntime& jsRuntime);
33 
34     ~JsEnterpriseAdminExtension() override;
35 
36     /**
37      * @brief Create JsEnterpriseAdminExtension.
38      *
39      * @param runtime The runtime.
40      * @return The JsEnterpriseAdminExtension instance.
41      */
42     static JsEnterpriseAdminExtension* Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime);
43 
44     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>& record,
45               const std::shared_ptr<AppExecFwk::OHOSApplication>& application,
46               std::shared_ptr<AppExecFwk::AbilityHandler>& handler,
47               const sptr<IRemoteObject>& token) override;
48 
49     void OnStart(const AAFwk::Want& want) override;
50 
51     sptr<IRemoteObject> OnConnect(const AAFwk::Want& want) override;
52 
53     void OnDisconnect(const AAFwk::Want& want) override;
54 
55     void OnStop() override;
56 
57     void OnAdminEnabled();
58 
59     void OnAdminDisabled();
60 
61     void OnBundleAdded(const std::string &bundleName, int32_t accountId);
62 
63     void OnBundleRemoved(const std::string &bundleName, int32_t accountId);
64 
65     void OnAppStart(const std::string &bundleName);
66 
67     void OnAppStop(const std::string &bundleName);
68 
69     void OnSystemUpdate(const UpdateInfo &updateInfo);
70 private:
71     napi_value CallObjectMethod(const char* name, napi_value* argv, size_t argc);
72 
73     void JsEnterpriseAdminExtensionContextInit();
74 
75     void GetSrcPath(std::string& srcPath);
76 
77     napi_value CreateUpdateInfoObject(napi_env env, const UpdateInfo &updateInfo);
78 
79     AbilityRuntime::JsRuntime& jsRuntime_;
80 
81     std::unique_ptr<NativeReference> jsObj_{nullptr};
82 };
83 }  // namespace EDM
84 }  // namespace OHOS
85 #endif  // FRAMEWORK_EXTENSION_INCLUDE_JS_ENTERPRISE_ADMIN_EXTENSION_H